forked from jaredly/stylecleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·79 lines (64 loc) · 2.28 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
const glob = require('glob');
const path = require('path');
const os = require('os');
const processFile = require('./processFile');
function resolveHome(filepath) {
if (filepath[0] === '~') {
return path.join(os.homedir(), filepath.slice(1));
}
return filepath;
}
let [_, __, cmd, ...files] = process.argv;
const cmds = ['check', 'fix', 'fix-force'];
if (process.argv.length === 2) {
cmd = 'check';
files = ['./**/*.js'];
}
if (!cmd || !files.length || cmd === 'help' || cmds.indexOf(cmd) === -1) {
console.log(`\
Usage: stylecleanup [command] some/file/to/check.js
command: one of 'check', 'fix', 'fix-force'
globs are also supported, e.g.
stylecleanup check './src/**/*.js'
check: find & report missing & unused styles
fix: remove all unused styles, but skip styles that *might* be used, but
can't be verified because e.g. the stylesheet variable is passed around,
or there's a computed property access. If you do 'styles[something]' it
might be accessing anything, so we can't know for sure that apparently
unused styles are actually unused.
fix-force: remove all unnused styles, and all 'potentially' unused styles
`);
process.exit();
}
const add = (a, b) => a + b;
const append = (a, b) => [...a, ...b];
const allfiles = files
.map((file) =>
glob
.sync(resolveHome(file))
.filter((x) => x.indexOf('/node_modules/') === -1),
)
.reduce(append, []);
console.log(`Checking ${allfiles.length} files matching ${files.join(' ')}`);
const results = allfiles.map((file) => processFile(file, cmd));
const removed = results.map((r) => r.removed).reduce(add, 0);
const actual = results.filter((node) => node.hasIssue.length > 0).length > 0;
if (cmd === 'check') {
if (actual) {
return process.exit(1);
} else {
return process.exit(0);
}
}
if (cmd !== 'check') {
console.log();
console.log(`Removed ${removed} unused styles`);
const skipped = results.map((r) => r.skipped).reduce(append, []);
if (skipped.length) {
const total = skipped.map((s) => s.count).reduce(add);
console.log(
`Skipped ${total} potentially unused styles in the following ${skipped.length} files b/c of unusual stylesheet references. Run with the 'check' command for more info`,
);
skipped.forEach((f) => console.log(' -', f.count, f.file));
}
}