This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add bundle command * docs(changeset): feat: add bundle command * chore: clean up * chore: code style
- Loading branch information
Showing
6 changed files
with
79 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@scalar/cli": patch | ||
--- | ||
|
||
feat: add bundle command |
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
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,57 @@ | ||
import fs from 'node:fs' | ||
import { Command } from 'commander' | ||
import kleur from 'kleur' | ||
import type { OpenAPI } from 'openapi-types' | ||
import { loadOpenApiFile, useGivenFileOrConfiguration } from '../../utils' | ||
|
||
export function BundleCommand() { | ||
const cmd = new Command('bundle') | ||
|
||
cmd.description('Resolve all references in an OpenAPI file') | ||
cmd.argument('[file]', 'file to bundle') | ||
cmd.option('-o, --output <file>', 'output file') | ||
cmd.action(async (fileArgument: string) => { | ||
const { output } = cmd.opts() | ||
|
||
const startTime = performance.now() | ||
|
||
const file = useGivenFileOrConfiguration(fileArgument) | ||
|
||
const newContent = (await loadOpenApiFile(file)) | ||
.specification as OpenAPI.Document | ||
|
||
// Replace file content with newContent | ||
const cache = [] | ||
const json = JSON.stringify( | ||
newContent, | ||
(key, value) => { | ||
if (typeof value === 'object' && value !== null) { | ||
if (cache.indexOf(value) !== -1) { | ||
// Circular reference found, discard key | ||
return | ||
} | ||
// Store value in our collection | ||
cache.push(value) | ||
} | ||
return value | ||
}, | ||
2, | ||
) | ||
|
||
fs.writeFileSync(output ?? file, json, 'utf8') | ||
|
||
const endTime = performance.now() | ||
|
||
console.log( | ||
kleur.green('OpenAPI Schema bundled'), | ||
kleur.grey( | ||
`in ${kleur.white( | ||
`${kleur.bold(`${Math.round(endTime - startTime)}`)} ms`, | ||
)}`, | ||
), | ||
) | ||
console.log() | ||
}) | ||
|
||
return cmd | ||
} |
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