-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·64 lines (56 loc) · 1.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env node
'use strict';
const path = require('path');
const latestVersion = require('latest-version');
const argvs = process.argv;
const parsedParams = allElementsAfter("versioncheck", argvs); // ex. ['versioncheck', '-v']
const keyword = parsedParams[1]; // ex. '-v'
if (keyword == "-v" || keyword == "--version" || keyword == "version") {
const mypackagejson = require('./package.json');
console.log("v" + mypackagejson.version);
process.exit();
}
try {
require(path.join(process.cwd(), 'package.json'));
} catch (e) {
console.error("Can't find package.json file.");
process.exit(1);
}
const pjson = require(path.join(process.cwd(), 'package.json'));
let dependencyKeys = Object.keys(pjson.dependencies).concat(Object.keys(pjson.devDependencies));
let dependencyValues = Object.values(pjson.dependencies).concat(Object.values(pjson.devDependencies));
let packagesPromises = [];
dependencyKeys.forEach((dependencyKey, index) => {
packagesPromises.push(new Promise(function(resolve, reject) {
let myIndex = index;
latestVersion(dependencyKey).then(version => {
if (version != dependencyValues[myIndex] && !dependencyValues[myIndex].includes("github:") && !dependencyValues[myIndex].includes("/")) {
resolve({package: dependencyKey, version: version});
}
resolve(null);
});
}));
});
async function handleVersions() {
let updates = await Promise.all(packagesPromises);
dependencyKeys.forEach((key, index) => {
let thePackage = updates.find((mypackage) => (mypackage || {package: ""}).package == key);
if (thePackage) {
console.log(thePackage.package + " - v" + thePackage.version);
}
});
}
handleVersions();
function allElementsAfter(keyword, array) {
let pastValue = false;
let newArray = [];
array.forEach(function (element) {
if (element.includes(keyword)) {
pastValue = true;
}
if (pastValue) {
newArray.push(element);
}
});
return newArray;
}