BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / fs-extra / lib / mkdirs / mkdirs.js

mkdirs.js

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
mkdirs.js 1.6 KiB Download Raw
1var fs = require('graceful-fs')
2var path = require('path')
3var invalidWin32Path = require('./win32').invalidWin32Path
4
5var o777 = parseInt('0777', 8)
6
7function mkdirs (p, opts, callback, made) {
8 if (typeof opts === 'function') {
9 callback = opts
10 opts = {}
11 } else if (!opts || typeof opts !== 'object') {
12 opts = { mode: opts }
13 }
14
15 if (process.platform === 'win32' && invalidWin32Path(p)) {
16 var errInval = new Error(p + ' contains invalid WIN32 path characters.')
17 errInval.code = 'EINVAL'
18 return callback(errInval)
19 }
20
21 var mode = opts.mode
22 var xfs = opts.fs || fs
23
24 if (mode === undefined) {
25 mode = o777 & (~process.umask())
26 }
27 if (!made) made = null
28
29 callback = callback || function () {}
30 p = path.resolve(p)
31
32 xfs.mkdir(p, mode, function (er) {
33 if (!er) {
34 made = made || p
35 return callback(null, made)
36 }
37 switch (er.code) {
38 case 'ENOENT':
39 if (path.dirname(p) === p) return callback(er)
40 mkdirs(path.dirname(p), opts, function (er, made) {
41 if (er) callback(er, made)
42 else mkdirs(p, opts, callback, made)
43 })
44 break
45
46 // In the case of any other error, just see if there's a dir
47 // there already. If so, then hooray! If not, then something
48 // is borked.
49 default:
50 xfs.stat(p, function (er2, stat) {
51 // if the stat fails, then that's super weird.
52 // let the original error be the failure reason.
53 if (er2 || !stat.isDirectory()) callback(er, made)
54 else callback(null, made)
55 })
56 break
57 }
58 })
59}
60
61module.exports = mkdirs