本文整理匯總了TypeScript中cross-spawn類的典型用法代碼示例。如果您正苦於以下問題:TypeScript cross-spawn類的具體用法?TypeScript cross-spawn怎麽用?TypeScript cross-spawn使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了cross-spawn類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: spawn
import * as spawn from 'cross-spawn';
import * as cp from 'child_process';
const p1: cp.ChildProcess = spawn('my-cmd', ['foo', 'bar']);
const pw: cp.ChildProcess = spawn('my-cmd', ['foo', 'bar'], {env: process.env});
pw.on('error', (err: Error) => {
console.log(err);
});
const r1: Error = spawn.sync('foo').error;
const r2: string[] = spawn.sync('foo').output;
const r3: Buffer = spawn.sync('foo').stdout;
const r4: Buffer = spawn.sync('foo').stderr;
const r5: number = spawn.sync('foo').status;
const r6: string = spawn.sync('foo').signal;
const r7: string = spawn.sync('foo', {encoding: 'utf8'}).stdout;
spawn.sync('foo', ['bar']);
spawn.sync('foo', ['bar'], {stdio: 'inherit'});
示例2: Promise
new Promise((resolve, reject) => {
const npm = spawn('npm', ['--prefix', cwd, 'install'], {
stdio: 'inherit',
});
npm.on('close', resolve);
npm.on('error', reject);
});
示例3: Promise
return new Promise((resolve, reject) => {
nreplProcess = spawn('lein', LEIN_ARGS, {
cwd: vscode.workspace.rootPath,
detached: !(os.platform() === 'win32')
});
nreplProcess.stdout.addListener('data', data => {
const nreplConnectionMatch = data.toString().match(R_NREPL_CONNECTION_INFO);
// Send any stdout messages to the output channel
nreplChannel.append(data.toString());
if (nreplConnectionMatch && nreplConnectionMatch[1]) {
const [host, port] = nreplConnectionMatch[1].split(':');
return resolve({ host, port: Number.parseInt(port) });
}
});
nreplProcess.stderr.on('data', data => {
// Send any stderr messages to the output channel
nreplChannel.append(data.toString());
});
nreplProcess.on('exit', (code) => {
// nREPL process has exited before we were able to read a host / port.
const message = `nREPL exited with code ${code}`
nreplChannel.appendLine(message);
// Bring the output channel to the foreground so that the user can
// use the output to debug the problem.
nreplChannel.show();
return reject({ nreplError: message});
});
});
示例4: Promise
return new Promise((resolve, reject) => {
const args = cmd.split(/\s/g)
const child = crossSpawn(args[0], args.slice(1), {
cwd: this.config.cwd,
})
child.stdout.on('data', data => {
this.out.log(data.toString())
})
child.stderr.on('data', data => {
this.out.log(data.toString())
})
child.on('error', err => {
this.out.error(err)
})
child.on('close', code => {
if (code !== 0) {
reject(code)
} else {
resolve()
}
})
})
示例5: Promise
return new Promise((resolve, reject) => {
let buffer = ''
const cp = crossSpawn(cmd, args, options)
cp.stdout.on('data', data => {
buffer += data.toString()
})
cp.stderr.on('data', data => {
buffer += data.toString()
})
cp.on('error', err => {
if (buffer.length > 0) {
reject(buffer)
} else {
reject(err)
}
})
cp.on('close', code => {
if (code === 0) {
resolve(buffer)
} else {
reject(buffer)
}
})
})