Skip to content

Commit ab92b24

Browse files
authored
BREAKING: Use internal fork of make-dir for mkdirs implementation (#756)
* BREAKING: Use internal fork of make-dir for mkdirs implementation Resolves #619 Everything should work similarly to how it did before; except that we no longer return a file path on success (to match fs.mkdir). Also, errors may be different. * Hopefully fix Windows tests - Error codes are different - Match fs.mkdir behavior on Windows when creating root * Port sindresorhus/make-dir#24 * Add comment for clarity * Use at-least-node for version sniffing * Consistent error codes across OSes * Allow different error codes on different Node versions
1 parent 075c2d1 commit ab92b24

11 files changed

+167
-241
lines changed

lib/mkdirs/__tests__/clobber.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ describe('mkdirp / clobber', () => {
4242
it('should clobber', done => {
4343
fse.mkdirp(file, 0o755, err => {
4444
assert.ok(err)
45-
if (os.platform().indexOf('win') === 0) {
46-
assert.strictEqual(err.code, 'EEXIST')
47-
} else {
48-
assert.strictEqual(err.code, 'ENOTDIR')
49-
}
45+
assert.strictEqual(err.code, 'ENOTDIR')
5046
done()
5147
})
5248
})

lib/mkdirs/__tests__/issue-93.test.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const os = require('os')
44
const fse = require(process.cwd())
55
const path = require('path')
66
const assert = require('assert')
7+
const util = require('util')
78

89
/* global before, describe, it */
910

@@ -23,13 +24,19 @@ describe('mkdirp: issue-93, win32, when drive does not exist, it should return a
2324

2425
it('should return a cleaner error than inifinite loop, stack crash', done => {
2526
const file = 'R:\\afasd\\afaff\\fdfd' // hopefully drive 'r' does not exist on appveyor
27+
// Different error codes on different Node versions (matches native mkdir behavior)
28+
const assertErr = (err) => assert(
29+
['EPERM', 'ENOENT'].includes(err.code),
30+
`expected 'EPERM' or 'ENOENT', got ${util.inspect(err.code)}`
31+
)
32+
2633
fse.mkdirp(file, err => {
27-
assert.strictEqual(err.code, 'ENOENT')
34+
assertErr(err)
2835

2936
try {
3037
fse.mkdirsSync(file)
3138
} catch (err) {
32-
assert.strictEqual(err.code, 'ENOENT')
39+
assertErr(err)
3340
}
3441

3542
done()

lib/mkdirs/__tests__/opts-undef.test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const os = require('os')
55
const fse = require(process.cwd())
66
const path = require('path')
77
const assert = require('assert')
8-
const mkdirs = require('../mkdirs')
98

109
/* global beforeEach, describe, it */
1110

@@ -22,7 +21,7 @@ describe('mkdirs / opts-undef', () => {
2221
const newDir = path.join(TEST_DIR, 'doest', 'not', 'exist')
2322
assert(!fs.existsSync(newDir))
2423

25-
mkdirs(newDir, undefined, err => {
24+
fse.mkdirs(newDir, undefined, err => {
2625
assert.ifError(err)
2726
assert(fs.existsSync(newDir))
2827
done()

lib/mkdirs/__tests__/return.test.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

lib/mkdirs/__tests__/return_sync.test.js

Lines changed: 0 additions & 39 deletions
This file was deleted.

lib/mkdirs/__tests__/root.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ const assert = require('assert')
88
/* global describe, it */
99

1010
describe('mkdirp / root', () => {
11-
// '/' on unix, 'c:/' on windows.
11+
// '/' on unix
1212
const dir = path.normalize(path.resolve(path.sep)).toLowerCase()
1313

14-
// if not 'c:\\' or 'd:\\', it's probably a network mounted drive, this fails then. TODO: investigate
15-
if (process.platform === 'win32' && (dir.indexOf('c:\\') === -1) && (dir.indexOf('d:\\') === -1)) return
14+
// Windows does not have permission to mkdir on root
15+
if (process.platform === 'win32') return
1616

1717
it('should', done => {
1818
fse.mkdirp(dir, 0o755, err => {
19-
if (err) throw err
19+
if (err) return done(err)
2020
fs.stat(dir, (er, stat) => {
21-
if (er) throw er
21+
if (er) return done(er)
2222
assert.ok(stat.isDirectory(), 'target is a directory')
2323
done()
2424
})

lib/mkdirs/index.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'use strict'
2-
const u = require('universalify').fromCallback
3-
const mkdirs = u(require('./mkdirs'))
4-
const mkdirsSync = require('./mkdirs-sync')
2+
const u = require('universalify').fromPromise
3+
const { makeDir: _makeDir, makeDirSync } = require('./make-dir')
4+
const makeDir = u(_makeDir)
55

66
module.exports = {
7-
mkdirs,
8-
mkdirsSync,
7+
mkdirs: makeDir,
8+
mkdirsSync: makeDirSync,
99
// alias
10-
mkdirp: mkdirs,
11-
mkdirpSync: mkdirsSync,
12-
ensureDir: mkdirs,
13-
ensureDirSync: mkdirsSync
10+
mkdirp: makeDir,
11+
mkdirpSync: makeDirSync,
12+
ensureDir: makeDir,
13+
ensureDirSync: makeDirSync
1414
}

lib/mkdirs/make-dir.js

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
// Adapted from https://github.com/sindresorhus/make-dir
2+
// Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6+
'use strict'
7+
const fs = require('../fs')
8+
const path = require('path')
9+
const atLeastNode = require('at-least-node')
10+
11+
const useNativeRecursiveOption = atLeastNode('10.12.0')
12+
13+
// https://github.com/nodejs/node/issues/8987
14+
// https://github.com/libuv/libuv/pull/1088
15+
const checkPath = pth => {
16+
if (process.platform === 'win32') {
17+
const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''))
18+
19+
if (pathHasInvalidWinCharacters) {
20+
const error = new Error(`Path contains invalid characters: ${pth}`)
21+
error.code = 'EINVAL'
22+
throw error
23+
}
24+
}
25+
}
26+
27+
const processOptions = options => {
28+
// Must be defined here so we get fresh process.umask()
29+
const defaults = { mode: 0o777 & (~process.umask()) }
30+
if (typeof options === 'number') options = { mode: options }
31+
return { ...defaults, ...options }
32+
}
33+
34+
const permissionError = pth => {
35+
// This replicates the exception of `fs.mkdir` with native the
36+
// `recusive` option when run on an invalid drive under Windows.
37+
const error = new Error(`operation not permitted, mkdir '${pth}'`)
38+
error.code = 'EPERM'
39+
error.errno = -4048
40+
error.path = pth
41+
error.syscall = 'mkdir'
42+
return error
43+
}
44+
45+
module.exports.makeDir = async (input, options) => {
46+
checkPath(input)
47+
options = processOptions(options)
48+
49+
if (useNativeRecursiveOption) {
50+
const pth = path.resolve(input)
51+
52+
return fs.mkdir(pth, {
53+
mode: options.mode,
54+
recursive: true
55+
})
56+
}
57+
58+
const make = async pth => {
59+
try {
60+
await fs.mkdir(pth, options.mode)
61+
} catch (error) {
62+
if (error.code === 'EPERM') {
63+
throw error
64+
}
65+
66+
if (error.code === 'ENOENT') {
67+
if (path.dirname(pth) === pth) {
68+
throw permissionError(pth)
69+
}
70+
71+
if (error.message.includes('null bytes')) {
72+
throw error
73+
}
74+
75+
await make(path.dirname(pth))
76+
return make(pth)
77+
}
78+
79+
try {
80+
const stats = await fs.stat(pth)
81+
if (!stats.isDirectory()) {
82+
// This error is never exposed to the user
83+
// it is caught below, and the original error is thrown
84+
throw new Error('The path is not a directory')
85+
}
86+
} catch {
87+
throw error
88+
}
89+
}
90+
}
91+
92+
return make(path.resolve(input))
93+
}
94+
95+
module.exports.makeDirSync = (input, options) => {
96+
checkPath(input)
97+
options = processOptions(options)
98+
99+
if (useNativeRecursiveOption) {
100+
const pth = path.resolve(input)
101+
102+
return fs.mkdirSync(pth, {
103+
mode: options.mode,
104+
recursive: true
105+
})
106+
}
107+
108+
const make = pth => {
109+
try {
110+
fs.mkdirSync(pth, options.mode)
111+
} catch (error) {
112+
if (error.code === 'EPERM') {
113+
throw error
114+
}
115+
116+
if (error.code === 'ENOENT') {
117+
if (path.dirname(pth) === pth) {
118+
throw permissionError(pth)
119+
}
120+
121+
if (error.message.includes('null bytes')) {
122+
throw error
123+
}
124+
125+
make(path.dirname(pth))
126+
return make(pth)
127+
}
128+
129+
try {
130+
if (!fs.statSync(pth).isDirectory()) {
131+
// This error is never exposed to the user
132+
// it is caught below, and the original error is thrown
133+
throw new Error('The path is not a directory')
134+
}
135+
} catch {
136+
throw error
137+
}
138+
}
139+
}
140+
141+
return make(path.resolve(input))
142+
}

lib/mkdirs/mkdirs-sync.js

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)