11cb0ef41Sopenharmony_ci#!/usr/bin/env node
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst usage = () => `
41cb0ef41Sopenharmony_ciusage: mkdirp [DIR1,DIR2..] {OPTIONS}
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ci  Create each supplied directory including any necessary parent directories
71cb0ef41Sopenharmony_ci  that don't yet exist.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci  If the directory already exists, do nothing.
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciOPTIONS are:
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci  -m<mode>       If a directory needs to be created, set the mode as an octal
141cb0ef41Sopenharmony_ci  --mode=<mode>  permission string.
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci  -v --version   Print the mkdirp version number
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci  -h --help      Print this helpful banner
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci  -p --print     Print the first directories created for each path provided
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  --manual       Use manual implementation, even if native is available
231cb0ef41Sopenharmony_ci`
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ciconst dirs = []
261cb0ef41Sopenharmony_ciconst opts = {}
271cb0ef41Sopenharmony_cilet print = false
281cb0ef41Sopenharmony_cilet dashdash = false
291cb0ef41Sopenharmony_cilet manual = false
301cb0ef41Sopenharmony_cifor (const arg of process.argv.slice(2)) {
311cb0ef41Sopenharmony_ci  if (dashdash)
321cb0ef41Sopenharmony_ci    dirs.push(arg)
331cb0ef41Sopenharmony_ci  else if (arg === '--')
341cb0ef41Sopenharmony_ci    dashdash = true
351cb0ef41Sopenharmony_ci  else if (arg === '--manual')
361cb0ef41Sopenharmony_ci    manual = true
371cb0ef41Sopenharmony_ci  else if (/^-h/.test(arg) || /^--help/.test(arg)) {
381cb0ef41Sopenharmony_ci    console.log(usage())
391cb0ef41Sopenharmony_ci    process.exit(0)
401cb0ef41Sopenharmony_ci  } else if (arg === '-v' || arg === '--version') {
411cb0ef41Sopenharmony_ci    console.log(require('../package.json').version)
421cb0ef41Sopenharmony_ci    process.exit(0)
431cb0ef41Sopenharmony_ci  } else if (arg === '-p' || arg === '--print') {
441cb0ef41Sopenharmony_ci    print = true
451cb0ef41Sopenharmony_ci  } else if (/^-m/.test(arg) || /^--mode=/.test(arg)) {
461cb0ef41Sopenharmony_ci    const mode = parseInt(arg.replace(/^(-m|--mode=)/, ''), 8)
471cb0ef41Sopenharmony_ci    if (isNaN(mode)) {
481cb0ef41Sopenharmony_ci      console.error(`invalid mode argument: ${arg}\nMust be an octal number.`)
491cb0ef41Sopenharmony_ci      process.exit(1)
501cb0ef41Sopenharmony_ci    }
511cb0ef41Sopenharmony_ci    opts.mode = mode
521cb0ef41Sopenharmony_ci  } else
531cb0ef41Sopenharmony_ci    dirs.push(arg)
541cb0ef41Sopenharmony_ci}
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciconst mkdirp = require('../')
571cb0ef41Sopenharmony_ciconst impl = manual ? mkdirp.manual : mkdirp
581cb0ef41Sopenharmony_ciif (dirs.length === 0)
591cb0ef41Sopenharmony_ci  console.error(usage())
601cb0ef41Sopenharmony_ci
611cb0ef41Sopenharmony_ciPromise.all(dirs.map(dir => impl(dir, opts)))
621cb0ef41Sopenharmony_ci  .then(made => print ? made.forEach(m => m && console.log(m)) : null)
631cb0ef41Sopenharmony_ci  .catch(er => {
641cb0ef41Sopenharmony_ci    console.error(er.message)
651cb0ef41Sopenharmony_ci    if (er.code)
661cb0ef41Sopenharmony_ci      console.error('  code: ' + er.code)
671cb0ef41Sopenharmony_ci    process.exit(1)
681cb0ef41Sopenharmony_ci  })
69