Skip to content

Commit

Permalink
Use pathExists() internally
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Apr 26, 2017
1 parent b209cab commit df125be
Show file tree
Hide file tree
Showing 18 changed files with 95 additions and 23 deletions.
2 changes: 0 additions & 2 deletions lib/__tests__/promise.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ const methods = [
'emptyDir',
'ensureFile',
'ensureDir',
'ensureLink',
'ensureSymlink',
'mkdirs',
'move',
'readJson',
Expand Down
4 changes: 3 additions & 1 deletion lib/copy/copy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions lib/ensure/__tests__/link.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
23 changes: 23 additions & 0 deletions lib/ensure/__tests__/symlink.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
7 changes: 5 additions & 2 deletions lib/ensure/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions lib/ensure/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/ensure/symlink-paths.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 6 additions & 2 deletions lib/ensure/symlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,23 @@ 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)
srcpath = relative.toDst
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)
Expand Down
5 changes: 3 additions & 2 deletions lib/json/output-json.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion lib/mkdirs/__tests__/mkdirp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/mkdirs/__tests__/perm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/mkdirs/__tests__/perm_sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/mkdirs/__tests__/race.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/mkdirs/__tests__/rel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lib/mkdirs/__tests__/sync.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions lib/mkdirs/__tests__/umask.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion lib/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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 => {
Expand Down
3 changes: 2 additions & 1 deletion lib/remove/__tests__/remove.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit df125be

Please sign in to comment.