mkdirp-native.js 969 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. const {dirname} = require('path')
  2. const {findMade, findMadeSync} = require('./find-made.js')
  3. const {mkdirpManual, mkdirpManualSync} = require('./mkdirp-manual.js')
  4. const mkdirpNative = (path, opts) => {
  5. opts.recursive = true
  6. const parent = dirname(path)
  7. if (parent === path)
  8. return opts.mkdirAsync(path, opts)
  9. return findMade(opts, path).then(made =>
  10. opts.mkdirAsync(path, opts).then(() => made)
  11. .catch(er => {
  12. if (er.code === 'ENOENT')
  13. return mkdirpManual(path, opts)
  14. else
  15. throw er
  16. }))
  17. }
  18. const mkdirpNativeSync = (path, opts) => {
  19. opts.recursive = true
  20. const parent = dirname(path)
  21. if (parent === path)
  22. return opts.mkdirSync(path, opts)
  23. const made = findMadeSync(opts, path)
  24. try {
  25. opts.mkdirSync(path, opts)
  26. return made
  27. } catch (er) {
  28. if (er.code === 'ENOENT')
  29. return mkdirpManualSync(path, opts)
  30. else
  31. throw er
  32. }
  33. }
  34. module.exports = {mkdirpNative, mkdirpNativeSync}