javascriptjsonnode.jsnpmfs-extra

Creating Directories using NPM Module fs-extra


I need some help.

I need to be able to create a directory structure which looks like the following:

parent
  child1
    folder1
    folder2
  child2
    folder1
    folder2

I'm currently using fs-extra (npm module). My issue is that the folder structure needs to be read from a .JSON file. When running say buildDir.js, it should read the .JSON file and create the above structure under a dist (distribution) folder. My current .JSON files looks like:

{
    "directories": [
        {
            "type": "folder",
            "name": "parent",
            "path": "parent/child1"
        },
        ...
    ]
}

p.s I'm quite new to Javascript so my code my be a little sparse in places. Any help would be great.


Solution

  • var parsedJson = JSON.parse(fs.readFileSync("directories.json", "UTF-8"));
    parsedJson.directories.forEach(function(value){
        if(value.type == "folder"){
            fs.mkdirSync(value.path);
        }
    });
    console.log("It worked!");