A Bash Script With Node.JS

Search for a command to run...

No comments yet. Be the first to comment.
A quick analysis of the top 25 CWEs in the last 5 years show a repeating pattern

In this article, you will learn to build a Markdown parser which compiles into HTML with JavaScript and Regular Expression Markdown is a markup language like HTML. It is quite popular among developers to write blogs, readme files, and documentation. ...

Handling error in express applications can be more challenging than building web applications and APIs Building web applications or APIs with express.js is not difficult in my opinion. I think it is more difficult to handle errors, error status codes...

This blog is a follow-up to my last blog in which I talked about promises. If you are unfamiliar with promises in JavaScript, please read that article before reading this one. Also, some familiarity with exception handling would be useful. Async Awai...
JavaScript is a scripting language, therefore you can write scripts to automate your workflow with JavaScript.
I created a node script recently to automate one workflow that I do repeatedly. Every time I create a new JavaScript project, I’d create a .prettierrc file and add 4 configurations.
The file looks something like this,
{
"semi": true,
"singleQuote": true,
"tabWidth": 2,
"useTabs": false,
}
For those who don’t know what this is, it is a configuration file for prettier. You can install it in your code editor to format your source code in a certain way.
The configuration I created above will make sure that my JS source code will have a semicolon at the end, it will use a single quote on strings, it will use 2 spaces for indentation and instead of using tabs, the editor will use space for formatting indentations.
This process is extremely repetitive. I would do this twice or thrice somedays when I am testing some simple snippets. I care about consistent formatting even in simple snippets that I create to test code (maybe because I have OCD).
This is why I decided to create this script to save me from this trouble and it was a great learning experience. In my opinion, it will open opportunities for me to do so much with this knowledge.
Let’s first understand the pieces that create this script and fit it all in together.
On the top of your scripts, we have to specify the path to the interpreter we plan to use. In our case, it is node. To do so, we have to use a shebang line.
For node
#!/usr/bin/env node
Some other examples
- for python
#!/usr/bin/env python3
- for bash
#!/bin/bash
For this script to work, it has to know where it is being run at. For example, if I run this script on Projects directory on my home directory (in Linux), it should be able to identify that and create a .prettierrc file inside ~/Projects.
For that, we can extract the current working directory from the node’s global process object.
process.cwd()
This gives us the path to the current working directory.
Node ships a file system module by default. We need to grab the writeFile function from the fs module and write our .prettierrc file to our file system.
Read the official docs.
Example
Now as we have all the background information, we can create the script.
First, create a file called prettier-config or prettier-config.js and paste the following content.
Note: the extension is not mandatory but remember what you name it because we have to access it later.
Here, I am just fitting together the information provided above and mixing it to create the .prettierrc file.
The script stores the configuration file in an object called configs. You can add as more configs as you like in this file.
JSON and JS objects might seem similar but they have some minor differences. For example, the keys in a JSON have to be wrapped inside double-quotes. That is the reason why I have used JSON.stringify(configs) to convert the JS object into a JSON string while writing to the file.
We generally want our scripts to be available everywhere in our system. Also, we have to give executable permission to our scripts.
Moving our script to /usr/local/bin on our file system will allow us to use the script anywhere on our system. To do so, move the script file,
mv prettier-config /usr/local/bin/prettier-config
sudo mv prettier-config /usr/local/bin/prettier-config
We want our scripts to have executable (x) permission and we want it to provide this permission to the owner/user, group and others (a).
sudo chmod a+x /usr/local/bin/prettier-config
Now if you try to run prettier-config command anywhere on your file system, you will get a .prettierrc file with necessary configuration created.

If you like my content and want to have a chat then please hit me up on Twitter.
Originally published at https://www.bigomega.dev.