Skip to content

Commit 4385590

Browse files
mynnxeverm039
andauthored
fix: allow custom redocly file using --redocly (#2548)
* fix: allow custom redocly file using --redocly * chore: include the fixture * chore: refactor for clarity * chore: added changeset --------- Co-authored-by: everm039 <mark.everett@disney.com>
1 parent f06fb17 commit 4385590

5 files changed

Lines changed: 55 additions & 4 deletions

File tree

.changeset/short-cougars-hear.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
Fixes the `--redocly` flag so that it no longer hangs and is able to lookup the Redocly file at a custom path

packages/openapi-typescript/bin/cli.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ function done(input, output, time) {
167167
console.log(`🚀 ${c.green(`${input}${c.bold(output)}`)} ${c.dim(`[${formatTime(time)}]`)}`);
168168
}
169169

170+
function findRedocConfigPath() {
171+
if (!flags.redocly) {
172+
return findConfig();
173+
}
174+
const explicitPath = path.resolve(flags.redocly);
175+
if (!fs.existsSync(explicitPath)) {
176+
return undefined;
177+
}
178+
const stat = fs.statSync(explicitPath);
179+
return stat.isDirectory() ? findConfig(explicitPath) : explicitPath;
180+
}
181+
170182
async function main() {
171183
if ("help" in flags) {
172184
// biome-ignore lint/suspicious/noConsole: this is a CLI
@@ -188,10 +200,12 @@ async function main() {
188200

189201
const input = flags._[0];
190202

191-
// load Redocly config
192-
const maybeRedoc = findConfig(flags.redocly ? path.dirname(flags.redocly) : undefined);
193-
const redocly = maybeRedoc
194-
? await loadConfig({ configPath: maybeRedoc })
203+
const redocConfigPath = findRedocConfigPath();
204+
if (flags.redocly && !redocConfigPath) {
205+
errorAndExit(`Redocly config not found at: ${flags.redocly}`);
206+
}
207+
const redocly = redocConfigPath
208+
? await loadConfig({ configPath: redocConfigPath })
195209
: await createConfig({}, { extends: ["minimal"] });
196210

197211
// handle Redoc APIs

packages/openapi-typescript/test/cli.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,21 @@ describe("CLI", () => {
160160
}
161161
});
162162

163+
test("--redocly explicit config path", async () => {
164+
const altOutput = new URL("./test/fixtures/redocly-flag/output-alt/", root);
165+
fs.rmSync(altOutput, { recursive: true, force: true });
166+
167+
await execa(cmd, ["--redocly", "test/fixtures/redocly-flag/redocly.alt.yaml"], {
168+
cwd,
169+
});
170+
171+
for (const schema of ["a", "b", "c"]) {
172+
await expect(
173+
fs.readFileSync(new URL(`./test/fixtures/redocly-flag/output-alt/${schema}.ts`, root), "utf8"),
174+
).toMatchFileSnapshot(fileURLToPath(new URL("./examples/simple-example.ts", root)));
175+
}
176+
});
177+
163178
test.skipIf(os.platform() === "win32")("lint error", async () => {
164179
const cwd = new URL("./fixtures/redocly-lint-error", import.meta.url);
165180

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
output
2+
output-alt
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
extends:
2+
- recommended
3+
4+
apis:
5+
a@v1:
6+
root: ./openapi/a.yaml
7+
x-openapi-ts:
8+
output: ./output-alt/a.ts
9+
b@v1:
10+
root: ./openapi/b.yaml
11+
x-openapi-ts:
12+
output: ./output-alt/b.ts
13+
c@v1:
14+
root: ./openapi/c.yaml
15+
x-openapi-ts:
16+
output: ./output-alt/c.ts

0 commit comments

Comments
 (0)