Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switch from the traditional callback API to Observable
- Loading branch information
Showing
6 changed files
with
3,280 additions
and
3,328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,39 @@ | ||
/*! | ||
* read-multiple-files | MIT (c) Shinnosuke Watanabe | ||
* https://github.com/shinnn/read-multiple-files | ||
*/ | ||
'use strict'; | ||
|
||
const {promisify} = require('util'); | ||
|
||
const inspectWithKind = require('inspect-with-kind'); | ||
const {readFile} = require('graceful-fs'); | ||
const runParalell = require('run-parallel'); | ||
const stripBom = require('strip-bom'); | ||
const stripBomBuf = require('strip-bom-buf'); | ||
const Observable = require('zen-observable'); | ||
|
||
const ARG_ERR = 'Expected 2 or 3 arguments (paths: <Array|Set>[, options: <Object>], callback: <Function>)'; | ||
const promisifiedReadFile = promisify(readFile); | ||
|
||
module.exports = function readMultipleFiles(...args) { | ||
const argLen = args.length; | ||
|
||
if (argLen !== 2 && argLen !== 3) { | ||
throw new RangeError(`${ARG_ERR}, but got ${ | ||
argLen === 1 ? '1 argument' : `${argLen || 'no'} arguments` | ||
} instead.`); | ||
} | ||
|
||
const [filePaths, options, cb] = argLen === 3 ? args : [args[0], {}, args[1]]; | ||
|
||
if (typeof cb !== 'function') { | ||
throw new TypeError(inspectWithKind(cb) + | ||
' is not a function. Last argument to read-multiple-files must be a callback function.'); | ||
} | ||
|
||
if (!Array.isArray(filePaths) && !(filePaths instanceof Set)) { | ||
throw new TypeError(inspectWithKind(filePaths) + | ||
' is neither Array nor Set. First Argument to read-multiple-files must be file paths (<Array> or <Set>).'); | ||
} | ||
|
||
runParalell([...filePaths].map(filePath => done => readFile(filePath, options, done)), (err, results) => { | ||
if (err) { | ||
cb(err); | ||
return; | ||
} | ||
|
||
if (results.length === 0) { | ||
cb(null, results); | ||
return; | ||
} | ||
|
||
if (Buffer.isBuffer(results[0])) { | ||
cb(null, results.map(stripBomBuf)); | ||
return; | ||
} | ||
|
||
cb(null, results.map(stripBom)); | ||
}); | ||
return new Observable(observer => { | ||
const argLen = args.length; | ||
|
||
if (argLen !== 1 && argLen !== 2) { | ||
throw new RangeError(`Expected 1 or 2 arguments (paths: <Array|Set>[, options: <Object>]), but got ${ | ||
argLen === 0 ? 'no' : argLen | ||
} arguments instead.`); | ||
} | ||
|
||
const [paths, options] = args; | ||
|
||
if (!Array.isArray(paths) && !(paths instanceof Set)) { | ||
const error = new TypeError(`Expected file paths (<Array> or <Set>), but got ${ | ||
inspectWithKind(paths) | ||
} instead.`); | ||
|
||
error.code = 'ERR_INVALID_ARG_TYPE'; | ||
throw error; | ||
} | ||
|
||
Promise.all([...paths].map(async path => { | ||
observer.next({ | ||
path, | ||
contents: await promisifiedReadFile(path, options) | ||
}); | ||
})).then(() => observer.complete(), err => observer.error(err)); | ||
}); | ||
}; |
Oops, something went wrong.