Open History to see the last commit
1/**
2 * Post-processing service for linting and formatting generated code.
3 *
4 * @since 4.0.0
5 */
6import * as Context from "effect/Context"
7import * as Data from "effect/Data"
8import * as Effect from "effect/Effect"
9import * as Layer from "effect/Layer"
10import * as Stream from "effect/Stream"
11import * as ChildProcess from "effect/unstable/process/ChildProcess"
12import * as ChildProcessSpawner from "effect/unstable/process/ChildProcessSpawner"
13
14/**
15 * Error during post-processing (lint or format).
16 *
17 * **Example** (Creating a post-process error)
18 *
19 * ```ts
20 * import * as PostProcess from "@effect/ai-codegen/PostProcess"
21 *
22 * const error = new PostProcess.PostProcessError({
23 * step: "lint",
24 * command: "pnpm exec oxlint --fix /path/to/file.ts",
25 * filePath: "/path/to/file.ts",
26 * exitCode: 1,
27 * stdout: "",
28 * stderr: "error: some lint error",
29 * cause: new Error("Lint failed")
30 * })
31 * ```
32 *
33 * @category errors
34 * @since 4.0.0
35 */
36export class PostProcessError extends Data.TaggedError("PostProcessError")<{
37 readonly step: "lint" | "format"
38 readonly command: string
39 readonly filePath: string
40 readonly exitCode?: number | undefined
41 readonly stdout: string
42 readonly stderr: string
43 readonly cause: unknown
44}> {
45 override get message(): string {
46 const lines: Array<string> = [
47 `${this.step} failed for ${this.filePath}`,
48 `command: ${this.command}`
49 ]
50 if (this.exitCode !== undefined) {
51 lines.push(`exit code: ${this.exitCode}`)
52 }
53 if (this.stderr.length > 0) {
54 lines.push(`stderr:\n${this.stderr}`)
55 }
56 if (this.stdout.length > 0) {
57 lines.push(`stdout:\n${this.stdout}`)
58 }
59 return lines.join("\n")
60 }
61}
62
63/**
64 * Service for post-processing generated code.
65 *
66 * @category models
67 * @since 4.0.0
68 */
69export interface PostProcessor {
70 readonly lint: (filePath: string) => Effect.Effect<void, PostProcessError>
71 readonly format: (filePath: string) => Effect.Effect<void, PostProcessError>
72}
73
74/**
75 * Service tag for linting and formatting generated code.
76 *
77 * @category services
78 * @since 4.0.0
79 */
80export const PostProcessor: Context.Service<PostProcessor, PostProcessor> = Context.Service(
81 "@effect/ai-codegen/PostProcessor"
82)
83
84/**
85 * Layer providing the PostProcessor service.
86 *
87 * @category layers
88 * @since 4.0.0
89 */
90export const layer: Layer.Layer<
91 PostProcessor,
92 never,
93 ChildProcessSpawner.ChildProcessSpawner
94> = Effect.gen(function*() {
95 const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
96
97 const collectStream = (stream: Stream.Stream<Uint8Array, any>) =>
98 stream.pipe(
99 Stream.decodeText(),
100 Stream.mkString,
101 Effect.orElseSucceed(() => "")
102 )
103
104 const runCommand = Effect.fn("runCommand")(function*(
105 command: string,
106 args: ReadonlyArray<string>,
107 step: "lint" | "format",
108 filePath: string
109 ) {
110 const fullCommand = [command, ...args].join(" ")
111 const cmd = ChildProcess.make(command, args, {
112 stdout: "pipe",
113 stderr: "pipe"
114 })
115
116 yield* Effect.scoped(Effect.gen(function*() {
117 const handle = yield* spawner.spawn(cmd).pipe(
118 Effect.mapError((cause) =>
119 new PostProcessError({ step, command: fullCommand, filePath, stdout: "", stderr: "", cause })
120 )
121 )
122
123 const [stdout, stderr] = yield* Effect.all([
124 collectStream(handle.stdout),
125 collectStream(handle.stderr)
126 ])
127
128 const exitCode = yield* handle.exitCode.pipe(
129 Effect.mapError((cause) =>
130 new PostProcessError({ step, command: fullCommand, filePath, stdout, stderr, cause })
131 )
132 )
133
134 if (exitCode !== 0) {
135 return yield* new PostProcessError({
136 step,
137 command: fullCommand,
138 filePath,
139 exitCode,
140 stdout,
141 stderr,
142 cause: new Error(`Command exited with code ${exitCode}`)
143 })
144 }
145 }))
146 })
147
148 const lint = Effect.fn("lint")(function*(filePath: string) {
149 yield* runCommand("pnpm", ["exec", "oxlint", "--silent", "--quiet", "--fix", filePath], "lint", filePath)
150 })
151
152 const format = Effect.fn("format")(function*(filePath: string) {
153 yield* runCommand("pnpm", ["exec", "dprint", "--log-level", "silent", "fmt", filePath], "format", filePath)
154 })
155
156 return { lint, format }
157}).pipe(Layer.effect(PostProcessor))