11cb0ef41Sopenharmony_ci# mkdirp
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciLike `mkdir -p`, but in Node.js!
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciNow with a modern API and no\* bugs!
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci<small>\* may contain some bugs</small>
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci# example
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci## pow.js
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci```js
141cb0ef41Sopenharmony_ciconst mkdirp = require('mkdirp')
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// return value is a Promise resolving to the first directory created
171cb0ef41Sopenharmony_cimkdirp('/tmp/foo/bar/baz').then(made =>
181cb0ef41Sopenharmony_ci  console.log(`made directories, starting with ${made}`))
191cb0ef41Sopenharmony_ci```
201cb0ef41Sopenharmony_ci
211cb0ef41Sopenharmony_ciOutput (where `/tmp/foo` already exists)
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci```
241cb0ef41Sopenharmony_cimade directories, starting with /tmp/foo/bar
251cb0ef41Sopenharmony_ci```
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciOr, if you don't have time to wait around for promises:
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci```js
301cb0ef41Sopenharmony_ciconst mkdirp = require('mkdirp')
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_ci// return value is the first directory created
331cb0ef41Sopenharmony_ciconst made = mkdirp.sync('/tmp/foo/bar/baz')
341cb0ef41Sopenharmony_ciconsole.log(`made directories, starting with ${made}`)
351cb0ef41Sopenharmony_ci```
361cb0ef41Sopenharmony_ci
371cb0ef41Sopenharmony_ciAnd now /tmp/foo/bar/baz exists, huzzah!
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_ci# methods
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_ci```js
421cb0ef41Sopenharmony_ciconst mkdirp = require('mkdirp')
431cb0ef41Sopenharmony_ci```
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci## mkdirp(dir, [opts]) -> Promise<String | undefined>
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ciCreate a new directory and any necessary subdirectories at `dir` with octal
481cb0ef41Sopenharmony_cipermission string `opts.mode`. If `opts` is a string or number, it will be
491cb0ef41Sopenharmony_citreated as the `opts.mode`.
501cb0ef41Sopenharmony_ci
511cb0ef41Sopenharmony_ciIf `opts.mode` isn't specified, it defaults to `0o777 &
521cb0ef41Sopenharmony_ci(~process.umask())`.
531cb0ef41Sopenharmony_ci
541cb0ef41Sopenharmony_ciPromise resolves to first directory `made` that had to be created, or
551cb0ef41Sopenharmony_ci`undefined` if everything already exists.  Promise rejects if any errors
561cb0ef41Sopenharmony_ciare encountered.  Note that, in the case of promise rejection, some
571cb0ef41Sopenharmony_cidirectories _may_ have been created, as recursive directory creation is not
581cb0ef41Sopenharmony_cian atomic operation.
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ciYou can optionally pass in an alternate `fs` implementation by passing in
611cb0ef41Sopenharmony_ci`opts.fs`. Your implementation should have `opts.fs.mkdir(path, opts, cb)`
621cb0ef41Sopenharmony_ciand `opts.fs.stat(path, cb)`.
631cb0ef41Sopenharmony_ci
641cb0ef41Sopenharmony_ciYou can also override just one or the other of `mkdir` and `stat` by
651cb0ef41Sopenharmony_cipassing in `opts.stat` or `opts.mkdir`, or providing an `fs` option that
661cb0ef41Sopenharmony_cionly overrides one of these.
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci## mkdirp.sync(dir, opts) -> String|null
691cb0ef41Sopenharmony_ci
701cb0ef41Sopenharmony_ciSynchronously create a new directory and any necessary subdirectories at
711cb0ef41Sopenharmony_ci`dir` with octal permission string `opts.mode`. If `opts` is a string or
721cb0ef41Sopenharmony_cinumber, it will be treated as the `opts.mode`.
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ciIf `opts.mode` isn't specified, it defaults to `0o777 &
751cb0ef41Sopenharmony_ci(~process.umask())`.
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciReturns the first directory that had to be created, or undefined if
781cb0ef41Sopenharmony_cieverything already exists.
791cb0ef41Sopenharmony_ci
801cb0ef41Sopenharmony_ciYou can optionally pass in an alternate `fs` implementation by passing in
811cb0ef41Sopenharmony_ci`opts.fs`. Your implementation should have `opts.fs.mkdirSync(path, mode)`
821cb0ef41Sopenharmony_ciand `opts.fs.statSync(path)`.
831cb0ef41Sopenharmony_ci
841cb0ef41Sopenharmony_ciYou can also override just one or the other of `mkdirSync` and `statSync`
851cb0ef41Sopenharmony_ciby passing in `opts.statSync` or `opts.mkdirSync`, or providing an `fs`
861cb0ef41Sopenharmony_cioption that only overrides one of these.
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci## mkdirp.manual, mkdirp.manualSync
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ciUse the manual implementation (not the native one).  This is the default
911cb0ef41Sopenharmony_ciwhen the native implementation is not available or the stat/mkdir
921cb0ef41Sopenharmony_ciimplementation is overridden.
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_ci## mkdirp.native, mkdirp.nativeSync
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ciUse the native implementation (not the manual one).  This is the default
971cb0ef41Sopenharmony_ciwhen the native implementation is available and stat/mkdir are not
981cb0ef41Sopenharmony_cioverridden.
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci# implementation
1011cb0ef41Sopenharmony_ci
1021cb0ef41Sopenharmony_ciOn Node.js v10.12.0 and above, use the native `fs.mkdir(p,
1031cb0ef41Sopenharmony_ci{recursive:true})` option, unless `fs.mkdir`/`fs.mkdirSync` has been
1041cb0ef41Sopenharmony_cioverridden by an option.
1051cb0ef41Sopenharmony_ci
1061cb0ef41Sopenharmony_ci## native implementation
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci- If the path is a root directory, then pass it to the underlying
1091cb0ef41Sopenharmony_ci  implementation and return the result/error.  (In this case, it'll either
1101cb0ef41Sopenharmony_ci  succeed or fail, but we aren't actually creating any dirs.)
1111cb0ef41Sopenharmony_ci- Walk up the path statting each directory, to find the first path that
1121cb0ef41Sopenharmony_ci  will be created, `made`.
1131cb0ef41Sopenharmony_ci- Call `fs.mkdir(path, { recursive: true })` (or `fs.mkdirSync`)
1141cb0ef41Sopenharmony_ci- If error, raise it to the caller.
1151cb0ef41Sopenharmony_ci- Return `made`.
1161cb0ef41Sopenharmony_ci
1171cb0ef41Sopenharmony_ci## manual implementation
1181cb0ef41Sopenharmony_ci
1191cb0ef41Sopenharmony_ci- Call underlying `fs.mkdir` implementation, with `recursive: false`
1201cb0ef41Sopenharmony_ci- If error:
1211cb0ef41Sopenharmony_ci  - If path is a root directory, raise to the caller and do not handle it
1221cb0ef41Sopenharmony_ci  - If ENOENT, mkdirp parent dir, store result as `made`
1231cb0ef41Sopenharmony_ci  - stat(path)
1241cb0ef41Sopenharmony_ci    - If error, raise original `mkdir` error
1251cb0ef41Sopenharmony_ci    - If directory, return `made`
1261cb0ef41Sopenharmony_ci    - Else, raise original `mkdir` error
1271cb0ef41Sopenharmony_ci- else
1281cb0ef41Sopenharmony_ci  - return `undefined` if a root dir, or `made` if set, or `path`
1291cb0ef41Sopenharmony_ci
1301cb0ef41Sopenharmony_ci## windows vs unix caveat
1311cb0ef41Sopenharmony_ci
1321cb0ef41Sopenharmony_ciOn Windows file systems, attempts to create a root directory (ie, a drive
1331cb0ef41Sopenharmony_ciletter or root UNC path) will fail.  If the root directory exists, then it
1341cb0ef41Sopenharmony_ciwill fail with `EPERM`.  If the root directory does not exist, then it will
1351cb0ef41Sopenharmony_cifail with `ENOENT`.
1361cb0ef41Sopenharmony_ci
1371cb0ef41Sopenharmony_ciOn posix file systems, attempts to create a root directory (in recursive
1381cb0ef41Sopenharmony_cimode) will succeed silently, as it is treated like just another directory
1391cb0ef41Sopenharmony_cithat already exists.  (In non-recursive mode, of course, it fails with
1401cb0ef41Sopenharmony_ci`EEXIST`.)
1411cb0ef41Sopenharmony_ci
1421cb0ef41Sopenharmony_ciIn order to preserve this system-specific behavior (and because it's not as
1431cb0ef41Sopenharmony_ciif we can create the parent of a root directory anyway), attempts to create
1441cb0ef41Sopenharmony_cia root directory are passed directly to the `fs` implementation, and any
1451cb0ef41Sopenharmony_cierrors encountered are not handled.
1461cb0ef41Sopenharmony_ci
1471cb0ef41Sopenharmony_ci## native error caveat
1481cb0ef41Sopenharmony_ci
1491cb0ef41Sopenharmony_ciThe native implementation (as of at least Node.js v13.4.0) does not provide
1501cb0ef41Sopenharmony_ciappropriate errors in some cases (see
1511cb0ef41Sopenharmony_ci[nodejs/node#31481](https://github.com/nodejs/node/issues/31481) and
1521cb0ef41Sopenharmony_ci[nodejs/node#28015](https://github.com/nodejs/node/issues/28015)).
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ciIn order to work around this issue, the native implementation will fall
1551cb0ef41Sopenharmony_ciback to the manual implementation if an `ENOENT` error is encountered.
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci# choosing a recursive mkdir implementation
1581cb0ef41Sopenharmony_ci
1591cb0ef41Sopenharmony_ciThere are a few to choose from!  Use the one that suits your needs best :D
1601cb0ef41Sopenharmony_ci
1611cb0ef41Sopenharmony_ci## use `fs.mkdir(path, {recursive: true}, cb)` if:
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci- You wish to optimize performance even at the expense of other factors.
1641cb0ef41Sopenharmony_ci- You don't need to know the first dir created.
1651cb0ef41Sopenharmony_ci- You are ok with getting `ENOENT` as the error when some other problem is
1661cb0ef41Sopenharmony_ci  the actual cause.
1671cb0ef41Sopenharmony_ci- You can limit your platforms to Node.js v10.12 and above.
1681cb0ef41Sopenharmony_ci- You're ok with using callbacks instead of promises.
1691cb0ef41Sopenharmony_ci- You don't need/want a CLI.
1701cb0ef41Sopenharmony_ci- You don't need to override the `fs` methods in use.
1711cb0ef41Sopenharmony_ci
1721cb0ef41Sopenharmony_ci## use this module (mkdirp 1.x) if:
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci- You need to know the first directory that was created.
1751cb0ef41Sopenharmony_ci- You wish to use the native implementation if available, but fall back
1761cb0ef41Sopenharmony_ci  when it's not.
1771cb0ef41Sopenharmony_ci- You prefer promise-returning APIs to callback-taking APIs.
1781cb0ef41Sopenharmony_ci- You want more useful error messages than the native recursive mkdir
1791cb0ef41Sopenharmony_ci  provides (at least as of Node.js v13.4), and are ok with re-trying on
1801cb0ef41Sopenharmony_ci  `ENOENT` to achieve this.
1811cb0ef41Sopenharmony_ci- You need (or at least, are ok with) a CLI.
1821cb0ef41Sopenharmony_ci- You need to override the `fs` methods in use.
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci## use [`make-dir`](http://npm.im/make-dir) if:
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci- You do not need to know the first dir created (and wish to save a few
1871cb0ef41Sopenharmony_ci  `stat` calls when using the native implementation for this reason).
1881cb0ef41Sopenharmony_ci- You wish to use the native implementation if available, but fall back
1891cb0ef41Sopenharmony_ci  when it's not.
1901cb0ef41Sopenharmony_ci- You prefer promise-returning APIs to callback-taking APIs.
1911cb0ef41Sopenharmony_ci- You are ok with occasionally getting `ENOENT` errors for failures that
1921cb0ef41Sopenharmony_ci  are actually related to something other than a missing file system entry.
1931cb0ef41Sopenharmony_ci- You don't need/want a CLI.
1941cb0ef41Sopenharmony_ci- You need to override the `fs` methods in use.
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci## use mkdirp 0.x if:
1971cb0ef41Sopenharmony_ci
1981cb0ef41Sopenharmony_ci- You need to know the first directory that was created.
1991cb0ef41Sopenharmony_ci- You need (or at least, are ok with) a CLI.
2001cb0ef41Sopenharmony_ci- You need to override the `fs` methods in use.
2011cb0ef41Sopenharmony_ci- You're ok with using callbacks instead of promises.
2021cb0ef41Sopenharmony_ci- You are not running on Windows, where the root-level ENOENT errors can
2031cb0ef41Sopenharmony_ci  lead to infinite regress.
2041cb0ef41Sopenharmony_ci- You think vinyl just sounds warmer and richer for some weird reason.
2051cb0ef41Sopenharmony_ci- You are supporting truly ancient Node.js versions, before even the advent
2061cb0ef41Sopenharmony_ci  of a `Promise` language primitive.  (Please don't.  You deserve better.)
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci# cli
2091cb0ef41Sopenharmony_ci
2101cb0ef41Sopenharmony_ciThis package also ships with a `mkdirp` command.
2111cb0ef41Sopenharmony_ci
2121cb0ef41Sopenharmony_ci```
2131cb0ef41Sopenharmony_ci$ mkdirp -h
2141cb0ef41Sopenharmony_ci
2151cb0ef41Sopenharmony_ciusage: mkdirp [DIR1,DIR2..] {OPTIONS}
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci  Create each supplied directory including any necessary parent directories
2181cb0ef41Sopenharmony_ci  that don't yet exist.
2191cb0ef41Sopenharmony_ci
2201cb0ef41Sopenharmony_ci  If the directory already exists, do nothing.
2211cb0ef41Sopenharmony_ci
2221cb0ef41Sopenharmony_ciOPTIONS are:
2231cb0ef41Sopenharmony_ci
2241cb0ef41Sopenharmony_ci  -m<mode>       If a directory needs to be created, set the mode as an octal
2251cb0ef41Sopenharmony_ci  --mode=<mode>  permission string.
2261cb0ef41Sopenharmony_ci
2271cb0ef41Sopenharmony_ci  -v --version   Print the mkdirp version number
2281cb0ef41Sopenharmony_ci
2291cb0ef41Sopenharmony_ci  -h --help      Print this helpful banner
2301cb0ef41Sopenharmony_ci
2311cb0ef41Sopenharmony_ci  -p --print     Print the first directories created for each path provided
2321cb0ef41Sopenharmony_ci
2331cb0ef41Sopenharmony_ci  --manual       Use manual implementation, even if native is available
2341cb0ef41Sopenharmony_ci```
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci# install
2371cb0ef41Sopenharmony_ci
2381cb0ef41Sopenharmony_ciWith [npm](http://npmjs.org) do:
2391cb0ef41Sopenharmony_ci
2401cb0ef41Sopenharmony_ci```
2411cb0ef41Sopenharmony_cinpm install mkdirp
2421cb0ef41Sopenharmony_ci```
2431cb0ef41Sopenharmony_ci
2441cb0ef41Sopenharmony_cito get the library locally, or
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci```
2471cb0ef41Sopenharmony_cinpm install -g mkdirp
2481cb0ef41Sopenharmony_ci```
2491cb0ef41Sopenharmony_ci
2501cb0ef41Sopenharmony_cito get the command everywhere, or
2511cb0ef41Sopenharmony_ci
2521cb0ef41Sopenharmony_ci```
2531cb0ef41Sopenharmony_cinpx mkdirp ...
2541cb0ef41Sopenharmony_ci```
2551cb0ef41Sopenharmony_ci
2561cb0ef41Sopenharmony_cito run the command without installing it globally.
2571cb0ef41Sopenharmony_ci
2581cb0ef41Sopenharmony_ci# platform support
2591cb0ef41Sopenharmony_ci
2601cb0ef41Sopenharmony_ciThis module works on node v8, but only v10 and above are officially
2611cb0ef41Sopenharmony_cisupported, as Node v8 reached its LTS end of life 2020-01-01, which is in
2621cb0ef41Sopenharmony_cithe past, as of this writing.
2631cb0ef41Sopenharmony_ci
2641cb0ef41Sopenharmony_ci# license
2651cb0ef41Sopenharmony_ci
2661cb0ef41Sopenharmony_ciMIT
267