Skip to content

Instantly share code, notes, and snippets.

@kaiquewdev
Created January 11, 2014 15:27
Show Gist options
  • Select an option

  • Save kaiquewdev/8372223 to your computer and use it in GitHub Desktop.

Select an option

Save kaiquewdev/8372223 to your computer and use it in GitHub Desktop.
Exemplo de uma ferramenta para debug.
var spawn = require('child_process').spawn;
var fs = require('fs');
var args = process.argv.slice(2);
var program = null;
var filename = args[ 0 ];
function execHandler ( err, stdout, stderr ) {
if ( err ) console.log( 'error: %s', err );
if ( stdout ) console.log( 'stdout: ', stdout );
if ( stderr ) console.log( 'stderr: %s', stderr );
}
function spawnDataHandler ( chunk ) {
process.stdout.write( chunk.toString('utf-8') );
}
function programKillHandler () {
if ( program ) { program.kill('SIGHUP'); }
}
function execFileHandler () {
programKillHandler();
program = spawn('node', [ filename ]);
process.stdin.pipe( program.stdin );
program.stdout.on('data', spawnDataHandler);
}
function fileWatchHandler ( curr, prev ) {
execFileHandler();
console.log( [ '\n', 'Restarted', '\n', curr.mtime, '\n' ].join('') );
}
function processExitHandler () {
process.stdin.destroy();
programKillHandler();
}
(function main () {
if ( filename ) {
console.log('Watching the file: %s\n', filename);
process.on('exit', processExitHandler);
process.stdin.resume();
execFileHandler();
fs.watchFile( filename, fileWatchHandler );
} else {
process.exit( 1 );
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment