Skip to content

Commit

Permalink
Adding async exec
Browse files Browse the repository at this point in the history
  • Loading branch information
kamaal111 committed Apr 24, 2024
1 parent 5c7c9e3 commit b2bcd88
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"peerDependencies": {},
"name": "@kamaalio/kamaal",
"author": "Kamaal Farah",
"dependencies": {
"@thames/monads": "^0.6.2"
},
"devDependencies": {
"@babel/core": "^7.24.4",
"@babel/preset-env": "^7.24.4",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * as objects from './objects';
export * as maths from './maths';
export * as strings from './strings';
export * as arrays from './arrays';
export * as shell from './shell';
42 changes: 42 additions & 0 deletions src/shell/async-exec.ts
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;
1 change: 1 addition & 0 deletions src/shell/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './async-exec';
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,11 @@
eslint-visitor-keys "^3.4.3"
espree "^9.6.1"

"@thames/monads@^0.6.2":
version "0.6.2"
resolved "https://registry.yarnpkg.com/@thames/monads/-/monads-0.6.2.tgz#471fcbbc21f4beb578156f18c2e27c82af51a273"
integrity sha512-LxsJ5g4kYK/VS+s+b5fEz6GG1v5eyysURaDE3hOjQB1vmWaCsRoqAKOwpl8YF9FZ86on1BwxIHev3ZF8u41X7g==

"@tsconfig/node10@^1.0.7":
version "1.0.11"
resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.11.tgz#6ee46400685f130e278128c7b38b7e031ff5b2f2"
Expand Down

0 comments on commit b2bcd88

Please sign in to comment.