To create folder and file by the node js, give the command by cmd.

To create folder and file by the node js, give the command by cmd.

<!--define the filesystem as constant-->
const fs = require('fs');
<!-- read command from the cmd by process, argv[2] get the string in array-->
const filename = process.argv[2] || 'Project';

<!--make the folder and have a call back method.-->
// fs.mkdir('apple', { recursive: true }, (err) => {
//     console.log("in the callback")
//     if (err) throw err;
// });

try {
    fs.mkdirSync(filename); // create folder without callback
    fs.writeFileSync(`${filename}/index.html`'') //create a file
    fs.writeFileSync(`${filename}/app.js`'')
    fs.writeFileSync(`${filename}/style.css`'')
}
catch (e) {
    console.log("someting is wrong");
    console.log(e);
}

To get a string from the cmd and say hello to every string.


const args = process.argv.slice(2);
for (let arg of args) {
    console.log(`hii there, ${arg}`)
}



 

Comments