-
-
Save Raynos/e312c81ac46b96135517 to your computer and use it in GitHub Desktop.
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
{ | |
"foo": "/path/to/foo" | |
, "bar": "/path/to/bar" | |
} |
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
module.exports = hash | |
// hash := (tasks:Object<String, Continuable<T>>) | |
// => Continuable<Object<String, T>> | |
function hash(tasks) { | |
return function continuable(callback) { | |
var result = {} | |
var ended = false | |
var count = 0 | |
var keys = Object.keys(tasks) | |
var length = keys.length | |
keys.forEach(function (key) { | |
var source = tasks[key] | |
source(function (err, value) { | |
if (err && !ended) { | |
callback(err) | |
} else if (!err) { | |
result[key] = value | |
if (++count === length) { | |
callback(null, result) | |
} | |
} | |
}) | |
}) | |
} | |
} |
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
var fs = require("fs") | |
var map = require("continuable/map") | |
var hash = require("./hash") | |
function stat (uri) { | |
return function continuable (callback) { | |
fs.stat(uri, callback) | |
} | |
} | |
function loadFiles(files, root, config) { | |
var uris = files.reduce(function (acc, key) { | |
acc[key] = path.resolve(root, config[key]) | |
return acc | |
}, {}) | |
var stats = Object.keys(uris).reduce(function (acc, key) { | |
return map(function (stat) { | |
return { stat: stat, uri: uris[key] } | |
})(stat(uris[key])) | |
}) | |
return hash(stats) | |
} | |
// MAIN function, load a config given a root directory and a config | |
// object | |
loadFiles(["foo", "bar"])(function (err, result) { | |
if (err) return console.error('Error:', err) | |
console.log(result) | |
}) |
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
// expected result: | |
{ foo: | |
{ stat: | |
{ dev: 2065, | |
mode: 16893, | |
nlink: 2, | |
uid: 1000, | |
gid: 1000, | |
rdev: 0, | |
blksize: 4096, | |
ino: 11701430, | |
size: 4096, | |
blocks: 8, | |
atime: Tue Apr 30 2013 14:49:26 GMT+1000 (EST), | |
mtime: Tue Apr 30 2013 14:49:26 GMT+1000 (EST), | |
ctime: Tue Apr 30 2013 14:49:26 GMT+1000 (EST) }, | |
uri: '/path/to/foo' }, | |
bar: | |
{ stat: | |
{ dev: 2065, | |
mode: 16893, | |
nlink: 2, | |
uid: 1000, | |
gid: 1000, | |
rdev: 0, | |
blksize: 4096, | |
ino: 11817511, | |
size: 4096, | |
blocks: 8, | |
atime: Tue Apr 30 2013 14:49:33 GMT+1000 (EST), | |
mtime: Tue Apr 30 2013 14:49:33 GMT+1000 (EST), | |
ctime: Tue Apr 30 2013 14:49:33 GMT+1000 (EST) }, | |
uri: '/path/to/bar' } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment