Skip to content

Commit

Permalink
Universalify fs-extra methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanZim committed Apr 26, 2017
1 parent dba0cbb commit 03b2080
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 75 deletions.
29 changes: 29 additions & 0 deletions lib/__tests__/promise.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

/* eslint-env mocha */

const fse = require('..')

const methods = [
'copy',
'emptyDir',
'ensureFile',
'ensureDir',
'ensureLink',
'ensureSymlink',
'mkdirs',
'move',
'readJson',
'readJSON',
'remove',
'writeJson',
'writeJSON'
]

describe('promise support', () => {
methods.forEach(method => {
it(method, done => {
fse[method]().catch(() => done())
})
})
})
3 changes: 2 additions & 1 deletion lib/copy/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const u = require('universalify').fromCallback
module.exports = {
copy: require('./copy')
copy: u(require('./copy'))
}
5 changes: 3 additions & 2 deletions lib/empty/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use strict'

const u = require('universalify').fromCallback
const fs = require('fs')
const path = require('path')
const mkdir = require('../mkdirs')
const remove = require('../remove')

function emptyDir (dir, callback) {
const emptyDir = u(function emptyDir (dir, callback) {
callback = callback || function () {}
fs.readdir(dir, (err, items) => {
if (err) return mkdir.mkdirs(dir, callback)
Expand All @@ -23,7 +24,7 @@ function emptyDir (dir, callback) {
})
}
})
}
})

