-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
102 lines (89 loc) · 2.72 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";
const path = require("path");
const micromatch = require("micromatch");
const { analyze } = require("./lib/analyze");
async function main(argv, output) {
let retCode = 0;
try {
const root = argv._[0] || ".";
const ignoreModules = argv.ignores ? argv.ignores.split(",") : [];
const ignoreDirs = argv["ignore-dirs"]
? argv["ignore-dirs"].split(",")
: [];
let {
dependencies,
devDependencies,
usedDependencies,
usedDevDependencies,
unusedDependencies,
unusedDevDependencies,
missingDependencies,
missingDevDependencies,
} = await analyze(root, ignoreDirs, output);
unusedDependencies = unusedDependencies.filter(
(dep) => !micromatch.isMatch(dep, ignoreModules)
);
if (unusedDependencies.length > 0) {
output.log("Unused dependencies");
for (const dep of unusedDependencies) {
output.log(`* ${dep}`);
}
retCode = 1;
}
unusedDevDependencies = unusedDevDependencies.filter(
(dep) => !micromatch.isMatch(dep, ignoreModules)
);
if (unusedDevDependencies.length > 0) {
output.log("Unused devDependencies");
for (const dep of unusedDevDependencies) {
output.log(`* ${dep}`);
}
retCode = 1;
}
missingDependencies = missingDependencies.filter(
(dep) => !ignoreModules.includes(dep)
);
if (missingDependencies.length > 0) {
output.log("Missing dependencies");
for (const dep of missingDependencies) {
const referencedFrom = usedDependencies[dep];
const x = [ ...referencedFrom ]
.map((ref) => `"${path.relative(root, ref)}"`)
.join(", ");
if (devDependencies[dep]) {
output.log(
`* ${dep}: ${x} (exists in devDependencies but needed in dependencies!)`
);
} else {
output.log(`* ${dep}: ${x}`);
}
}
retCode = 1;
}
missingDevDependencies = missingDevDependencies.filter(
(dep) => !ignoreModules.includes(dep)
);
if (missingDevDependencies.length > 0) {
output.log("Missing devDependencies");
for (const dep of missingDevDependencies) {
const referencedFrom = usedDevDependencies[dep];
const x = [ ...referencedFrom ]
.map((ref) => `"${path.relative(root, ref)}"`)
.join(", ");
if (dependencies[dep]) {
output.log(
`* ${dep}: ${x} (exists in dependencies but is only used as dev, maybe move it to devDependencies?)`
);
} else {
output.log(`* ${dep}: ${x}`);
}
}
retCode = 1;
}
} catch (e) {
output.error(e.message);
retCode = 1;
}
return retCode;
}
module.exports = main;