难道是因为内容太长了吗?死活提交不了啊,只能分篇了...
===============================================
child_process.execSync():
function execSync(/*command, options*/) {
var opts = normalizeExecArgs.apply(null, arguments);
var inheritStderr = opts.options ? !opts.options.stdio : true;
var ret = spawnSync(opts.file, opts.args, opts.options);
ret.cmd = opts.cmd;
if (inheritStderr)
process.stderr.write(ret.stderr);
var err = checkExecSyncError(ret);
if (err)
throw err;
else
return ret.stdout;
}
exports.execSync = execSync;
child_process.execFileSync():
function execFileSync(/*command, args, options*/) {
var opts = normalizeSpawnArguments.apply(null, arguments);
var inheritStderr = !opts.options.stdio;
var ret = spawnSync(opts.file, opts.args.slice(1), opts.options);
if (inheritStderr)
process.stderr.write(ret.stderr);
var err = checkExecSyncError(ret);
if (err)
throw err;
else
return ret.stdout;
}
exports.execFileSync = execFileSync;
child_process.spawnSync():
function spawnSync(/*file, args, options*/) {
var opts = normalizeSpawnArguments.apply(null, arguments);
var options = opts.options;
var i;
debug('spawnSync', opts.args, options);
options.file = opts.file;
options.args = opts.args;
options.envPairs = opts.envPairs;
if (options.killSignal)
options.killSignal = lookupSignal(options.killSignal);
options.stdio = _validateStdio(options.stdio || 'pipe', true).stdio;
if (options.input) {
var stdin = options.stdio[0] = util._extend({}, options.stdio[0]);
stdin.input = options.input;
}
// We may want to pass data in on any given fd, ensure it is a valid buffer
for (i = 0; i < options.stdio.length; i++) {
var input = options.stdio[i] && options.stdio[i].input;
if (input != null) {
var pipe = options.stdio[i] = util._extend({}, options.stdio[i]);
if (Buffer.isBuffer(input))
pipe.input = input;
else if (typeof input === 'string')
pipe.input = new Buffer(input, options.encoding);
else
throw new TypeError(util.format(
'stdio[%d] should be Buffer or string not %s',
i,
typeof input));
}
}
var result = spawn_sync.spawn(options);
if (result.output && options.encoding) {
for (i = 0; i < result.output.length; i++) {
if (!result.output[i])
continue;
result.output[i] = result.output[i].toString(options.encoding);
}
}
result.stdout = result.output && result.output[1];
result.stderr = result.output && result.output[2];
if (result.error) {
result.error = errnoException(result.error, 'spawnSync ' + opts.file);
result.error.path = opts.file;
result.error.spawnargs = opts.args.slice(1);
}
util._extend(result, opts);
return result;
}
exports.spawnSync = spawnSync;
child_process.spawn():
var spawn = exports.spawn = function(/*file, args, options*/) {
var opts = normalizeSpawnArguments.apply(null, arguments);
var options = opts.options;
var child = new ChildProcess();
debug('spawn', opts.args, options);
child.spawn({
file: opts.file,
args: opts.args,
cwd: options.cwd,
windowsVerbatimArguments: !!options.windowsVerbatimArguments,
detached: !!options.detached,
envPairs: opts.envPairs,
stdio: options.stdio,
uid: options.uid,
gid: options.gid
});
return child;
};
从上述前6组源码中可以看到,execFileSync()调用spawnSync(),execSync()调用spawnSync(),而spawnSync()调用spawn();exec()调用execFile(),而execFile()调用spawn();fork()调用spawn()。也就是说前6个方法最终都是调用spawn(),而spawn()的本质是创建ChildProcess的实例并返回。