|
1 | | -import SwaggerParser from "@apidevtools/swagger-parser"; |
2 | 1 | import { cac } from "cac"; |
3 | | -import type { OpenAPIObject } from "openapi3-ts/oas31"; |
4 | | -import { basename, join } from "pathe"; |
5 | | -import { type } from "arktype"; |
6 | 2 |
|
7 | | -import { writeFile } from "fs/promises"; |
8 | | -import { allowedRuntimes, generateFile } from "./generator.ts"; |
9 | | -import { mapOpenApiEndpoints } from "./map-openapi-endpoints.ts"; |
10 | | -import { generateTanstackQueryFile } from "./tanstack-query.generator.ts"; |
11 | 3 | import { readFileSync } from "fs"; |
12 | | -import { prettify } from "./format.ts"; |
13 | | -import { dirname } from "path"; |
| 4 | +import { generateClientFiles } from "./generate-client-files.ts"; |
| 5 | +import { allowedRuntimes } from "./generator.ts"; |
14 | 6 |
|
15 | 7 | const { name, version } = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8")); |
16 | | -const cwd = process.cwd(); |
17 | 8 | const cli = cac(name); |
18 | | -const now = new Date(); |
19 | | - |
20 | | -const optionsSchema = type({ "output?": "string", runtime: allowedRuntimes, tanstack: "boolean | string" }); |
21 | 9 |
|
22 | 10 | cli |
23 | 11 | .command("<input>", "Generate") |
|
29 | 17 | ) |
30 | 18 | .option("--tanstack [name]", "Generate tanstack client, defaults to false, can optionally specify a name for the generated file") |
31 | 19 | .action(async (input, _options) => { |
32 | | - const options = optionsSchema.assert(_options); |
33 | | - const openApiDoc = (await SwaggerParser.bundle(input)) as OpenAPIObject; |
34 | | - |
35 | | - const ctx = mapOpenApiEndpoints(openApiDoc); |
36 | | - console.log(`Found ${ctx.endpointList.length} endpoints`); |
37 | | - |
38 | | - const content = await prettify(generateFile({ ...ctx, runtime: options.runtime })); |
39 | | - const outputPath = join( |
40 | | - cwd, |
41 | | - options.output ?? input + `.${options.runtime === "none" ? "client" : options.runtime}.ts`, |
42 | | - ); |
43 | | - |
44 | | - console.log("Generating client...", outputPath); |
45 | | - await writeFile(outputPath, content); |
46 | | - |
47 | | - if (options.tanstack) { |
48 | | - const tanstackContent = await generateTanstackQueryFile({ |
49 | | - ...ctx, |
50 | | - relativeApiClientPath: './' + basename(outputPath), |
51 | | - }); |
52 | | - const tanstackOutputPath = join(dirname(outputPath), typeof options.tanstack === "string" ? options.tanstack : `tanstack.client.ts`); |
53 | | - console.log("Generating tanstack client...", tanstackOutputPath); |
54 | | - await writeFile(tanstackOutputPath, tanstackContent); |
55 | | - } |
56 | | - |
57 | | - console.log(`Done in ${new Date().getTime() - now.getTime()}ms !`); |
| 20 | + return generateClientFiles(input, _options); |
58 | 21 | }); |
59 | 22 |
|
60 | 23 | cli.help(); |
|
0 commit comments