Created
March 25, 2015 16:32
-
-
Save veltman/0c61f9f29872751a379c 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
'use strict'; | |
var fs = require('fs'); | |
var glob = require('glob'); | |
var path = require('path'); | |
var yaml = require('js-yaml'); | |
module.exports = function(dataDir) { | |
var payload = {}, | |
files = glob.sync(dataDir.replace(/\/?$/, '/') + '**/*.{json,yaml,yml}'); | |
files.forEach(function(file) { | |
var props = path.parse(file); | |
var extension = props.ext; | |
var basename = props.name; | |
var data; | |
if (extension === '.json') { | |
data = JSON.parse(fs.readFileSync(file, 'utf8')); | |
} else if (extension === '.yaml' || extension === '.yml') { | |
data = yaml.safeLoad(fs.readFileSync(file, 'utf8')); | |
} else { | |
return; | |
} | |
var obj = payload; | |
var dirs = props.dir.split(path.sep); | |
dirs.shift(); // dump the root dataDir | |
dirs.forEach(function(dir) { | |
if (!obj.hasOwnProperty(dir)) { | |
obj[dir] = {}; | |
} | |
obj = obj[dir]; | |
}); | |
obj[basename] = data; | |
}); | |
return payload; // Does not work for async reasons I understand, but need to make play along | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment