DEV Community

Cover image for Creating a Node.js command-line package
Luke Garrigan
Luke Garrigan

Posted on

Creating a Node.js command-line package

Introduction

If you're anything like me, then the following is probably a daily occurence.

I'm in my daily scrum, other programmers are listing off what they did yesterday and what they plan on doing today. I'm next, yet I can't remember what day it is let alone what I was working on yesterday. I quickly fumble around on Jira looking at tasks I completed yesterday and trying to replay the days conquest in my head, it's not working. I look at timesheets to see if it gives me anything more to go on, nothing. It's my turn, great.

git-scrum

On a day-to-day basis I work on a large number of micro-services, they all have their own respected repositories and each of those repositories has with it many, many branches. I wanted something to output my previous days work, listing the commits in a nice format for me to quickly get a grasp of what I was doing. I initially started to use git-standup which is a great tool for listing off your latest commits in all your repositories. However, it doesn't display which branch the commit belongs to, which to me is very important as the branch name holds far more context than my commits.

So I decided to make my own tool, git-scrum. It is very basic and misses all the bells and whistles git-standup has — with the numerous flags you can pass in to manipulate the search — but it outputs just what I need, and maybe, at somepoint I'll add more to it.

To use it is really simple:

  1. npm install git-scrum -g
  2. Type git-scrum in the parent folder for all your repos

And something like the following should output:
https://camo.githubusercontent.com/4269a1b6cedbf40baad22f8e37d4b63b8d146533/68747470733a2f2f692e696d6775722e636f6d2f544839397256562e706e67

How to create a node script

So once your node command-line app is ready, you need to make it executable. My main entry-point for my app is index.js so I'll be using that for demo.

1. Make your entrypoint file a Node.js script

Let's make index.js executable, to do that we need to add the following to the top of the file:
#!/usr/bin/env node

2. Choose the name you'll use in the console to execute your script

In order to make our script executable anywhere, we need to add it into the PATH. So in our package.json add the following:

  "bin": {
    "git-scrum": "./index.js"
  }
Enter fullscreen mode Exit fullscreen mode

3. Test your script

To test git-scrum before releasing to npm I tested my script locally. To do this I ran:

npm install -g .

With the current setup this will allow you to use your script anywhere on your system.

4. Deploy to npm

When your happy with your script, get it deployed.
Assuming you have an account:

  1. npm login - fill out your username, password and email
  2. npm publish

5. Install your published script

Now that we've released our amazing script to the world, let's try it out ourselves, making sure we've not published the wrong version — which I have done many, many times.

npm install -g git-scrum

And you're good to go!

Thank you, if you like my rambling check out my personal blogging site at https://codeheir.com/

Top comments (0)