The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | /** |
| 2 | * The `graphqlgen` command. |
| 3 | * |
| 4 | * `graphqlgen [--config <path>] [--watch | --check]` loads `graphql.config.ts` |
| 5 | * from the current directory, or the file `--config` names, generates every |
| 6 | * `.graphql.ts` module in memory with {@link Generator.generate} and writes |
| 7 | * the files whose bytes changed. Generated files whose `.graphql` source is |
| 8 | * gone are deleted. Any error diagnostic means nothing is written. |
| 9 | * |
| 10 | * With `--watch` it keeps running and regenerates whenever the config, the |
| 11 | * schema or a document changes. A failing cycle keeps the last good output. |
| 12 | * |
| 13 | * Exit codes: `0` on success, `1` when generation reports an error or |
| 14 | * `--check` finds out-of-date files, `2` for invalid CLI usage or when the |
| 15 | * config can't be loaded (with `--watch`, only at startup). |
| 16 | * |
| 17 | * @since 4.0.0 |
| 18 | */ |
| 19 | import * as CliError from "effect/cli/CliError" |
| 20 | import * as Command from "effect/cli/Command" |
| 21 | import * as Flag from "effect/cli/Flag" |
| 22 | import * as Console from "effect/Console" |
| 23 | import * as Data from "effect/Data" |
| 24 | import * as Effect from "effect/Effect" |
| 25 | import * as FileSystem from "effect/FileSystem" |
| 26 | import * as Option from "effect/Option" |
| 27 | import * as Path from "effect/Path" |
| 28 | import * as Runtime from "effect/Runtime" |
| 29 | import type * as Config from "./Config.ts" |
| 30 | import * as Generator from "./Generator.ts" |
| 31 | import { |
| 32 | type Changes, |
| 33 | ConfigLoadError, |
| 34 | displayPath, |
| 35 | errorSummary, |
| 36 | formatDiagnostic, |
| 37 | GenerateError, |
| 38 | loadConfig, |
| 39 | planChanges, |
| 40 | toGenerateError, |
| 41 | writeChanges |
| 42 | } from "./internal/Cli.ts" |
| 43 | import * as Watch from "./internal/Watch.ts" |
| 44 | |
| 45 | /** |
| 46 | * The config file could not be found, imported or decoded, or the generator |
| 47 | * rejected it, exiting with code `2`. Generation reported at least one error |
| 48 | * diagnostic, or a file could not be read or written, exiting with code `1`. |
| 49 | * |
| 50 | * @stability experimental |
| 51 | * @category errors |
| 52 | * @since 4.0.0 |
| 53 | */ |
| 54 | export { ConfigLoadError, GenerateError } from "./internal/Cli.ts" |
| 55 | |
| 56 | /** |
| 57 | * `--check` found generated files that would be created, changed or deleted. |
| 58 | * Exits with code `1`. |
| 59 | * |
| 60 | * @stability experimental |
| 61 | * @category errors |
| 62 | * @since 4.0.0 |
| 63 | */ |
| 64 | export class CheckError extends Data.TaggedError("CheckError")<{ |
| 65 | readonly message: string |
| 66 | }> { |
| 67 | override readonly [Runtime.errorExitCode] = 1 |
| 68 | override readonly [Runtime.errorReported] = false |
| 69 | } |
| 70 | |
| 71 | const config = Flag.String("config").pipe( |
| 72 | Flag.withAlias("c"), |
| 73 | Flag.withDescription("The config file to load (default: graphql.config.ts in the current directory)"), |
| 74 | Flag.optional |
| 75 | ) |
| 76 | |
| 77 | const watch = Flag.Boolean("watch").pipe( |
| 78 | Flag.withAlias("w"), |
| 79 | Flag.withDescription("Regenerate whenever the schema, a document or the config changes"), |
| 80 | Flag.withDefault(false) |
| 81 | ) |
| 82 | |
| 83 | const check = Flag.Boolean("check").pipe( |
| 84 | Flag.withDescription("Write nothing; list the files that are out of date and exit 1 if there are any"), |
| 85 | Flag.withDefault(false) |
| 86 | ) |
| 87 | |
| 88 | const root = Command.make("graphqlgen", { config, watch, check }).pipe( |
| 89 | Command.withHandler(Effect.fnUntraced(function*(flags) { |
| 90 | if (flags.watch && flags.check) { |
| 91 | return yield* new CliError.UserError({ cause: "--watch and --check cannot be used together." }) |
| 92 | } |
| 93 | const fs = yield* FileSystem.FileSystem |
| 94 | const path = yield* Path.Path |
| 95 | const configPath = path.resolve(Option.getOrElse(flags.config, () => "graphql.config.ts")) |
| 96 | if (flags.watch) { |
| 97 | return yield* Watch.run({ configPath, watch: Watch.fileSystemWatch(fs, path) }) |
| 98 | } |
| 99 | const loaded = yield* loadConfig(configPath) |
| 100 | const result = yield* generateFiles(loaded, path.dirname(configPath)) |
| 101 | const changes = yield* planChanges(result) |
| 102 | if (flags.check) return yield* reportCheck(changes) |
| 103 | yield* writeChanges(changes) |
| 104 | yield* listChanges(path, changes, { create: "created", update: "updated", delete: "deleted" }) |
| 105 | })) |
| 106 | ) |
| 107 | |
| 108 | /** |
| 109 | * Runs the `graphqlgen` command-line program. |
| 110 | * |
| 111 | * **Details** |
| 112 | * |
| 113 | * Diagnostics go to stderr as `file:line:col: error|warning: message` followed |
| 114 | * by a code frame. Written, deleted and, with `--check`, out-of-date files are |
| 115 | * listed on stdout. Failures carry their exit code through |
| 116 | * `Runtime.errorExitCode`. |
| 117 | * |
| 118 | * @stability experimental |
| 119 | * @category running |
| 120 | * @since 4.0.0 |
| 121 | */ |
| 122 | export const run: Effect.Effect< |
| 123 | void, |
| 124 | CliError.CliError | ConfigLoadError | GenerateError | CheckError, |
| 125 | Command.Environment |
| 126 | > = Command.run(root, { version: "0.0.0" }).pipe( |
| 127 | Effect.mapError((error) => |
| 128 | CliError.isCliError(error) && Runtime.getErrorExitCode(error) !== 0 |
| 129 | ? Object.assign(error, { [Runtime.errorExitCode]: 2 }) |
| 130 | : error |
| 131 | ), |
| 132 | // Command.run has already printed CLI errors. |
| 133 | Effect.tapError((error) => CliError.isCliError(error) ? Effect.void : Console.error(error.message)) |
| 134 | ) |
| 135 | |
| 136 | // ----------------------------------------------------------------------------- |
| 137 | // Single run |
| 138 | // ----------------------------------------------------------------------------- |
| 139 | |
| 140 | const generateFiles = Effect.fnUntraced(function*(config: Config.Config, cwd: string) { |
| 141 | const path = yield* Path.Path |
| 142 | const result = yield* Generator.generate(config, { cwd }).pipe( |
| 143 | Effect.catchTags({ |
| 144 | ConfigError: (error) => Effect.fail(new ConfigLoadError({ message: `error: ${error.message}` })), |
| 145 | PlatformError: (error) => Effect.fail(toGenerateError(error)) |
| 146 | }) |
| 147 | ) |
| 148 | for (const diagnostic of result.diagnostics) { |
| 149 | yield* Console.error(formatDiagnostic(path, cwd, diagnostic)) |
| 150 | } |
| 151 | const errors = result.diagnostics.filter((diagnostic) => diagnostic.severity === "error").length |
| 152 | if (errors > 0) { |
| 153 | return yield* new GenerateError({ message: errorSummary(errors) }) |
| 154 | } |
| 155 | return result |
| 156 | }) |
| 157 | |
| 158 | const reportCheck = Effect.fnUntraced(function*(changes: Changes) { |
| 159 | const path = yield* Path.Path |
| 160 | const count = changes.creates.length + changes.updates.length + changes.deletes.length |
| 161 | if (count === 0) return |
| 162 | yield* listChanges(path, changes, { create: "would create", update: "would update", delete: "would delete" }) |
| 163 | return yield* new CheckError({ |
| 164 | message: `${count} generated ${count === 1 ? "file is" : "files are"} out of date. Run graphqlgen to update ${ |
| 165 | count === 1 ? "it" : "them" |
| 166 | }.` |
| 167 | }) |
| 168 | }) |
| 169 | |
| 170 | const listChanges = ( |
| 171 | path: Path.Path, |
| 172 | changes: Changes, |
| 173 | verbs: { readonly create: string; readonly update: string; readonly delete: string } |
| 174 | ) => |
| 175 | Effect.forEach([ |
| 176 | ...changes.creates.map((file) => `${verbs.create} ${displayPath(path, file.path)}`), |
| 177 | ...changes.updates.map((file) => `${verbs.update} ${displayPath(path, file.path)}`), |
| 178 | ...changes.deletes.map((file) => `${verbs.delete} ${displayPath(path, file)}`) |
| 179 | ], (line) => |
| 180 | Console.log(line), { discard: true }) |