-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbin.js
More file actions
executable file
·54 lines (44 loc) · 1.3 KB
/
bin.js
File metadata and controls
executable file
·54 lines (44 loc) · 1.3 KB
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
#!/usr/bin/env node
const { homedir } = require('os');
const { normalize, resolve } = require('path');
const { premove } = require('./sync');
const argv = process.argv.slice(2);
if (argv.includes('--help') || argv.includes('-h')) {
let msg = '\n Description\n Remove all items recursively\n';
msg += '\n Usage\n $ premove [options] [...paths]\n';
msg += '\n Options';
msg += '\n --cwd Directory to resolve from (default ".")';
msg += '\n --help Displays this message\n';
console.log(msg); process.exit(0);
}
let cwd = resolve('.');
let idx = argv.indexOf('--cwd');
if (idx !== -1) {
let num = 1;
if ((idx + 1) !== argv.length) {
cwd = resolve(cwd, argv[idx + num++]);
}
argv.splice(idx, num);
}
const home = homedir();
const isFlag = /^-{1,2}/;
const isRoot = /^(\/|[a-zA-Z]:\\)$/;
let i=0, tmp, dirs=[];
for (; i < argv.length; i++) {
tmp = argv[i];
if (!tmp || isFlag.test(tmp)) continue;
tmp = normalize(resolve(cwd, tmp));
if (tmp === home || isRoot.test(tmp)) {
console.error('! will not remove home and/or root directory :: "%s"', argv[i]);
continue;
}
if (!tmp.startsWith(cwd)) {
console.error('! will not remove items outside `--cwd` scope :: "%s"', argv[i]);
continue;
}
dirs.push(tmp);
}
dirs.sort(); // length
for (i=0; i < dirs.length;) {
premove(dirs[i++]);
}