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

make-dir.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Adapted from https://github.com/sindresorhus/make-dir
  2. // Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  4. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  5. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  6. 'use strict'
  7. const fs = require('../fs')
  8. const path = require('path')
  9. const atLeastNode = require('at-least-node')
  10. const useNativeRecursiveOption = atLeastNode('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 processOptions = options => {
  24. const defaults = { mode: 0o777 }
  25. if (typeof options === 'number') options = { mode: options }
  26. return { ...defaults, ...options }
  27. }
  28. const permissionError = pth => {
  29. // This replicates the exception of `fs.mkdir` with native the
  30. // `recusive` option when run on an invalid drive under Windows.
  31. const error = new Error(`operation not permitted, mkdir '${pth}'`)
  32. error.code = 'EPERM'
  33. error.errno = -4048
  34. error.path = pth
  35. error.syscall = 'mkdir'
  36. return error
  37. }
  38. module.exports.makeDir = async (input, options) => {
  39. checkPath(input)
  40. options = processOptions(options)
  41. if (useNativeRecursiveOption) {
  42. const pth = path.resolve(input)
  43. return fs.mkdir(pth, {
  44. mode: options.mode,
  45. recursive: true
  46. })
  47. }
  48. const make = async pth => {
  49. try {
  50. await fs.mkdir(pth, options.mode)
  51. } catch (error) {
  52. if (error.code === 'EPERM') {
  53. throw error
  54. }
  55. if (error.code === 'ENOENT') {
  56. if (path.dirname(pth) === pth) {
  57. throw permissionError(pth)
  58. }
  59. if (error.message.includes('null bytes')) {
  60. throw error
  61. }
  62. await make(path.dirname(pth))
  63. return make(pth)
  64. }
  65. try {
  66. const stats = await fs.stat(pth)
  67. if (!stats.isDirectory()) {
  68. // This error is never exposed to the user
  69. // it is caught below, and the original error is thrown
  70. throw new Error('The path is not a directory')
  71. }
  72. } catch {
  73. throw error
  74. }
  75. }
  76. }
  77. return make(path.resolve(input))
  78. }
  79. module.exports.makeDirSync = (input, options) => {
  80. checkPath(input)
  81. options = processOptions(options)
  82. if (useNativeRecursiveOption) {
  83. const pth = path.resolve(input)
  84. return fs.mkdirSync(pth, {
  85. mode: options.mode,
  86. recursive: true
  87. })
  88. }
  89. const make = pth => {
  90. try {
  91. fs.mkdirSync(pth, options.mode)
  92. } catch (error) {
  93. if (error.code === 'EPERM') {
  94. throw error
  95. }
  96. if (error.code === 'ENOENT') {
  97. if (path.dirname(pth) === pth) {
  98. throw permissionError(pth)
  99. }
  100. if (error.message.includes('null bytes')) {
  101. throw error
  102. }
  103. make(path.dirname(pth))
  104. return make(pth)
  105. }
  106. try {
  107. if (!fs.statSync(pth).isDirectory()) {
  108. // This error is never exposed to the user
  109. // it is caught below, and the original error is thrown
  110. throw new Error('The path is not a directory')
  111. }
  112. } catch {
  113. throw error
  114. }
  115. }
  116. }
  117. return make(path.resolve(input))
  118. }