2020-12-21 04:22:06 -05:00
|
|
|
import { spawn } from 'child_process';
|
2021-03-14 02:42:46 -05:00
|
|
|
import { SpawnOptions } from 'child_process';
|
2020-12-21 04:22:06 -05:00
|
|
|
import readline from 'readline';
|
|
|
|
import buildDebug from 'debug';
|
2019-12-14 16:52:26 -05:00
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
const debug = buildDebug('verdaccio:e2e:process');
|
|
|
|
|
|
|
|
export type ExecOutput = {
|
|
|
|
stdout: string;
|
|
|
|
stderr: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function _exec(options: SpawnOptions, cmd, args): Promise<ExecOutput> {
|
|
|
|
debug('start _exec %o %o %o', options, cmd, args);
|
2019-12-14 16:52:26 -05:00
|
|
|
let stdout = '';
|
2020-12-21 04:22:06 -05:00
|
|
|
let stderr;
|
2019-12-14 16:52:26 -05:00
|
|
|
const env = options.env;
|
2020-12-21 04:22:06 -05:00
|
|
|
debug(`Running \`${cmd} ${args.join(' ')}`);
|
|
|
|
debug(`CWD: %o`, options.cwd);
|
|
|
|
debug(`ENV: ${JSON.stringify(env)}`);
|
2019-12-14 16:52:26 -05:00
|
|
|
const spawnOptions = {
|
2020-12-21 04:22:06 -05:00
|
|
|
cwd: options.cwd,
|
|
|
|
stdio: options.stdio || 'pipe',
|
2020-08-13 16:36:23 -05:00
|
|
|
...(env ? { env } : {}),
|
2019-12-14 16:52:26 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
if (process.platform.startsWith('win')) {
|
|
|
|
args.unshift('/c', cmd);
|
|
|
|
cmd = 'cmd.exe';
|
|
|
|
spawnOptions['stdio'] = 'pipe';
|
|
|
|
}
|
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
const childProcess = spawn(cmd, args, spawnOptions);
|
|
|
|
const rl = readline.createInterface({ input: childProcess.stdout });
|
2019-12-15 16:18:47 -05:00
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
rl.on('line', function (line) {
|
|
|
|
stdout += line;
|
2019-12-14 16:52:26 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
const err = new Error(`Running "${cmd} ${args.join(' ')}" returned error code `);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
childProcess.on('exit', (error) => {
|
|
|
|
if (!error) {
|
|
|
|
resolve({ stdout, stderr });
|
|
|
|
} else {
|
|
|
|
err.message += `${error}...\n\nSTDOUT:\n${stdout}\n\nSTDERR:\n${stderr}\n`;
|
2020-12-21 04:22:06 -05:00
|
|
|
reject({ stdout, stderr: err });
|
2019-12-14 16:52:26 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
// export function execAndWaitForOutputToMatch(
|
|
|
|
// cmd: string,
|
|
|
|
// args: string[],
|
|
|
|
// match: RegExp,
|
|
|
|
// spawnOptions: SpawnOptions = {}
|
|
|
|
// ): any {
|
|
|
|
// return _exec({ waitForMatch: match, ...spawnOptions, silence: true }, cmd, args);
|
|
|
|
// }
|
|
|
|
|
|
|
|
export function pnpmGlobal(rootFolder, ...args) {
|
|
|
|
const pnpmCmd = require.resolve('pnpm');
|
|
|
|
debug('pnpmCommand %o', pnpmCmd);
|
|
|
|
debug('run pnpm on %o', rootFolder);
|
|
|
|
return _exec(
|
|
|
|
{
|
|
|
|
cwd: rootFolder,
|
|
|
|
},
|
2021-09-08 12:06:37 -05:00
|
|
|
process.execPath,
|
|
|
|
[pnpmCmd, ...args]
|
2020-12-21 04:22:06 -05:00
|
|
|
);
|
2019-12-14 16:52:26 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
export function npm(...args): Promise<ExecOutput> {
|
2019-12-15 11:06:28 -05:00
|
|
|
return _exec({}, 'npm', args);
|
2019-12-14 16:52:26 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
export function yarn(...args): Promise<ExecOutput> {
|
|
|
|
return _exec({}, 'yarn', args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pnpm(...args): Promise<ExecOutput> {
|
|
|
|
return _exec({}, 'pnpm', args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function pnpmWithCwd(cwd, ...args): Promise<ExecOutput> {
|
|
|
|
return _exec({ cwd }, 'pnpm', args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function yarnWithCwd(cwd, ...args): Promise<ExecOutput> {
|
|
|
|
return _exec({ cwd }, 'yarn', args);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function nodeCwd(cwd, ...args): Promise<ExecOutput> {
|
|
|
|
return _exec({ cwd }, 'yarn', args);
|
2019-12-15 16:18:47 -05:00
|
|
|
}
|
|
|
|
|
2020-12-21 04:22:06 -05:00
|
|
|
export function silentNpm(...args): Promise<ExecOutput> {
|
|
|
|
debug('run silent npm %o', args);
|
2021-03-14 02:42:46 -05:00
|
|
|
return _exec({ silent: true }, 'npm', args);
|
2019-12-14 16:52:26 -05:00
|
|
|
}
|