-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { exec } from 'node:child_process'; | ||
import type { ExecException, ExecOptions } from 'node:child_process'; | ||
|
||
import { Err, Ok } from '@thames/monads'; | ||
import type { Result } from '@thames/monads'; | ||
|
||
async function internalAsyncExec( | ||
command: string, | ||
options?: ExecOptions | ||
): Promise<string> { | ||
return await new Promise((resolve, reject) => { | ||
exec(command, options ?? {}, (error, stdout, stderr) => { | ||
if (error != null) { | ||
reject(error); | ||
return; | ||
} | ||
|
||
if (stderr !== '') { | ||
reject(stderr); | ||
return; | ||
} | ||
|
||
resolve(stdout); | ||
}); | ||
}); | ||
} | ||
|
||
export async function asyncExec( | ||
command: string, | ||
commandWorkingDirectory?: string | ||
): Promise<Result<string, ExecException | string>> { | ||
let result: Awaited<ReturnType<typeof internalAsyncExec>>; | ||
try { | ||
result = await internalAsyncExec(command, { cwd: commandWorkingDirectory }); | ||
} catch (error) { | ||
return Err(error as ExecException | string); | ||
} | ||
|
||
return Ok(result); | ||
} | ||
|
||
export default asyncExec; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './async-exec'; |
This file contains 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