Created
March 15, 2019 06:48
-
-
Save zzuhan/d1719dd540d6b02c8e4169a29660774e to your computer and use it in GitHub Desktop.
[node-childprocess] nodejs子进程 #nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 摘自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