index.js 3.0 KB

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