How I handled configuration in my Node CLI package

How I handled configuration in my Node CLI package

How I handled configuration in my Node CLI package

Many folks in the United States are now working from home and social distancing. I am one of them. After cleaning, noodling on the guitar, reading, and pacing around for some time I decided to revisit an open source project I haven’t touched in a year or two: get-open-prs.

get-open-prs is CLI that allows people to quickly see the pull requests opened by their teammates from the command line. Not only that, it’ll allow you to quickly toggle through them with the arrow keys, and when you hit enter, your browser will automatically open that pull request. It does this by saving a Github personal access token and a list of Github usernames representing your teams. Of course this is overridable and changeable at any point.

I’d like to talk about the biggest change I made to this package, which related to configuration.

I moved away from environment variables for the Github usernames and token to a library called conf. Environment variables were a quick and dirty solution but not terribly user-friendly in my opinion. I had considered node-config as well, but it seems like that solution is more geared towards backend services and not CLIs. The biggest disadvantage is that node-config doesn’t work correctly for globally installed packages.

conf does work because it sets the configuration in a file that is in the operating system default location for application configurations. For mac that would be /Users/username/Library/Preferences. This is determined by a dependency called env-paths.

I added more prompts to automatically guide the user through setting these values for the first time and save them with conf. I feel like this change makes this tool way more user-friendly.

The other change I made inspired was inspired partly by my experiences with Clean Architecture. I refactored the main function so that dependencies are injected into a higher order function that then builds the function I use for the app. There’s still more that I can do but I feel a lot more secure that this CLI does what it’s supposed to do with these tests. When I have time, I’d like to write more tests for some of those dependencies to get the code coverage up, and perhaps switch to typescript so I can more explicitly define some of the interfaces.

Check it out! I’m definitely open to feedback, suggestions, and of course, pull requests.

Leave a Reply

Your email address will not be published. Required fields are marked *