function emptyDirSync (dir) {
let items
Expand Down
8 changes: 3 additions & 5 deletions lib/ensure/file.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const u = require('universalify').fromCallback
const path = require('path')
const fs = require('graceful-fs')
const mkdir = require('../mkdirs')
Expand Down Expand Up @@ -37,9 +38,6 @@ function createFileSync (file) {
}

module.exports = {
createFile,
createFileSync,
// alias
ensureFile: createFile,
ensureFileSync: createFileSync
createFile: u(createFile),
createFileSync
}
8 changes: 3 additions & 5 deletions lib/ensure/link.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const u = require('universalify').fromCallback
const path = require('path')
const fs = require('graceful-fs')
const mkdir = require('../mkdirs')
Expand Down Expand Up @@ -52,9 +53,6 @@ function createLinkSync (srcpath, dstpath, callback) {
}

module.exports = {
createLink,
createLinkSync,
// alias
ensureLink: createLink,
ensureLinkSync: createLinkSync
createLink: u(createLink),
createLinkSync
}
8 changes: 3 additions & 5 deletions lib/ensure/symlink.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const u = require('universalify').fromCallback
const path = require('path')
const fs = require('graceful-fs')
const _mkdirs = require('../mkdirs')
Expand Down Expand Up @@ -56,9 +57,6 @@ function createSymlinkSync (srcpath, dstpath, type, callback) {
}

module.exports = {
createSymlink,
createSymlinkSync,
// alias
ensureSymlink: createSymlink,
ensureSymlinkSync: createSymlinkSync
createSymlink: u(createSymlink),
createSymlinkSync
}
53 changes: 25 additions & 28 deletions lib/fs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,37 @@ const u = require('universalify').fromCallback
const fs = require('graceful-fs')

const api = [
'rename',
'ftruncate',
'chown',
'fchown',
'lchown',
'access',
'appendFile',
'chmod',
'chown',
'close',
'fchmod',
'stat',
'lstat',
'fchown',
'fdatasync',
'fstat',
'fsync',
'ftruncate',
'futimes',
'lchown',
'link',
'symlink',
'lstat',
'mkdir',
'open',
'read',
'readFile',
'readdir',
'readlink',
'realpath',
'unlink',
'rename',
'rmdir',
'mkdir',
'readdir',
'close',
'open',
'stat',
'symlink',
'truncate',
'unlink',
'utimes',
'futimes',
'fsync',
'fdatasync',
'write',
'read',
'readFile',
'writeFile',
'appendFile',
'truncate'
'writeFile'
]
// fs.mkdtemp() was added in Node.js v5.10.0, so check if it exists
typeof fs.mkdtemp === 'function' && api.push('mkdtemp')
Expand All @@ -52,13 +53,9 @@ api.forEach(method => {
// since we are a drop-in replacement for the native module
exports.exists = function (filename, callback) {
if (typeof callback === 'function') {
return fs.exists(filename, function (exists) {
callback(exists)
})
return fs.exists(filename, callback)
}
return new Promise(function (resolve) {
return fs.exists(filename, function (exists) {
resolve(exists)
})
return new Promise(resolve => {
return fs.exists(filename, resolve)
})
}
8 changes: 8 additions & 0 deletions lib/json/__tests__/output-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ describe('json', () => {
})
})

it('should support Promises', () => {
const file = path.join(TEST_DIR, 'this-dir', 'prob-does-not', 'exist', 'file.json')
assert(!fs.existsSync(file))

const data = {name: 'JP'}
return outputJson(file, data)
})

describe('> when an option is passed, like JSON replacer', () => {
it('should pass the option along to jsonfile module', done => {
const file = path.join(TEST_DIR, 'this-dir', 'does-not', 'exist', 'really', 'file.json')
Expand Down
11 changes: 8 additions & 3 deletions lib/json/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict'

const u = require('universalify').fromCallback
const jsonFile = require('./jsonfile')

jsonFile.outputJsonSync = require('./output-json-sync')
jsonFile.outputJson = require('./output-json')
jsonFile.outputJson = u(require('./output-json'))
// aliases
jsonFile.outputJSONSync = require('./output-json-sync')
jsonFile.outputJSON = require('./output-json')
jsonFile.outputJSONSync = jsonFile.outputJSONSync
jsonFile.outputJSON = jsonFile.outputJson
jsonFile.writeJSON = jsonFile.writeJson
jsonFile.writeJSONSync = jsonFile.writeJsonSync
jsonFile.readJSON = jsonFile.readJson
jsonFile.readJSONSync = jsonFile.readJsonSync

module.exports = jsonFile
11 changes: 4 additions & 7 deletions lib/json/jsonfile.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
'use strict'

const u = require('universalify').fromCallback
const jsonFile = require('jsonfile')

module.exports = {
// jsonfile exports
readJson: jsonFile.readFile,
readJSON: jsonFile.readFile,
readJson: u(jsonFile.readFile),
readJsonSync: jsonFile.readFileSync,
readJSONSync: jsonFile.readFileSync,
writeJson: jsonFile.writeFile,
writeJSON: jsonFile.writeFile,
writeJsonSync: jsonFile.writeFileSync,
writeJSONSync: jsonFile.writeFileSync
writeJson: u(jsonFile.writeFile),
writeJsonSync: jsonFile.writeFileSync
}
17 changes: 11 additions & 6 deletions lib/mkdirs/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
'use strict'
const u = require('universalify').fromCallback
const mkdirs = u(require('./mkdirs'))
const mkdirsSync = require('./mkdirs-sync')

module.exports = {
mkdirs: require('./mkdirs'),
mkdirsSync: require('./mkdirs-sync'),
mkdirs: mkdirs,
mkdirsSync: mkdirsSync,
// alias
mkdirp: require('./mkdirs'),
mkdirpSync: require('./mkdirs-sync'),
ensureDir: require('./mkdirs'),
ensureDirSync: require('./mkdirs-sync')
mkdirp: mkdirs,
mkdirpSync: mkdirsSync,
ensureDir: mkdirs,
ensureDirSync: mkdirsSync
}
3 changes: 2 additions & 1 deletion lib/move/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// this needs a cleanup

const u = require('universalify').fromCallback
const fs = require('graceful-fs')
const ncp = require('../copy/ncp')
const path = require('path')
Expand Down Expand Up @@ -157,5 +158,5 @@ function moveDirAcrossDevice (source, dest, overwrite, callback) {
}

module.exports = {
move
move: u(move)
}
5 changes: 5 additions & 0 deletions lib/output/__tests__/output.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ describe('output', () => {
done()
})
})
it('should support promises', () => {
const file = path.join(TEST_DIR, Math.random() + 't-ne', Math.random() + '.txt')
assert(!fs.existsSync(file))
return fse.outputFile(file, 'hi jp')
})
})

describe('> when the file does exist', () => {
Expand Down
3 changes: 2 additions & 1 deletion lib/output/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const u = require('universalify').fromCallback
const fs = require('graceful-fs')
const path = require('path')
const mkdir = require('../mkdirs')
Expand Down Expand Up @@ -32,6 +33,6 @@ function outputFileSync (file, data, encoding) {
}

module.exports = {
outputFile,
outputFile: u(outputFile),
outputFileSync
}
14 changes: 3 additions & 11 deletions lib/remove/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
'use strict'

const u = require('universalify').fromCallback
const rimraf = require('./rimraf')

function removeSync (dir) {
return rimraf.sync(dir, {disableGlob: true})
}

function remove (dir, callback) {
const options = {disableGlob: true}
return callback ? rimraf(dir, options, callback) : rimraf(dir, options, function () {})
}

module.exports = {
remove,
removeSync
remove: u(rimraf),
removeSync: rimraf.sync
}

0 comments on commit 03b2080

Please sign in to comment.