-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (35 loc) · 1.35 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
'use strict';
const readPackages = require('read-packages');
const readLatestVersion = require('latest-version');
const compareVersion = require('compare-versions');
const isEmpty = require('lodash.isempty');
module.exports = async options => {
try {
options = {
dir: process.cwd(),
excludedPackages: [],
preservePrefix: false,
...options
};
const getVersionPrefix = version => version.charAt(0);
const initialPackages = await readPackages({dir: options.dir, flattenPackages: true});
const alteredPackages = await readPackages({dir: options.dir, removePrefix: true, flattenPackages: true});
const packageNames = Object.keys(alteredPackages);
const outdatedPackages = {};
for (const name of packageNames) {
const currentVersionPrefix = getVersionPrefix(initialPackages[name]);
const currentVersion = alteredPackages[name];
const latestVersion = await readLatestVersion(name); // eslint-disable-line no-await-in-loop
if (compareVersion(currentVersion, latestVersion) === -1) {
const currentVersionPrefixAttached = `${currentVersionPrefix}${currentVersion}`;
outdatedPackages[name] = {
current: options.preservePrefix ? currentVersionPrefixAttached : currentVersion,
latest: latestVersion
};
}
}
return isEmpty(outdatedPackages) ? null : outdatedPackages;
} catch (error) {
console.error(error);
}
};