DEV Community

Jennifer Wadella
Jennifer Wadella

Posted on

Using Starship to Track Current NPM Registry

I will be forever grateful to Jason Lengstorf for introducing me to Starship! It's a super fast, highly-customizable shell that comes with some great features out of the box. The default will show you your current directory, branch if you're in a repo, current package version, and current Node version. My setup looks like this:

Starship customized command prompt

As a consultant, and even more so as a DIRECTOR of consultants, I spend my days hopping between client code bases, node versions, internal NPM registries, and my own personal content creation. As someone who never has enough hours in a day, I'm always looking for ways to improve my processes and keep myself from trolling myself unintentionally.

The number one thing that gets me every time is not realizing when I'm on an internal client NPM registry and try to do an npm install.😬 I use npmrc to create registry profiles and quickly switch between them, but with my chaotic days I don't usually remember which profile I have set!

Thankfully, you can create custom modules in Starship to customize your command prompt needs! I created a custom module to always show me which registry I'm currently using.

// ~/.config/starship.toml

[custom.npmrc]
disabled = false
command = """
NPMREGISTRY=$(command npm config get registry)
if [[ "$NPMREGISTRY" =~ "clientregistrynamespace" ]]; then
    NPMREGISTRY_SYMBOL=""
else
    NPMREGISTRY_SYMBOL=" "
fi
echo "registry $NPMREGISTRY_SYMBOL"
"""
when = "true"
style = "bold blue"
Enter fullscreen mode Exit fullscreen mode

If you're not a bash scripting pro (I certainly am NOT), I'll walk you through what's going on here. This is code in my custom config toml file for Starship. Most of this configuration is based on how Starship returns information about different Modules, and bash scripting is done in the "command" option. You can see more about how to configure current Modules or create your own commands on their website.

I'm running the command npm config get registry and converting the output of the command to a string variable for comparison with the $. The command spits out https://registry.npmjs.org/ or whatever registry you're currently on. A custom registry url might look something like https://npm.clientregistrynamespace.com. I'm checking to see if my clients registry name is in the command output - if it is, I'm returning a symbol representing that client, and if it isn't I'm returning an NPM icon from Nerd Fonts.

Starship NPM Registry display demo

Now I can always see what registry I'm on!

You can see other custom commands built by community members here: https://github.com/starship/starship/discussions/1252

Top comments (0)