Skip to content

Commit c551140

Browse files
authored
BREAKING: Use native fs.mkdir recursive everywhere (#894)
Moved last bit of make-dir code to its own file
1 parent 05d2e93 commit c551140

File tree

2 files changed

+37
-130
lines changed

2 files changed

+37
-130
lines changed

lib/mkdirs/make-dir.js

Lines changed: 16 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,27 @@
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.
61
'use strict'
72
const fs = require('../fs')
8-
const path = require('path')
9-
const atLeastNode = require('at-least-node')
3+
const { checkPath } = require('./utils')
104

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 => {
5+
const getMode = options => {
286
const defaults = { mode: 0o777 }
29-
if (typeof options === 'number') options = { mode: options }
30-
return { ...defaults, ...options }
31-
}
32-
33-
const permissionError = pth => {
34-
// This replicates the exception of `fs.mkdir` with native the
35-
// `recusive` option when run on an invalid drive under Windows.
36-
const error = new Error(`operation not permitted, mkdir '${pth}'`)
37-
error.code = 'EPERM'
38-
error.errno = -4048
39-
error.path = pth
40-
error.syscall = 'mkdir'
41-
return error
7+
if (typeof options === 'number') return options
8+
return ({ ...defaults, ...options }).mode
429
}
4310

44-
module.exports.makeDir = async (input, options) => {
45-
checkPath(input)
46-
options = processOptions(options)
47-
48-
if (useNativeRecursiveOption) {
49-
const pth = path.resolve(input)
50-
51-
return fs.mkdir(pth, {
52-
mode: options.mode,
53-
recursive: true
54-
})
55-
}
56-
57-
const make = async pth => {
58-
try {
59-
await fs.mkdir(pth, options.mode)
60-
} catch (error) {
61-
if (error.code === 'EPERM') {
62-
throw error
63-
}
64-
65-
if (error.code === 'ENOENT') {
66-
if (path.dirname(pth) === pth) {
67-
throw permissionError(pth)
68-
}
11+
module.exports.makeDir = async (dir, options) => {
12+
checkPath(dir)
6913

70-
if (error.message.includes('null bytes')) {
71-
throw error
72-
}
73-
74-
await make(path.dirname(pth))
75-
return make(pth)
76-
}
77-
78-
try {
79-
const stats = await fs.stat(pth)
80-
if (!stats.isDirectory()) {
81-
// This error is never exposed to the user
82-
// it is caught below, and the original error is thrown
83-
throw new Error('The path is not a directory')
84-
}
85-
} catch {
86-
throw error
87-
}
88-
}
89-
}
90-
91-
return make(path.resolve(input))
14+
return fs.mkdir(dir, {
15+
mode: getMode(options),
16+
recursive: true
17+
})
9218
}
9319

94-
module.exports.makeDirSync = (input, options) => {
95-
checkPath(input)
96-
options = processOptions(options)
97-
98-
if (useNativeRecursiveOption) {
99-
const pth = path.resolve(input)
100-
101-
return fs.mkdirSync(pth, {
102-
mode: options.mode,
103-
recursive: true
104-
})
105-
}
106-
107-
const make = pth => {
108-
try {
109-
fs.mkdirSync(pth, options.mode)
110-
} catch (error) {
111-
if (error.code === 'EPERM') {
112-
throw error
113-
}
114-
115-
if (error.code === 'ENOENT') {
116-
if (path.dirname(pth) === pth) {
117-
throw permissionError(pth)
118-
}
119-
120-
if (error.message.includes('null bytes')) {
121-
throw error
122-
}
123-
124-
make(path.dirname(pth))
125-
return make(pth)
126-
}
127-
128-
try {
129-
if (!fs.statSync(pth).isDirectory()) {
130-
// This error is never exposed to the user
131-
// it is caught below, and the original error is thrown
132-
throw new Error('The path is not a directory')
133-
}
134-
} catch {
135-
throw error
136-
}
137-
}
138-
}
20+
module.exports.makeDirSync = (dir, options) => {
21+
checkPath(dir)
13922

140-
return make(path.resolve(input))
23+
return fs.mkdirSync(dir, {
24+
mode: getMode(options),
25+
recursive: true
26+
})
14127
}

lib/mkdirs/utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 path = require('path')
8+
9+
// https://github.com/nodejs/node/issues/8987
10+
// https://github.com/libuv/libuv/pull/1088
11+
module.exports.checkPath = function checkPath (pth) {
12+
if (process.platform === 'win32') {
13+
const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''))
14+
15+
if (pathHasInvalidWinCharacters) {
16+
const error = new Error(`Path contains invalid characters: ${pth}`)
17+
error.code = 'EINVAL'
18+
throw error
19+
}
20+
}
21+
}

0 commit comments

Comments
 (0)