Comparing version 2.1.0 to 3.0.0
/// <reference types="node"/> | ||
import * as fs from 'fs'; | ||
export interface Options { | ||
declare namespace makeDir { | ||
interface Options { | ||
/** | ||
Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/). | ||
@default 0o777 & (~process.umask()) | ||
*/ | ||
readonly mode?: number; | ||
/** | ||
Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs). | ||
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. | ||
@default require('fs') | ||
*/ | ||
readonly fs?: typeof fs; | ||
} | ||
} | ||
declare const makeDir: { | ||
/** | ||
* Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/). | ||
* | ||
* @default 0o777 & (~process.umask()) | ||
*/ | ||
readonly mode?: number; | ||
Make a directory and its parents if needed - Think `mkdir -p`. | ||
@param path - Directory to create. | ||
@returns The path to the created directory. | ||
@example | ||
``` | ||
import makeDir = require('make-dir'); | ||
(async () => { | ||
const path = await makeDir('unicorn/rainbow/cake'); | ||
console.log(path); | ||
//=> '/Users/sindresorhus/fun/unicorn/rainbow/cake' | ||
// Multiple directories: | ||
const paths = await Promise.all([ | ||
makeDir('unicorn/rainbow'), | ||
makeDir('foo/bar') | ||
]); | ||
console.log(paths); | ||
// [ | ||
// '/Users/sindresorhus/fun/unicorn/rainbow', | ||
// '/Users/sindresorhus/fun/foo/bar' | ||
// ] | ||
})(); | ||
``` | ||
*/ | ||
(path: string, options?: makeDir.Options): Promise<string>; | ||
/** | ||
* Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs). | ||
* | ||
* 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. | ||
* | ||
* @default require('fs') | ||
*/ | ||
readonly fs?: typeof fs; | ||
} | ||
Synchronously make a directory and its parents if needed - Think `mkdir -p`. | ||
/** | ||
* Make a directory and its parents if needed - Think `mkdir -p`. | ||
* | ||
* @param path - Directory to create. | ||
* @returns A `Promise` for the path to the created directory. | ||
*/ | ||
export default function makeDir( | ||
path: string, | ||
options?: Options | ||
): Promise<string>; | ||
@param path - Directory to create. | ||
@returns The path to the created directory. | ||
*/ | ||
sync(path: string, options?: makeDir.Options): string; | ||
}; | ||
/** | ||
* Synchronously make a directory and its parents if needed - Think `mkdir -p`. | ||
* | ||
* @param path - Directory to create. | ||
* @returns The path to the created directory. | ||
*/ | ||
export function sync(path: string, options?: Options): string; | ||
export = makeDir; |
73
index.js
'use strict'; | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const pify = require('pify'); | ||
const {promisify} = require('util'); | ||
const semver = require('semver'); | ||
@@ -39,9 +39,11 @@ | ||
const makeDir = (input, options) => Promise.resolve().then(() => { | ||
const makeDir = async (input, options) => { | ||
checkPath(input); | ||
options = Object.assign({}, defaults, options); | ||
options = { | ||
...defaults, | ||
...options | ||
}; | ||
// TODO: Use util.promisify when targeting Node.js 8 | ||
const mkdir = pify(options.fs.mkdir); | ||
const stat = pify(options.fs.stat); | ||
const mkdir = promisify(options.fs.mkdir); | ||
const stat = promisify(options.fs.stat); | ||
@@ -51,45 +53,54 @@ if (useNativeRecursiveOption && options.fs.mkdir === fs.mkdir) { | ||
return mkdir(pth, { | ||
await mkdir(pth, { | ||
mode: options.mode, | ||
recursive: true | ||
}).then(() => pth); | ||
}); | ||
return pth; | ||
} | ||
const make = pth => { | ||
return mkdir(pth, options.mode) | ||
.then(() => pth) | ||
.catch(error => { | ||
if (error.code === 'EPERM') { | ||
const make = async pth => { | ||
try { | ||
await mkdir(pth, options.mode); | ||
return pth; | ||
} 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; | ||
} | ||
if (error.code === 'ENOENT') { | ||
if (path.dirname(pth) === pth) { | ||
throw permissionError(pth); | ||
} | ||
await make(path.dirname(pth)); | ||
if (error.message.includes('null bytes')) { | ||
throw error; | ||
} | ||
return make(pth); | ||
} | ||
return make(path.dirname(pth)).then(() => make(pth)); | ||
} | ||
const stats = await stat(pth); | ||
if (!stats.isDirectory()) { | ||
throw error; | ||
} | ||
return stat(pth) | ||
.then(stats => stats.isDirectory() ? pth : Promise.reject()) | ||
.catch(() => { | ||
throw error; | ||
}); | ||
}); | ||
return pth; | ||
} | ||
}; | ||
return make(path.resolve(input)); | ||
}); | ||
}; | ||
module.exports = makeDir; | ||
module.exports.default = makeDir; | ||
module.exports.sync = (input, options) => { | ||
checkPath(input); | ||
options = Object.assign({}, defaults, options); | ||
options = { | ||
...defaults, | ||
...options | ||
}; | ||
@@ -96,0 +107,0 @@ if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) { |
{ | ||
"name": "make-dir", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Make a directory and its parents if needed - Think `mkdir -p`", | ||
@@ -13,6 +13,6 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=6" | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && nyc ava && tsd-check" | ||
"test": "xo && nyc ava && tsd" | ||
}, | ||
@@ -45,17 +45,16 @@ "files": [ | ||
"dependencies": { | ||
"pify": "^4.0.1", | ||
"semver": "^5.6.0" | ||
"semver": "^6.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/graceful-fs": "^4.1.3", | ||
"@types/node": "^11.10.4", | ||
"ava": "^1.2.0", | ||
"codecov": "^3.0.0", | ||
"graceful-fs": "^4.1.11", | ||
"nyc": "^13.1.0", | ||
"path-type": "^3.0.0", | ||
"@types/node": "^11.12.2", | ||
"ava": "^1.4.0", | ||
"codecov": "^3.2.0", | ||
"graceful-fs": "^4.1.15", | ||
"nyc": "^13.3.0", | ||
"path-type": "^4.0.0", | ||
"tempy": "^0.2.1", | ||
"tsd-check": "^0.3.0", | ||
"tsd": "^0.7.1", | ||
"xo": "^0.24.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
9481
1
170
+ Addedsemver@6.3.1(transitive)
- Removedpify@^4.0.1
- Removedpify@4.0.1(transitive)
- Removedsemver@5.7.2(transitive)
Updatedsemver@^6.0.0