1

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:

  • kit-cli init or kit-cli am/as : will ask few questions and based on that it will create all the files and folders to get started.

  • kit-cli add option : will create and add some codes into existing files and will run some system based commands if required.

I have following approach in my mind:

  • Putting my content into JSON files.

  • Based on User's choices, read content from JSON file and create folders and files.

  • No idea about how I will add/remove codes from a existing file.

Questions:

  • How should I store and create the folder/file structure assuming that the folder structure will vary based on user's choices.

  • How should I edit/add/remove code from an existing file.

DDD
  • 119
  • 3
  • 10

1 Answers1

1

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
  • Does fs module provide functionality to write into an existing file at a specific line by searching any phrase or code? – DDD Mar 26 '19 at 11:43
  • For that you can use readFile to store the values in a variable and add some more content into it and then you use writeFile to save. I will add readFile to my answer. – Carlos Jafet Neto Mar 26 '19 at 11:45
  • The problem is I will not be dealing with JSON files. It will be a .js file and I have to read the file, search for a keyword and add some code just after that keyword. – DDD Mar 26 '19 at 11:54
  • Can you share this code? We can use the same process. – Carlos Jafet Neto Mar 26 '19 at 11:55
  • I update the answer with the code to read js files. We create an array out of every line in the file sou you can deal with it. – Carlos Jafet Neto Mar 26 '19 at 12:01
  • https://glitch.com/edit/#!/join/40890967-b5e2-461c-9729-ae0080f65949 I have added an example of what I want. – DDD Mar 26 '19 at 12:04
  • I update the answer with the code to add new modules to the file so you can see how you would do it with any other part of the file. – Carlos Jafet Neto Mar 26 '19 at 12:30
  • Okay I would have given you the thumbs up but I don't have enough reputations. Although I have one more question to ask, I understand how I will insert the modules but what about the function call or other stuff that will be placed in between the file. – DDD Mar 26 '19 at 12:36
  • Do you want one example to add something inside an existing function. Let me do it! Thanks! – Carlos Jafet Neto Mar 26 '19 at 12:37
  • Yes please, it will be a great help. – DDD Mar 26 '19 at 12:49
  • Sure! i add it there! – Carlos Jafet Neto Mar 26 '19 at 12:55
  • Just one fix, though! We need to add our new modules first so we do not replace the modules at the second line. This way we do not need to do any extra validation. lines[i] = mod + lines[i]; – Carlos Jafet Neto Mar 26 '19 at 13:02