Easy NPM lifecycle hooks for custom commands
Recently, while developing a component for Compoze, we ran into the need to run some logic before one of our commands executed.
We were aware of the first class lifecycle hooks, for example postintall & preinstall, but we were not sure if NPM supported hooks for custom commands. After a quick spin through the docs it turns out NPM has a simple convention for applying hooks to any command!
Here is the excerpt from the docs:
npm run <user defined>
pre<user-defined>
<user-defined>
post<user-defined>
In order to tell NPM to execute your logic, simple prepend the word “pre” or
“post” to the name of your custom command. NPM will then execute your logic either before or after, respectively.
For example, if we wanted to ensure our app was built before we did a deployment, we could configure a “predeploy” task that builds our app.
Now we can ensure that our app has been built before we do a deployment!