-
Notifications
You must be signed in to change notification settings - Fork 1
[IDP-1766] Improve configuration #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8540d8b
[IDP-1766] Improve configuration
tjosepo 14bdf5f
throw error if not zod error
tjosepo 13c8abf
remove default from cli args
tjosepo 695e648
Add test and changeset
tjosepo 88d8c48
lint
tjosepo 03bf958
remove rc support
tjosepo c880564
fix config path resolution
tjosepo 2a442e9
fix file path / URL issues
tjosepo 006873b
fix test
tjosepo 3b635bb
fix it again
tjosepo c8d6613
make test more compact, lint
tjosepo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,23 @@ | ||
--- | ||
"@workleap/create-schemas": minor | ||
--- | ||
|
||
[BREAKING] Major overhaul to configurations. | ||
|
||
The following command line arguments are no longer supported: | ||
|
||
- `--additionalProperties` | ||
- `--alphabetize` | ||
- `--arrayLength` | ||
- `--defaultNonNullable` | ||
- `--propertiesRequiredByDefault` | ||
- `--emptyObjectsUnknown` | ||
- `--enum` | ||
- `--enumValues` | ||
- `--excludeDeprecated` | ||
- `--exportType` | ||
- `--help` | ||
- `--immutable` | ||
- `--pathParamsAsTypes` | ||
|
||
These options now need to be declared in the `create-schemas.config.ts` file using the `openApiTsOptions` property. |
This file contains hidden or 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 hidden or 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,31 @@ | ||
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"; | ||
import { fileURLToPath } from "node:url"; | ||
|
||
try { | ||
// Access command-line arguments | ||
const config = await resolveConfig(parseArgs()); | ||
|
||
const contents = await generateSchemas(config); | ||
|
||
// Write the content to a file | ||
mkdirSync(dirname(fileURLToPath(config.output)), { recursive: true }); | ||
writeFileSync(fileURLToPath(config.output), contents); | ||
} catch (error) { | ||
if (error instanceof ZodError) { | ||
printConfigurationErrors(error); | ||
} else { | ||
throw 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 hidden or 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,89 @@ | ||
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"; | ||
import { toFullyQualifiedURL } from "./utils.ts"; | ||
|
||
const CONFIG_FILE_DEFAULT = "create-schemas.config"; | ||
const OUTPUT_FILE_DEFAULT = "openapi-types.ts"; | ||
const ROOT_DEFAULT = process.cwd(); | ||
|
||
type OpenApiTsOptions = Omit<OriginalOpenAPITSOptions, "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(), | ||
watch: z.boolean().optional().default(false), | ||
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") | ||
.option("-i, --input <path>", "path to the OpenAPI schema file") | ||
.option("-o, --output <path>", "output file path") | ||
.option("--watch", "watch for changes") | ||
.option("--cwd <path>", "path to working directory") | ||
.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, | ||
rcFile: false, | ||
omit$Keys: true, | ||
defaultConfig: { | ||
configFile: CONFIG_FILE_DEFAULT, | ||
root: ROOT_DEFAULT, | ||
output: OUTPUT_FILE_DEFAULT | ||
}, | ||
overrides: inlineConfig | ||
}); | ||
|
||
const resolvedConfig = resolvedConfigSchema.parse(config); | ||
|
||
resolvedConfig.root = toFullyQualifiedURL(root); | ||
resolvedConfig.input = toFullyQualifiedURL(resolvedConfig.input, resolvedConfig.root); | ||
resolvedConfig.output = toFullyQualifiedURL(resolvedConfig.output, resolvedConfig.root); | ||
|
||
return resolvedConfig; | ||
} | ||
|
||
export function defineConfig(config: UserConfig): UserConfig { | ||
return config; | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
import { isAbsolute, join } from "node:path"; | ||
import { fileURLToPath, pathToFileURL } from "node:url"; | ||
|
||
function isFileURLAsText(input: string | URL): input is `file://${string}` { | ||
tjosepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return typeof input === "string" && input.startsWith("file://"); | ||
} | ||
|
||
function isHttpURLAsText( | ||
tjosepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input: string | ||
): input is `http://${string}` | `https://${string}` { | ||
if (!URL.canParse(input)) { | ||
return false; | ||
} | ||
|
||
const url = new URL(input); | ||
|
||
return url.protocol === "http:" || url.protocol === "https:"; | ||
} | ||
|
||
/** | ||
* Converts a path into a fully qualified path URL. | ||
* | ||
* - `foo` -> `file:///path/to/cwd/foo` | ||
* - `https://example.com/foo` -> `https://example.com/foo` | ||
* | ||
* This format is works for both file path and HTTP URLs, but the result needs | ||
* to be converted to a `URL` instance or into a file path depending on the | ||
* usage. | ||
*/ | ||
export function toFullyQualifiedURL( | ||
tjosepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
input: string | URL, | ||
root: string | URL = process.cwd() | ||
): string { | ||
if (input instanceof URL) { | ||
return input.toString(); | ||
} | ||
|
||
if (isHttpURLAsText(input)) { | ||
return input; | ||
} | ||
|
||
if (isFileURLAsText(input)) { | ||
return input; | ||
} | ||
|
||
if (isAbsolute(input)) { | ||
return pathToFileURL(input).toString(); | ||
} | ||
|
||
if (root instanceof URL || isHttpURLAsText(root)) { | ||
const rootAsURL = new URL(root); | ||
|
||
const pathname = join(rootAsURL.pathname, input); | ||
|
||
return new URL(pathname, root).toString(); | ||
} | ||
|
||
if (isFileURLAsText(root)) { | ||
const rootAsPath = fileURLToPath(root); | ||
|
||
return pathToFileURL(join(rootAsPath, input)).toString(); | ||
} | ||
|
||
if (isAbsolute(root)) { | ||
return pathToFileURL(join(root, input)).toString(); | ||
} | ||
|
||
return pathToFileURL(join(process.cwd(), root, input)).toString(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.