51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const { execFile } = require('node:child_process');
|
|
|
|
function run(command, args = [], options = {}) {
|
|
const {
|
|
cwd,
|
|
timeout = 60_000,
|
|
maxBuffer = 8 * 1024 * 1024,
|
|
env,
|
|
input = null,
|
|
allowExitCodes = []
|
|
} = options;
|
|
|
|
return new Promise((resolve, reject) => {
|
|
const child = execFile(command, args, {
|
|
cwd,
|
|
timeout,
|
|
maxBuffer,
|
|
windowsHide: true,
|
|
encoding: 'utf8',
|
|
env: { ...process.env, ...(env || {}) }
|
|
}, (error, stdout, stderr) => {
|
|
if (error && !allowExitCodes.includes(error.code)) {
|
|
const message = (stderr || stdout || error.message).trim();
|
|
const wrapped = new Error(message);
|
|
wrapped.code = error.code;
|
|
if (/\.git[\\/]index\.lock[\s\S]*File exists/i.test(message) || /Unable to create .*index\.lock/i.test(message)) {
|
|
wrapped.code = 'GIT_INDEX_LOCKED';
|
|
wrapped.recoverable = true;
|
|
} else if (error.code === 'ENAMETOOLONG') {
|
|
wrapped.code = 'GIT_ARGUMENT_LIST_TOO_LONG';
|
|
wrapped.recoverable = true;
|
|
}
|
|
wrapped.stdout = stdout;
|
|
wrapped.stderr = stderr;
|
|
wrapped.command = `${command} ${args.join(' ')}`;
|
|
reject(wrapped);
|
|
return;
|
|
}
|
|
resolve({ stdout: stdout || '', stderr: stderr || '', exitCode: error?.code || 0 });
|
|
});
|
|
if (input !== null && input !== undefined) {
|
|
child.stdin.on('error', () => {});
|
|
child.stdin.end(input);
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = { run };
|