1#!/usr/bin/env node
2
3const usage = () => `
4usage: 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
11OPTIONS 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
25const dirs = []
26const opts = {}
27let print = false
28let dashdash = false
29let manual = false
30for (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
56const mkdirp = require('../')
57const impl = manual ? mkdirp.manual : mkdirp
58if (dirs.length === 0)
59  console.error(usage())
60
61Promise.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  })
69