/
_runCli.ts
37 lines (31 loc) · 1.21 KB
/
_runCli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {PortablePath, npath} from '@yarnpkg/fslib';
import {spawn} from 'child_process';
import * as path from 'path';
import {pathToFileURL} from 'url';
export async function runCli(cwd: PortablePath, argv: Array<string>, withCustomRegistry?: boolean): Promise<{exitCode: number | null, stdout: string, stderr: string}> {
const out: Array<Buffer> = [];
const err: Array<Buffer> = [];
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [`--no-warnings`, ...(withCustomRegistry ? [`--import`, pathToFileURL(path.join(__dirname, `_registryServer.mjs`)) as any as string] : [`-r`, require.resolve(`./recordRequests.js`)]), require.resolve(`../dist/corepack.js`), ...argv], {
cwd: npath.fromPortablePath(cwd),
env: process.env,
stdio: `pipe`,
});
child.stdout.on(`data`, chunk => {
out.push(chunk);
});
child.stderr.on(`data`, chunk => {
err.push(chunk);
});
child.on(`error`, error => {
reject(error);
});
child.on(`close`, exitCode => {
resolve({
exitCode,
stdout: Buffer.concat(out).toString(),
stderr: Buffer.concat(err).toString(),
});
});
});
}