Comparing version 1.3.0 to 2.0.0
95
index.js
@@ -5,2 +5,3 @@ 'use strict'; | ||
const pify = require('pify'); | ||
const semver = require('semver'); | ||
@@ -12,2 +13,4 @@ const defaults = { | ||
const useNativeRecursiveOption = semver.satisfies(process.version, '>=10.12.0'); | ||
// https://github.com/nodejs/node/issues/8987 | ||
@@ -20,5 +23,5 @@ // https://github.com/libuv/libuv/pull/1088 | ||
if (pathHasInvalidWinCharacters) { | ||
const err = new Error(`Path contains invalid characters: ${pth}`); | ||
err.code = 'EINVAL'; | ||
throw err; | ||
const error = new Error(`Path contains invalid characters: ${pth}`); | ||
error.code = 'EINVAL'; | ||
throw error; | ||
} | ||
@@ -28,18 +31,47 @@ } | ||
module.exports = (input, opts) => Promise.resolve().then(() => { | ||
const permissionError = pth => { | ||
// This replicates the exception of `fs.mkdir` with native the | ||
// `recusive` option when run on an invalid drive under Windows. | ||
const error = new Error(`operation not permitted, mkdir '${pth}'`); | ||
error.code = 'EPERM'; | ||
error.errno = -4048; | ||
error.path = pth; | ||
error.syscall = 'mkdir'; | ||
return error; | ||
}; | ||
module.exports = (input, options) => Promise.resolve().then(() => { | ||
checkPath(input); | ||
opts = Object.assign({}, defaults, opts); | ||
options = Object.assign({}, defaults, options); | ||
const mkdir = pify(opts.fs.mkdir); | ||
const stat = pify(opts.fs.stat); | ||
// TODO: Use util.promisify when targeting Node.js 8 | ||
const mkdir = pify(options.fs.mkdir); | ||
const stat = pify(options.fs.stat); | ||
if (useNativeRecursiveOption && options.fs.mkdir === fs.mkdir) { | ||
const pth = path.resolve(input); | ||
return mkdir(pth, { | ||
mode: options.mode, | ||
recursive: true | ||
}).then(() => pth); | ||
} | ||
const make = pth => { | ||
return mkdir(pth, opts.mode) | ||
return mkdir(pth, options.mode) | ||
.then(() => pth) | ||
.catch(err => { | ||
if (err.code === 'ENOENT') { | ||
if (err.message.includes('null bytes') || path.dirname(pth) === pth) { | ||
throw err; | ||
.catch(error => { | ||
if (error.code === 'EPERM') { | ||
throw error; | ||
} | ||
if (error.code === 'ENOENT') { | ||
if (path.dirname(pth) === pth) { | ||
throw permissionError(pth); | ||
} | ||
if (error.message.includes('null bytes')) { | ||
throw error; | ||
} | ||
return make(path.dirname(pth)).then(() => make(pth)); | ||
@@ -51,3 +83,3 @@ } | ||
.catch(() => { | ||
throw err; | ||
throw error; | ||
}); | ||
@@ -60,15 +92,34 @@ }); | ||
module.exports.sync = (input, opts) => { | ||
module.exports.sync = (input, options) => { | ||
checkPath(input); | ||
opts = Object.assign({}, defaults, opts); | ||
options = Object.assign({}, defaults, options); | ||
if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) { | ||
const pth = path.resolve(input); | ||
fs.mkdirSync(pth, { | ||
mode: options.mode, | ||
recursive: true | ||
}); | ||
return pth; | ||
} | ||
const make = pth => { | ||
try { | ||
opts.fs.mkdirSync(pth, opts.mode); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
if (err.message.includes('null bytes') || path.dirname(pth) === pth) { | ||
throw err; | ||
options.fs.mkdirSync(pth, options.mode); | ||
} catch (error) { | ||
if (error.code === 'EPERM') { | ||
throw error; | ||
} | ||
if (error.code === 'ENOENT') { | ||
if (path.dirname(pth) === pth) { | ||
throw permissionError(pth); | ||
} | ||
if (error.message.includes('null bytes')) { | ||
throw error; | ||
} | ||
make(path.dirname(pth)); | ||
@@ -79,7 +130,7 @@ return make(pth); | ||
try { | ||
if (!opts.fs.statSync(pth).isDirectory()) { | ||
if (!options.fs.statSync(pth).isDirectory()) { | ||
throw new Error('The path is not a directory'); | ||
} | ||
} catch (_) { | ||
throw err; | ||
throw error; | ||
} | ||
@@ -86,0 +137,0 @@ } |
{ | ||
"name": "make-dir", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"description": "Make a directory and its parents if needed - Think `mkdir -p`", | ||
@@ -13,3 +13,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=4" | ||
"node": ">=6" | ||
}, | ||
@@ -44,13 +44,14 @@ "scripts": { | ||
"dependencies": { | ||
"pify": "^3.0.0" | ||
"pify": "^4.0.1", | ||
"semver": "^5.6.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"ava": "^1.2.0", | ||
"codecov": "^3.0.0", | ||
"graceful-fs": "^4.1.11", | ||
"nyc": "^11.3.0", | ||
"nyc": "^13.1.0", | ||
"path-type": "^3.0.0", | ||
"tempy": "^0.2.1", | ||
"xo": "^0.20.0" | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# make-dir [![Build Status: macOS & Linux](https://travis-ci.org/sindresorhus/make-dir.svg?branch=master)](https://travis-ci.org/sindresorhus/make-dir) [![Build status: Windows](https://ci.appveyor.com/api/projects/status/e0vtt8y600w91gcs/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/make-dir/branch/master) [![codecov](https://codecov.io/gh/sindresorhus/make-dir/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/make-dir) | ||
# make-dir [![Build Status](https://travis-ci.org/sindresorhus/make-dir.svg?branch=master)](https://travis-ci.org/sindresorhus/make-dir) [![codecov](https://codecov.io/gh/sindresorhus/make-dir/branch/master/graph/badge.svg)](https://codecov.io/gh/sindresorhus/make-dir) | ||
@@ -14,2 +14,3 @@ > Make a directory and its parents if needed - Think `mkdir -p` | ||
- Doesn't bundle a CLI | ||
- Uses native the `fs.mkdir/mkdirSync` [`recursive` option](https://nodejs.org/dist/latest/docs/api/fs.html#fs_fs_mkdir_path_options_callback) in Node.js >=10.12.0 unless [overridden](#fs) | ||
@@ -36,6 +37,8 @@ | ||
makeDir('unicorn/rainbow/cake').then(path => { | ||
(async () => { | ||
const path = await makeDir('unicorn/rainbow/cake'); | ||
console.log(path); | ||
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake' | ||
}); | ||
})(); | ||
``` | ||
@@ -56,6 +59,8 @@ | ||
Promise.all([ | ||
makeDir('unicorn/rainbow') | ||
makeDir('foo/bar') | ||
]).then(paths => { | ||
(async () => { | ||
const paths = await Promise.all([ | ||
makeDir('unicorn/rainbow'), | ||
makeDir('foo/bar') | ||
]); | ||
console.log(paths); | ||
@@ -68,3 +73,3 @@ /* | ||
*/ | ||
}); | ||
})(); | ||
``` | ||
@@ -107,3 +112,5 @@ | ||
Using a custom `fs` implementation will block the use of the native `recursive` option if `fs.mkdir` or `fs.mkdirSync` is not the native function. | ||
## Related | ||
@@ -110,0 +117,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
7923
110
124
2
+ Addedsemver@^5.6.0
+ Addedpify@4.0.1(transitive)
+ Addedsemver@5.7.2(transitive)
- Removedpify@3.0.0(transitive)
Updatedpify@^4.0.1