|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | + |
| 5 | +const __filename = fileURLToPath(import.meta.url) |
| 6 | +const __dirname = path.dirname(__filename) |
| 7 | +const NUM_ROUTERS = 500 |
| 8 | +const ROUTER_DIR = path.resolve(__dirname, '../src/router') |
| 9 | +const CONSUME_DIR = path.resolve(__dirname, '../src/consume') |
| 10 | + |
| 11 | +const PROCEDURE_1 = `procedure.input({} as Schema<{ who: string }, { who: string }>).query(({input}) => \`hello \${input.who}\`)` |
| 12 | +const PROCEDURE_2 = `procedure.input({} as Schema<{ id: string }, { id: string }>).mutation(({input}) => ({ id: input.id }))` |
| 13 | + |
| 14 | +function createRouter(routerName: string): string { |
| 15 | + return ` |
| 16 | +import { procedure, router, Schema } from '../trpc'; |
| 17 | +
|
| 18 | +export const ${routerName} = router({ |
| 19 | + procedure_1: ${PROCEDURE_1}, |
| 20 | + procedure_2: ${PROCEDURE_2}, |
| 21 | + nested: router({ |
| 22 | + procedure_1: ${PROCEDURE_1}, |
| 23 | + procedure_2: ${PROCEDURE_2}, |
| 24 | + nested: router({ |
| 25 | + procedure_1: ${PROCEDURE_1}, |
| 26 | + procedure_2: ${PROCEDURE_2} |
| 27 | + }), |
| 28 | + }), |
| 29 | +})`.trim() |
| 30 | +} |
| 31 | + |
| 32 | +function createConsume(routerName: string): string { |
| 33 | + return ` |
| 34 | +import { client } from '../client'; |
| 35 | +
|
| 36 | +const procedure_1: string = await client.${routerName}.procedure_1.query({ who: 'world' }); |
| 37 | +const procedure_2: { id: string } = await client.${routerName}.procedure_2.mutate({ id: '123' }); |
| 38 | +const nested_procedure_1: string = await client.${routerName}.nested.procedure_1.query({ who: 'world' }); |
| 39 | +const nested_procedure_2: { id: string } = await client.${routerName}.nested.procedure_2.mutate({ id: '123' }); |
| 40 | +const nested_nested_procedure_1: string = await client.${routerName}.nested.nested.procedure_1.query({ who: 'world' }); |
| 41 | +const nested_nested_procedure_2: { id: string } = await client.${routerName}.nested.nested.procedure_2.mutate({ id: '123' }); |
| 42 | +`.trim() |
| 43 | +} |
| 44 | + |
| 45 | +const routerNames = Array.from({ length: NUM_ROUTERS }, (_, i) => `router_${i}`) |
| 46 | + |
| 47 | +await fs.mkdir(ROUTER_DIR, { recursive: true }) |
| 48 | +await fs.mkdir(CONSUME_DIR, { recursive: true }) |
| 49 | + |
| 50 | +routerNames.forEach(async (routerName) => { |
| 51 | + await fs.writeFile(path.join(ROUTER_DIR, `${routerName}.ts`), createRouter(routerName)) |
| 52 | + await fs.writeFile(path.join(CONSUME_DIR, `${routerName}.ts`), createConsume(routerName)) |
| 53 | +}) |
| 54 | + |
| 55 | +const routerIndex = ` |
| 56 | +import { router as routerFn } from '../trpc' |
| 57 | +${routerNames.map(routerName => `import { ${routerName} } from './${routerName}';`).join('\n')} |
| 58 | +
|
| 59 | +export const router = routerFn({ |
| 60 | + ${routerNames.map(routerName => `${routerName},`).join('\n ')} |
| 61 | +}) |
| 62 | +` |
| 63 | + |
| 64 | +await fs.writeFile(path.join(ROUTER_DIR, 'index.ts'), routerIndex) |
0 commit comments