-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
443 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
import fs from "node:fs"; | ||
import path from "path"; | ||
import { getOpenApiTsOptionForArgs, getOutputPath } from "./argsHelper.ts"; | ||
import { ZodError } from "zod"; | ||
import { parseArgs, resolveConfig } from "./config.ts"; | ||
import { generateSchemas } from "./openapiTypescriptHelper.ts"; | ||
|
||
console.log("Received command: ", process.argv); | ||
|
||
// Access command-line arguments | ||
const args = process.argv.slice(2); | ||
|
||
const openApiTsOptions = getOpenApiTsOptionForArgs(args); | ||
|
||
const openApiPath = args[0]; | ||
const outputPath = getOutputPath(args); | ||
|
||
if (!openApiPath || !outputPath) { | ||
throw new Error("Both openApiPath and outputPath must be provided"); | ||
import { mkdirSync, writeFileSync } from "node:fs"; | ||
import { dirname } from "node:path"; | ||
|
||
try { | ||
// Access command-line arguments | ||
const config = await resolveConfig(parseArgs()); | ||
|
||
const contents = await generateSchemas(config); | ||
|
||
// Write the content to a file | ||
mkdirSync(dirname(config.output), { recursive: true }); | ||
writeFileSync(config.output, contents); | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
printConfigurationErrors(error); | ||
} | ||
} | ||
|
||
console.log("Starting OpenAPI TypeScript types generation..."); | ||
console.log(`\t-openApiPath: ${openApiPath}`); | ||
console.log(`\t-outputPath: ${outputPath}`); | ||
|
||
const contents = await generateSchemas(openApiPath, outputPath, openApiTsOptions); | ||
|
||
// Write the content to a file | ||
fs.mkdirSync(path.dirname(outputPath), { recursive: true }); | ||
fs.writeFileSync(outputPath, contents); | ||
function printConfigurationErrors(error: ZodError) { | ||
console.log("Invalid configuration:"); | ||
error.errors.forEach(issue => { | ||
console.log(` - ${issue.path.join(".")}: ${issue.message}`); | ||
}); | ||
console.log("Use --help to see available options."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { Command } from "commander"; | ||
import { loadConfig } from "c12"; | ||
import * as z from "zod"; | ||
import packageJson from "../package.json" with { type: "json" }; | ||
import type { OpenAPITSOptions as OriginalOpenAPITSOptions } from "openapi-typescript"; | ||
|
||
const CONFIG_FILE_DEFAULT = "create-schemas.config"; | ||
const OUTPUT_FILE_DEFAULT = "openapi-types.ts"; | ||
const ROOT_DEFAULT = "."; | ||
|
||
type OpenApiTsOptions = Omit<OriginalOpenAPITSOptions, "cwd" | "transform" | "postTransform" | "silent" | "version">; | ||
|
||
export interface UserConfig { | ||
root?: string; | ||
input?: string; | ||
output?: string; | ||
openApiTsOptions?: OpenApiTsOptions; | ||
} | ||
|
||
export interface InlineConfig extends UserConfig { | ||
configFile?: string; | ||
} | ||
|
||
const resolvedConfigSchema = z.object({ | ||
configFile: z.string(), | ||
root: z.string(), | ||
input: z.string(), | ||
output: z.string(), | ||
openApiTsOptions: z.custom<OpenApiTsOptions>().optional().default({}) | ||
}); | ||
|
||
export type ResolvedConfig = z.infer<typeof resolvedConfigSchema>; | ||
|
||
export function parseArgs(argv?: string[]): InlineConfig { | ||
const program = new Command(); | ||
|
||
program | ||
.name("create-schemas") | ||
.version(packageJson.version, "-v, --version", "display version number") | ||
.argument("[input]") | ||
.option("-c, --config <file>", "use specified config file", CONFIG_FILE_DEFAULT) | ||
.option("-i, --input <path>", "path to the OpenAPI schema file") | ||
.option("-o, --output <path>", "output file path", OUTPUT_FILE_DEFAULT) | ||
.option("--cwd <path>", "path to working directory", ROOT_DEFAULT) | ||
.helpOption("-h, --help", "display available CLI options") | ||
.parse(argv); | ||
|
||
const opts = program.opts(); | ||
const args = program.args; | ||
|
||
return { | ||
configFile: opts.config, | ||
root: opts.cwd, | ||
input: opts.input || args[0], | ||
output: opts.output | ||
}; | ||
} | ||
|
||
export async function resolveConfig(inlineConfig: InlineConfig = {}): Promise<ResolvedConfig> { | ||
const { configFile = CONFIG_FILE_DEFAULT, root = ROOT_DEFAULT } = inlineConfig; | ||
|
||
const { config } = await loadConfig<InlineConfig>({ | ||
configFile, | ||
cwd: root, | ||
omit$Keys: true, | ||
defaultConfig: { | ||
configFile: CONFIG_FILE_DEFAULT, | ||
root: ROOT_DEFAULT, | ||
output: OUTPUT_FILE_DEFAULT | ||
}, | ||
overrides: inlineConfig | ||
}); | ||
|
||
const resolvedConfig = resolvedConfigSchema.parse(config); | ||
|
||
return resolvedConfig; | ||
} | ||
|
||
export function defineConfig(config: UserConfig): UserConfig { | ||
return config; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export { | ||
defineConfig, | ||
resolveConfig, | ||
type UserConfig, | ||
type InlineConfig, | ||
type ResolvedConfig | ||
} from "./config.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { defineBuildConfig } from "@workleap/tsup-configs"; | ||
|
||
export default defineBuildConfig({ | ||
entry: ["src/bin.ts"], | ||
entry: ["src/bin.ts", "src/index.ts"], | ||
platform: "node" | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import { defineDevConfig } from "@workleap/tsup-configs"; | ||
|
||
export default defineDevConfig({ | ||
entry: ["src/bin.ts"], | ||
entry: ["src/bin.ts", "src/index.ts"], | ||
platform: "node" | ||
}); |
Oops, something went wrong.