Node Package Scripts
For example checkout one of my other projects:
And this is just a fairly simple example that doesnt include multiple-packages in one solution.
Because all the scripts are contained in json you are very restricted with what you can do. For example in my above screenshot, you can see that there is quite a bit of duplication, if only there was a way I could use variables and functions…
You can see im able to use all the latest and greatest JS features such as template strings and lambda function declarations. I can pull in other packages and import config files from disk.
Its reminiscent of the day when we used to build our projects using Grunt and Gulp except its in a declarative structure, making it easy to read and reason about.
So how do you run those commands? Well for example say you want to run the tests simply type
npm start test or perhaps you want to watch the resources directory type
npm start watch.resources.
You can get information on the your available commands with
npm start help
You can annotate those commands with some helpful text such as:
will result in:
The only missing bit of the puzzle was what happens when you are working in a multi-package solution such as the one in Markd2)?
Just as a reminder the folder structure for the project looks like:
Ideally what I want to be able to do is from the top level directory type
npm start extension.build.staging which will then run the
build.staging command within the context of the “extension” package.
Unfortunately NPS didnt have this ability out of the box so I decided to have a go of coding it up myself. Once done I posted it on the nps-utils github repo and was invited by its maintainer (and all OSS god) Kent Dodds to submit a PR with the feature.
So after a little bit of back-and-forth (this was the most strict OSS project I had ever committed too, but thats a good thing) I was able to get my feature merged in and I was even added as a contributor of the project:
So now with my addition you can include other NPS scripts within yours and thus achieve what I wanted:
So next time you are working on a JS project with non-trivial amount of scripts in your
package.json think about adding