From 0b3db5a8aec343dbd938d62d2dc6e1ff18dcdb1f Mon Sep 17 00:00:00 2001 From: Ryan Chang Date: Sat, 28 Sep 2024 02:43:13 +0800 Subject: [PATCH 1/2] refactor: unified stdout/stderr for git cli commands --- src/git.ts | 76 ++++++++++++++++-------------------------------------- 1 file changed, 22 insertions(+), 54 deletions(-) diff --git a/src/git.ts b/src/git.ts index a92b7b3..c4e4ca2 100644 --- a/src/git.ts +++ b/src/git.ts @@ -9,10 +9,12 @@ import { import { getWorkspace } from './utils/cwd' -export async function switchBranch(branch: string) { +async function execGit(args: string[]) { const debugOutput: string[] = [] const warningOutput: string[] = [] - await exec('git', ['checkout', '-b', branch], { + const errorOutput: string[] = [] + + await exec('git', args, { silent: true, ignoreReturnCode: true, listeners: { @@ -20,14 +22,25 @@ export async function switchBranch(branch: string) { debugOutput.push(data) }, errline: (error: string) => { - if (/^(fatal|error):/.test(error)) core.error(error) + if (/^(fatal|error):/.test(error)) errorOutput.push(error) else warningOutput.push(error) }, }, }) - if (debugOutput.length > 0) core.debug(debugOutput.join('\n')) - if (warningOutput.length > 0) core.warning(warningOutput.join('\n')) + for (const msg of debugOutput) core.debug(msg) + for (const msg of warningOutput) core.warning(msg) + for (const msg of errorOutput) core.error(msg) + + return { + debug: debugOutput, + warn: warningOutput, + error: errorOutput, + } +} + +export async function switchBranch(branch: string) { + await execGit(['checkout', '-b', branch]) } export async function pushCurrentBranch() { @@ -36,67 +49,22 @@ export async function pushCurrentBranch() { pushArgs.splice(1, 0, '--force') } - const debugOutput: string[] = [] - const warningOutput: string[] = [] - await exec('git', pushArgs, { - silent: true, - ignoreReturnCode: true, - listeners: { - stdline: (data: string) => { - debugOutput.push(data) - }, - errline: (error: string) => { - if (/^(fatal|error):/.test(error)) core.error(error) - else warningOutput.push(error) - }, - }, - }) - - if (debugOutput.length > 0) core.debug(debugOutput.join('\n')) - if (warningOutput.length > 0) core.warning(warningOutput.join('\n')) + await execGit(pushArgs) } export async function addFileChanges(globPatterns: string[]): Promise { const workspace = getWorkspace() const workspacePaths = globPatterns.map((p) => join(workspace, p)) - const debugOutput: string[] = [] - const warningOutput: string[] = [] - await exec('git', ['add', '--', ...workspacePaths], { - silent: true, - ignoreReturnCode: true, - listeners: { - stdline: (data: string) => { - debugOutput.push(data) - }, - errline: (error: string) => { - if (/^(fatal|error):/.test(error)) core.error(error) - else warningOutput.push(error) - }, - }, - }) - - if (debugOutput.length > 0) core.debug(debugOutput.join('\n')) - if (warningOutput.length > 0) core.warning(warningOutput.join('\n')) + await execGit(['add', '--', ...workspacePaths]) } export async function getFileChanges(): Promise { - const output: string[] = [] - await exec('git', ['status', '-suall', '--porcelain'], { - listeners: { - stdline: (data: string) => { - output.push(data) - }, - errline: (error: string) => { - if (/^(fatal|error):/.test(error)) core.error(error) - else core.warning(error) - }, - }, - }) + const { debug } = await execGit(['status', '-suall', '--porcelain']) const additions: FileAddition[] = [] const deletions: FileDeletion[] = [] - output.forEach((line) => { + debug.forEach((line) => { const staged = line.charAt(0) const filePath = line.slice(3) switch (staged) { From e0a0724b6a7fc3c5c56fa126b21d0367aee3e344 Mon Sep 17 00:00:00 2001 From: Ryan Chang Date: Sat, 28 Sep 2024 02:43:25 +0800 Subject: [PATCH 2/2] chore: update bundle dist --- dist/index.js | 88 ++++++++++++++------------------------------------- 1 file changed, 24 insertions(+), 64 deletions(-) diff --git a/dist/index.js b/dist/index.js index f47999c..54ca666 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30495,11 +30495,12 @@ const core = __importStar(__nccwpck_require__(2186)); const exec_1 = __nccwpck_require__(1514); const node_path_1 = __nccwpck_require__(9411); const cwd_1 = __nccwpck_require__(7119); -function switchBranch(branch) { +function execGit(args) { return __awaiter(this, void 0, void 0, function* () { const debugOutput = []; const warningOutput = []; - yield (0, exec_1.exec)('git', ['checkout', '-b', branch], { + const errorOutput = []; + yield (0, exec_1.exec)('git', args, { silent: true, ignoreReturnCode: true, listeners: { @@ -30508,16 +30509,28 @@ function switchBranch(branch) { }, errline: (error) => { if (/^(fatal|error):/.test(error)) - core.error(error); + errorOutput.push(error); else warningOutput.push(error); }, }, }); - if (debugOutput.length > 0) - core.debug(debugOutput.join('\n')); - if (warningOutput.length > 0) - core.warning(warningOutput.join('\n')); + for (const msg of debugOutput) + core.debug(msg); + for (const msg of warningOutput) + core.warning(msg); + for (const msg of errorOutput) + core.error(msg); + return { + debug: debugOutput, + warn: warningOutput, + error: errorOutput, + }; + }); +} +function switchBranch(branch) { + return __awaiter(this, void 0, void 0, function* () { + yield execGit(['checkout', '-b', branch]); }); } function pushCurrentBranch() { @@ -30526,75 +30539,22 @@ function pushCurrentBranch() { if (core.getBooleanInput('branch-push-force')) { pushArgs.splice(1, 0, '--force'); } - const debugOutput = []; - const warningOutput = []; - yield (0, exec_1.exec)('git', pushArgs, { - silent: true, - ignoreReturnCode: true, - listeners: { - stdline: (data) => { - debugOutput.push(data); - }, - errline: (error) => { - if (/^(fatal|error):/.test(error)) - core.error(error); - else - warningOutput.push(error); - }, - }, - }); - if (debugOutput.length > 0) - core.debug(debugOutput.join('\n')); - if (warningOutput.length > 0) - core.warning(warningOutput.join('\n')); + yield execGit(pushArgs); }); } function addFileChanges(globPatterns) { return __awaiter(this, void 0, void 0, function* () { const workspace = (0, cwd_1.getWorkspace)(); const workspacePaths = globPatterns.map((p) => (0, node_path_1.join)(workspace, p)); - const debugOutput = []; - const warningOutput = []; - yield (0, exec_1.exec)('git', ['add', '--', ...workspacePaths], { - silent: true, - ignoreReturnCode: true, - listeners: { - stdline: (data) => { - debugOutput.push(data); - }, - errline: (error) => { - if (/^(fatal|error):/.test(error)) - core.error(error); - else - warningOutput.push(error); - }, - }, - }); - if (debugOutput.length > 0) - core.debug(debugOutput.join('\n')); - if (warningOutput.length > 0) - core.warning(warningOutput.join('\n')); + yield execGit(['add', '--', ...workspacePaths]); }); } function getFileChanges() { return __awaiter(this, void 0, void 0, function* () { - const output = []; - yield (0, exec_1.exec)('git', ['status', '-suall', '--porcelain'], { - listeners: { - stdline: (data) => { - output.push(data); - }, - errline: (error) => { - if (/^(fatal|error):/.test(error)) - core.error(error); - else - core.warning(error); - }, - }, - }); + const { debug } = yield execGit(['status', '-suall', '--porcelain']); const additions = []; const deletions = []; - output.forEach((line) => { + debug.forEach((line) => { const staged = line.charAt(0); const filePath = line.slice(3); switch (staged) {