Created
January 11, 2014 15:27
-
-
Save kaiquewdev/8372223 to your computer and use it in GitHub Desktop.
Exemplo de uma ferramenta para debug.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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