Skip to content

Commit df125be

Browse files
committed
Use pathExists() internally
1 parent b209cab commit df125be

File tree

18 files changed

+95
-23
lines changed

18 files changed

+95
-23
lines changed

lib/__tests__/promise.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ const methods = [
99
'emptyDir',
1010
'ensureFile',
1111
'ensureDir',
12-
'ensureLink',
13-
'ensureSymlink',
1412
'mkdirs',
1513
'move',
1614
'readJson',

lib/copy/copy.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const fs = require('graceful-fs')
44
const path = require('path')
55
const ncp = require('./ncp')
66
const mkdir = require('../mkdirs')
7+
const pathExists = require('../path-exists').pathExists
78

89
function copy (src, dest, options, callback) {
910
if (typeof options === 'function' && !callback) {
@@ -39,7 +40,8 @@ function copy (src, dest, options, callback) {
3940
dir = path.dirname(dest)
4041
}
4142

42-
fs.exists(dir, dirExists => {
43+
pathExists(dir, (err, dirExists) => {
44+
if (err) return callback(err)
4345
if (dirExists) return ncp(src, dest, options, callback)
4446
mkdir.mkdirs(dir, err => {
4547
if (err) return callback(err)

lib/ensure/__tests__/link.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,30 @@ describe('fse-ensure-link', () => {
197197
})
198198
})
199199

200+
describe('ensureLink() promise support', () => {
201+
tests.filter(test => test[2] === 'file-success').forEach(test => {
202+
const args = test[0].slice(0)
203+
const srcpath = args[0]
204+
const dstpath = args[1]
205+
206+
it(`should create link file using src ${srcpath} and dst ${dstpath}`, () => {
207+
return ensureLink(srcpath, dstpath)
208+
.then(() => {
209+
const srcContent = fs.readFileSync(srcpath, 'utf8')
210+
const dstDir = path.dirname(dstpath)
211+
const dstBasename = path.basename(dstpath)
212+
const isSymlink = fs.lstatSync(dstpath).isFile()
213+
const dstContent = fs.readFileSync(dstpath, 'utf8')
214+
const dstDirContents = fs.readdirSync(dstDir)
215+
216+
assert.equal(isSymlink, true)
217+
assert.equal(srcContent, dstContent)
218+
assert(dstDirContents.indexOf(dstBasename) >= 0)
219+
})
220+
})
221+
})
222+
})
223+
200224
describe('fs.linkSync()', () => {
201225
const fn = fs.linkSync
202226
tests.forEach(test => {

lib/ensure/__tests__/symlink.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,29 @@ describe('fse-ensure-symlink', () => {
387387
})
388388
})
389389

390+
describe('ensureSymlink() promise support', () => {
391+
tests.filter(test => test[2] === 'file-success').forEach(test => {
392+
const args = test[0]
393+
const srcpath = args[0]
394+
const dstpath = args[1]
395+
it(`should create symlink file using src ${srcpath} and dst ${dstpath}`, () => {
396+
return ensureSymlink(srcpath, dstpath)
397+
.then(() => {
398+
const relative = symlinkPathsSync(srcpath, dstpath)
399+
const srcContent = fs.readFileSync(relative.toCwd, 'utf8')
400+
const dstDir = path.dirname(dstpath)
401+
const dstBasename = path.basename(dstpath)
402+
const isSymlink = fs.lstatSync(dstpath).isSymbolicLink()
403+
const dstContent = fs.readFileSync(dstpath, 'utf8')
404+
const dstDirContents = fs.readdirSync(dstDir)
405+
assert.equal(isSymlink, true)
406+
assert.equal(srcContent, dstContent)
407+
assert(dstDirContents.indexOf(dstBasename) >= 0)
408+
})
409+
})
410+
})
411+
})
412+
390413
describe('fs.symlinkSync()', () => {
391414
const fn = fs.symlinkSync
392415
tests.forEach(test => {

lib/ensure/file.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const u = require('universalify').fromCallback
44
const path = require('path')
55
const fs = require('graceful-fs')
66
const mkdir = require('../mkdirs')
7+
const pathExists = require('../path-exists').pathExists
78

89
function createFile (file, callback) {
910
function makeFile () {
@@ -13,10 +14,12 @@ function createFile (file, callback) {
1314
})
1415
}
1516

16-
fs.exists(file, fileExists => {
17+
pathExists(file, (err, fileExists) => {
18+
if (err) return callback(err)
1719
if (fileExists) return callback()
1820
const dir = path.dirname(file)
19-
fs.exists(dir, dirExists => {
21+
pathExists(dir, (err, dirExists) => {
22+
if (err) return callback(err)
2023
if (dirExists) return makeFile()
2124
mkdir.mkdirs(dir, err => {
2225
if (err) return callback(err)

lib/ensure/link.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const u = require('universalify').fromCallback
44
const path = require('path')
55
const fs = require('graceful-fs')
66
const mkdir = require('../mkdirs')
7+
const pathExists = require('../path-exists').pathExists
78

89
function createLink (srcpath, dstpath, callback) {
910
function makeLink (srcpath, dstpath) {
@@ -13,7 +14,8 @@ function createLink (srcpath, dstpath, callback) {
1314
})
1415
}
1516

16-
fs.exists(dstpath, destinationExists => {
17+
pathExists(dstpath, (err, destinationExists) => {
18+
if (err) return callback(err)
1719
if (destinationExists) return callback(null)
1820
fs.lstat(srcpath, (err, stat) => {
1921
if (err) {
@@ -22,7 +24,8 @@ function createLink (srcpath, dstpath, callback) {
2224
}
2325

2426
const dir = path.dirname(dstpath)
25-
fs.exists(dir, dirExists => {
27+
pathExists(dir, (err, dirExists) => {
28+
if (err) return callback(err)
2629
if (dirExists) return makeLink(srcpath, dstpath)
2730
mkdir.mkdirs(dir, err => {
2831
if (err) return callback(err)

lib/ensure/symlink-paths.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const path = require('path')
44
const fs = require('graceful-fs')
5+
const pathExists = require('../path-exists').pathExists
56

67
/**
78
* Function that returns two types of paths, one relative to symlink, and one
@@ -40,7 +41,8 @@ function symlinkPaths (srcpath, dstpath, callback) {
4041
} else {
4142
const dstdir = path.dirname(dstpath)
4243
const relativeToDst = path.join(dstdir, srcpath)
43-
return fs.exists(relativeToDst, exists => {
44+
return pathExists(relativeToDst, (err, exists) => {
45+
if (err) return callback(err)
4446
if (exists) {
4547
return callback(null, {
4648
'toCwd': relativeToDst,

lib/ensure/symlink.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@ const _symlinkType = require('./symlink-type')
1515
const symlinkType = _symlinkType.symlinkType
1616
const symlinkTypeSync = _symlinkType.symlinkTypeSync
1717

18+
const pathExists = require('../path-exists').pathExists
19+
1820
function createSymlink (srcpath, dstpath, type, callback) {
1921
callback = (typeof type === 'function') ? type : callback
2022
type = (typeof type === 'function') ? false : type
2123

22-
fs.exists(dstpath, destinationExists => {
24+
pathExists(dstpath, (err, destinationExists) => {
25+
if (err) return callback(err)
2326
if (destinationExists) return callback(null)
2427
symlinkPaths(srcpath, dstpath, (err, relative) => {
2528
if (err) return callback(err)
2629
srcpath = relative.toDst
2730
symlinkType(relative.toCwd, type, (err, type) => {
2831
if (err) return callback(err)
2932
const dir = path.dirname(dstpath)
30-
fs.exists(dir, dirExists => {
33+
pathExists(dir, (err, dirExists) => {
34+
if (err) return callback(err)
3135
if (dirExists) return fs.symlink(srcpath, dstpath, type, callback)
3236
mkdirs(dir, err => {
3337
if (err) return callback(err)

lib/json/output-json.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict'
22

3-
const fs = require('graceful-fs')
43
const path = require('path')
54
const mkdir = require('../mkdirs')
5+
const pathExists = require('../path-exists').pathExists
66
const jsonFile = require('./jsonfile')
77

88
function outputJson (file, data, options, callback) {
@@ -13,7 +13,8 @@ function outputJson (file, data, options, callback) {
1313

1414
const dir = path.dirname(file)
1515

16-
fs.exists(dir, itDoes => {
16+
pathExists(dir, (err, itDoes) => {
17+
if (err) return callback(err)
1718
if (itDoes) return jsonFile.writeJson(file, data, options, callback)
1819

1920
mkdir.mkdirs(dir, err => {

lib/mkdirs/__tests__/mkdirp.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ describe('mkdirp / mkdirp', () => {
3131

3232
fse.mkdirp(file, o755, err => {
3333
assert.ifError(err)
34-
fs.exists(file, ex => {
34+
fse.pathExists(file, (err, ex) => {
35+
assert.ifError(err)
3536
assert.ok(ex, 'file created')
3637
fs.stat(file, (err, stat) => {
3738
assert.ifError(err)

0 commit comments

Comments
 (0)