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