Skip to content

Commit adf51f2

Browse files
committed
feat: refactor project structure and enhance utility functions
1 parent 17676a4 commit adf51f2

76 files changed

Lines changed: 1236 additions & 733 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/common/error.ts

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/common/nil.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/common/throttle.ts

Lines changed: 0 additions & 68 deletions
This file was deleted.

src/exec/index.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/exec/safe/index.ts

Lines changed: 4 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,5 @@
1-
import { err, ok } from "@/result";
1+
export { NonZeroExitError } from "../shared/error";
2+
export { Process, exec, exec as x, exec as $ } from "./process";
23

3-
import { NonZeroExitError } from "../error";
4-
import { BaseProcess } from "../process";
5-
import { readStreamLines, readStreams } from "../stream";
6-
7-
import type { Exec, ProcessInstance, ProcessOptions } from "./types";
8-
import type { ExecParams, Output } from "../types";
9-
import type { Result } from "@/result";
10-
11-
export class Process
12-
extends BaseProcess<Result<Output, Error>, Result<string, Error>>
13-
implements ProcessInstance
14-
{
15-
declare exec: Exec;
16-
declare pipe: Exec;
17-
18-
declare protected options: Partial<ProcessOptions>;
19-
20-
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
21-
constructor(options?: Partial<ProcessOptions>) {
22-
super(options);
23-
}
24-
25-
override async *[Symbol.asyncIterator](): AsyncIterator<Result<string, Error>> {
26-
const p = this.process;
27-
if (!p) return err(new Error("No process was started"));
28-
29-
for await (const line of readStreamLines([this.streamOut, this.streamErr])) {
30-
yield ok(line);
31-
}
32-
33-
await this.cleanup();
34-
35-
const result = this.maybeThrow();
36-
if (result.isErr()) return result;
37-
}
38-
39-
protected override async thenImpl(): Promise<Result<Output, Error>> {
40-
const p = this.process;
41-
if (!p) return err(new Error("No process was started"));
42-
43-
const [stdout, stderr] = await readStreams([this.streamOut, this.streamErr]);
44-
45-
await this.cleanup();
46-
47-
const output: Output = {
48-
stdout,
49-
stderr,
50-
exitCode: this.exitCode,
51-
};
52-
const result = this.maybeThrow(output);
53-
if (result.isErr()) return result;
54-
55-
return ok(output);
56-
}
57-
58-
protected maybeThrow(output?: Output): Result<void, Error> {
59-
if (this.thrownError) return err(this.thrownError);
60-
61-
const shouldThrowOnError =
62-
this.options.throwOnError && this.exitCode !== 0 && this.exitCode !== undefined;
63-
if (!shouldThrowOnError) return ok();
64-
65-
return err(new NonZeroExitError(this, output));
66-
}
67-
68-
protected override child() {
69-
return new Process({ stdin: this });
70-
}
71-
}
72-
73-
export const exec: Exec = (...params: ExecParams): any => {
74-
const self = new Process();
75-
76-
return BaseProcess["execImpl"](self, params);
77-
};
4+
export type { Output } from "../shared/types";
5+
export type { Exec, ProcessInstance, ProcessOptions } from "./types";

src/exec/safe/process.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { Err, Ok } from "@/result";
2+
3+
import { NonZeroExitError } from "../shared/error";
4+
import { BaseProcess } from "../shared/process";
5+
import { readStreamLines, readStreams } from "../shared/stream";
6+
7+
import type { Exec, ProcessInstance, ProcessOptions } from "./types";
8+
import type { ExecParams, Output } from "../shared/types";
9+
import type { Result } from "@/result";
10+
11+
export class Process
12+
extends BaseProcess<Result<Output, Error>, Result<string, Error>>
13+
implements ProcessInstance
14+
{
15+
declare exec: Exec;
16+
declare pipe: Exec;
17+
18+
declare protected options: Partial<ProcessOptions>;
19+
20+
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
21+
constructor(options?: Partial<ProcessOptions>) {
22+
super(options);
23+
}
24+
25+
override async *[Symbol.asyncIterator](): AsyncIterator<Result<string, Error>> {
26+
const p = this.process;
27+
if (!p) return Err(new Error("No process was started"));
28+
29+
for await (const line of readStreamLines([this.streamOut, this.streamErr])) {
30+
yield Ok(line);
31+
}
32+
33+
await this.cleanup();
34+
35+
const result = this.maybeThrow();
36+
if (result.isErr()) return result;
37+
}
38+
39+
protected override async thenImpl(): Promise<Result<Output, Error>> {
40+
const p = this.process;
41+
if (!p) return Err(new Error("No process was started"));
42+
43+
const [stdout, stderr] = await readStreams([this.streamOut, this.streamErr]);
44+
45+
await this.cleanup();
46+
47+
const output: Output = {
48+
stdout,
49+
stderr,
50+
exitCode: this.exitCode,
51+
};
52+
const result = this.maybeThrow(output);
53+
if (result.isErr()) return result;
54+
55+
return Ok(output);
56+
}
57+
58+
protected maybeThrow(output?: Output): Result<void, Error> {
59+
if (this.thrownError) return Err(this.thrownError);
60+
61+
const shouldThrowOnError =
62+
this.options.throwOnError && this.exitCode !== 0 && this.exitCode !== undefined;
63+
if (!shouldThrowOnError) return Ok();
64+
65+
return Err(new NonZeroExitError(this, output));
66+
}
67+
68+
protected override child() {
69+
return new Process({ stdin: this });
70+
}
71+
}
72+
73+
export const exec: Exec = (...params: ExecParams): any => {
74+
const self = new Process();
75+
76+
return BaseProcess["execImpl"](self, params);
77+
};

src/exec/safe/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type {
66
BaseProcessInstance,
77
BaseProcessOptions,
88
Output,
9-
} from "../types";
9+
} from "../shared/types";
1010
import type { Result } from "@/result";
1111

1212
export type ProcessOptions = BaseProcessOptions<ProcessInstance>;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { tokenizeArgs } from "args-tokenizer";
22

3-
import { getErrorMessage } from "@/common";
4-
import { isArray, isPlainObject, isString } from "@/remeda";
3+
import { isArray, isPlainObject, isString } from "@/fp";
4+
import { getErrorMessage } from "@/tools";
55

66
import type {
77
BaseProcessOptions,
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { isString } from "remeda";
2-
3-
import { concatTemplateStrings, createPromiseWithResolvers } from "@/common";
1+
import { isString } from "@/fp";
2+
import { concatTemplateStrings, createPromiseWithResolvers } from "@/tools";
43

54
import { isAbortError } from "./error";
65
import {

0 commit comments

Comments
 (0)