-
Notifications
You must be signed in to change notification settings - Fork 78
feat: split command for api-gen package #2013
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
7 commits
Select commit
Hold shift + click to select a range
1cd8009
feat: first iteration split command
BrocksiNet 49cde75
Merge branch 'main' into feat/api-gen-split-command
patzick 0b2b43d
Apply suggestion from @Copilot
patzick 62bb64b
Apply suggestion from @Copilot
patzick da3ea68
Apply suggestion from @Copilot
patzick 8893add
Apply suggestion from @patzick
patzick 2ef86de
fix: tests
patzick 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 |
---|---|---|
|
@@ -30,4 +30,5 @@ jobs: | |
- name: Run benchmarks | ||
uses: CodSpeedHQ/action@v4 | ||
with: | ||
mode: "instrumentation" | ||
run: pnpm run test:bench |
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
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,157 @@ | ||
import { mkdirSync, writeFileSync } from "node:fs"; | ||
import { resolve } from "node:path"; | ||
import { bundle, lint, loadConfig } from "@redocly/openapi-core"; | ||
import { format } from "prettier"; | ||
import { | ||
createNewSchema, | ||
filterPathsByTag, | ||
getTags, | ||
getTagsFromPath, | ||
getUniquePaths, | ||
getUsedComponents, | ||
removeUnusedComponents, | ||
} from "../utils/schemaSplitter"; | ||
|
||
export type SplitOptions = { | ||
outputDir?: string; | ||
schemaFile: string; | ||
filterBy?: string; | ||
splitBy?: "tags" | "paths"; | ||
verboseLinting?: boolean; | ||
list?: "tags" | "paths"; | ||
}; | ||
|
||
export async function split(options: SplitOptions): Promise<void> { | ||
const { outputDir, schemaFile, filterBy, splitBy, verboseLinting, list } = | ||
options; | ||
|
||
if (!schemaFile) { | ||
throw new Error(`Schema file not found: ${schemaFile}`); | ||
} | ||
|
||
const finalSplitBy = splitBy || "tags"; | ||
|
||
const config = await loadConfig({ | ||
// TODO: add config if needed | ||
}); | ||
|
||
const document = await bundle({ | ||
ref: schemaFile, | ||
config, | ||
dereference: false, | ||
}); | ||
|
||
const problems = await lint({ | ||
ref: schemaFile, | ||
config, | ||
}); | ||
|
||
const errors = problems.filter((p) => p.severity === "error"); | ||
const warnings = problems.filter((p) => p.severity === "warn"); | ||
|
||
if (errors.length > 0 || warnings.length > 0) { | ||
console.error( | ||
`Schema has ${errors.length} errors and ${warnings.length} warnings.\n`, | ||
); | ||
if (verboseLinting) { | ||
console.error("Details:", problems); | ||
} | ||
} | ||
|
||
const paths = getUniquePaths(document.bundle.parsed); | ||
const tags = getTags(document.bundle.parsed); | ||
|
||
if (list) { | ||
if (list === "tags") { | ||
console.log( | ||
tags | ||
.map((t) => `"${t.name.toLowerCase()}"`) | ||
.sort() | ||
.join(", "), | ||
); | ||
} else if (list === "paths") { | ||
console.log( | ||
paths | ||
.map((p) => `"${p}"`) | ||
.sort() | ||
.join(", "), | ||
); | ||
} else { | ||
throw new Error(`Invalid list option: ${list}`); | ||
} | ||
return; | ||
} | ||
|
||
if (finalSplitBy !== "tags" && finalSplitBy !== "paths") { | ||
throw new Error(`Invalid splitBy option: ${finalSplitBy}`); | ||
} | ||
|
||
console.log(`Splitting by ${finalSplitBy}...`); | ||
|
||
if (finalSplitBy === "tags") { | ||
for (const tag of tags) { | ||
if (filterBy && tag.name.toLowerCase() !== filterBy.toLowerCase()) { | ||
continue; | ||
} | ||
|
||
const newSchema = createNewSchema(document.bundle.parsed); | ||
newSchema.paths = filterPathsByTag(document.bundle.parsed, tag.name); | ||
if (newSchema.info) { | ||
newSchema.info.title = `${newSchema.info.title} - ${tag.name}`; | ||
} | ||
newSchema.tags = [tag]; | ||
|
||
const usedComponents = getUsedComponents(newSchema); | ||
const finalSchema = removeUnusedComponents(newSchema, usedComponents); | ||
|
||
const fileName = `${tag.name.replace(/ /g, "-")}.json`.toLowerCase(); | ||
const outputPath = resolve(outputDir || "output", fileName); | ||
|
||
mkdirSync(outputDir || "output", { recursive: true }); | ||
const formattedSchema = await format( | ||
JSON.stringify(finalSchema, null, 2), | ||
{ | ||
parser: "json", | ||
}, | ||
); | ||
writeFileSync(outputPath, formattedSchema); | ||
console.log(`Generated ${outputPath}`); | ||
} | ||
} else if (finalSplitBy === "paths") { | ||
for (const path of paths) { | ||
if (filterBy && path !== filterBy) { | ||
continue; | ||
} | ||
|
||
const newSchema = createNewSchema(document.bundle.parsed); | ||
newSchema.paths = { | ||
[path]: document.bundle.parsed.paths?.[path], | ||
}; | ||
if (newSchema.info) { | ||
newSchema.info.title = `${newSchema.info.title} - ${path.replace( | ||
"/", | ||
"_", | ||
)}`; | ||
} | ||
newSchema.tags = getTagsFromPath(document.bundle.parsed, path); | ||
|
||
const usedComponents = getUsedComponents(newSchema); | ||
const finalSchema = removeUnusedComponents(newSchema, usedComponents); | ||
|
||
const fileName = `${path | ||
.replace(/[^a-zA-Z0-9]/g, "-") | ||
.replace(/^-/, "")}.json`.toLowerCase(); | ||
const outputPath = resolve(outputDir || "output", fileName); | ||
|
||
mkdirSync(outputDir || "output", { recursive: true }); | ||
const formattedSchema = await format( | ||
JSON.stringify(finalSchema, null, 2), | ||
{ | ||
parser: "json", | ||
}, | ||
); | ||
writeFileSync(outputPath, formattedSchema); | ||
console.log(`Generated ${outputPath}`); | ||
} | ||
} | ||
} |
Oops, something went wrong.
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.