Skip to content

Commit

Permalink
Require Node.js 8 and drop pify dependency (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell authored and sindresorhus committed Apr 1, 2019
1 parent 379001f commit f04481d
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -6,6 +6,5 @@ language: node_js
node_js:
- '10'
- '8'
- '6'
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov > coverage.lcov && ./node_modules/.bin/codecov'
72 changes: 42 additions & 30 deletions index.js
@@ -1,7 +1,7 @@
'use strict';
const fs = require('fs');
const path = require('path');
const pify = require('pify');
const {promisify} = require('util');
const semver = require('semver');

const defaults = {
Expand Down Expand Up @@ -36,61 +36,73 @@ const permissionError = pth => {
return error;
};

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);

if (useNativeRecursiveOption && options.fs.mkdir === fs.mkdir) {
const pth = path.resolve(input);

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;
// TODO: Remove this for the next major release
module.exports.default = makeDir;

module.exports.sync = (input, options) => {
checkPath(input);
options = Object.assign({}, defaults, options);
options = {
...defaults,
...options
};

if (useNativeRecursiveOption && options.fs.mkdirSync === fs.mkdirSync) {
const pth = path.resolve(input);
Expand Down
5 changes: 2 additions & 3 deletions package.json
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=6"
"node": ">=8"
},
"scripts": {
"test": "xo && nyc ava && tsd"
Expand Down Expand Up @@ -41,7 +41,6 @@
"file-system"
],
"dependencies": {
"pify": "^4.0.1",
"semver": "^5.6.0"
},
"devDependencies": {
Expand All @@ -51,7 +50,7 @@
"codecov": "^3.2.0",
"graceful-fs": "^4.1.15",
"nyc": "^13.3.0",
"path-type": "^3.0.0",
"path-type": "^4.0.0",
"tempy": "^0.2.1",
"tsd": "^0.7.1",
"xo": "^0.24.0"
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/util.js
Expand Up @@ -11,7 +11,7 @@ export const assertDirectory = (t, directory, mode = 0o777 & (~process.umask()))
mode = 0o666;
}

t.true(pathType.dirSync(directory));
t.true(pathType.isDirectorySync(directory));
t.is(fs.statSync(directory).mode & 0o777, mode);
};

Expand Down

0 comments on commit f04481d

Please sign in to comment.