Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

feat: bundle #11

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/flat-comics-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@scalar/cli": patch
---

feat: add bundle command
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Command-line interface to work with OpenAPI files
* Upload your OpenAPI files to Scalar
* Get a fully mocked API for testing purposes
* Preview your API reference
* Bundle multiple OpenAPI files

## Quickstart

Expand All @@ -34,8 +35,9 @@ scalar --version
scalar init
scalar format openapi.json
scalar validate openapi.json
scalar reference openapi.json
scalar mock openapi.json
scalar bundle openapi.json --output bundle.json
scalar reference openapi.json --watch
scalar mock openapi.json --watch
scalar share openapi.json
```

Expand Down
10 changes: 10 additions & 0 deletions packages/cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ You can also change the port like this:
scalar mock openapi.json --watch --port 8080
```

### bundle

Some OpenAPI files reference other files from the file system or an URL. You can bundle those files and make them a single file:

```bash
scalar bundle openapi.json --output bundle.json
```

If you don’t provide an `output` file name, the input file will be overwritten.

### init

If you’re tired of passing the file name again and again, just configure it once:
Expand Down
57 changes: 57 additions & 0 deletions packages/cli/src/commands/bundle/BundleCommand.ts
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
}
1 change: 1 addition & 0 deletions packages/cli/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './bundle/BundleCommand'
export * from './format/FormatCommand'
export * from './init/InitCommand'
export * from './mock/MockCommand'
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Command } from 'commander'
import { version } from '../package.json'
import {
BundleCommand,
FormatCommand,
InitCommand,
MockCommand,
Expand All @@ -23,6 +24,7 @@ program.addCommand(InitCommand(), {
})
program.addCommand(FormatCommand())
program.addCommand(ValidateCommand())
program.addCommand(BundleCommand())
program.addCommand(ReferenceCommand())
program.addCommand(MockCommand())
program.addCommand(ShareCommand())
Expand Down
Loading