To allow people to have an isolated environment to work on and get immediate feedback on their changes, I leverage the Gulp.js + Node.js duo.
“Gulp.js is a task runner that runs on Node.js that it is normally used to automate UI development workflows such as LESS to CSS conversion, making HTML templates, minify CSS and JS, etc. However, it can also be used to fire up local web servers, custom processes and many other things.”
To make things really simple, I set up Gulp to allow anyone to enter
gulp dev on a terminal to start the following workflow:
- Prepare distribution files
– Compile LESS or SASS style sheets
– Compile templates (AngularJS)
– Version resources for cache busting (using a file hash)
- Start stub servers
– Start an API mockup server
– Start a server that can serve local layouts HTML files
- Open a browser pointing to the local App (chrome or IE depending on the platform)
- Fire up file watchers to trigger specific builds and browser reloads when project files change.
The following is an extract of the entry point for these gulp tasks (
DEV START.servers task is imported by the
requireDir('./gulp-tasks') instruction. This task triggers other dependent workflows that spin up plain Node.js web servers that are, in turn, kept alive by a process supervisor library called forever-monitor.
There is a catch, however. If you are working on a Unix-friendly system (like a Mac), processes are normally managed hierarchically. In a normal situation, a root process (let’s say
gulp dev) will create child processes (
DEV START.api.server and
DEV START.layouts.server) that will only live during the lifetime of their parent. This is great because whenever we terminate the parent process, all its children are terminated too.
In a Windows environment, processes management is done in a slightly different way – even if you close a parent process its child processes will stay alive doing what they were already doing. Child processes will contain only the parent ID as a reference. This means that it is still possible to mimic Unix process tree behaviour, but it is just a little bit more tedious and some library creators avoid dealing with the problem. This is the case with Gulp.
So in our scenario we listen to the
SIGINT signal and gracefully terminate all processes when it is raised through keyboard input (hitting Ctrl-B). This prevents developers on windows from having to go to the task manager and terminate orphaned child processes themselves.
I am also using
process.once(... event listener instead of
process.on(... to prevent an error surfaced on Macs when Ctrl-B is hit more than once. We don’t want the OS to complain when we try to terminate a process that has already been terminated :).
That is it for now..