DEV Community

loading...
Cover image for Side Project: Wrote A Node Program That Can Write A Bash Script To Your Computer (Mac/Linux maybe Windows???)

Side Project: Wrote A Node Program That Can Write A Bash Script To Your Computer (Mac/Linux maybe Windows???)

jimmymcbride profile image Jimmy McBride ・2 min read

I don't know how well it will work on Windows, so if you're on a windows machine, give it a try and let me know if it works or not! 🔥

Check out my GitHub repo to test it out!

What the program does:

You're most likely going to be in the repo's dir when you node index.js to run the program, so it starts by changing into your home directory. Once inside the home directory, it checks to see if you have a bin folder. If so, it moves on; if not, it makes on for you.

Then it checks your path variable, if $HOME/bin is not in your path then it exports $HOME/bin to your path variable from your .bashrc file. If you don't have a .bashrc file, then one is created. If you do have one, it's appended to the end of your file.

Once both of those checks are finished, then it changes to your (potentially new) home/bin directory and creates a file named hello, adds text for a simple greeting to be echoed on command and changes the permissions of the file to 755. And just like that, I was able to write a program to write a bash script on your computer.

Now it's your turn!

Start writing more bash scripts on your machine! Everything is already set for you now. All you have to do is create a file in your home/bin, write the script (don't forget the shebang!) and chmod 755 file_name and your good to go!

I hope you enjoy making your work environment more productive like I do! If you want to read about the basics of writing your own bash scripts, check out my blog, Write A Bash Script 101. For a slightly more in-depth look at different tools that you can use in your bash scripts, check out my blog, Bash Script Tool Kit.

Discussion (4)

pic
Editor guide
Collapse
drmikecrowe profile image
drmikecrowe

node_modules should be in your .gitignore

Collapse
jimmymcbride profile image
Jimmy McBride Author

I only added shelljs as a dependancy. I left the node_modules in to take out the npm install/yarn install step. If I had added more packages, then I would have ignored it. Since it's so light, I left it in to leave out a step.

Collapse
drmikecrowe profile image
drmikecrowe • Edited

No, I'm referring to the node_modules directory in your repo. You typically can omit committing the actual node_modules.

Just add node_modules to your .gitignore and do a git rm --cached node_modules

Thread Thread
jimmymcbride profile image
Jimmy McBride Author

I left them in because if I do, you don't have to npm install or yarn install. If I added node_modules/ to my .gitignore file. If I did a .gitignore with my node modules then you would have to npm install or yarn install to run node index.js. It just takes a step out of the equation. Figured it would be a little more user-friendly if you don't have to worry about the yarn/npm install.

If I had even a couple more node_modules I would have just added the .gitignore, but I only have node_modules for a single, light library.