I have a node.js app and I have created a native addon. (In case you need background info, native addons for node.js can be created like this).
In my development environment I want it to watch source files (these are C++ source files, in this case) and automatically rebuild my addon when the C++ source files change, and also automatically restart the node application after the build completes.
I'm certain there's more than one way to accomplish this, but I went down the road of trying nodemon. But I couldn't figure out how to get nodemon to wait for the build to finish before restarting the application.
I figure npm can probably do this itself too with a script, perhaps with some kind of a watch package. So I'm open to alternative approaches if there's something easier.
So when any of my source files change, really all I need is for node-gyp build
to run at the right point in the restart workflow (stop node, recompile, restart node). Right now it rebuilds the addon and restarts the application without waiting for the build to complete, and that's undesirable.
Here's my nodemon.json file:
{
"watch": [
"addon/"
],
"ignore": [
"addon/build/"
],
"events": {
"restart": "cd addon && node-gyp build"
},
"ext": "js,json,cc,h"
}
This doesn't work because the "restart" is like an event, in that my script that runs node-gyp fires and executes, but it doesn't block restarting until the build is finished.
I was hoping it would work more like middleware, where it would run the build script after the app is terminated, but before it restarts the app. I also consulted this relevant nodemon issue, but there's no concrete suggestion there about how to make that happen.
I need to change something so that the rebuild (node-gyp step) will happen automatically when the source changes and then restart nicely.
Note: Later I'll restructure it so that it only rebuilds the addon when the relevant C++ source files change, and just restarts the app when .js files change without rebuilding the C++ addon, but first I need to figure out getting the node-gyp build step to happen at the right time.