-
Notifications
You must be signed in to change notification settings - Fork 264
/
loaddir.js
32 lines (25 loc) · 850 Bytes
/
loaddir.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function errorHandler(error) {
throw error;
}
var Do = require('do');
var fs = Do.convert(require('fs'), ['readdir', 'stat', 'readFile']);
// Checks the `stat` of a file path and outputs the file contents if it's
// a real file
function loadFile(path, callback, errback) {
fs.stat(path)(function (stat) {
// Output undefined when the path isn't a regular file
if (!stat.isFile()) {
callback();
return;
}
// Pass through the read to regular files as is.
fs.readFile(path, 'utf8')(callback, errback)
}, errback);
}
// Load an array of the contents of all files in a directory.
function loaddir(path) { return function (callback, errback) {
fs.readdir(path)(function (filenames) {
Do.filterMap(filenames, loadFile)(callback, errback);
}, errback);
}}
loaddir(__dirname)(console.dir, errorHandler)