node.jsnpmstarter-kits

How to dynamically create and write content inside files and folders with nodejs if making a starter kit generator


I want to create a Project Starter Kit Generator for a framework just like Angular CLI with node.js.

Following are the features that will be needed assuming the command would be kit-cli:

I have following approach in my mind:

Questions:


Solution

  • You can use node file system for that or a pure shell script. With node:

    const fs = require("fs");
    
    fs.writeFile(filePath, fileData, (err,res) => {
        if(err) console.log(err);
        console.log(res);
    });
    

    To write files in node:

    fs.readFile('./file3.json', (err,res) => {
        let file = res;
        console.log(res.toString('utf-8'));
    });
    

    You can wrap the fs.writeFile so you can reuse it with all files you need, like this:

    createFile('./file1.json', '{"pro1": "value1"', "prop2": "value2"}');
    createFile('./file2.json', '{"pro3": "value3"', "prop4": "value4"}');
    createFile('./file3.json', '{"pro5": "value5"', "prop6": "value6"}');
    
    function createFile(path, data) {
        fs.writeFile(path, data, (err,res) => {
            if(err) console.log(err);
            console.log(res);
        });
    }
    

    This is how you can read a js file:

    fs.readFile('./file.js', (err,res) => {
        let file = res.toString('utf-8');
        console.log(file);
        let lines = file.split('\n');
        for(var i = 0; i < lines.length; i++) {
            console.log(lines[i]);
        }
    });
    

    This is how you would add modules to it:

    let modules = ['const def = require("def");', 'const xyz = require("xyz");'];
    
    fs.readFile('./abc.js', (err,res) => {
        let file = res.toString('utf-8');
        let lines = file.split('\n');
        for(var i = 0; i < lines.length; i++) {
            if(i==0) {
                let mod = '';
                for(var j = 0; j < modules.length; j++) {
                   mod += modules[j] + "\n"; 
                }
                lines[i] = mod + lines[i];
            }
        }
        let newFile = lines.join('\n');
        createFile('./abc.js', newFile);
    });
    

    We are checking if we are at the first line of the file, so we know thats where we place the modules imports.

    You can define all your modules in the array and add it to the first line before the first module, thats why:

    lines[i] = mod + lines[i];
    

    Then we take all lines and add back a new line between them to save our file.

    let newFile = lines.join('\n');
    createFile('./abc.js', newFile);
    

    This is how you can check if you are at the end of a method declaration:

    if(lines[i].includes('});')) {
            lines[i] = '\tconsole.log("xyz added");\n\n' + lines[i];
    }
    

    To make sure you add to the right method instead of all of them:

    if(lines[i].includes('});') && lines[i].includes('abc')) {
        lines[i] = '\tconsole.log("xyz added");\n\n' + lines[i];
    }
    

    And add the comment in your method declaration:

    abc.method(function () { 
      console.log("abc called");
    }); // end of abc