Skip to content

Commit

Permalink
resolveOptions (updated 22:50)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Sep 5, 2024
1 parent 82b2734 commit 7c5d03d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 63 deletions.
1 change: 1 addition & 0 deletions packages/typegen/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"js-yaml": "^4.1.0",
"strip-ansi": "^7.1.0",
"ts-morph": "^21.0.1",
"tsx": "^4.19.0",
"vitest": "^1.2.2"
}
}
5 changes: 2 additions & 3 deletions packages/typegen/src/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const getWithWarning = <T>(logger: Options['logger'], message: string, value: T)
return value
}

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

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

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

Expand Down
2 changes: 1 addition & 1 deletion packages/typegen/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {changedFiles, checkClean, containsIgnoreComment, globList, promiseDotOne
export type {Options} from './types'

export const generate = async (inputOptions: Partial<Options>) => {
const options = defaults.getParams(inputOptions)
const options = defaults.resolveOptions(inputOptions)
const logger = options.logger

const pool = createClient(options.connectionString, options.poolConfig)
Expand Down
123 changes: 64 additions & 59 deletions packages/typegen/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,75 @@
import {existsSync} from 'fs'
import {trpcServer, z, TrpcCliMeta} from 'trpc-cli'
import * as defaults from './defaults'
import {generate} from './generate'
import {generate, Options} from './generate'

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

const Options = z.object({
config: z
.string()
.describe(
'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.',
)
.default(defaults.typegenConfigFile),
rootDir: z.string().describe('Path to the source directory containing SQL queries.').default(defaults.defaultRootDir),
connectionString: z
.string()
.describe('URL for connecting to postgres.') //
.default(defaults.defaultConnectionURI),
psql: z
.string()
.describe(
'psql command used to query postgres via CLI client. If using docker, you may want to use `docker-compose exec -T postgres psql`',
)
.default(defaults.defaultPsqlCommand),
defaultType: z
.string()
.describe('TypeScript fallback type for when no type is found.')
.default(defaults.defaultTypeScriptType),
include: z
.array(z.string())
.describe('Glob pattern of files to search for SQL queries in.')
.default(defaults.defaultIncludePatterns),
exclude: z
.array(z.string())
.describe('Glob pattern for files to be excluded from processing.')
.default(defaults.defaultExcludePatterns),
since: z
.string()
.optional()
.describe('Limit affected files to those which have been changed since the given git ref.'),
migrate: z
.enum(['<=0.8.0'])
.optional()
.describe('Before generating types, attempt to migrate a codebase which has used a prior version of this tool.'),
skipCheckClean: z
.boolean()
.optional()
.describe('If enabled, the tool will not check the git status to ensure changes are checked in.'),
watch: z
.boolean()
.optional()
.describe(
'Run the type checker in watch mode. Files will be run through the code generator when changed or added.',
),
lazy: z.boolean().optional().describe('Skip initial processing of input files. Only useful with --watch.'),
} satisfies {
[K in keyof Options & {config: unknown}]: unknown
})
export const router = trpc.router({
generate: trpc.procedure
.meta({
description: 'Scans source files for SQL queries and generates TypeScript interfaces for them.',
})
.input(
z.object({
config: z
.string()
.describe(
'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.',
)
.default(defaults.typegenConfigFile),
rootDir: z
.string()
.describe('Path to the source directory containing SQL queries.')
.default(defaults.defaultRootDir),
connectionString: z
.string()
.describe('URL for connecting to postgres.') //
.default(defaults.defaultConnectionURI),
psql: z
.string()
.describe(
'psql command used to query postgres via CLI client. If using docker, you may want to use `docker-compose exec -T postgres psql`',
)
.default(defaults.defaultPsqlCommand),
defaultType: z
.string()
.describe('TypeScript fallback type for when no type is found.')
.default(defaults.defaultTypeScriptType),
include: z
.array(z.string())
.describe('Glob pattern of files to search for SQL queries in.')
.default(defaults.defaultIncludePatterns),
exclude: z
.array(z.string())
.describe('Glob pattern for files to be excluded from processing.')
.default(defaults.defaultExcludePatterns),
since: z
.string()
.optional()
.describe('Limit affected files to those which have been changed since the given git ref.'),
migrate: z
.enum(['<=0.8.0'])
.optional()
.describe(
'Before generating types, attempt to migrate a codebase which has used a prior version of this tool.',
),
skipCheckClean: z
.boolean()
.optional()
.describe('If enabled, the tool will not check the git status to ensure changes are checked in.'),
watch: z
.boolean()
.optional()
.describe(
'Run the type checker in watch mode. Files will be run through the code generator when changed or added.',
),
lazy: z.boolean().optional().describe('Skip initial processing of input files. Only useful with --watch.'),
}),
)
.mutation(async ({input}) => generate(input)),
.input(Options)
.mutation(async ({input: {config, psql, ...input}}) => {
const configModule = config && existsSync(config) ? ((await import(config)) as Record<string, unknown>) : null
const baseOptions = configModule ? Options.parse(configModule?.default ?? configModule) : {}
return generate({
...baseOptions,
...(psql && {psql}),
...input,
})
}),
})

0 comments on commit 7c5d03d

Please sign in to comment.