drifterz28
8/26/2014 - 10:54 PM

Simple watching to exec makefile commands

Simple watching to exec makefile commands

/* global console: false */
// Run -
// npm install node-watch
// sudo gem install terminal-notifier

var child_process = require('child_process');
var exec = child_process.exec;
var spawn = child_process.spawn;
var sys = require('util');
var watch = require('node-watch');
var make;
var process;

function puts(error, stdout, stderr) {
    if (error) {
        spawn('terminal-notifier', ['-message', error, '-title', 'Watcher - ERROR']);
        sys.puts(error);
    }
    sys.puts(stdout);
    var output = stdout.split('\n');
    output = output.splice(-3);
    output = output.join('\n');
    output = output.replace(/  /g, '\n');
    spawn('terminal-notifier', ['-message', output, '-title', 'Watcher']);
}

console.log('watching and waiting ctl+c to quit');

watch(['cygnus/phplib/mvc', 'cygnus/build/scss'], function(filename) {
    spawn('terminal-notifier', ['-message', filename + ' Changed', '-title', 'Watcher']);
    console.log(filename, ' changed.');

    if (filename.search('.scss') !== -1) {
        make = spawn('cd', ['cygnus', '&&', 'make', 'build-css']);
        process = 'Sass';

    } else if (filename.search('.js') !== -1) {
        // cd cygnus && make build-js
        make = spawn('cd', ['cygnus', '&&', 'make', 'build-js']);
        process = 'Javascript';

    } else {
        make = spawn('echo', ['sync']);
        process = 'RSync';
    }

    make.on('close', function (code) {
        var message;
        if (code === 0) {
            message =  'Process ' + process + ' Complete';
        } else {
            message =  'Process ' + process + ' Failed ' + code;
        }
        if (process !== 'RSync') {
            spawn('terminal-notifier', ['-message', message, '-title', 'Watcher']);
            console.log(message);
        }
        exec('make sync', puts); //run rsync
    });
});