-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli.js
executable file
·88 lines (77 loc) · 2.01 KB
/
cli.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
#!/usr/bin/env node
const nmPrune = require('.');
const { Confirm, } = require('enquirer');
const prettyBytes = require('pretty-bytes');
const fs = require('fs-extra');
const meow = require('meow');
const cli = meow(
`
Usage
$ nm-prune <input>
Options
-f, --force Skip confirmation and run
-l, --prune-license Prune license files too
Examples
$ nm-prune --force
Scanning node_modules…
Pruning /Users/dave/code/nm-prune/node_modules
Delete 1773 files (6.54 MB) and 126 folders
Removing files…
✔ Files removed
Removing directories…
✔ Directories removed
`,
{
flags: {
force: {
type: 'boolean',
alias: 'f',
default: false,
},
pruneLicense: {
type: 'boolean',
alias: 'l',
default: false,
},
},
}
);
const force = !!cli.flags.force;
const pruneLicense = !!cli.flags.pruneLicense;
// eslint-disable-next-line no-console
const log = str => console.log(str);
log('Scanning node_modules…\n');
nmPrune.prep(process.cwd(), { pruneLicense, }).then(info =>
new Promise((resolve) => {
log(`Pruning ${info.modulePath}`);
if (info.usingCustomPrune) {
log(`Using custom prune: ${info.prunePath}`);
}
const message = `Delete ${info.fileCount} files (${prettyBytes(info.size)}) and ${
info.dirCount
} folders`;
if (force) {
log(message);
return resolve(true);
}
const question = new Confirm({ message, });
return question.run().then(resolve, (err) => {
log(err);
});
}).then((doDelete) => {
if (!doDelete) {
log('Ok, nothing has changed');
return null;
}
log('\nRemoving files…');
return Promise.all(info.files.map(fullPath => fs.remove(fullPath)))
.then(() => {
log('✔ Files removed\n');
log('Removing directories…');
return Promise.all(info.dirs.map(fullPath => fs.remove(fullPath)));
})
.then(() => {
log('✔ Directories removed');
})
.catch(e => log(e));
}));