From df125bebf7ce2be5ab59fb48be470cc5163b61bd Mon Sep 17 00:00:00 2001 From: RyanZim Date: Wed, 26 Apr 2017 10:00:50 -0400 Subject: [PATCH 1/2] Use pathExists() internally --- lib/__tests__/promise.test.js | 2 -- lib/copy/copy.js | 4 +++- lib/ensure/__tests__/link.test.js | 24 ++++++++++++++++++++++++ lib/ensure/__tests__/symlink.test.js | 23 +++++++++++++++++++++++ lib/ensure/file.js | 7 +++++-- lib/ensure/link.js | 7 +++++-- lib/ensure/symlink-paths.js | 4 +++- lib/ensure/symlink.js | 8 ++++++-- lib/json/output-json.js | 5 +++-- lib/mkdirs/__tests__/mkdirp.test.js | 3 ++- lib/mkdirs/__tests__/perm.test.js | 3 ++- lib/mkdirs/__tests__/perm_sync.test.js | 6 ++++-- lib/mkdirs/__tests__/race.test.js | 3 ++- lib/mkdirs/__tests__/rel.test.js | 3 ++- lib/mkdirs/__tests__/sync.test.js | 3 ++- lib/mkdirs/__tests__/umask.test.js | 6 ++++-- lib/output/index.js | 4 +++- lib/remove/__tests__/remove.test.js | 3 ++- 18 files changed, 95 insertions(+), 23 deletions(-) diff --git a/lib/__tests__/promise.test.js b/lib/__tests__/promise.test.js index 9b3806d7..80aeca5e 100644 --- a/lib/__tests__/promise.test.js +++ b/lib/__tests__/promise.test.js @@ -9,8 +9,6 @@ const methods = [ 'emptyDir', 'ensureFile', 'ensureDir', - 'ensureLink', - 'ensureSymlink', 'mkdirs', 'move', 'readJson', diff --git a/lib/copy/copy.js b/lib/copy/copy.js index d66c8981..309a93df 100644 --- a/lib/copy/copy.js +++ b/lib/copy/copy.js @@ -4,6 +4,7 @@ const fs = require('graceful-fs') const path = require('path') const ncp = require('./ncp') const mkdir = require('../mkdirs') +const pathExists = require('../path-exists').pathExists function copy (src, dest, options, callback) { if (typeof options === 'function' && !callback) { @@ -39,7 +40,8 @@ function copy (src, dest, options, callback) { dir = path.dirname(dest) } - fs.exists(dir, dirExists => { + pathExists(dir, (err, dirExists) => { + if (err) return callback(err) if (dirExists) return ncp(src, dest, options, callback) mkdir.mkdirs(dir, err => { if (err) return callback(err) diff --git a/lib/ensure/__tests__/link.test.js b/lib/ensure/__tests__/link.test.js index 8e27b6fb..ebd1c853 100644 --- a/lib/ensure/__tests__/link.test.js +++ b/lib/ensure/__tests__/link.test.js @@ -197,6 +197,30 @@ describe('fse-ensure-link', () => { }) }) + describe('ensureLink() promise support', () => { + tests.filter(test => test[2] === 'file-success').forEach(test => { + const args = test[0].slice(0) + const srcpath = args[0] + const dstpath = args[1] + + it(`should create link file using src ${srcpath} and dst ${dstpath}`, () => { + return ensureLink(srcpath, dstpath) + .then(() => { + const srcContent = fs.readFileSync(srcpath, 'utf8') + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isFile() + const dstContent = fs.readFileSync(dstpath, 'utf8') + const dstDirContents = fs.readdirSync(dstDir) + + assert.equal(isSymlink, true) + assert.equal(srcContent, dstContent) + assert(dstDirContents.indexOf(dstBasename) >= 0) + }) + }) + }) + }) + describe('fs.linkSync()', () => { const fn = fs.linkSync tests.forEach(test => { diff --git a/lib/ensure/__tests__/symlink.test.js b/lib/ensure/__tests__/symlink.test.js index 6f939585..64fdcbb5 100644 --- a/lib/ensure/__tests__/symlink.test.js +++ b/lib/ensure/__tests__/symlink.test.js @@ -387,6 +387,29 @@ describe('fse-ensure-symlink', () => { }) }) + describe('ensureSymlink() promise support', () => { + tests.filter(test => test[2] === 'file-success').forEach(test => { + const args = test[0] + const srcpath = args[0] + const dstpath = args[1] + it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, () => { + return ensureSymlink(srcpath, dstpath) + .then(() => { + const relative = symlinkPathsSync(srcpath, dstpath) + const srcContent = fs.readFileSync(relative.toCwd, 'utf8') + const dstDir = path.dirname(dstpath) + const dstBasename = path.basename(dstpath) + const isSymlink = fs.lstatSync(dstpath).isSymbolicLink() + const dstContent = fs.readFileSync(dstpath, 'utf8') + const dstDirContents = fs.readdirSync(dstDir) + assert.equal(isSymlink, true) + assert.equal(srcContent, dstContent) + assert(dstDirContents.indexOf(dstBasename) >= 0) + }) + }) + }) + }) + describe('fs.symlinkSync()', () => { const fn = fs.symlinkSync tests.forEach(test => { diff --git a/lib/ensure/file.js b/lib/ensure/file.js index c562bc86..962c21cb 100644 --- a/lib/ensure/file.js +++ b/lib/ensure/file.js @@ -4,6 +4,7 @@ const u = require('universalify').fromCallback const path = require('path') const fs = require('graceful-fs') const mkdir = require('../mkdirs') +const pathExists = require('../path-exists').pathExists function createFile (file, callback) { function makeFile () { @@ -13,10 +14,12 @@ function createFile (file, callback) { }) } - fs.exists(file, fileExists => { + pathExists(file, (err, fileExists) => { + if (err) return callback(err) if (fileExists) return callback() const dir = path.dirname(file) - fs.exists(dir, dirExists => { + pathExists(dir, (err, dirExists) => { + if (err) return callback(err) if (dirExists) return makeFile() mkdir.mkdirs(dir, err => { if (err) return callback(err) diff --git a/lib/ensure/link.js b/lib/ensure/link.js index 12796383..49fe3791 100644 --- a/lib/ensure/link.js +++ b/lib/ensure/link.js @@ -4,6 +4,7 @@ const u = require('universalify').fromCallback const path = require('path') const fs = require('graceful-fs') const mkdir = require('../mkdirs') +const pathExists = require('../path-exists').pathExists function createLink (srcpath, dstpath, callback) { function makeLink (srcpath, dstpath) { @@ -13,7 +14,8 @@ function createLink (srcpath, dstpath, callback) { }) } - fs.exists(dstpath, destinationExists => { + pathExists(dstpath, (err, destinationExists) => { + if (err) return callback(err) if (destinationExists) return callback(null) fs.lstat(srcpath, (err, stat) => { if (err) { @@ -22,7 +24,8 @@ function createLink (srcpath, dstpath, callback) { } const dir = path.dirname(dstpath) - fs.exists(dir, dirExists => { + pathExists(dir, (err, dirExists) => { + if (err) return callback(err) if (dirExists) return makeLink(srcpath, dstpath) mkdir.mkdirs(dir, err => { if (err) return callback(err) diff --git a/lib/ensure/symlink-paths.js b/lib/ensure/symlink-paths.js index e52d0396..4a6b78ab 100644 --- a/lib/ensure/symlink-paths.js +++ b/lib/ensure/symlink-paths.js @@ -2,6 +2,7 @@ const path = require('path') const fs = require('graceful-fs') +const pathExists = require('../path-exists').pathExists /** * Function that returns two types of paths, one relative to symlink, and one @@ -40,7 +41,8 @@ function symlinkPaths (srcpath, dstpath, callback) { } else { const dstdir = path.dirname(dstpath) const relativeToDst = path.join(dstdir, srcpath) - return fs.exists(relativeToDst, exists => { + return pathExists(relativeToDst, (err, exists) => { + if (err) return callback(err) if (exists) { return callback(null, { 'toCwd': relativeToDst, diff --git a/lib/ensure/symlink.js b/lib/ensure/symlink.js index d879f514..847c1b9d 100644 --- a/lib/ensure/symlink.js +++ b/lib/ensure/symlink.js @@ -15,11 +15,14 @@ const _symlinkType = require('./symlink-type') const symlinkType = _symlinkType.symlinkType const symlinkTypeSync = _symlinkType.symlinkTypeSync +const pathExists = require('../path-exists').pathExists + function createSymlink (srcpath, dstpath, type, callback) { callback = (typeof type === 'function') ? type : callback type = (typeof type === 'function') ? false : type - fs.exists(dstpath, destinationExists => { + pathExists(dstpath, (err, destinationExists) => { + if (err) return callback(err) if (destinationExists) return callback(null) symlinkPaths(srcpath, dstpath, (err, relative) => { if (err) return callback(err) @@ -27,7 +30,8 @@ function createSymlink (srcpath, dstpath, type, callback) { symlinkType(relative.toCwd, type, (err, type) => { if (err) return callback(err) const dir = path.dirname(dstpath) - fs.exists(dir, dirExists => { + pathExists(dir, (err, dirExists) => { + if (err) return callback(err) if (dirExists) return fs.symlink(srcpath, dstpath, type, callback) mkdirs(dir, err => { if (err) return callback(err) diff --git a/lib/json/output-json.js b/lib/json/output-json.js index 2f928c50..d45edb89 100644 --- a/lib/json/output-json.js +++ b/lib/json/output-json.js @@ -1,8 +1,8 @@ 'use strict' -const fs = require('graceful-fs') const path = require('path') const mkdir = require('../mkdirs') +const pathExists = require('../path-exists').pathExists const jsonFile = require('./jsonfile') function outputJson (file, data, options, callback) { @@ -13,7 +13,8 @@ function outputJson (file, data, options, callback) { const dir = path.dirname(file) - fs.exists(dir, itDoes => { + pathExists(dir, (err, itDoes) => { + if (err) return callback(err) if (itDoes) return jsonFile.writeJson(file, data, options, callback) mkdir.mkdirs(dir, err => { diff --git a/lib/mkdirs/__tests__/mkdirp.test.js b/lib/mkdirs/__tests__/mkdirp.test.js index 7ad38686..dc52e1f9 100644 --- a/lib/mkdirs/__tests__/mkdirp.test.js +++ b/lib/mkdirs/__tests__/mkdirp.test.js @@ -31,7 +31,8 @@ describe('mkdirp / mkdirp', () => { fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) diff --git a/lib/mkdirs/__tests__/perm.test.js b/lib/mkdirs/__tests__/perm.test.js index 873ed6ac..77c6d19e 100644 --- a/lib/mkdirs/__tests__/perm.test.js +++ b/lib/mkdirs/__tests__/perm.test.js @@ -27,7 +27,8 @@ describe('mkdirp / perm', () => { fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) diff --git a/lib/mkdirs/__tests__/perm_sync.test.js b/lib/mkdirs/__tests__/perm_sync.test.js index cfd53be7..4352171f 100644 --- a/lib/mkdirs/__tests__/perm_sync.test.js +++ b/lib/mkdirs/__tests__/perm_sync.test.js @@ -26,7 +26,8 @@ describe('mkdirp / perm_sync', () => { const file = path.join(TEST_DIR, (Math.random() * (1 << 30)).toString(16) + '.json') fse.mkdirpSync(file, o755) - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) @@ -46,7 +47,8 @@ describe('mkdirp / perm_sync', () => { it('sync root perm', done => { const file = TEST_DIR fse.mkdirpSync(file, o755) - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) diff --git a/lib/mkdirs/__tests__/race.test.js b/lib/mkdirs/__tests__/race.test.js index 982b7138..afec9f41 100644 --- a/lib/mkdirs/__tests__/race.test.js +++ b/lib/mkdirs/__tests__/race.test.js @@ -44,7 +44,8 @@ describe('mkdirp / race', () => { function mk (file, callback) { fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) diff --git a/lib/mkdirs/__tests__/rel.test.js b/lib/mkdirs/__tests__/rel.test.js index 4971d0ed..29a940a6 100644 --- a/lib/mkdirs/__tests__/rel.test.js +++ b/lib/mkdirs/__tests__/rel.test.js @@ -41,7 +41,8 @@ describe('mkdirp / relative', () => { fse.mkdirp(file, o755, err => { assert.ifError(err) - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) diff --git a/lib/mkdirs/__tests__/sync.test.js b/lib/mkdirs/__tests__/sync.test.js index a7033fef..b745478d 100644 --- a/lib/mkdirs/__tests__/sync.test.js +++ b/lib/mkdirs/__tests__/sync.test.js @@ -40,7 +40,8 @@ describe('mkdirp / sync', () => { assert.fail(err) } - fs.exists(file, ex => { + fse.pathExists(file, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(file, (err, stat) => { assert.ifError(err) diff --git a/lib/mkdirs/__tests__/umask.test.js b/lib/mkdirs/__tests__/umask.test.js index a3d06e7d..7195806b 100644 --- a/lib/mkdirs/__tests__/umask.test.js +++ b/lib/mkdirs/__tests__/umask.test.js @@ -42,7 +42,8 @@ describe('mkdirp', () => { fse.mkdirp(_rndDir, err => { assert.ifError(err) - fs.exists(_rndDir, ex => { + fse.pathExists(_rndDir, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(_rndDir, (err, stat) => { assert.ifError(err) @@ -65,7 +66,8 @@ describe('mkdirp', () => { return done(err) } - fs.exists(_rndDir, ex => { + fse.pathExists(_rndDir, (err, ex) => { + assert.ifError(err) assert.ok(ex, 'file created') fs.stat(_rndDir, (err, stat) => { assert.ifError(err) diff --git a/lib/output/index.js b/lib/output/index.js index 14187478..53d59057 100644 --- a/lib/output/index.js +++ b/lib/output/index.js @@ -4,6 +4,7 @@ const u = require('universalify').fromCallback const fs = require('graceful-fs') const path = require('path') const mkdir = require('../mkdirs') +const pathExists = require('../path-exists').pathExists function outputFile (file, data, encoding, callback) { if (typeof encoding === 'function') { @@ -12,7 +13,8 @@ function outputFile (file, data, encoding, callback) { } const dir = path.dirname(file) - fs.exists(dir, itDoes => { + pathExists(dir, (err, itDoes) => { + if (err) return callback(err) if (itDoes) return fs.writeFile(file, data, encoding, callback) mkdir.mkdirs(dir, err => { diff --git a/lib/remove/__tests__/remove.test.js b/lib/remove/__tests__/remove.test.js index d7c2fbdf..92979d57 100644 --- a/lib/remove/__tests__/remove.test.js +++ b/lib/remove/__tests__/remove.test.js @@ -71,7 +71,8 @@ describe('remove', () => { assert(fs.existsSync(file)) const existsChecker = setInterval(() => { - fs.exists(file, (itDoes) => { + fse.pathExists(file, (err, itDoes) => { + assert.ifError(err) if (!itDoes) { clearInterval(existsChecker) done() From 891f483cfc925681ed51836f3e1328c3066e4225 Mon Sep 17 00:00:00 2001 From: RyanZim Date: Wed, 26 Apr 2017 10:13:40 -0400 Subject: [PATCH 2/2] Upgrade standard --- lib/remove/rimraf.js | 6 ++---- lib/util/buffer.js | 1 + package.json | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/remove/rimraf.js b/lib/remove/rimraf.js index 2f4bb7e7..28d4aeb0 100644 --- a/lib/remove/rimraf.js +++ b/lib/remove/rimraf.js @@ -275,13 +275,11 @@ function rmdirSync (p, options, originalEr) { try { options.rmdirSync(p) } catch (er) { - if (er.code === 'ENOENT') { - return - } else if (er.code === 'ENOTDIR') { + if (er.code === 'ENOTDIR') { throw originalEr } else if (er.code === 'ENOTEMPTY' || er.code === 'EEXIST' || er.code === 'EPERM') { rmkidsSync(p, options) - } else { + } else if (er.code !== 'ENOENT') { throw er } } diff --git a/lib/util/buffer.js b/lib/util/buffer.js index 6cd4e310..93af51b2 100644 --- a/lib/util/buffer.js +++ b/lib/util/buffer.js @@ -1,3 +1,4 @@ +/* eslint-disable node/no-deprecated-api */ module.exports = function (size) { if (typeof Buffer.allocUnsafe === 'function') { try { diff --git a/package.json b/package.json index 608aa8d5..7cf614e0 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,7 @@ "read-dir-files": "^0.1.1", "rimraf": "^2.2.8", "secure-random": "^1.1.1", - "standard": "^8.5.0" + "standard": "^10.0.2" }, "main": "./lib/index", "scripts": {