cmd.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/env node
  2. const usage = () => `
  3. usage: mkdirp [DIR1,DIR2..] {OPTIONS}
  4. Create each supplied directory including any necessary parent directories
  5. that don't yet exist.
  6. If the directory already exists, do nothing.
  7. OPTIONS are:
  8. -m<mode> If a directory needs to be created, set the mode as an octal
  9. --mode=<mode> permission string.
  10. -v --version Print the mkdirp version number
  11. -h --help Print this helpful banner
  12. -p --print Print the first directories created for each path provided
  13. --manual Use manual implementation, even if native is available
  14. `
  15. const dirs = []
  16. const opts = {}
  17. let print = false
  18. let dashdash = false
  19. let manual = false
  20. for (const arg of process.argv.slice(2)) {
  21. if (dashdash)
  22. dirs.push(arg)
  23. else if (arg === '--')
  24. dashdash = true
  25. else if (arg === '--manual')
  26. manual = true
  27. else if (/^-h/.test(arg) || /^--help/.test(arg)) {
  28. console.log(usage())
  29. process.exit(0)
  30. } else if (arg === '-v' || arg === '--version') {
  31. console.log(require('../package.json').version)
  32. process.exit(0)
  33. } else if (arg === '-p' || arg === '--print') {
  34. print = true
  35. } else if (/^-m/.test(arg) || /^--mode=/.test(arg)) {
  36. const mode = parseInt(arg.replace(/^(-m|--mode=)/, ''), 8)
  37. if (isNaN(mode)) {
  38. console.error(`invalid mode argument: ${arg}\nMust be an octal number.`)
  39. process.exit(1)
  40. }
  41. opts.mode = mode
  42. } else
  43. dirs.push(arg)
  44. }
  45. const mkdirp = require('../')
  46. const impl = manual ? mkdirp.manual : mkdirp
  47. if (dirs.length === 0)
  48. console.error(usage())
  49. Promise.all(dirs.map(dir => impl(dir, opts)))
  50. .then(made => print ? made.forEach(m => m && console.log(m)) : null)
  51. .catch(er => {
  52. console.error(er.message)
  53. if (er.code)
  54. console.error(' code: ' + er.code)
  55. process.exit(1)
  56. })