You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.8 KiB

4 years ago
  1. # make-dir [![Build Status](https://travis-ci.org/sindresorhus/make-dir.svg?branch=master)](https://travis-ci.org/sindresorhus/make-dir) [![codecov](https://codecov.io/gh/sindresorhus/make-dir/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/make-dir)
  2. > Make a directory and its parents if needed - Think `mkdir -p`
  3. ## Advantages over [`mkdirp`](https://github.com/substack/node-mkdirp)
  4. - Promise API *(Async/await ready!)*
  5. - Fixes many `mkdirp` issues: [#96](https://github.com/substack/node-mkdirp/pull/96) [#70](https://github.com/substack/node-mkdirp/issues/70) [#66](https://github.com/substack/node-mkdirp/issues/66)
  6. - 100% test coverage
  7. - CI-tested on macOS, Linux, and Windows
  8. - Actively maintained
  9. - Doesn't bundle a CLI
  10. - Uses native the `fs.mkdir/mkdirSync` [`recursive` option](https://nodejs.org/dist/latest/docs/api/fs.html#fs_fs_mkdir_path_options_callback) in Node.js >=10.12.0 unless [overridden](#fs)
  11. ## Install
  12. ```
  13. $ npm install make-dir
  14. ```
  15. ## Usage
  16. ```
  17. $ pwd
  18. /Users/sindresorhus/fun
  19. $ tree
  20. .
  21. ```
  22. ```js
  23. const makeDir = require('make-dir');
  24. (async () => {
  25. const path = await makeDir('unicorn/rainbow/cake');
  26. console.log(path);
  27. //=> '/Users/sindresorhus/fun/unicorn/rainbow/cake'
  28. })();
  29. ```
  30. ```
  31. $ tree
  32. .
  33. └── unicorn
  34. └── rainbow
  35. └── cake
  36. ```
  37. Multiple directories:
  38. ```js
  39. const makeDir = require('make-dir');
  40. (async () => {
  41. const paths = await Promise.all([
  42. makeDir('unicorn/rainbow'),
  43. makeDir('foo/bar')
  44. ]);
  45. console.log(paths);
  46. /*
  47. [
  48. '/Users/sindresorhus/fun/unicorn/rainbow',
  49. '/Users/sindresorhus/fun/foo/bar'
  50. ]
  51. */
  52. })();
  53. ```
  54. ## API
  55. ### makeDir(path, [options])
  56. Returns a `Promise` for the path to the created directory.
  57. ### makeDir.sync(path, [options])
  58. Returns the path to the created directory.
  59. #### path
  60. Type: `string`
  61. Directory to create.
  62. #### options
  63. Type: `Object`
  64. ##### mode
  65. Type: `integer`<br>
  66. Default: `0o777 & (~process.umask())`
  67. Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/).
  68. ##### fs
  69. Type: `Object`<br>
  70. Default: `require('fs')`
  71. Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs).
  72. Using a custom `fs` implementation will block the use of the native `recursive` option if `fs.mkdir` or `fs.mkdirSync` is not the native function.
  73. ## Related
  74. - [make-dir-cli](https://github.com/sindresorhus/make-dir-cli) - CLI for this module
  75. - [del](https://github.com/sindresorhus/del) - Delete files and directories
  76. - [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching
  77. - [cpy](https://github.com/sindresorhus/cpy) - Copy files
  78. - [cpy-cli](https://github.com/sindresorhus/cpy-cli) - Copy files on the command-line
  79. - [move-file](https://github.com/sindresorhus/move-file) - Move a file
  80. ## License
  81. MIT © [Sindre Sorhus](https://sindresorhus.com)