[6a3a178] | 1 | #!/usr/bin/env node
|
---|
| 2 |
|
---|
| 3 | const rimraf = require('./')
|
---|
| 4 |
|
---|
| 5 | const path = require('path')
|
---|
| 6 |
|
---|
| 7 | const isRoot = arg => /^(\/|[a-zA-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 |
|
---|
| 17 | let help = false
|
---|
| 18 | let dashdash = false
|
---|
| 19 | let noglob = false
|
---|
| 20 | let preserveRoot = true
|
---|
| 21 | const args = process.argv.slice(2).filter(arg => {
|
---|
| 22 | if (dashdash)
|
---|
| 23 | return !!arg
|
---|
| 24 | else if (arg === '--')
|
---|
| 25 | dashdash = true
|
---|
| 26 | else if (arg === '--no-glob' || arg === '-G')
|
---|
| 27 | noglob = true
|
---|
| 28 | else if (arg === '--glob' || arg === '-g')
|
---|
| 29 | noglob = false
|
---|
| 30 | else if (arg.match(/^(-+|\/)(h(elp)?|\?)$/))
|
---|
| 31 | help = true
|
---|
| 32 | else if (arg === '--preserve-root')
|
---|
| 33 | preserveRoot = true
|
---|
| 34 | else if (arg === '--no-preserve-root')
|
---|
| 35 | preserveRoot = false
|
---|
| 36 | else
|
---|
| 37 | return !!arg
|
---|
| 38 | }).filter(arg => !preserveRoot || filterOutRoot(arg))
|
---|
| 39 |
|
---|
| 40 | const go = n => {
|
---|
| 41 | if (n >= args.length)
|
---|
| 42 | return
|
---|
| 43 | const options = noglob ? { glob: false } : {}
|
---|
| 44 | rimraf(args[n], options, er => {
|
---|
| 45 | if (er)
|
---|
| 46 | throw er
|
---|
| 47 | go(n+1)
|
---|
| 48 | })
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | if (help || args.length === 0) {
|
---|
| 52 | // If they didn't ask for help, then this is not a "success"
|
---|
| 53 | const log = help ? console.log : console.error
|
---|
| 54 | log('Usage: rimraf <path> [<path> ...]')
|
---|
| 55 | log('')
|
---|
| 56 | log(' Deletes all files and folders at "path" recursively.')
|
---|
| 57 | log('')
|
---|
| 58 | log('Options:')
|
---|
| 59 | log('')
|
---|
| 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')
|
---|
| 66 | process.exit(help ? 0 : 1)
|
---|
| 67 | } else
|
---|
| 68 | go(0)
|
---|