1import * as _ from 'lodash-es' 2import { spawn } from 'child_process' 5export const live2Spawn = async ({cmd, noOutCmd=false, usePty=false}: {cmd: string, noOutCmd?: boolean, usePty?: boolean}) => { 10 return await new Promise<{exitCode: number | null, stdout: string, stderr: string, isSuccess: boolean}>((resolv, reject) => { 11 const spawnObj = (!process.stdout.isTTY && usePty) 12 ? spawn('script', process.platform === 'darwin' 13 ? ['-q', '/dev/null', '/bin/sh', '-c', cmd] 14 : ['-q', '-e', '-c', cmd, '/dev/null'] 16 : spawn(cmd, {shell: true, stdio: 'inherit'}) 21 const listenerFncs = _.map([ 25 ] as const, (sigStr) => { 27 console.log(sigStr, 'efef203f9j3f9j9j') 31 process.on(sigStr, fnc) 32 return [sigStr, fnc] as const 35 spawnObj.on('exit', function (exitCode) { 36 _.each(listenerFncs, ([sigStr, fnc]) => { 37 process.removeListener(sigStr, fnc) 39 const isSuccess = exitCode == 0 40 resolv({exitCode, stdout, stderr, isSuccess}) 45export const live2SpawnThrow = async (props: {cmd: string, noOutCmd?: boolean, usePty?: boolean}) => { 46 const ret = await live2Spawn(props) 47 const {isSuccess} = ret