chore: include output in configDispatch

allows for acting on the output after executing a command. This will be
useful with https://github.com/zen-browser/surfer/pull/15 to only apply
`commit.gpgSign false` if a gpg key is not present in the config.
This commit is contained in:
Peter Cardenas 2025-03-16 12:58:00 -07:00
parent fd234005ec
commit 7a581dc168
No known key found for this signature in database
GPG key ID: 3F72404517E2E286
2 changed files with 16 additions and 10 deletions

View file

@ -129,11 +129,11 @@ const genericBuild = async (os: string, fast = false): Promise<boolean> => {
`Mach contents: \n ${readFileSync(join(ENGINE_DIR, 'mach'))}\n\n===END===` `Mach contents: \n ${readFileSync(join(ENGINE_DIR, 'mach'))}\n\n===END===`
) )
return await configDispatch('./mach', { return (await configDispatch('./mach', {
args: buildOptions, args: buildOptions,
cwd: ENGINE_DIR, cwd: ENGINE_DIR,
killOnError: true, killOnError: true,
}) })).success
} }
const parseDate = (d: number) => { const parseDate = (d: number) => {

View file

@ -2,13 +2,15 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this // License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
import execa from 'execa' import execa from 'execa';
import { BASH_PATH } from '../constants' import { BASH_PATH } from '../constants';
import { log } from '../log' import { log } from '../log';
export const removeTimestamp = (input: string): string => export const removeTimestamp = (input: string): string =>
input.replace(/\s\d{1,5}:\d\d\.\d\d /g, '') input.replace(/\s\d{1,5}:\d\d\.\d\d /g, '')
type CommandResult = { success: boolean, output: string[] };
export const configDispatch = ( export const configDispatch = (
cmd: string, cmd: string,
config?: { config?: {
@ -26,7 +28,7 @@ export const configDispatch = (
shell?: 'default' | 'unix' shell?: 'default' | 'unix'
env?: Record<string, string> env?: Record<string, string>
} }
): Promise<boolean> => { ): Promise<CommandResult> => {
// Provide a default logger if none was specified by the user // Provide a default logger if none was specified by the user
const logger = config?.logger || ((data: string) => log.info(data)) const logger = config?.logger || ((data: string) => log.info(data))
@ -53,11 +55,15 @@ export const configDispatch = (
} }
} }
const output: string[] = []
const handle = (data: string | Error, killOnError?: boolean) => { const handle = (data: string | Error, killOnError?: boolean) => {
const dataAsString = data.toString() const dataAsString = data.toString()
for (const line of dataAsString.split('\n')) { for (const line of dataAsString.split('\n')) {
if (line.length > 0) logger(removeTimestamp(line)) if (line.length > 0) {
output.push(line)
logger(removeTimestamp(line))
}
} }
if (killOnError) { if (killOnError) {
@ -81,8 +87,8 @@ export const configDispatch = (
proc.stdout?.on('error', (d) => handle(d, config?.killOnError || false)) proc.stdout?.on('error', (d) => handle(d, config?.killOnError || false))
proc.stderr?.on('error', (d) => handle(d, config?.killOnError || false)) proc.stderr?.on('error', (d) => handle(d, config?.killOnError || false))
proc.on('exit', () => { proc.on('exit', (code) => {
resolve(true) resolve({ success: code === 0, output })
}) })
}) })
} }
@ -96,7 +102,7 @@ export const dispatch = (
cwd?: string, cwd?: string,
killOnError?: boolean, killOnError?: boolean,
logger = (data: string) => log.info(data) logger = (data: string) => log.info(data)
): Promise<boolean> => { ): Promise<CommandResult> => {
return configDispatch(cmd, { return configDispatch(cmd, {
args: arguments_, args: arguments_,
cwd: cwd, cwd: cwd,