Skip to content

Commit 2bacded

Browse files
committed
feat: remove sync tests and related types for Result and ResultAsync
- Deleted the sync tests for Result and ResultAsync to streamline the codebase. - Updated type imports in `types.ts` to reflect the removal of async-related types. - Refactored utility functions in `utils.ts` to simplify error handling and promise checks.
1 parent 2344f28 commit 2bacded

File tree

9 files changed

+252
-1896
lines changed

9 files changed

+252
-1896
lines changed

src/common/shell.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,49 @@
11
import { isString } from "@/remeda";
2-
import { ResultAsync } from "@/result";
2+
import { Result } from "@/result";
33

44
import { errorToMessage } from "./error";
55
import { PromiseWithResolvers } from "./promise";
66

7-
export function $(cmd: string): ResultAsync<{ stdout: string; stderr: string }, string>;
8-
export function $(
7+
export interface ShellExecResult {
8+
stdout: string;
9+
stderr: string;
10+
}
11+
12+
const REGEXP_NULL_CHAR = /\x00+/g;
13+
const REGEXP_SAFE_CHARS = /^[A-Za-z0-9,:=_./-]+$/;
14+
const REGEXP_SINGLE_QUOTES = /'+/g;
15+
16+
export async function $(cmd: string): Promise<Result<ShellExecResult, string>>;
17+
export async function $(
918
cmd: TemplateStringsArray,
1019
...values: any[]
11-
): ResultAsync<{ stdout: string; stderr: string }, string>;
12-
export function $(cmd: string | TemplateStringsArray, ...values: any[]) {
20+
): Promise<Result<ShellExecResult, string>>;
21+
export async function $(cmd: string | TemplateStringsArray, ...values: any[]) {
22+
const { exec } = await import("node:child_process");
23+
1324
const command = isString(cmd)
1425
? cmd
1526
: cmd.reduce((acc, part, index) => acc + part + (values[index] ?? ""), "");
1627

17-
const promise = import("node:child_process").then(({ exec }) => {
18-
const { promise, reject, resolve } = PromiseWithResolvers();
28+
const fn = async () => {
29+
const { promise, reject, resolve } = PromiseWithResolvers<ShellExecResult>();
1930

2031
exec(command, (error, stdout, stderr) => {
2132
if (error) {
2233
reject(error);
2334
} else {
24-
resolve({ stdout, stderr });
35+
resolve({ stdout: stdout.trim(), stderr: stderr.trim() });
2536
}
2637
});
2738

28-
return promise;
29-
});
39+
return await promise;
40+
};
41+
const onThrow = errorToMessage(`Failed to execute command: ${cmd}`);
42+
const result = Result.try(fn, onThrow);
3043

31-
return ResultAsync.fromPromise(promise, errorToMessage(`Failed to execute command: ${cmd}`));
44+
return result;
3245
}
3346

34-
const REGEXP_NULL_CHAR = /\x00+/g;
35-
const REGEXP_SAFE_CHARS = /^[A-Za-z0-9,:=_./-]+$/;
36-
const REGEXP_SINGLE_QUOTES = /'+/g;
37-
3847
export const quoteShellArg = (arg: string) => {
3948
if (!arg) return "''";
4049

0 commit comments

Comments
 (0)