Skip to content

Instantly share code, notes, and snippets.

@bustardcelly
Created May 6, 2014 02:00
Show Gist options
  • Save bustardcelly/399bafdf1b0c52593067 to your computer and use it in GitHub Desktop.
Save bustardcelly/399bafdf1b0c52593067 to your computer and use it in GitHub Desktop.
Little script to run node on change to single file target
#!/usr/bin/env node
var path = require('path');
var watch = require('node-watch');
var parseArgs = require('minimist');
var argv = parseArgs(process.argv.slice(2));
var exec = require('child_process').spawn;
var trimNewLine = /\s+$/;
require('colors');
if(argv._.length > 0) {
var filepath = argv._[0];
watch(filepath, function(filename) {
var child = exec('node', [filename]);
child.stdout.on('data', function(data) {
console.log((data.toString().replace(trimNewLine,'')).white);
});
child.stderr.on('data', function(data) {
console.log(('Error: ' + data.toString()).red);
});
child.on('exit', function(err) {
if(err) {
console.log(('Error in running node on ' + filename).yellow);
}
});
});
}
@bustardcelly
Copy link
Author

  1. Grab the script, drop it somewhere, and in the same directory run:
    $ npm install minimist && npm install node-watch && npm install colors
  2. Add alias for ease of use:
    alias watchfile='node /path/to/node-watcher.js $1'
  3. Run anywhere:
    $ watchfile quickidea.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment