Skip to content

Commit

Permalink
feat: add option to not log command (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
flo-monin authored Apr 5, 2024
1 parent 0a61018 commit 604ad85
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/util/exec.util.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import type { ProcessEnvOptions, SpawnOptions } from 'node:child_process'
import cp from 'node:child_process'

export interface ExecOptions extends SpawnOptions {
/**
* Defaults to true.
* Set to false to skip logging.
*/
log?: boolean
}

export async function execVoidCommand(
cmd: string,
args: string[] = [],
opt: SpawnOptions = {},
opt: ExecOptions = {},
): Promise<void> {
logExec(cmd, args, opt)

Expand All @@ -29,11 +37,7 @@ export async function execVoidCommand(
})
}

export function execVoidCommandSync(
cmd: string,
args: string[] = [],
opt: SpawnOptions = {},
): void {
export function execVoidCommandSync(cmd: string, args: string[] = [], opt: ExecOptions = {}): void {
logExec(cmd, args, opt)

const r = cp.spawnSync(cmd, [...args], {
Expand All @@ -58,7 +62,13 @@ export function execVoidCommandSync(
}
}

function logExec(cmd: string, args: string[] = [], opt: ProcessEnvOptions = {}): void {
function logExec(
cmd: string,
args: string[] = [],
opt: ProcessEnvOptions & ExecOptions = {},
): void {
if (opt.log === false) return

const cmdline = [
...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('=')),
cmd,
Expand Down

0 comments on commit 604ad85

Please sign in to comment.