From a86a1518e37cf8e66f332dd7aaf11b330b09ebec Mon Sep 17 00:00:00 2001 From: segayuu Date: Wed, 20 May 2020 22:47:16 +0900 Subject: [PATCH] refactor: remove callback (#69) * Remove cacllback * Remove callback test * Update readme --- README.md | 26 +++---- lib/fs.js | 97 ++++++----------------- package.json | 2 - test/index.js | 210 +------------------------------------------------- 4 files changed, 42 insertions(+), 293 deletions(-) diff --git a/README.md b/README.md index d2ac5b4..6b15d67 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ const fs = require('hexo-fs'); > Some methods in the original fs module are not listed below, but they're available in hexo-fs. -### exists(path, [callback]) +### exists(path) Test whether or not the given `path` exists by checking with the file system. @@ -37,7 +37,7 @@ Test whether or not the given `path` exists by checking with the file system. Synchronous version of `fs.exists`. -### mkdirs(path, [callback]) +### mkdirs(path) Creates a directory and its parent directories if they does not exist. @@ -45,7 +45,7 @@ Creates a directory and its parent directories if they does not exist. Synchronous version of `fs.mkdirs`. -### writeFile(path, data, [options], [callback]) +### writeFile(path, data, [options]) Writes data to a file. @@ -59,7 +59,7 @@ Option | Description | Default Synchronous version of `fs.writeFile`. -### appendFile(path, data, [options], [callback]) +### appendFile(path, data, [options]) Appends data to a file. @@ -77,7 +77,7 @@ Synchronous version of `fs.appendFile`. Copies a file from `src` to `dest`. -### copyDir(src, dest, [options], [callback]) +### copyDir(src, dest, [options]) Copies a directory from `src` to `dest`. It returns an array of copied files. @@ -86,7 +86,7 @@ Option | Description | Default `ignoreHidden` | Ignore hidden files | true `ignorePattern` | Ignore files which pass the regular expression | -### listDir(path, [options], [callback]) +### listDir(path, [options]) Lists files in a directory. @@ -99,7 +99,7 @@ Option | Description | Default Synchronous version of `fs.listDir`. -### readFile(path, [options], [callback]) +### readFile(path, [options]) Reads the entire contents of a file. @@ -113,7 +113,7 @@ Option | Description | Default Synchronous version of `fs.readFile`. -### emptyDir(path, [options], [callback]) +### emptyDir(path, [options]) Deletes all files in a directory. It returns an array of deleted files. @@ -127,7 +127,7 @@ Option | Description | Default Synchronous version of `fs.emptyDir`. -### rmdir(path, [callback]) +### rmdir(path) Removes a directory and all files in it. @@ -135,13 +135,13 @@ Removes a directory and all files in it. Synchronous version of `fs.rmdir`. -### watch(path, [options], [callback]) +### watch(path, [options]) Watches changes of a file or a directory. See [Chokidar API](https://github.com/paulmillr/chokidar#api) for more info. -### ensurePath(path, [callback]) +### ensurePath(path) Ensures the given path is available to use or appends a number to the path. @@ -149,11 +149,11 @@ Ensures the given path is available to use or appends a number to the path. Synchronous version of `fs.ensurePath`. -### ensureWriteStream(path, [options], [callback]) +### ensureWriteStream(path, [options]) Creates the parent directories if they does not exist and returns a writable stream. -### ensureWriteStream(path, [options]) +### ensureWriteStreamSync(path, [options]) Synchronous version of `fs.ensureWriteStream`. diff --git a/lib/fs.js b/lib/fs.js index 70701fe..ab41e37 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -9,15 +9,12 @@ const { escapeRegExp } = require('hexo-util'); const rEOL = /\r\n/g; -function exists(path, callback) { +function exists(path) { if (!path) throw new TypeError('path is required!'); const promise = fsPromises.access(path).then(() => true, err => { if (err.code !== 'ENOENT') throw err; return false; - }).then(exist => { - if (typeof callback === 'function') callback(exist); - return exist; }); return Promise.resolve(promise); @@ -36,10 +33,10 @@ function existsSync(path) { return true; } -function mkdirs(path, callback) { +function mkdirs(path) { if (!path) throw new TypeError('path is required!'); - return Promise.resolve(fsPromises.mkdir(path, { recursive: true })).asCallback(callback); + return Promise.resolve(fsPromises.mkdir(path, { recursive: true })); } function mkdirsSync(path) { @@ -52,17 +49,12 @@ function checkParent(path) { return Promise.resolve(fsPromises.mkdir(dirname(path), { recursive: true })); } -function writeFile(path, data, options, callback) { +function writeFile(path, data, options = {}) { if (!path) throw new TypeError('path is required!'); if (!data) data = ''; - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - - return checkParent(path).then(() => fsPromises.writeFile(path, data, options)).asCallback(callback); + return checkParent(path).then(() => fsPromises.writeFile(path, data, options)); } function writeFileSync(path, data, options) { @@ -72,15 +64,10 @@ function writeFileSync(path, data, options) { fs.writeFileSync(path, data, options); } -function appendFile(path, data, options, callback) { +function appendFile(path, data, options = {}) { if (!path) throw new TypeError('path is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - - return checkParent(path).then(() => fsPromises.appendFile(path, data, options)).asCallback(callback); + return checkParent(path).then(() => fsPromises.appendFile(path, data, options)); } function appendFileSync(path, data, options) { @@ -90,15 +77,11 @@ function appendFileSync(path, data, options) { fs.appendFileSync(path, data, options); } -function copyFile(src, dest, flags, callback) { +function copyFile(src, dest, flags) { if (!src) throw new TypeError('src is required!'); if (!dest) throw new TypeError('dest is required!'); - if (typeof flags === 'function') { - callback = flags; - flags = undefined; - } - return checkParent(dest).then(() => fsPromises.copyFile(src, dest, flags)).asCallback(callback); + return checkParent(dest).then(() => fsPromises.copyFile(src, dest, flags)); } function trueFn() { @@ -153,18 +136,13 @@ function _copyDirWalker(src, dest, results, parent, options) { }); } -function copyDir(src, dest, options = {}, callback) { +function copyDir(src, dest, options = {}) { if (!src) throw new TypeError('src is required!'); if (!dest) throw new TypeError('dest is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - const results = []; - return checkParent(dest).then(() => _copyDirWalker(src, dest, results, '', options)).return(results).asCallback(callback); + return checkParent(dest).then(() => _copyDirWalker(src, dest, results, '', options)).return(results); } async function _listDirWalker(path, results, parent, options) { @@ -183,17 +161,12 @@ async function _listDirWalker(path, results, parent, options) { await Promise.all(promises); } -function listDir(path, options = {}, callback) { +function listDir(path, options = {}) { if (!path) throw new TypeError('path is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - const results = []; - return Promise.resolve(_listDirWalker(path, results, '', options)).return(results).asCallback(callback); + return Promise.resolve(_listDirWalker(path, results, '', options)).return(results); } function _listDirSyncWalker(path, results, parent, options) { @@ -242,15 +215,10 @@ async function _readFile(path, options) { return content; } -function readFile(path, options = {}, callback) { +function readFile(path, options = {}) { if (!path) throw new TypeError('path is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - - return Promise.resolve(_readFile(path, options)).asCallback(callback); + return Promise.resolve(_readFile(path, options)); } function readFileSync(path, options = {}) { @@ -291,15 +259,10 @@ async function _emptyDir(path, parent, options) { return results; } -function emptyDir(path, options = {}, callback) { +function emptyDir(path, options = {}) { if (!path) throw new TypeError('path is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - - return Promise.resolve(_emptyDir(path, '', options)).asCallback(callback); + return Promise.resolve(_emptyDir(path, '', options)); } function _emptyDirSync(path, options, parent) { @@ -345,10 +308,10 @@ async function _rmdir(path) { return fsPromises.rmdir(path); } -function rmdir(path, callback) { +function rmdir(path) { if (!path) throw new TypeError('path is required!'); - return Promise.resolve(_rmdir(path)).asCallback(callback); + return Promise.resolve(_rmdir(path)); } function _rmdirSync(path) { @@ -373,20 +336,15 @@ function rmdirSync(path) { _rmdirSync(path); } -function watch(path, options, callback) { +function watch(path, options = {}) { if (!path) throw new TypeError('path is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - const watcher = chokidar.watch(path, options); return new Promise((resolve, reject) => { watcher.on('ready', resolve); watcher.on('error', reject); - }).thenReturn(watcher).asCallback(callback); + }).thenReturn(watcher); } function _findUnusedPath(path, files) { @@ -417,10 +375,10 @@ async function _ensurePath(path) { return _findUnusedPath(path, files); } -function ensurePath(path, callback) { +function ensurePath(path) { if (!path) throw new TypeError('path is required!'); - return Promise.resolve(_ensurePath(path)).asCallback(callback); + return Promise.resolve(_ensurePath(path)); } function ensurePathSync(path) { @@ -432,15 +390,10 @@ function ensurePathSync(path) { return _findUnusedPath(path, files); } -function ensureWriteStream(path, options, callback) { +function ensureWriteStream(path, options = {}) { if (!path) throw new TypeError('path is required!'); - if (!callback && typeof options === 'function') { - callback = options; - options = {}; - } - - return checkParent(path).then(() => fs.createWriteStream(path, options)).asCallback(callback); + return checkParent(path).then(() => fs.createWriteStream(path, options)); } function ensureWriteStreamSync(path, options) { diff --git a/package.json b/package.json index 1f2b313..379ae17 100644 --- a/package.json +++ b/package.json @@ -35,10 +35,8 @@ }, "devDependencies": { "chai": "^4.1.2", - "chai-as-promised": "^7.1.1", "eslint": "^7.0.0", "eslint-config-hexo": "^4.1.0", - "iferr": "^1.0.2", "mocha": "^7.0.0", "nyc": "^15.0.0" }, diff --git a/test/index.js b/test/index.js index 555b5a1..2322ac5 100644 --- a/test/index.js +++ b/test/index.js @@ -1,14 +1,13 @@ 'use strict'; -require('chai').use(require('chai-as-promised')).should(); const { should } = require('chai'); +should(); const { join, dirname } = require('path'); const Promise = require('bluebird'); const fs = require('../lib/fs'); -const { tiferr } = require('iferr'); -function createDummyFolder(path, callback) { +function createDummyFolder(path) { const filesMap = { // Normal files in a hidden folder [join('.hidden', 'a.txt')]: 'a', @@ -26,7 +25,7 @@ function createDummyFolder(path, callback) { // A hidden files in a normal folder [join('folder', '.j')]: 'j' }; - return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key])).asCallback(callback); + return Promise.map(Object.keys(filesMap), key => fs.writeFile(join(path, key), filesMap[key])); } describe('fs', () => { @@ -41,18 +40,6 @@ describe('fs', () => { exist.should.eql(true); }); - it('exists() - callback', callback => { - fs.exists(tmpDir, exist => { - try { - exist.should.be.true; - } catch (e) { - callback(e); - return; - } - callback(); - }); - }); - it('exists() - path is required', async () => { try { await fs.exists(); @@ -72,17 +59,6 @@ describe('fs', () => { await fs.rmdir(join(tmpDir, 'a')); }); - it('mkdirs() - callback', callback => { - const target = join(tmpDir, 'a', 'b', 'c'); - - fs.mkdirs(target, tiferr(callback, () => { - fs.exists(target, exist => { - exist.should.be.true; - fs.rmdir(join(tmpDir, 'a'), callback); - }); - })); - }); - it('mkdirs() - path is required', async () => { try { await fs.mkdirs(); @@ -124,18 +100,6 @@ describe('fs', () => { await fs.rmdir(join(tmpDir, 'a')); }); - it('writeFile() - callback', callback => { - const target = join(tmpDir, 'a', 'b', 'test.txt'); - const body = 'foo'; - - fs.writeFile(target, body, tiferr(callback, () => { - fs.readFile(target, tiferr(callback, content => { - content.should.eql(body); - fs.rmdir(join(tmpDir, 'a'), callback); - })); - })); - }); - it('writeFile() - path is required', async () => { try { await fs.writeFile(); @@ -181,21 +145,6 @@ describe('fs', () => { await fs.rmdir(join(tmpDir, 'a')); }); - it('appendFile() - callback', callback => { - const target = join(tmpDir, 'a', 'b', 'test.txt'); - const body = 'foo'; - const body2 = 'bar'; - - fs.writeFile(target, body, tiferr(callback, () => { - fs.appendFile(target, body2, tiferr(callback, () => { - fs.readFile(target, tiferr(callback, content => { - content.should.eql(body + body2); - fs.rmdir(join(tmpDir, 'a'), callback); - })); - })); - })); - }); - it('appendFile() - path is required', async () => { try { await fs.appendFile(); @@ -245,25 +194,6 @@ describe('fs', () => { ]); }); - it('copyFile() - callback', callback => { - const src = join(tmpDir, 'test.txt'); - const dest = join(tmpDir, 'a', 'b', 'test.txt'); - const body = 'foo'; - - fs.writeFile(src, body, tiferr(callback, () => { - fs.copyFile(src, dest, tiferr(callback, () => { - fs.readFile(dest, tiferr(callback, content => { - content.should.eql(body); - - Promise.all([ - fs.unlink(src), - fs.rmdir(join(tmpDir, 'a')) - ]).asCallback(callback); - })); - })); - })); - }); - it('copyFile() - src is required', async () => { try { await fs.copyFile(); @@ -307,30 +237,6 @@ describe('fs', () => { await Promise.all([fs.rmdir(src), fs.rmdir(dest)]); }); - it('copyDir() - callback', callback => { - const src = join(tmpDir, 'a'); - const dest = join(tmpDir, 'b'); - - const finenames = [ - 'e.txt', - 'f.js', - join('folder', 'h.txt'), - join('folder', 'i.js') - ]; - - createDummyFolder(src, tiferr(callback, () => { - fs.copyDir(src, dest, tiferr(callback, files => { - files.should.have.members(finenames); - fs.rmdir(src, tiferr(callback, () => { - Promise.map(finenames, path => fs.readFile(join(dest, path))).asCallback(tiferr(callback, result => { - result.should.eql(['e', 'f', 'h', 'i']); - fs.rmdir(dest, callback); - })); - })); - })); - })); - }); - it('copyDir() - src is required', async () => { try { await fs.copyDir(); @@ -415,24 +321,6 @@ describe('fs', () => { await fs.rmdir(target); }); - it('listDir() - callback', callback => { - const target = join(tmpDir, 'test'); - - const filenames = [ - 'e.txt', - 'f.js', - join('folder', 'h.txt'), - join('folder', 'i.js') - ]; - - createDummyFolder(target, tiferr(callback, () => { - fs.listDir(target, tiferr(callback, paths => { - paths.should.have.members(filenames); - fs.rmdir(target, callback); - })); - })); - }); - it('listDir() - path is required', async () => { try { await fs.listDir(); @@ -543,18 +431,6 @@ describe('fs', () => { await fs.unlink(target); }); - it('readFile() - callback', callback => { - const target = join(tmpDir, 'test.txt'); - const body = 'test'; - - fs.writeFile(target, body, tiferr(callback, () => { - fs.readFile(target, tiferr(callback, content => { - content.should.eql(body); - fs.unlink(target, callback); - })); - })); - }); - it('readFile() - path is required', async () => { try { await fs.readFile(); @@ -674,39 +550,6 @@ describe('fs', () => { await fs.rmdir(target); }); - it('emptyDir() - callback', callback => { - const target = join(tmpDir, 'test'); - - const checkExistsMap = { - [join('.hidden', 'a.txt')]: true, - [join('.hidden', 'b.js')]: true, - [join('.hidden', 'c', 'd')]: true, - 'e.txt': false, - 'f.js': false, - '.g': true, - [join('folder', 'h.txt')]: false, - [join('folder', 'i.js')]: false, - [join('folder', '.j')]: true - }; - - createDummyFolder(target, tiferr(callback, () => { - fs.emptyDir(target, tiferr(callback, files => { - files.should.have.members([ - 'e.txt', - 'f.js', - join('folder', 'h.txt'), - join('folder', 'i.js') - ]); - - return Promise.map(Object.keys(checkExistsMap), path => { - return fs.exists(join(target, path)).should.become(checkExistsMap[path]); - }).asCallback(tiferr(callback, () => { - fs.rmdir(target, callback); - })); - })); - })); - }); - it('emptyDir() - path is required', async () => { try { await fs.emptyDir(); @@ -933,24 +776,6 @@ describe('fs', () => { exist.should.eql(false); }); - it('rmdir() - callback', callback => { - const target = join(tmpDir, 'test'); - - createDummyFolder(target, tiferr(callback, () => { - fs.rmdir(target, tiferr(callback, () => { - fs.exists(target, exist => { - try { - exist.should.be.false; - } catch (e) { - callback(e); - return; - } - callback(); - }); - })); - })); - }); - it('rmdir() - path is required', async () => { try { await fs.rmdir(); @@ -1024,18 +849,6 @@ describe('fs', () => { result.should.eql(target); }); - it('ensurePath() - callback', callback => { - const target = join(tmpDir, 'test'); - const filenames = ['foo.txt', 'foo-1.txt', 'foo-2.md', 'bar.txt']; - - Promise.map(filenames, path => fs.writeFile(join(target, path))).asCallback(tiferr(callback, () => { - fs.ensurePath(join(target, 'foo.txt'), tiferr(callback, path => { - path.should.eql(join(target, 'foo-2.txt')); - fs.rmdir(target, callback); - })); - })); - }); - it('ensurePath() - path is required', async () => { try { await fs.ensurePath(); @@ -1088,21 +901,6 @@ describe('fs', () => { await fs.unlink(target); }); - it('ensureWriteStream() - callback', callback => { - const target = join(tmpDir, 'foo', 'bar.txt'); - - fs.ensureWriteStream(target, tiferr(callback, stream => { - stream.path.should.eql(target); - - stream.on('error', callback); - stream.on('close', () => { - fs.unlink(target, callback); - }); - - stream.end(); - })); - }); - it('ensureWriteStreamSync()', callback => { const target = join(tmpDir, 'foo', 'bar.txt'); const stream = fs.ensureWriteStreamSync(target); @@ -1111,7 +909,7 @@ describe('fs', () => { stream.on('error', callback); stream.on('close', () => { - fs.rmdir(dirname(target), callback); + fs.rmdir(dirname(target)).asCallback(callback); }); stream.end();