1 | #!/usr/bin/env node
|
2 |
|
3 | import { Command } from '@molt/command'
|
4 | import * as Path from 'node:path'
|
5 | import { z } from 'zod'
|
6 | import { generateFiles } from '../layers/2_generator/files.js'
|
7 |
|
8 | const args = Command.create().description(`Generate a type safe GraphQL client.`)
|
9 | .parameter(`schema`, z.string().min(1).describe(`File path to where your GraphQL schema is.`))
|
10 | .parameter(
|
11 | `output`,
|
12 | z.string().min(1).describe(
|
13 | `Directory path for where to output the generated TypeScript files.`,
|
14 | ),
|
15 | )
|
16 | .parametersExclusive(
|
17 | `schemaErrorType`,
|
18 | $ =>
|
19 | $.parameter(
|
20 | `schemaErrorTypes`,
|
21 | z.boolean().describe(
|
22 | `Use the schema error types pattern. All object types whose name starts with "Error" will be considered to be error types. If you want to specify a custom name pattern then use the other parameter "schemaErrorTypePattern".`,
|
23 | ),
|
24 | )
|
25 | .parameter(
|
26 | `schemaErrorTypePattern`,
|
27 | z.string().min(1).describe(
|
28 | `Designate objects whose name matches this JS regular expression as being error types in your schema.`,
|
29 | ),
|
30 | ).default(`schemaErrorTypes`, true),
|
31 | )
|
32 | .parameter(`format`, z.boolean().describe(`Format the generated files using dprint.`).default(true))
|
33 | .settings({
|
34 | parameters: {
|
35 | environment: false,
|
36 | },
|
37 | })
|
38 | .parse()
|
39 |
|
40 | await generateFiles({
|
41 | sourceDirPath: Path.dirname(args.schema),
|
42 | outputDirPath: args.output,
|
43 | format: args.format,
|
44 | errorTypeNamePattern: args.schemaErrorType._tag === `schemaErrorTypePattern`
|
45 | ? new RegExp(args.schemaErrorType.value)
|
46 | : args.schemaErrorType.value
|
47 | ? /^Error.+/
|
48 | : undefined,
|
49 | })
|