Skip to content

Instantly share code, notes, and snippets.

@Raynos
Forked from rvagg/config.json
Last active December 16, 2015 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/e312c81ac46b96135517 to your computer and use it in GitHub Desktop.
Save Raynos/e312c81ac46b96135517 to your computer and use it in GitHub Desktop.
{
"foo": "/path/to/foo"
, "bar": "/path/to/bar"
}
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)
}
}
})
})
}
}
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)
})
// 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