Skip to content
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

feat(medusa-oas-cli): updated redocly-cli to v1.7 #6211

Merged
merged 2 commits into from
Jan 25, 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/strong-walls-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa-oas-cli": minor
---

feat(medusa-oas-cli): updated redocly-cli to v1.7
14 changes: 7 additions & 7 deletions packages/oas/medusa-oas-cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,53 +167,53 @@ Specify in which directory should the files be outputted. Accepts relative and a
If the directory doesn't exist, it will be created. Defaults to `./`.

```bash
yarn medusa-oas docs --out-dir ./docs`
yarn medusa-oas docs --src-file ./store.oas.json --out-dir ./docs`
```

#### `--config <path>`

Specify the path to a Redocly config file.

```bash
yarn medusa-oas --config ./redocly-config.yaml
yarn medusa-oas --src-file ./store.oas.json --config ./redocly-config.yaml
```

#### `--dry-run`

Will sanitize the OAS but will not output file. Useful for troubleshooting circular reference issues.

```bash
yarn medusa-oas docs --dry-run
yarn medusa-oas docs --src-file ./store.oas.json --dry-run
```

#### `--clean`

Delete destination directory content before generating client.

```bash
yarn medusa-oas docs --clean
yarn medusa-oas docs --src-file ./store.oas.json --clean
```

#### `--split`

Creates a multi-file structure output. Uses `redocly split` internally.

```bash
yarn medusa-oas docs --split
yarn medusa-oas docs --src-file ./store.oas.json --split
```

#### `--preview`

Generate a preview of the API documentation in a browser. Does not output files. Uses `redocly preview-docs` internally.

```bash
yarn medusa-oas docs --preview
yarn medusa-oas docs --src-file ../../../www/apps/api-reference/specs/store.oas.json --preview
```

#### `--html`

Generate a zero-dependency static HTML file. Uses `redocly build-docs` internally.

```bash
yarn medusa-oas docs --html
yarn medusa-oas docs --src-file ./store.oas.json --html
```
6 changes: 4 additions & 2 deletions packages/oas/medusa-oas-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"author": "Medusa",
"license": "MIT",
"devDependencies": {
"@types/js-yaml": "^4.0.9",
"jest": "^29.1.0",
"js-yaml": "^4.1.0",
"ts-jest": "^29.1.0",
Expand All @@ -40,12 +41,13 @@
"@medusajs/utils": "^1.10.5",
"@readme/json-schema-ref-parser": "^1.2.0",
"@readme/openapi-parser": "^2.4.0",
"@redocly/cli": "1.0.0-beta.123",
"@redocly/cli": "^1.7.0",
"@types/lodash": "^4.14.191",
"commander": "^10.0.0",
"execa": "1.0.0",
"lodash": "^4.17.21",
"openapi3-ts": "^3.1.2",
"swagger-inline": "^6.1.0"
"swagger-inline": "^6.1.0",
"yargs": "^17.7.2"
}
}
18 changes: 10 additions & 8 deletions packages/oas/medusa-oas-cli/src/command-docs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { previewDocs } from "@redocly/cli/lib/commands/preview-docs"
import { PreviewDocsOptions, previewDocs } from "@redocly/cli/lib/commands/preview-docs"
import { commandWrapper } from "@redocly/cli/lib/wrapper"
import { Command, Option, OptionValues } from "commander"
import execa from "execa"
import fs, { mkdir } from "fs/promises"
Expand All @@ -11,7 +12,8 @@ import {
} from "./utils/circular-patch-utils"
import { getTmpDirectory, isFile } from "./utils/fs-utils"
import { readJson } from "./utils/json-utils"
import { jsonFileToYamlFile, readYaml, writeYaml } from "./utils/yaml-utils"
import { jsonFileToYamlFile, readYaml, writeYaml, writeYamlFromJson } from "./utils/yaml-utils"
import yargs from "yargs"

/**
* Constants
Expand Down Expand Up @@ -131,7 +133,7 @@ export async function execute(cliParams: OptionValues): Promise<void> {
await mkdir(outDir, { recursive: true })
}

const srcFileSanitized = path.resolve(tmpDir, "tmp.oas.json")
const srcFileSanitized = path.resolve(tmpDir, "tmp.oas.yaml")
await sanitizeOAS(srcFile, srcFileSanitized, configTmpFile)
await circularReferenceCheck(srcFileSanitized)

Expand All @@ -146,7 +148,7 @@ export async function execute(cliParams: OptionValues): Promise<void> {
if (shouldSplit) {
await generateReference(srcFileSanitized, outDir)
} else {
await jsonFileToYamlFile(srcFileSanitized, path.join(outDir, finalOASFile))
await writeYaml(path.join(outDir, finalOASFile), await fs.readFile(srcFileSanitized, "utf8"))
}
if (shouldBuildHTML) {
const outHTMLFile = path.resolve(outDir, "index.html")
Expand Down Expand Up @@ -186,7 +188,7 @@ const mergeConfig = async (
isArray(objValue) ? objValue.concat(srcValue) : undefined
) as RedoclyConfig

await writeYaml(configFileOut, config)
await writeYamlFromJson(configFileOut, config)
}

const createTmpConfig = async (
Expand All @@ -199,7 +201,7 @@ const createTmpConfig = async (
)
config.plugins.push(medusaPluginAbsolutePath)

await writeYaml(configFileOut, config)
await writeYamlFromJson(configFileOut, config)
}

const sanitizeOAS = async (
Expand Down Expand Up @@ -254,12 +256,12 @@ const generateReference = async (
}

const preview = async (oasFile: string, configFile: string): Promise<void> => {
await previewDocs({
await commandWrapper(previewDocs)({
port: 8080,
host: "127.0.0.1",
api: oasFile,
config: configFile,
})
} as yargs.Arguments<PreviewDocsOptions>)
}

const buildHTML = async (
Expand Down
6 changes: 5 additions & 1 deletion packages/oas/medusa-oas-cli/src/utils/yaml-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ export const readYaml = async (filePath): Promise<unknown> => {
return yaml.load(yamlString)
}

export const writeYaml = async (filePath, jsonObject): Promise<void> => {
export const writeYaml = async (filePath: string, yamlContent: string): Promise<void> => {
await fs.writeFile(filePath, yamlContent, "utf8")
}

export const writeYamlFromJson = async (filePath, jsonObject): Promise<void> => {
const yamlString = yaml.dump(jsonObject)
await fs.writeFile(filePath, yamlString, "utf8")
}
Expand Down
Loading
Loading