Skip to content

Commit 7c5d03d

Browse files
committed
resolveOptions (updated 22:50)
1 parent 82b2734 commit 7c5d03d

File tree

4 files changed

+68
-63
lines changed

4 files changed

+68
-63
lines changed

packages/typegen/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"js-yaml": "^4.1.0",
5959
"strip-ansi": "^7.1.0",
6060
"ts-morph": "^21.0.1",
61+
"tsx": "^4.19.0",
6162
"vitest": "^1.2.2"
6263
}
6364
}

packages/typegen/src/defaults.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const getWithWarning = <T>(logger: Options['logger'], message: string, value: T)
3030
return value
3131
}
3232

33-
export const getParams = (partial: Partial<Options>): Options => {
33+
export const resolveOptions = (partial: Partial<Options>): Options => {
3434
const {
3535
logger = console,
3636
connectionString = getWithWarning(
@@ -64,8 +64,7 @@ export const getParams = (partial: Partial<Options>): Options => {
6464
`The 'glob' option is deprecated. Instead please use 'include', 'exclude' or 'since' respectively.`,
6565
)
6666

67-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
68-
assert.strictEqual(Object.keys(rest).length, 0, `Unexpected configuration keys: ${Object.keys(rest)}`)
67+
assert.strictEqual(Object.keys(rest).length, 0, `Unexpected configuration keys: ${Object.keys(rest).join(', ')}`)
6968

7069
assert.ok(!connectionString.includes(' \'"'), `Connection URI should not contain spaces or quotes`)
7170

packages/typegen/src/generate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {changedFiles, checkClean, containsIgnoreComment, globList, promiseDotOne
1818
export type {Options} from './types'
1919

2020
export const generate = async (inputOptions: Partial<Options>) => {
21-
const options = defaults.getParams(inputOptions)
21+
const options = defaults.resolveOptions(inputOptions)
2222
const logger = options.logger
2323

2424
const pool = createClient(options.connectionString, options.poolConfig)

packages/typegen/src/router.ts

Lines changed: 64 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,75 @@
1+
import {existsSync} from 'fs'
12
import {trpcServer, z, TrpcCliMeta} from 'trpc-cli'
23
import * as defaults from './defaults'
3-
import {generate} from './generate'
4+
import {generate, Options} from './generate'
45

56
const trpc = trpcServer.initTRPC.meta<TrpcCliMeta>().create()
67

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+
})
760
export const router = trpc.router({
861
generate: trpc.procedure
962
.meta({
1063
description: 'Scans source files for SQL queries and generates TypeScript interfaces for them.',
1164
})
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+
}),
7075
})

0 commit comments

Comments
 (0)