本文整理汇总了TypeScript中child_process.ChildProcess.once方法的典型用法代码示例。如果您正苦于以下问题:TypeScript ChildProcess.once方法的具体用法?TypeScript ChildProcess.once怎么用?TypeScript ChildProcess.once使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类child_process.ChildProcess
的用法示例。
在下文中一共展示了ChildProcess.once方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: handleProcess
function handleProcess(event: string, childProcess: ChildProcess, command: string, resolve: ((value?: any) => void) | null, reject: (reason?: any) => void) {
childProcess.on("error", reject)
let out = ""
if (childProcess.stdout != null) {
childProcess.stdout.on("data", (data: string) => {
out += data
})
}
let errorOut = ""
if (childProcess.stderr != null) {
childProcess.stderr.on("data", (data: string) => {
errorOut += data
})
}
childProcess.once(event, (code: number) => {
if (log.isDebugEnabled) {
const fields: any = {
command: path.basename(command),
code,
pid: childProcess.pid,
}
if (out.length > 0) {
fields.out = out
}
log.debug(fields, "exited")
}
if (code === 0) {
if (resolve != null) {
resolve(out)
}
}
else {
function formatOut(text: string, title: string) {
return text.length === 0 ? "" : `\n${title}:\n${text}`
}
reject(new Error(`${command} exited with code ${code}${formatOut(out, "Output")}${formatOut(errorOut, "Error output")}`))
}
})
}
示例2: startDebugSession
export function startDebugSession(debuggerType: DebuggerType, debuggerFilename?: string): DebugSession {
let debuggerArgs: string[];
switch (debuggerType) {
case DebuggerType.LLDB:
setProcessEnvironment();
if (!debuggerFilename) {
// lldb-mi.exe should be on the PATH
debuggerFilename = 'lldb-mi';
}
debuggerArgs = ['--interpreter'];
break;
case DebuggerType.GDB:
if (!debuggerFilename) {
debuggerFilename = 'gdb';
}
debuggerArgs = ['--interpreter', 'mi'];
break;
default:
throw new Error('Unknown debugger type!');
}
const debuggerProcess: ChildProcess = spawn(debuggerFilename, debuggerArgs);
let debugSession: DebugSession = null;
if (debuggerProcess) {
if (debuggerType === DebuggerType.GDB) {
debugSession = new GDBDebugSession(debuggerProcess.stdout, debuggerProcess.stdin);
} else {
debugSession = new DebugSession(debuggerProcess.stdout, debuggerProcess.stdin);
}
if (debugSession) {
debuggerProcess.once('exit',
(code: number, signal: string) => { debugSession.end(false); }
);
}
}
return debugSession;
};
示例3: handleProcess
export function handleProcess(event: string, childProcess: ChildProcess, command: string, resolve: ((value?: any) => void) | null, reject: (reason?: any) => void) {
childProcess.on("error", reject)
let out = ""
if (!debug.enabled && childProcess.stdout != null) {
childProcess.stdout.on("data", (data: string) => {
out += data
})
}
let errorOut = ""
if (childProcess.stderr != null) {
childProcess.stderr.on("data", (data: string) => {
errorOut += data
})
}
childProcess.once(event, (code: number) => {
if (code === 0 && debug.enabled) {
debug(`${path.basename(command)} (${childProcess.pid}) exited with exit code 0`)
}
if (code === 0) {
if (resolve != null) {
resolve()
}
}
else {
function formatOut(text: string, title: string) {
return text.length === 0 ? "" : `\n${title}:\n${text}`
}
reject(new Error(`${command} exited with code ${code}${formatOut(out, "Output")}${formatOut(errorOut, "Error output")}`))
}
})
}
示例4: Promise
let promise = new Promise((resolve, reject) => {
let { ignoreStdio, ...nodeOptions } = options;
// @ts-ignore: cross-spawn declares "args" to be a regular array instead of a read-only one
child = spawn(command, args, nodeOptions);
let stdout = '';
let stderr = '';
if (!ignoreStdio) {
if (child.stdout) {
child.stdout.on('data', data => {
stdout += data;
});
}
if (child.stderr) {
child.stderr.on('data', data => {
stderr += data;
});
}
}
let completionListener = (code: number | null, signal: string | null) => {
child.removeListener('error', errorListener);
let result: SpawnResult = {
pid: child.pid,
output: [stdout, stderr],
stdout,
stderr,
status: code,
signal,
};
if (code !== 0) {
let error = signal
? new Error(`${command} exited with signal: ${signal}`)
: new Error(`${command} exited with non-zero code: ${code}`);
if (error.stack && previousStackString) {
error.stack += `\n${previousStackString}`
}
Object.assign(error, result);
reject(error);
} else {
resolve(result);
}
};
let errorListener = (error: Error) => {
if (ignoreStdio) {
child.removeListener('exit', completionListener);
} else {
child.removeListener('close', completionListener);
}
Object.assign(error, {
pid: child.pid,
output: [stdout, stderr],
stdout,
stderr,
status: null,
signal: null,
});
reject(error);
};
if (ignoreStdio) {
child.once('exit', completionListener);
} else {
child.once('close', completionListener);
}
child.once('error', errorListener);
}) as SpawnPromise<SpawnResult>;