Skip to content

Commit 933dcd4

Browse files
committed
refactor(ng-dev): extract common pattern for processing async commands in child-process (#2423)
Extract the common pattern for processing the ChildProcess object to be shared instead of requiring management of the same action in two places.: PR Close #2423
1 parent 632f928 commit 933dcd4

File tree

5 files changed

+284
-459
lines changed

5 files changed

+284
-459
lines changed

.github/local-actions/branch-manager/main.js

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -56702,49 +56702,6 @@ var ChildProcess = class {
5670256702
childProcess.on("close", (status) => status === 0 ? resolve() : reject(status));
5670356703
});
5670456704
}
56705-
static spawn(command, args, options = {}) {
56706-
return new Promise((resolve, reject) => {
56707-
const commandText = `${command} ${args.join(" ")}`;
56708-
const outputMode = options.mode;
56709-
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56710-
Log.debug(`Executing command: ${commandText}`);
56711-
const childProcess = _spawn(command, args, { ...options, env: env3, shell: true, stdio: "pipe" });
56712-
let logOutput = "";
56713-
let stdout = "";
56714-
let stderr = "";
56715-
if (options.input !== void 0) {
56716-
childProcess.stdin.write(options.input);
56717-
childProcess.stdin.end();
56718-
}
56719-
childProcess.stderr.on("data", (message) => {
56720-
stderr += message;
56721-
logOutput += message;
56722-
if (outputMode === void 0 || outputMode === "enabled") {
56723-
process.stderr.write(message);
56724-
}
56725-
});
56726-
childProcess.stdout.on("data", (message) => {
56727-
stdout += message;
56728-
logOutput += message;
56729-
if (outputMode === void 0 || outputMode === "enabled") {
56730-
process.stderr.write(message);
56731-
}
56732-
});
56733-
childProcess.on("close", (exitCode, signal) => {
56734-
const exitDescription = exitCode !== null ? `exit code "${exitCode}"` : `signal "${signal}"`;
56735-
const printFn = outputMode === "on-error" ? Log.error : Log.debug;
56736-
const status = statusFromExitCodeAndSignal(exitCode, signal);
56737-
printFn(`Command "${commandText}" completed with ${exitDescription}.`);
56738-
printFn(`Process output:
56739-
${logOutput}`);
56740-
if (status === 0 || options.suppressErrorOnFailingExitCode) {
56741-
resolve({ stdout, stderr, status });
56742-
} else {
56743-
reject(outputMode === "silent" ? logOutput : void 0);
56744-
}
56745-
});
56746-
});
56747-
}
5674856705
static spawnSync(command, args, options = {}) {
5674956706
const commandText = `${command} ${args.join(" ")}`;
5675056707
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
@@ -56756,44 +56713,14 @@ ${logOutput}`);
5675656713
}
5675756714
throw new Error(stderr);
5675856715
}
56716+
static spawn(command, args, options = {}) {
56717+
const commandText = `${command} ${args.join(" ")}`;
56718+
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56719+
return processAsyncCmd(commandText, options, _spawn(command, args, { ...options, env: env3, shell: true, stdio: "pipe" }));
56720+
}
5675956721
static exec(command, options = {}) {
56760-
return new Promise((resolve, reject) => {
56761-
var _a2, _b;
56762-
const outputMode = options.mode;
56763-
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56764-
Log.debug(`Executing command: ${command}`);
56765-
const childProcess = _exec(command, { ...options, env: env3 });
56766-
let logOutput = "";
56767-
let stdout = "";
56768-
let stderr = "";
56769-
(_a2 = childProcess.stderr) == null ? void 0 : _a2.on("data", (message) => {
56770-
stderr += message;
56771-
logOutput += message;
56772-
if (outputMode === void 0 || outputMode === "enabled") {
56773-
process.stderr.write(message);
56774-
}
56775-
});
56776-
(_b = childProcess.stdout) == null ? void 0 : _b.on("data", (message) => {
56777-
stdout += message;
56778-
logOutput += message;
56779-
if (outputMode === void 0 || outputMode === "enabled") {
56780-
process.stderr.write(message);
56781-
}
56782-
});
56783-
childProcess.on("close", (exitCode, signal) => {
56784-
const exitDescription = exitCode !== null ? `exit code "${exitCode}"` : `signal "${signal}"`;
56785-
const printFn = outputMode === "on-error" ? Log.error : Log.debug;
56786-
const status = statusFromExitCodeAndSignal(exitCode, signal);
56787-
printFn(`Command "${command}" completed with ${exitDescription}.`);
56788-
printFn(`Process output:
56789-
${logOutput}`);
56790-
if (status === 0 || options.suppressErrorOnFailingExitCode) {
56791-
resolve({ stdout, stderr, status });
56792-
} else {
56793-
reject(outputMode === "silent" ? logOutput : void 0);
56794-
}
56795-
});
56796-
});
56722+
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56723+
return processAsyncCmd(command, options, _exec(command, { ...options, env: env3 }));
5679756724
}
5679856725
};
5679956726
function statusFromExitCodeAndSignal(exitCode, signal) {
@@ -56803,6 +56730,42 @@ function getEnvironmentForNonInteractiveCommand(userProvidedEnv) {
5680356730
const forceColorValue = supports_color_default2.stdout !== false ? supports_color_default2.stdout.level.toString() : void 0;
5680456731
return { FORCE_COLOR: forceColorValue, ...userProvidedEnv ?? process.env };
5680556732
}
56733+
function processAsyncCmd(command, options, childProcess) {
56734+
return new Promise((resolve, reject) => {
56735+
var _a2, _b;
56736+
let logOutput = "";
56737+
let stdout = "";
56738+
let stderr = "";
56739+
Log.debug(`Executing command: ${command}`);
56740+
(_a2 = childProcess.stderr) == null ? void 0 : _a2.on("data", (message) => {
56741+
stderr += message;
56742+
logOutput += message;
56743+
if (options.mode === void 0 || options.mode === "enabled") {
56744+
process.stderr.write(message);
56745+
}
56746+
});
56747+
(_b = childProcess.stdout) == null ? void 0 : _b.on("data", (message) => {
56748+
stdout += message;
56749+
logOutput += message;
56750+
if (options.mode === void 0 || options.mode === "enabled") {
56751+
process.stderr.write(message);
56752+
}
56753+
});
56754+
childProcess.on("close", (exitCode, signal) => {
56755+
const exitDescription = exitCode !== null ? `exit code "${exitCode}"` : `signal "${signal}"`;
56756+
const printFn = options.mode === "on-error" ? Log.error : Log.debug;
56757+
const status = statusFromExitCodeAndSignal(exitCode, signal);
56758+
printFn(`Command "${command}" completed with ${exitDescription}.`);
56759+
printFn(`Process output:
56760+
${logOutput}`);
56761+
if (status === 0 || options.suppressErrorOnFailingExitCode) {
56762+
resolve({ stdout, stderr, status });
56763+
} else {
56764+
reject(options.mode === "silent" ? logOutput : void 0);
56765+
}
56766+
});
56767+
});
56768+
}
5680656769

5680756770
//
5680856771
function determineRepoBaseDirFromCwd() {

.github/local-actions/changelog/main.js

Lines changed: 43 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -56575,49 +56575,6 @@ var ChildProcess = class {
5657556575
childProcess.on("close", (status) => status === 0 ? resolve() : reject(status));
5657656576
});
5657756577
}
56578-
static spawn(command, args, options = {}) {
56579-
return new Promise((resolve, reject) => {
56580-
const commandText = `${command} ${args.join(" ")}`;
56581-
const outputMode = options.mode;
56582-
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56583-
Log.debug(`Executing command: ${commandText}`);
56584-
const childProcess = _spawn(command, args, { ...options, env: env3, shell: true, stdio: "pipe" });
56585-
let logOutput = "";
56586-
let stdout = "";
56587-
let stderr = "";
56588-
if (options.input !== void 0) {
56589-
childProcess.stdin.write(options.input);
56590-
childProcess.stdin.end();
56591-
}
56592-
childProcess.stderr.on("data", (message) => {
56593-
stderr += message;
56594-
logOutput += message;
56595-
if (outputMode === void 0 || outputMode === "enabled") {
56596-
process.stderr.write(message);
56597-
}
56598-
});
56599-
childProcess.stdout.on("data", (message) => {
56600-
stdout += message;
56601-
logOutput += message;
56602-
if (outputMode === void 0 || outputMode === "enabled") {
56603-
process.stderr.write(message);
56604-
}
56605-
});
56606-
childProcess.on("close", (exitCode, signal) => {
56607-
const exitDescription = exitCode !== null ? `exit code "${exitCode}"` : `signal "${signal}"`;
56608-
const printFn = outputMode === "on-error" ? Log.error : Log.debug;
56609-
const status = statusFromExitCodeAndSignal(exitCode, signal);
56610-
printFn(`Command "${commandText}" completed with ${exitDescription}.`);
56611-
printFn(`Process output:
56612-
${logOutput}`);
56613-
if (status === 0 || options.suppressErrorOnFailingExitCode) {
56614-
resolve({ stdout, stderr, status });
56615-
} else {
56616-
reject(outputMode === "silent" ? logOutput : void 0);
56617-
}
56618-
});
56619-
});
56620-
}
5662156578
static spawnSync(command, args, options = {}) {
5662256579
const commandText = `${command} ${args.join(" ")}`;
5662356580
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
@@ -56629,44 +56586,14 @@ ${logOutput}`);
5662956586
}
5663056587
throw new Error(stderr);
5663156588
}
56589+
static spawn(command, args, options = {}) {
56590+
const commandText = `${command} ${args.join(" ")}`;
56591+
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56592+
return processAsyncCmd(commandText, options, _spawn(command, args, { ...options, env: env3, shell: true, stdio: "pipe" }));
56593+
}
5663256594
static exec(command, options = {}) {
56633-
return new Promise((resolve, reject) => {
56634-
var _a2, _b;
56635-
const outputMode = options.mode;
56636-
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56637-
Log.debug(`Executing command: ${command}`);
56638-
const childProcess = _exec(command, { ...options, env: env3 });
56639-
let logOutput = "";
56640-
let stdout = "";
56641-
let stderr = "";
56642-
(_a2 = childProcess.stderr) == null ? void 0 : _a2.on("data", (message) => {
56643-
stderr += message;
56644-
logOutput += message;
56645-
if (outputMode === void 0 || outputMode === "enabled") {
56646-
process.stderr.write(message);
56647-
}
56648-
});
56649-
(_b = childProcess.stdout) == null ? void 0 : _b.on("data", (message) => {
56650-
stdout += message;
56651-
logOutput += message;
56652-
if (outputMode === void 0 || outputMode === "enabled") {
56653-
process.stderr.write(message);
56654-
}
56655-
});
56656-
childProcess.on("close", (exitCode, signal) => {
56657-
const exitDescription = exitCode !== null ? `exit code "${exitCode}"` : `signal "${signal}"`;
56658-
const printFn = outputMode === "on-error" ? Log.error : Log.debug;
56659-
const status = statusFromExitCodeAndSignal(exitCode, signal);
56660-
printFn(`Command "${command}" completed with ${exitDescription}.`);
56661-
printFn(`Process output:
56662-
${logOutput}`);
56663-
if (status === 0 || options.suppressErrorOnFailingExitCode) {
56664-
resolve({ stdout, stderr, status });
56665-
} else {
56666-
reject(outputMode === "silent" ? logOutput : void 0);
56667-
}
56668-
});
56669-
});
56595+
const env3 = getEnvironmentForNonInteractiveCommand(options.env);
56596+
return processAsyncCmd(command, options, _exec(command, { ...options, env: env3 }));
5667056597
}
5667156598
};
5667256599
function statusFromExitCodeAndSignal(exitCode, signal) {
@@ -56676,6 +56603,42 @@ function getEnvironmentForNonInteractiveCommand(userProvidedEnv) {
5667656603
const forceColorValue = supports_color_default2.stdout !== false ? supports_color_default2.stdout.level.toString() : void 0;
5667756604
return { FORCE_COLOR: forceColorValue, ...userProvidedEnv ?? process.env };
5667856605
}
56606+
function processAsyncCmd(command, options, childProcess) {
56607+
return new Promise((resolve, reject) => {
56608+
var _a2, _b;
56609+
let logOutput = "";
56610+
let stdout = "";
56611+
let stderr = "";
56612+
Log.debug(`Executing command: ${command}`);
56613+
(_a2 = childProcess.stderr) == null ? void 0 : _a2.on("data", (message) => {
56614+
stderr += message;
56615+
logOutput += message;
56616+
if (options.mode === void 0 || options.mode === "enabled") {
56617+
process.stderr.write(message);
56618+
}
56619+
});
56620+
(_b = childProcess.stdout) == null ? void 0 : _b.on("data", (message) => {
56621+
stdout += message;
56622+
logOutput += message;
56623+
if (options.mode === void 0 || options.mode === "enabled") {
56624+
process.stderr.write(message);
56625+
}
56626+
});
56627+
childProcess.on("close", (exitCode, signal) => {
56628+
const exitDescription = exitCode !== null ? `exit code "${exitCode}"` : `signal "${signal}"`;
56629+
const printFn = options.mode === "on-error" ? Log.error : Log.debug;
56630+
const status = statusFromExitCodeAndSignal(exitCode, signal);
56631+
printFn(`Command "${command}" completed with ${exitDescription}.`);
56632+
printFn(`Process output:
56633+
${logOutput}`);
56634+
if (status === 0 || options.suppressErrorOnFailingExitCode) {
56635+
resolve({ stdout, stderr, status });
56636+
} else {
56637+
reject(options.mode === "silent" ? logOutput : void 0);
56638+
}
56639+
});
56640+
});
56641+
}
5667956642

5668056643
//
5668156644
function determineRepoBaseDirFromCwd() {

0 commit comments

Comments
 (0)