Skip to content

Commit

Permalink
refactor: remove callback (#69)
Browse files Browse the repository at this point in the history
* Remove cacllback

* Remove callback test

* Update readme
  • Loading branch information
segayuu authored May 20, 2020
1 parent 8bf0757 commit a86a151
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 293 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ 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.

### existsSync(path)

Synchronous version of `fs.exists`.

### mkdirs(path, [callback])
### mkdirs(path)

Creates a directory and its parent directories if they does not exist.

### mkdirsSync(path)

Synchronous version of `fs.mkdirs`.

### writeFile(path, data, [options], [callback])
### writeFile(path, data, [options])

Writes data to a file.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -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.

Expand All @@ -127,33 +127,33 @@ Option | Description | Default

Synchronous version of `fs.emptyDir`.

### rmdir(path, [callback])
### rmdir(path)

Removes a directory and all files in it.

### rmdirSync(path)

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.

### ensurePathSync(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`.

Expand Down
97 changes: 25 additions & 72 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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 = {}) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
Loading

0 comments on commit a86a151

Please sign in to comment.