From f04481d75b5964a25507c88db24e6dca1ba94c8d Mon Sep 17 00:00:00 2001 From: Corey Farrell Date: Mon, 1 Apr 2019 05:20:55 -0400 Subject: [PATCH] Require Node.js 8 and drop `pify` dependency (#13) --- .travis.yml | 1 - index.js | 72 ++++++++++++++++++++++++++------------------ package.json | 5 ++- test/helpers/util.js | 2 +- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/.travis.yml b/.travis.yml index 371a09d..0af8e23 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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' diff --git a/index.js b/index.js index 0262826..4f69583 100644 --- a/index.js +++ b/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 = { @@ -36,53 +36,62 @@ 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 @@ -90,7 +99,10 @@ 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); diff --git a/package.json b/package.json index 5a1c7d0..01c3c09 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "url": "sindresorhus.com" }, "engines": { - "node": ">=6" + "node": ">=8" }, "scripts": { "test": "xo && nyc ava && tsd" @@ -41,7 +41,6 @@ "file-system" ], "dependencies": { - "pify": "^4.0.1", "semver": "^5.6.0" }, "devDependencies": { @@ -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" diff --git a/test/helpers/util.js b/test/helpers/util.js index 44459ae..466b28d 100644 --- a/test/helpers/util.js +++ b/test/helpers/util.js @@ -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); };