The promisified version of Node modules caught my attention recently because the syntax is so much nicer than callbacks, in my opinion. As such, I started to fool around with the promisified version of writeFile from Node's fs module. This led me down a surprising rabbit hole where I landed upon Node’s child_process module.
According to the documentation, its .exec() method “spawns a shell then executes the command within that shell…” So, depending on the program, we can automate tasks like git commits and npm install commands, among other creative use cases. For example, I used it to open files within VS Code.
The program, shown below, automates the creation of an index.html file, style.css file, and an app.js file--all linked together and ready to start coding. It can be executed with the following command in your terminal:
node starter.js
You can even pass in a third argument which will populate the <title> element:
node starter.js “My New Website”
But in order to open the files in VS Code, be sure you have VS Code's shell command enabled from the Command Palette. Open the Command Palette with command/control + shift + P, or go to "View" --> "Command Palette". Then search for and click "Shell Command: Install 'code' command in PATH". Once that's done, you're all set! If you don't do this step, delete or comment out the exec() invocation.
Lastly, I use the promisified version of unlink() from Node’s fs module to delete the original file, since its purpose is served after running the program. 
Feel free to use this as a template and build upon it. I built upon it already and made a MERN Stack Build Template. It automates the creation of a full-blown project file structure for React, Node, Express, MongoDB, Mongoose, webpack, and Babel. Now, in a few seconds, I have a skeleton for a React app whose server and database are ready to go!​​​​​​​

You may also like

Back to Top