A Bash Script With Node.JS
The background of the snippet
.prettierrc file and add 4 configurations.
The file looks something like this,
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.
The major pieces
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.
Some other examples
- for python
- for bash
The working directory
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
For that, we can extract the current working directory from the node’s global process object.
This gives us the path to the current working directory.
The file system module
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.
Fitting it all together
Now as we have all the background information, we can create the script.
First, create a file called
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.
Executable permission and global access
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.