Last active
December 26, 2022 11:56
-
-
Save tzafrirben/6d9809d00e9fce1206b3b7255b63053a to your computer and use it in GitHub Desktop.
Dependency injection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ChildProcess } from 'child_process'; | |
export const execCommand = async (childProcess: ChildProcess) => { | |
return new Promise((resolve, reject) => { | |
let stdout = ''; | |
childProcess.stdout?.on('data', data => { | |
stdout = stdout + data.toString(); | |
}); | |
let stderr = ''; | |
childProcess.stderr?.on('data', data => { | |
stderr = stderr + data.toString(); | |
}); | |
childProcess.on('close', code => { | |
if (code === 0) { | |
resolve({ stdout, stderr, code }); | |
} else { | |
reject(new Error(stderr.trim())); | |
} | |
}); | |
childProcess.on('error', err => { | |
reject(err); | |
}); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment