Read a directory tree structure and parse contents of files into js object. Think require-dir for all file types (bring your own parser).
const ini = require('ini');
const yaml = require('yaml');
const slurpdir = require('slurpdir');
let args = {
dir: __dirname,
recursive: true,
parsers: {
ini: (input) => ini.parse(input.data.toString()),
yaml: (input) => yaml.eval(input.data.toString())
}
};
new slurpdir.Slurp({dir, recursive: true})
.on('file', (name, data, path) => {
console.log(name, Object.keys(data));
})
.once('end', () => console.log('done!'););
There's also a callback interface.
slurpdir(__dirname, {recursive: true}, (err, tree) => {
console.log(Object.keys(tree));
});
Need sync? Ok.
let tree = slurpdir.sync(__dirname, {recursive: true});
console.dir(tree);
When running in async mode and parsing javascript files slurp will actually use node's require
to
parse which means that it's not actually async. If you check the source for parsers.js
you will
find a couple of commented parsers for js which do enable async parsing of js, however both methods
have caveats.