Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

index.js 3.1KB

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