Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*eslint complexity: ["error", 9]*/
- export const handleShellResponse = (
- code: number | null,
- stdout: string | null,
- stderr: string | null,
- command: string,
- settings: ShellExecOptions,
- resolve: (value?: ShellExecSuccess) => void,
- reject: (value?: ShellExecError) => void,
- ): void => {
- switch (code) {
- case 0: // success
- return resolve(
- new ShellExecSuccess(code, stdout, stderr, command, settings),
- );
- // If there is an error then reject
- case null: // timeout
- return reject(
- new ShellExecTimeoutError(code, stdout, stderr, command, settings),
- );
- case 127: // not-found
- return reject(
- new ShellExecNotFoundError(code, stdout, stderr, command, settings),
- );
- case 1: // Catch-all
- case 2: // Misuse of shell builtins (according to Bash documentation)
- case 126: // Command invoked cannot execute
- case 128: // Invalid argument to exit
- case 130: // Script terminated by Control-C
- default:
- return reject(
- new ShellExecError(code, stdout, stderr, command, settings),
- );
- }
- };
- export const shellExec = async (
- command: string,
- settings: ShellExecOptions = SHELL_DEFAULT_SETTINGS,
- ): Promise<ShellExecResponse> => {
- return new Promise((resolve, reject) => {
- shell.exec(
- command,
- settings,
- (code: number | null, stdout: string | null, stderr: string | null) => {
- handleShellResponse(
- code,
- stdout,
- stderr,
- command,
- settings,
- resolve,
- reject,
- );
- },
- );
- });
- };
Advertisement
Add Comment
Please, Sign In to add comment