bin.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. const rimraf = require('./')
  3. const path = require('path')
  4. const isRoot = arg => /^(\/|[a-zA-Z]:\\)$/.test(path.resolve(arg))
  5. const filterOutRoot = arg => {
  6. const ok = preserveRoot === false || !isRoot(arg)
  7. if (!ok) {
  8. console.error(`refusing to remove ${arg}`)
  9. console.error('Set --no-preserve-root to allow this')
  10. }
  11. return ok
  12. }
  13. let help = false
  14. let dashdash = false
  15. let noglob = false
  16. let preserveRoot = true
  17. const args = process.argv.slice(2).filter(arg => {
  18. if (dashdash)
  19. return !!arg
  20. else if (arg === '--')
  21. dashdash = true
  22. else if (arg === '--no-glob' || arg === '-G')
  23. noglob = true
  24. else if (arg === '--glob' || arg === '-g')
  25. noglob = false
  26. else if (arg.match(/^(-+|\/)(h(elp)?|\?)$/))
  27. help = true
  28. else if (arg === '--preserve-root')
  29. preserveRoot = true
  30. else if (arg === '--no-preserve-root')
  31. preserveRoot = false
  32. else
  33. return !!arg
  34. }).filter(arg => !preserveRoot || filterOutRoot(arg))
  35. const go = n => {
  36. if (n >= args.length)
  37. return
  38. const options = noglob ? { glob: false } : {}
  39. rimraf(args[n], options, er => {
  40. if (er)
  41. throw er
  42. go(n+1)
  43. })
  44. }
  45. if (help || args.length === 0) {
  46. // If they didn't ask for help, then this is not a "success"
  47. const log = help ? console.log : console.error
  48. log('Usage: rimraf <path> [<path> ...]')
  49. log('')
  50. log(' Deletes all files and folders at "path" recursively.')
  51. log('')
  52. log('Options:')
  53. log('')
  54. log(' -h, --help Display this usage info')
  55. log(' -G, --no-glob Do not expand glob patterns in arguments')
  56. log(' -g, --glob Expand glob patterns in arguments (default)')
  57. log(' --preserve-root Do not remove \'/\' (default)')
  58. log(' --no-preserve-root Do not treat \'/\' specially')
  59. log(' -- Stop parsing flags')
  60. process.exit(help ? 0 : 1)
  61. } else
  62. go(0)