Today I Learned

hashrocket A Hashrocket project

NPM script hooks

When writing script commands in your package.json you can have additional scripting before & after your script with a special incantation of script commands.

Say you have a test script

{
    "scripts": {
    "test": "SOME TEST COMMAND"
  }
}

If you want to add a prerunner you just need to append the text pre to your command.

{
    "scripts": {
    "pretest": "SOME COMMAND THAT HAPPENS BEFORE",
    "test": "SOME TEST COMMAND"
  }
}

As you may have expected by now you'll do the same to get scripting after your command.

{
    "scripts": {
    "pretest": "COMMAND THAT HAPPENS BEFORE",
    "test": "TEST COMMAND",
    "posttest": "COMMAND THAY HAPPENS AFTER"
  }
}

This all happens via a naming scheme, so just remember pre & post.

More info in the documentation.

See More #javascript TILs
Looking for help? At Hashrocket, our JavaScript experts launch scalable, performant apps on the Web, Android and iOS. Contact us and find out how we can help you.