| 1 | /** |
| 2 | * Defines the `codegen` CLI command for regenerating annotated barrel files in |
| 3 | * the utilities package. The command wires together the code generation and |
| 4 | * globbing services, resolves the requested working directory, discovers |
| 5 | * `@barrel` annotations, and rewrites matching barrel exports. |
| 6 | * |
| 7 | * @since 4.0.0 |
| 8 | */ |
| 9 | import * as Command from "effect/cli/Command" |
| 10 | import * as Flag from "effect/cli/Flag" |
| 11 | import * as Effect from "effect/Effect" |
| 12 | import * as Layer from "effect/Layer" |
| 13 | import * as Path from "effect/Path" |
| 14 | import * as Codegen from "../Codegen.ts" |
| 15 | import * as Glob from "../Glob.ts" |
| 16 | |
| 17 | const CodegenLayer = Layer.provideMerge(Codegen.layer, Glob.layer) |
| 18 | |
| 19 | /** |
| 20 | * CLI command that regenerates annotated barrel files. |
| 21 | * |
| 22 | * **Details** |
| 23 | * |
| 24 | * The command scans `cwd` for files matching `pattern`, finds `@barrel` |
| 25 | * annotations, and rewrites each annotated barrel with generated exports from |
| 26 | * matching modules. |
| 27 | * |
| 28 | * @category commands |
| 29 | * @since 4.0.0 |
| 30 | */ |
| 31 | export const codegen = Command.make("codegen", { |
| 32 | cwd: Flag.Directory("cwd", { mustExist: true }).pipe(Flag.withDefault(".")), |
| 33 | pattern: Flag.String("pattern").pipe(Flag.withDefault("src/**/index.ts")) |
| 34 | }, (config) => |
| 35 | Effect.gen(function*() { |
| 36 | const path = yield* Path.Path |
| 37 | const generator = yield* Codegen.BarrelGenerator |
| 38 | const files = yield* generator.discoverFiles(config.pattern, path.resolve(config.cwd)) |
| 39 | |
| 40 | yield* Effect.forEach(files, (file) => generator.processFile(file), { |
| 41 | concurrency: "unbounded", |
| 42 | discard: true |
| 43 | }) |
| 44 | })).pipe(Command.provide(CodegenLayer)) |