Skip to content

Instantly share code, notes, and snippets.

@zzuhan
Created March 15, 2019 06:48
Show Gist options
  • Save zzuhan/d1719dd540d6b02c8e4169a29660774e to your computer and use it in GitHub Desktop.
Save zzuhan/d1719dd540d6b02c8e4169a29660774e to your computer and use it in GitHub Desktop.
[node-childprocess] nodejs子进程 #nodejs
// 摘自vscode-vsce中
function exec(command: string, options: IOptions = {stopOnError: true}, cancellationToken?: CancellationToken): Promise<{ stdout: string; stderr: string; }> {
return new Promise((c, e) => {
let disposeCancellationListener: Function = null;
const child = cp.exec(command, { ...options, encoding: 'utf8' } as any, (err, stdout: string, stderr: string) => {
if (disposeCancellationListener) {
disposeCancellationListener();
disposeCancellationListener = null;
}
// @ts-ignore
if ( options.stopOnError & err) { return e(err); }
c({ stdout, stderr });
});
if (cancellationToken) {
disposeCancellationListener = cancellationToken.subscribe(err => {
child.kill();
e(err);
});
}
});
}
//
checkNPM()
.then(() => exec('npm list --production --parseable --depth=99999', { cwd, maxBuffer: 5000 * 1024, stopOnError: false }))
.then(({ stdout }) => stdout
.split(/[\r\n]/)
.filter(dir => path.isAbsolute(dir)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment