-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
50 lines (45 loc) · 1.66 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
const execFile = require('child_process').execFile;
const quote = require('shell-quote/quote');
/**
* Get the output of `npm ls <module name>`
*
*/
const npmList = (module) => {
return new Promise((resolve, reject) => {
execFile('npm', ['ls', quote([module])], (err, stdout, stderr) => {
if (err && stdout && stdout.match('(empty)')) return reject(new Error(`${module} was not found in the node_modules tree`));
if (err) return reject(err);
return resolve({name: module, stdout: stdout});
});
});
};
/**
* Determine if `npm ls` has deduped to a single module
* or if multiple versions exist in the node_modules tree
*
* `npm ls` should only print the module's name once if it has
* been deduped (in NPM >=5.x the deduped modules will have a
* "deduped" string that we test for)
*
* Returns an object with the module name, a duplicates boolean,
* and a count of the number of modules found
*
*/
const testModule = (module) => {
// match the module name with a space prefixed so that namespaced
// packages of the same name will not count against the duplication check
const match = module.stdout.match(new RegExp(' ' + module.name + '@','g'));
const dedupeMatch = module.stdout.match(new RegExp('deduped','g'));
let duplicates = true;
if (dedupeMatch) {
if (match && dedupeMatch && (match.length - 1 === dedupeMatch.length)) duplicates = false;
} else {
if (match && match.length === 1) duplicates = false;
}
if (!duplicates) return { duplicates: false, count: 1, name: module.name };
return { duplicates: true, count: match.length, name: module.name };
};
module.exports = {
npmList: npmList,
testModule: testModule
};