2
2
3
3
const rimraf = require ( './' )
4
4
5
+ const path = require ( 'path' )
6
+
7
+ const isRoot = arg => / ^ ( \/ | [ a - z A - Z ] : \\ ) $ / . test ( path . resolve ( arg ) )
8
+ const filterOutRoot = arg => {
9
+ const ok = preserveRoot === false || ! isRoot ( arg )
10
+ if ( ! ok ) {
11
+ console . error ( `refusing to remove ${ arg } ` )
12
+ console . error ( 'Set --no-preserve-root to allow this' )
13
+ }
14
+ return ok
15
+ }
16
+
5
17
let help = false
6
18
let dashdash = false
7
19
let noglob = false
20
+ let preserveRoot = true
8
21
const args = process . argv . slice ( 2 ) . filter ( arg => {
9
22
if ( dashdash )
10
23
return ! ! arg
@@ -16,9 +29,13 @@ const args = process.argv.slice(2).filter(arg => {
16
29
noglob = false
17
30
else if ( arg . match ( / ^ ( - + | \/ ) ( h ( e l p ) ? | \? ) $ / ) )
18
31
help = true
32
+ else if ( arg === '--preserve-root' )
33
+ preserveRoot = true
34
+ else if ( arg === '--no-preserve-root' )
35
+ preserveRoot = false
19
36
else
20
37
return ! ! arg
21
- } )
38
+ } ) . filter ( arg => ! preserveRoot || filterOutRoot ( arg ) )
22
39
23
40
const go = n => {
24
41
if ( n >= args . length )
@@ -40,9 +57,12 @@ if (help || args.length === 0) {
40
57
log ( '' )
41
58
log ( 'Options:' )
42
59
log ( '' )
43
- log ( ' -h, --help Display this usage info' )
44
- log ( ' -G, --no-glob Do not expand glob patterns in arguments' )
45
- log ( ' -g, --glob Expand glob patterns in arguments (default)' )
60
+ log ( ' -h, --help Display this usage info' )
61
+ log ( ' -G, --no-glob Do not expand glob patterns in arguments' )
62
+ log ( ' -g, --glob Expand glob patterns in arguments (default)' )
63
+ log ( ' --preserve-root Do not remove \'/\' (default)' )
64
+ log ( ' --no-preserve-root Do not treat \'/\' specially' )
65
+ log ( ' -- Stop parsing flags' )
46
66
process . exit ( help ? 0 : 1 )
47
67
} else
48
68
go ( 0 )
0 commit comments