index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const fs = require('fs');
  3. const path = require('path');
  4. const pify = require('pify');
  5. const semver = require('semver');
  6. const defaults = {
  7. mode: 0o777 & (~process.umask()),
  8. fs
  9. };
  10. const useNativeRecursiveOption = semver.satisfies(process.version, '>=10.12.0');
  11. // https://github.com/nodejs/node/issues/8987
  12. // https://github.com/libuv/libuv/pull/1088
  13. const checkPath = pth => {
  14. if (process.platform === 'win32') {
  15. const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''));
  16. if (pathHasInvalidWinCharacters) {
  17. const error = new Error(`Path contains invalid characters: ${pth}`);
  18. error.code = 'EINVAL';
  19. throw error;
  20. }
  21. }
  22. };
  23. const permissionError = pth => {
  24. // This replicates the exception of `fs.mkdir` with native the
  25. // `recusive` option when run on an invalid drive under Windows.
  26. const error = new Error(`operation not permitted, mkdir '${pth}'`);
  27. error.code = 'EPERM';
  28. error.errno = -4048;
  29. error.path = pth;
  30. error.syscall = 'mkdir';
  31. return error;
  32. };
  33. const makeDir = (input, options) => Promise.resolve().then(() => {
  34. checkPath(input);
  35. options = Object.assign({}, defaults, options);
  36. // TODO: Use util.promisify when targeting Node.js 8
  37. const mkdir = pify(options.fs.mkdir);
  38. const stat = pify(options.fs.stat);
  39. if (useNativeRecursiveOption && options.fs.mkdir === fs.mkdir) {
  40. const pth = path.resolve(input);
  41. return mkdir(pth, {
  42. mode: options.mode,
  43. recursive: true
  44. }).then(() => pth);
  45. }
  46. const make = pth => {
  47. return mkdir(pth, options.mode)
  48. .then(() => pth)
  49. .catch(error => {
  50. if (error.code === 'EPERM') {
  51. throw error;
  52. }
  53. if (error.code === 'ENOENT') {
  54. if (path.dirname(pth) === pth) {
  55. throw permissionError(pth);
  56. }
  57. if (error.message.includes('null bytes')) {
  58. throw error;
  59. }
  60. return make(path.dirname(pth)).then(() => make(pth));
  61. }
  62. return stat(pth)
  63. .then(stats => stats.isDirectory() ? pth : Promise.reject())
  64. .catch(() => {
  65. throw error;
  66. });
  67. });
  68. };
  69. return make(path.resolve(input));
  70. });
  71. module.exports = makeDir;
  72. module.exports.default = makeDir;
  73. module.exports.sync = (input, options) => {
  74. checkPath(input);
  75. options = Object.assign({}, defaults, options);
  76. if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) {
  77. const pth = path.resolve(input);
  78. fs.mkdirSync(pth, {
  79. mode: options.mode,
  80. recursive: true
  81. });
  82. return pth;
  83. }
  84. const make = pth => {
  85. try {
  86. options.fs.mkdirSync(pth, options.mode);
  87. } catch (error) {
  88. if (error.code === 'EPERM') {
  89. throw error;
  90. }
  91. if (error.code === 'ENOENT') {
  92. if (path.dirname(pth) === pth) {
  93. throw permissionError(pth);
  94. }
  95. if (error.message.includes('null bytes')) {
  96. throw error;
  97. }
  98. make(path.dirname(pth));
  99. return make(pth);
  100. }
  101. try {
  102. if (!options.fs.statSync(pth).isDirectory()) {
  103. throw new Error('The path is not a directory');
  104. }
  105. } catch (_) {
  106. throw error;
  107. }
  108. }
  109. return pth;
  110. };
  111. return make(path.resolve(input));
  112. };