-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
280 lines (234 loc) · 9.93 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import * as trpcServer from '@trpc/server'
import * as cleye from 'cleye'
import colors from 'picocolors'
import {ZodError} from 'zod'
import {type JsonSchema7Type} from 'zod-to-json-schema'
import * as zodValidationError from 'zod-validation-error'
import {flattenedProperties, incompatiblePropertyPairs, getDescription} from './json-schema'
import {lineByLineConsoleLogger} from './logging'
import {AnyProcedure, AnyRouter, CreateCallerFactoryLike, isTrpc11Procedure} from './trpc-compat'
import {Logger, TrpcCliParams} from './types'
import {looksLikeInstanceof} from './util'
import {parseProcedureInputs} from './zod-procedure'
export * from './types'
export {z} from 'zod'
export * as zod from 'zod'
export * as trpcServer from '@trpc/server'
/** re-export of the @trpc/server package, just to avoid needing to install manually when getting started */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export {AnyRouter, AnyProcedure} from './trpc-compat'
export interface TrpcCli {
run: (params?: {argv?: string[]; logger?: Logger; process?: {exit: (code: number) => never}}) => Promise<void>
ignoredProcedures: {procedure: string; reason: string}[]
}
/**
* Run a trpc router as a CLI.
*
* @param router A trpc router
* @param context The context to use when calling the procedures - needed if your router requires a context
* @param alias A function that can be used to provide aliases for flags.
* @param default A procedure to use as the default command when the user doesn't specify one.
* @returns A CLI object with a `run` method that can be called to run the CLI. The `run` method will parse the command line arguments, call the appropriate trpc procedure, log the result and exit the process. On error, it will log the error and exit with a non-zero exit code.
*/
export function createCli<R extends AnyRouter>({router, ...params}: TrpcCliParams<R>): TrpcCli {
const procedures = Object.entries<AnyProcedure>(router._def.procedures as {}).map(([name, procedure]) => {
const procedureResult = parseProcedureInputs(procedure._def.inputs as unknown[])
if (!procedureResult.success) {
return [name, procedureResult.error] as const
}
const jsonSchema = procedureResult.value
const properties = flattenedProperties(jsonSchema.flagsSchema)
const incompatiblePairs = incompatiblePropertyPairs(jsonSchema.flagsSchema)
// trpc types are a bit of a lie - they claim to be `router._def.procedures.foo.bar` but really they're `router._def.procedures['foo.bar']`
const trpcProcedure = router._def.procedures[name] as AnyProcedure
let type: 'mutation' | 'query' | 'subscription'
if (isTrpc11Procedure(trpcProcedure)) {
type = trpcProcedure._def.type
} else if (trpcProcedure._def.mutation) {
type = 'mutation'
} else if (trpcProcedure._def.query) {
type = 'query'
} else if (trpcProcedure._def.subscription) {
type = 'subscription'
} else {
const keys = Object.keys(trpcProcedure._def).join(', ')
throw new Error(`Unknown procedure type for procedure object with keys ${keys}`)
}
return [name, {name, procedure, jsonSchema, properties, incompatiblePairs, type}] as const
})
const procedureEntries = procedures.flatMap(([k, v]) => {
return typeof v === 'string' ? [] : [[k, v] as const]
})
const procedureMap = Object.fromEntries(procedureEntries)
const ignoredProcedures = procedures.flatMap(([k, v]) => (typeof v === 'string' ? [{procedure: k, reason: v}] : []))
async function run(runParams?: {argv?: string[]; logger?: Logger; process?: {exit: (code: number) => never}}) {
const logger = {...lineByLineConsoleLogger, ...runParams?.logger}
const _process = runParams?.process || process
let verboseErrors: boolean = false
const cleyeCommands = procedureEntries.map(
([commandName, {procedure, jsonSchema, properties}]): CleyeCommandOptions => {
const flags = Object.fromEntries(
Object.entries(properties).map(([propertyKey, propertyValue]) => {
const cleyeType = getCleyeType(propertyValue)
let description: string | undefined = getDescription(propertyValue)
if ('required' in jsonSchema.flagsSchema && !jsonSchema.flagsSchema.required?.includes(propertyKey)) {
description = `${description} (optional)`.trim()
}
description ||= undefined
return [
propertyKey,
{
type: cleyeType,
description,
default: propertyValue.default as {},
},
]
}),
)
Object.entries(flags).forEach(([fullName, flag]) => {
const alias = params.alias?.(fullName, {command: commandName, flags})
if (alias) {
Object.assign(flag, {alias: alias})
}
})
return {
name: commandName,
help: procedure._def.meta,
parameters: jsonSchema.parameters,
flags: flags as {},
}
},
)
const defaultCommand = params.default && cleyeCommands.find(({name}) => name === params.default?.procedure)
const parsedArgv = cleye.cli(
{
flags: {
verboseErrors: {
type: Boolean,
description: `Throw raw errors (by default errors are summarised)`,
default: false,
},
},
...defaultCommand,
commands: cleyeCommands
.filter(cmd => cmd.name !== defaultCommand?.name)
.map(cmd => cleye.command(cmd)) as cleye.Command[],
},
undefined,
runParams?.argv,
)
const {verboseErrors: _verboseErrors, ...unknownFlags} = parsedArgv.unknownFlags as Record<string, unknown>
verboseErrors = _verboseErrors || parsedArgv.flags.verboseErrors
type Context = NonNullable<typeof params.context>
const createCallerFactory =
params.createCallerFactory ||
(trpcServer.initTRPC.context<Context>().create({}).createCallerFactory as CreateCallerFactoryLike)
const caller = createCallerFactory(router)(params.context)
const die: Fail = (message: string, {cause, help = true}: {cause?: unknown; help?: boolean} = {}) => {
if (verboseErrors !== undefined && verboseErrors) {
throw (cause as Error) || new Error(message)
}
logger.error?.(colors.red(message))
if (help) {
parsedArgv.showHelp()
}
return _process.exit(1)
}
let command = parsedArgv.command as string | undefined
if (!command && params.default) {
command = params.default.procedure as string
}
const procedureInfo = command && procedureMap[command]
if (!procedureInfo) {
const name = JSON.stringify(command || parsedArgv._[0])
const message = name ? `Command not found: ${name}.` : 'No command specified.'
return die(message)
}
if (Object.entries(unknownFlags).length > 0) {
const s = Object.entries(unknownFlags).length === 1 ? '' : 's'
return die(`Unexpected flag${s}: ${Object.keys(unknownFlags).join(', ')}`)
}
let {help, ...flags} = parsedArgv.flags
flags = Object.fromEntries(Object.entries(flags as {}).filter(([_k, v]) => v !== undefined)) // cleye returns undefined for flags which didn't receive a value
const incompatibleMessages = procedureInfo.incompatiblePairs
.filter(([a, b]) => a in flags && b in flags)
.map(([a, b]) => `--${a} and --${b} are incompatible and cannot be used together`)
if (incompatibleMessages?.length) {
return die(incompatibleMessages.join('\n'))
}
const input = procedureInfo.jsonSchema.getInput({_: parsedArgv._, flags}) as never
try {
const result: unknown = await (caller[procedureInfo.name] as Function)(input)
if (result) logger.info?.(result)
_process.exit(0)
} catch (err) {
throw transformError(err, die)
}
}
return {run, ignoredProcedures}
}
/** @deprecated renamed to `createCli` */
export const trpcCli = createCli
type Fail = (message: string, options?: {cause?: unknown; help?: boolean}) => never
function transformError(err: unknown, fail: Fail): unknown {
if (looksLikeInstanceof(err, Error) && err.message.includes('This is a client-only function')) {
return new Error('createCallerFactory version mismatch - pass in createCallerFactory explicitly', {cause: err})
}
if (looksLikeInstanceof(err, trpcServer.TRPCError)) {
const cause = err.cause
if (looksLikeInstanceof(cause, ZodError)) {
const originalIssues = cause.issues
try {
cause.issues = cause.issues.map(issue => {
if (typeof issue.path[0] !== 'string') return issue
return {
...issue,
path: ['--' + issue.path[0], ...issue.path.slice(1)],
}
})
const prettyError = zodValidationError.fromError(cause, {
prefixSeparator: '\n - ',
issueSeparator: '\n - ',
})
return fail(prettyError.message, {cause, help: true})
} finally {
cause.issues = originalIssues
}
}
if (err.code === 'INTERNAL_SERVER_ERROR') {
throw cause
}
if (err.code === 'BAD_REQUEST') {
return fail(err.message, {cause: err})
}
}
return err
}
type CleyeCommandOptions = cleye.Command['options']
type CleyeFlag = NonNullable<CleyeCommandOptions['flags']>[string]
function getCleyeType(schema: JsonSchema7Type): Extract<CleyeFlag, {type: unknown}>['type'] {
const _type = 'type' in schema && typeof schema.type === 'string' ? schema.type : null
switch (_type) {
case 'string': {
return String
}
case 'integer':
case 'number': {
return Number
}
case 'boolean': {
return Boolean
}
case 'array': {
return [String]
}
case 'object': {
return (s: string) => JSON.parse(s) as {}
}
default: {
_type satisfies 'null' | null // make sure we were exhaustive (forgot integer at one point)
return (value: unknown) => value
}
}
}