Synchronous Exec in Node.js
Everyone loves NodeJS in part because it embraces a non-blocking philosophy; interactions are asynchronous and thus, theoretically, allow for faster all-around processing. When creating a simple script for my upcoming redesign, I found the asynchronocity ... annoying. I was quickly sinking into callback hell with the server executions I was running through Node.js. Luckily I found a great package called exec-sync which allows synchronous execution of shell commands so that I don't find myself many callbacks deep.
The exec-sync package is available via npm:
npm install exec-sync
Don't you love package management?!
exec-synce works like any other Node.js component; require it and it's ready to use:
Note that instead of passing a callback to the execSync method, I can simply assume things are running from the top down, allowing me to avoid a bunch of nested callbacks that would only muddy up my code. Of course some processes could be asynchronous but exec-sync allows me to keep a top-down thought process. Node.js does have utilities for a few of these functions, but running shell keeps the code shorter.
The code above was just a tiny snippet of my build file. Without exec-sync, I would either need to nest and nest and nest callbacks, or get a Deferred implementation to handle all the callbacks. Using a Deferred implementation would allow for more speed for the build process, but when the process takes only a few seconds, the tradeoff for top-down execution is worth it.