This document is an attempt to loosely standardise the names of npm commands we use within our JavaScript projects. This is being proposed to introduce a level of familiarity between projects so that:
- The learning time common project commands for new developers is shortened.
- Context switching between projects is less of a headache.
- Developers make fewer mistakes during project setup.
- Boilerplate project code becomes more reusable, shortening time to create new projects and encourage similarity between projects at the same time.
This guide has been developed by analysing which npm commands and command patterns are already in use at Holiday Extras, and doing our best to preserve that while establishing a common standard.
The following observations have been made from analysing the scripts blocks of all of the package.json
files within the Holiday Extras Github organisation's projects.
Our npm commands tend to be structured into 5 main categories:
- Build related commands
- Test related commands
- Run related commands
- CI related commands
- Deploy/Publish related commands
There's also a long tail of miscellaneous commands.
We seem to almost universally use :
as a separator to namespace our commands. E.g. build:js
and build:css
.
Excluding npm supported hooks (prestop
, prestart
, etc), we have a not insignificant number of other commands with names that reflect when tasks should be run. This is in contrast to the majority of our commands have names that reflect what the command does.
Based on the above, we've come up with the following recommendations for command names.
Commands should be namespaced sensibly in a :
separated format where possible. Common namespaces that we should aim to share between our projects should include build
, test
, start
, ci
, deploy
, watch
, debug
and publish
.
While we do want to namespace, we don't want to stop people from using the top-level npm run test
or npm run build
commands. These should, however, be reserved for a 'do everything' task if one is readily available.
For example, npm run test
should run all the tests of all types, whereas you may have npm run test:unit
, npm run test:integration
and npm run test:journey
if you wanted to split up your test types into individual commands.
We shouldn't need to update all of our command names if we decide to switch technologies, and new developers shouldn't have to be familiar with a new technology just to get a project up and running. In support of that, we should use command names that don't reflect the underlying technology where it's not necessary:
Bad:
npm run karma
Good:
npm run test:browser
Commands names should succinctly reflect what the command is doing, not just when it should be run. Adding a namespace can help here. For example, instead of npm run postci
, npm run postci:clear-cache
might be a better command name that gives more insight as to what's going on. The obvious exception to this is npm supported hooks.