-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·52 lines (42 loc) · 1.18 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
#!/usr/bin/env node
const readline = require('readline');
const shell = require('shelljs');
const ora = require('ora');
let outputText = '';
let dirs = [];
let i = 0;
const spinner = ora('searching node_modules...').start();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// TODO: how to find node_modules for Windows users?
// const node_modules = shell.find('.').filter(function (file) {
// return file.match(/node_modules$/);
// });
const child = shell.exec('find . -name node_modules -type d', { async: true, silent: true });
child.stdout.on('data', function (data) {
outputText += data;
});
child.stdout.on('end', function () {
spinner.stop();
const node_modules = outputText.split('\n');
dirs = node_modules.filter(file => {
const count = file
.split('/')
.reduce((acc, cur) => cur === 'node_modules' ? acc + 1 : acc + 0, 0);
return file.match(/node_modules$/) && count === 1;
});
rl.emit('line');
});
rl.on('line', function (line) {
if (i === dirs.length) {
process.exit();
}
if (line === 'y') {
shell.rm('-rf', dirs[i-1]);
}
rl.setPrompt(`remove ${dirs[i]} (y/n) ? `);
rl.prompt();
i += 1;
});