Skip to content

Instantly share code, notes, and snippets.

@jareguo
Created October 14, 2015 16:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jareguo/134a4f40b43a667e03ed to your computer and use it in GitHub Desktop.
Save jareguo/134a4f40b43a667e03ed to your computer and use it in GitHub Desktop.
spawn a child process in gulp task
gulp.task('compile-cocos2d', function (done) {
console.log('Spawn ant in ' + paths.originCocos2dCompileDir);
var child = Spawn('ant', {
cwd: paths.originCocos2dCompileDir,
stdio: [0, 1, 'pipe']
});
child.on('error', function (err) {
var ANT = Chalk.inverse('ant');
if (err.code === 'ENOENT') {
console.error(Chalk.red('You should install %s to build cocos2d-html5'), ANT);
}
else {
console.error(Chalk.red('Failed to start %s') + ': %s', ANT, err.code);
}
process.exit(1);
});
child.stderr.on('data', function (data) {
process.stderr.write(Chalk.red(data.toString()));
});
child.on('exit', function (code) {
if (code === 0) {
done();
}
else {
process.exit(1);
}
});
});
@jareguo
Copy link
Author

jareguo commented Oct 15, 2015

stdio: [0, 1, 'pipe']
让 stdin, stdout 直接继承自当前标准流,stderr 重定向成 pipe,以便监听 child.stderr

process.stderr.write(Chalk.red(data.toString()));
将收到的内容直接重写到标准流里面,不使用 console.error/log,因为data就包含换行,不需要重复换行

if (code === 0) {
    done();
}
else {
    process.exit(1);
}

使用 exit code 来判断程序是否执行成功

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