Skip to content

Commit

Permalink
feat: add --preserve-root (default) option
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Aug 14, 2019
1 parent 736887a commit d6ab4ec
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,22 @@

const rimraf = require('./')

const path = require('path')

const isRoot = arg => /^(\/|[a-zA-Z]:\\)$/.test(path.resolve(arg))
const filterOutRoot = arg => {
const ok = preserveRoot === false || !isRoot(arg)
if (!ok) {
console.error(`refusing to remove ${arg}`)
console.error('Set --no-preserve-root to allow this')
}
return ok
}

let help = false
let dashdash = false
let noglob = false
let preserveRoot = true
const args = process.argv.slice(2).filter(arg => {
if (dashdash)
return !!arg
Expand All @@ -16,9 +29,13 @@ const args = process.argv.slice(2).filter(arg => {
noglob = false
else if (arg.match(/^(-+|\/)(h(elp)?|\?)$/))
help = true
else if (arg === '--preserve-root')
preserveRoot = true
else if (arg === '--no-preserve-root')
preserveRoot = false
else
return !!arg
})
}).filter(arg => !preserveRoot || filterOutRoot(arg))

const go = n => {
if (n >= args.length)
Expand All @@ -40,9 +57,12 @@ if (help || args.length === 0) {
log('')
log('Options:')
log('')
log(' -h, --help Display this usage info')
log(' -G, --no-glob Do not expand glob patterns in arguments')
log(' -g, --glob Expand glob patterns in arguments (default)')
log(' -h, --help Display this usage info')
log(' -G, --no-glob Do not expand glob patterns in arguments')
log(' -g, --glob Expand glob patterns in arguments (default)')
log(' --preserve-root Do not remove \'/\' (default)')
log(' --no-preserve-root Do not treat \'/\' specially')
log(' -- Stop parsing flags')
process.exit(help ? 0 : 1)
} else
go(0)

0 comments on commit d6ab4ec

Please sign in to comment.