You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.2 KiB

4 years ago
  1. 'use strict'
  2. const fs = require('graceful-fs')
  3. const path = require('path')
  4. const invalidWin32Path = require('./win32').invalidWin32Path
  5. const o777 = parseInt('0777', 8)
  6. function mkdirsSync (p, opts, made) {
  7. if (!opts || typeof opts !== 'object') {
  8. opts = { mode: opts }
  9. }
  10. let mode = opts.mode
  11. const xfs = opts.fs || fs
  12. if (process.platform === 'win32' && invalidWin32Path(p)) {
  13. const errInval = new Error(p + ' contains invalid WIN32 path characters.')
  14. errInval.code = 'EINVAL'
  15. throw errInval
  16. }
  17. if (mode === undefined) {
  18. mode = o777 & (~process.umask())
  19. }
  20. if (!made) made = null
  21. p = path.resolve(p)
  22. try {
  23. xfs.mkdirSync(p, mode)
  24. made = made || p
  25. } catch (err0) {
  26. if (err0.code === 'ENOENT') {
  27. if (path.dirname(p) === p) throw err0
  28. made = mkdirsSync(path.dirname(p), opts, made)
  29. mkdirsSync(p, opts, made)
  30. } else {
  31. // In the case of any other error, just see if there's a dir there
  32. // already. If so, then hooray! If not, then something is borked.
  33. let stat
  34. try {
  35. stat = xfs.statSync(p)
  36. } catch (err1) {
  37. throw err0
  38. }
  39. if (!stat.isDirectory()) throw err0
  40. }
  41. }
  42. return made
  43. }
  44. module.exports = mkdirsSync