I'm really enjoying playing with Phoenix, and I'm porting a little
personal rails app to phoenix to see how it all fits together. The rails
app in question uses vue-js, and I'm using the "singe page component"
files (.vue) files. As far as I could tell, that precludes using brunch
as there was no preprocessor to turn the .vue files into .js ones.
Anyway, I switched out brunch for browserify partly using this quite good article here:
http://martinholman.co.nz/blog/2015/09/27/elixir-phoenix-browserify/The
issue I was having was that when I killed the `mix phoenix.server` task, the
node processes were not being killed in turn. I went onto the slack
channel and was given a helpful pointer from @micmus that "elixir
expects processes like that to close themselves when the stdin of the
process is closed. If the process does not respect this it will
misbehave and may live past the erlang system. One way to solve it is to
wrap it in a well behaved script or something."
My original command to spin up the wachers was this line in /config/dev.exs:
watchers: [npm: ["run", "watch"]]
(note, it's different from the standard one which uses node)
Then in my package.json I have the scripts which look like this:
"scripts": {
"build-assets": "cp -r web/static/assets/* priv/static",
"watch-assets": "watch-run -p 'web/static/assets/*' npm run build-assets",
"watch-js": "watchify -vd -p browserify-hmr -e web/static/js/app.js -o priv/static/js/app.js",
"watch-css": "catw web/static/css/*.css -o priv/static/css/app.css -v",
"watch": "npm run watch-assets & npm run watch-js & npm run watch-css"
},
The scripts themselves run fine, it's just that the processes weren't getting killed.
My current solutionNow,
I have solved this in what is probably a horrible way, I have no idea,
but it works (which is nice). I created a script which is called by
node. It looks like this:
var exec = require('child_process').exec;
var child1 = exec('npm run watch-assets');
var child2 = exec('npm run watch-js');
var child3 = exec('npm run watch-css');
process.stdin.on('end', function() {
process.kill(child1.pid);
process.kill(child2.pid);
process.kill(child3.pid);
process.exit(0);
});
process.stdin.resume();
and I call this script from my config/dev.exs . It works fine, builds the assets, watches, and kills the processes on complete.
The actual question ;) :Is
there a better or more blessed way to do this? I'm not that up to speed
on the node ecosystem, processes, phoenix, programmi ... ;) I
jest, however, I'd love to know what the "right" way to do it is.