UI for Zipcoin Blue

mkdirs-sync.js 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. switch (err0.code) {
  27. case 'ENOENT':
  28. if (path.dirname(p) === p) throw err0
  29. made = mkdirsSync(path.dirname(p), opts, made)
  30. mkdirsSync(p, opts, made)
  31. break
  32. // In the case of any other error, just see if there's a dir
  33. // there already. If so, then hooray! If not, then something
  34. // is borked.
  35. default:
  36. let stat
  37. try {
  38. stat = xfs.statSync(p)
  39. } catch (err1) {
  40. throw err0
  41. }
  42. if (!stat.isDirectory()) throw err0
  43. break
  44. }
  45. }
  46. return made
  47. }
  48. module.exports = mkdirsSync