Skip to content

Instantly share code, notes, and snippets.

@gyandeeps
Created May 24, 2016 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gyandeeps/23a4c836b54f91d9d3cfed7ab37e7a0b to your computer and use it in GitHub Desktop.
Save gyandeeps/23a4c836b54f91d9d3cfed7ab37e7a0b to your computer and use it in GitHub Desktop.
Convert physical file structure to mock-fs compatible virtual structure.
var glob = require("glob");
var fs = require("fs");
var path = require("path");
var dir = "C:/Users/gs025879/Documents/webstrom/eslint/tests/fixtures/config-hierarchy";
var structure = glob.sync("**/*.*", {
cwd: dir,
dot:true
});
var finalStructure = {};
structure.reduce(function(prevValue, currentVal){
var pieces = currentVal.split("/");
var firstDir = pieces.shift();
if(!prevValue[firstDir]) {
prevValue[firstDir] = {};
}
pieces.reduce(function(coll, val, currentIndex){
if (currentIndex < pieces.length - 1) {
if (!coll[val]) {
coll[val] = {};
}
return coll[val];
}
else {
coll[val] = fs.readFileSync(path.resolve(dir, currentVal), {encoding: "utf8"});
}
return coll;
}, prevValue[firstDir]);
return prevValue;
}, finalStructure);
console.info(finalStructure);
fs.writeFileSync("mock.json", JSON.stringify(finalStructure));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment