|
| 1 | +import {existsSync} from 'fs' |
1 | 2 | import {trpcServer, z, TrpcCliMeta} from 'trpc-cli'
|
2 | 3 | import * as defaults from './defaults'
|
3 |
| -import {generate} from './generate' |
| 4 | +import {generate, Options} from './generate' |
4 | 5 |
|
5 | 6 | const trpc = trpcServer.initTRPC.meta<TrpcCliMeta>().create()
|
6 | 7 |
|
| 8 | +const Options = z.object({ |
| 9 | + config: z |
| 10 | + .string() |
| 11 | + .describe( |
| 12 | + 'Path to a module containing parameters to be passed to generate. Note: any options passed on the command line will override those in the config file.', |
| 13 | + ) |
| 14 | + .default(defaults.typegenConfigFile), |
| 15 | + rootDir: z.string().describe('Path to the source directory containing SQL queries.').default(defaults.defaultRootDir), |
| 16 | + connectionString: z |
| 17 | + .string() |
| 18 | + .describe('URL for connecting to postgres.') // |
| 19 | + .default(defaults.defaultConnectionURI), |
| 20 | + psql: z |
| 21 | + .string() |
| 22 | + .describe( |
| 23 | + 'psql command used to query postgres via CLI client. If using docker, you may want to use `docker-compose exec -T postgres psql`', |
| 24 | + ) |
| 25 | + .default(defaults.defaultPsqlCommand), |
| 26 | + defaultType: z |
| 27 | + .string() |
| 28 | + .describe('TypeScript fallback type for when no type is found.') |
| 29 | + .default(defaults.defaultTypeScriptType), |
| 30 | + include: z |
| 31 | + .array(z.string()) |
| 32 | + .describe('Glob pattern of files to search for SQL queries in.') |
| 33 | + .default(defaults.defaultIncludePatterns), |
| 34 | + exclude: z |
| 35 | + .array(z.string()) |
| 36 | + .describe('Glob pattern for files to be excluded from processing.') |
| 37 | + .default(defaults.defaultExcludePatterns), |
| 38 | + since: z |
| 39 | + .string() |
| 40 | + .optional() |
| 41 | + .describe('Limit affected files to those which have been changed since the given git ref.'), |
| 42 | + migrate: z |
| 43 | + .enum(['<=0.8.0']) |
| 44 | + .optional() |
| 45 | + .describe('Before generating types, attempt to migrate a codebase which has used a prior version of this tool.'), |
| 46 | + skipCheckClean: z |
| 47 | + .boolean() |
| 48 | + .optional() |
| 49 | + .describe('If enabled, the tool will not check the git status to ensure changes are checked in.'), |
| 50 | + watch: z |
| 51 | + .boolean() |
| 52 | + .optional() |
| 53 | + .describe( |
| 54 | + 'Run the type checker in watch mode. Files will be run through the code generator when changed or added.', |
| 55 | + ), |
| 56 | + lazy: z.boolean().optional().describe('Skip initial processing of input files. Only useful with --watch.'), |
| 57 | +} satisfies { |
| 58 | + [K in keyof Options & {config: unknown}]: unknown |
| 59 | +}) |
7 | 60 | export const router = trpc.router({
|
8 | 61 | generate: trpc.procedure
|
9 | 62 | .meta({
|
10 | 63 | description: 'Scans source files for SQL queries and generates TypeScript interfaces for them.',
|
11 | 64 | })
|
12 |
| - .input( |
13 |
| - z.object({ |
14 |
| - config: z |
15 |
| - .string() |
16 |
| - .describe( |
17 |
| - 'Path to a module containing parameters to be passed to generate. Note: any options passed on the command line will override those in the config file.', |
18 |
| - ) |
19 |
| - .default(defaults.typegenConfigFile), |
20 |
| - rootDir: z |
21 |
| - .string() |
22 |
| - .describe('Path to the source directory containing SQL queries.') |
23 |
| - .default(defaults.defaultRootDir), |
24 |
| - connectionString: z |
25 |
| - .string() |
26 |
| - .describe('URL for connecting to postgres.') // |
27 |
| - .default(defaults.defaultConnectionURI), |
28 |
| - psql: z |
29 |
| - .string() |
30 |
| - .describe( |
31 |
| - 'psql command used to query postgres via CLI client. If using docker, you may want to use `docker-compose exec -T postgres psql`', |
32 |
| - ) |
33 |
| - .default(defaults.defaultPsqlCommand), |
34 |
| - defaultType: z |
35 |
| - .string() |
36 |
| - .describe('TypeScript fallback type for when no type is found.') |
37 |
| - .default(defaults.defaultTypeScriptType), |
38 |
| - include: z |
39 |
| - .array(z.string()) |
40 |
| - .describe('Glob pattern of files to search for SQL queries in.') |
41 |
| - .default(defaults.defaultIncludePatterns), |
42 |
| - exclude: z |
43 |
| - .array(z.string()) |
44 |
| - .describe('Glob pattern for files to be excluded from processing.') |
45 |
| - .default(defaults.defaultExcludePatterns), |
46 |
| - since: z |
47 |
| - .string() |
48 |
| - .optional() |
49 |
| - .describe('Limit affected files to those which have been changed since the given git ref.'), |
50 |
| - migrate: z |
51 |
| - .enum(['<=0.8.0']) |
52 |
| - .optional() |
53 |
| - .describe( |
54 |
| - 'Before generating types, attempt to migrate a codebase which has used a prior version of this tool.', |
55 |
| - ), |
56 |
| - skipCheckClean: z |
57 |
| - .boolean() |
58 |
| - .optional() |
59 |
| - .describe('If enabled, the tool will not check the git status to ensure changes are checked in.'), |
60 |
| - watch: z |
61 |
| - .boolean() |
62 |
| - .optional() |
63 |
| - .describe( |
64 |
| - 'Run the type checker in watch mode. Files will be run through the code generator when changed or added.', |
65 |
| - ), |
66 |
| - lazy: z.boolean().optional().describe('Skip initial processing of input files. Only useful with --watch.'), |
67 |
| - }), |
68 |
| - ) |
69 |
| - .mutation(async ({input}) => generate(input)), |
| 65 | + .input(Options) |
| 66 | + .mutation(async ({input: {config, psql, ...input}}) => { |
| 67 | + const configModule = config && existsSync(config) ? ((await import(config)) as Record<string, unknown>) : null |
| 68 | + const baseOptions = configModule ? Options.parse(configModule?.default ?? configModule) : {} |
| 69 | + return generate({ |
| 70 | + ...baseOptions, |
| 71 | + ...(psql && {psql}), |
| 72 | + ...input, |
| 73 | + }) |
| 74 | + }), |
70 | 75 | })
|
0 commit comments