polyfill.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { dirname } = require('path')
  2. const fileURLToPath = require('../common/file-url-to-path/index.js')
  3. const fs = require('../fs.js')
  4. const defaultOptions = {
  5. mode: 0o777,
  6. recursive: false,
  7. }
  8. const mkdir = async (path, opts) => {
  9. const options = { ...defaultOptions, ...opts }
  10. // if we're not in recursive mode, just call the real mkdir with the path and
  11. // the mode option only
  12. if (!options.recursive) {
  13. return fs.mkdir(path, options.mode)
  14. }
  15. const makeDirectory = async (dir, mode) => {
  16. // we can't use dirname directly since these functions support URL
  17. // objects with the file: protocol as the path input, so first we get a
  18. // string path, then we can call dirname on that
  19. const parent = dir != null && dir.href && dir.origin
  20. ? dirname(fileURLToPath(dir))
  21. : dirname(dir)
  22. // if the parent is the dir itself, try to create it. anything but EISDIR
  23. // should be rethrown
  24. if (parent === dir) {
  25. try {
  26. await fs.mkdir(dir, opts)
  27. } catch (err) {
  28. if (err.code !== 'EISDIR') {
  29. throw err
  30. }
  31. }
  32. return undefined
  33. }
  34. try {
  35. await fs.mkdir(dir, mode)
  36. return dir
  37. } catch (err) {
  38. // ENOENT means the parent wasn't there, so create that
  39. if (err.code === 'ENOENT') {
  40. const made = await makeDirectory(parent, mode)
  41. await makeDirectory(dir, mode)
  42. // return the shallowest path we created, i.e. the result of creating
  43. // the parent
  44. return made
  45. }
  46. // an EEXIST means there's already something there
  47. // an EROFS means we have a read-only filesystem and can't create a dir
  48. // any other error is fatal and we should give up now
  49. if (err.code !== 'EEXIST' && err.code !== 'EROFS') {
  50. throw err
  51. }
  52. // stat the directory, if the result is a directory, then we successfully
  53. // created this one so return its path. otherwise, we reject with the
  54. // original error by ignoring the error in the catch
  55. try {
  56. const stat = await fs.stat(dir)
  57. if (stat.isDirectory()) {
  58. // if it already existed, we didn't create anything so return
  59. // undefined
  60. return undefined
  61. }
  62. } catch (_) {}
  63. // if the thing that's there isn't a directory, then just re-throw
  64. throw err
  65. }
  66. }
  67. return makeDirectory(path, options.mode)
  68. }
  69. module.exports = mkdir