🌳
pt0/serverF/childProcF/do2ExecFileF.mts
1import util from 'util'
2import { execFile, type ExecFileOptions } from 'child_process'
4export const aExecFile = util.promisify(execFile);
6type Do2ExecFileProps = {
7 cmdA: string[]
8 isQuiet?: boolean
9 opts?: ExecFileOptions
12type ExecResult = { stdout: string; stderr: string; code?: number; isSuccess: boolean }
14export const do2ExecFile = async ({cmdA, isQuiet=true, opts}: Do2ExecFileProps): Promise<ExecResult> => {
15 if (!isQuiet) {
16 console.log(cmdA.join(' '))
17 }
18 try {
19 const res = await aExecFile(cmdA[0], cmdA.slice(1), opts)
20 return {stdout: String(res.stdout), stderr: String(res.stderr), isSuccess: true}
21 } catch (ee) {
22 const err = ee as { stdout?: string; stderr?: string; code?: number; message?: string }
23 const stderr = err.stderr || err.message || ''
24 return {stdout: err.stdout || '', stderr, code: err.code, isSuccess: false}
25 }