Skip to content

Commit 3617f7d

Browse files
committed
Merge branch 'main' of github.com:thim81/openapi-format into overlay110
# Conflicts: # CHANGELOG.md # test/__utils__/test-utils.js
2 parents 85fe617 + ae0b746 commit 3617f7d

4 files changed

Lines changed: 53 additions & 17 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- Overlay: add OpenAPI Overlay 1.1.0 support
44
- Overlay: add `copy` action support with strict `from` validation
55
- Overlay: enforce strict action validation and type compatibility for update/copy actions
6+
- CLI: improve CLI playground URL handling
67
- Types: extend overlay typings with `copy`, `from`, and overlay metadata fields
78
- Docs: update overlay examples and guidance to 1.1.0
89

bin/cli.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,10 @@ async function run(oaFile, options) {
440440

441441
if (options?.playground) {
442442
try {
443-
const playgroundEndpoint = 'https://openapi-format-playground.vercel.app/api/share';
443+
const playgroundEndpoints = [
444+
'https://playground.openapi-format.com/api/share',
445+
'https://openapi-format-playground.vercel.app/api/share'
446+
];
444447
const config = {};
445448

446449
if (options.sortSet !== undefined) config.sortSet = await stringify(options.sortSet);
@@ -457,14 +460,20 @@ async function run(oaFile, options) {
457460
};
458461

459462
if (!process.env.CI) {
460-
const response = await fetch(playgroundEndpoint, {
461-
method: 'POST',
462-
headers: {
463-
'Content-Type': 'application/json',
464-
Source: 'openapi-format-cli'
465-
},
466-
body: JSON.stringify(payload)
467-
});
463+
let response;
464+
for (const playgroundEndpoint of playgroundEndpoints) {
465+
response = await fetch(playgroundEndpoint, {
466+
method: 'POST',
467+
headers: {
468+
'Content-Type': 'application/json',
469+
Source: 'openapi-format-cli'
470+
},
471+
body: JSON.stringify(payload)
472+
});
473+
474+
if (response.ok) break;
475+
if (response.status !== 405) break;
476+
}
468477

469478
if (!response.ok) {
470479
throw new Error(`HTTP error! status: ${response.status}`);

bin/cli.test.js

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,43 @@ describe('openapi-format CLI command', () => {
338338
fs.unlinkSync(outputPath);
339339
});
340340

341-
it.skip('should generate a playground share URL', async () => {
341+
it('should handle --playground in CI mode', async () => {
342342
const path = `test/yaml-filter-custom`;
343343
const inputFile = `${path}/input.yaml`;
344-
const outputFile = `${path}/output.yaml`;
345-
const output = await getLocalFile(outputFile);
346344
const setting = `${path}/customFilter.yaml`;
347345

348-
let result = await testUtils.cli([inputFile, `--filterFile ${setting}`, `--playground`], '.');
349-
// console.log('result', result)
346+
let result = await testUtils.cli([inputFile, `--filterFile ${setting}`, `--playground`], '.', {CI: 'true'});
347+
expect(result.code).toBe(0);
348+
expect(result.stderr).toContain('Running in CI/CD environment, no Share URL generated');
349+
});
350+
351+
it('should handle -p in CI mode', async () => {
352+
const path = `test/yaml-filter-custom`;
353+
const inputFile = `${path}/input.yaml`;
354+
const setting = `${path}/customFilter.yaml`;
355+
356+
let result = await testUtils.cli([inputFile, `--filterFile ${setting}`, `-p`], '.', {CI: 'true'});
350357
expect(result.code).toBe(0);
351-
expect(result.stdout).toContain('🌐');
358+
expect(result.stderr).toContain('Running in CI/CD environment, no Share URL generated');
359+
});
360+
361+
it('should handle playground share URL generation based on environment', async () => {
362+
const path = `test/yaml-filter-custom`;
363+
const inputFile = `${path}/input.yaml`;
364+
const setting = `${path}/customFilter.yaml`;
365+
const args = [inputFile, `--filterFile ${setting}`, `--playground`];
366+
367+
console.log('playground request args:', args);
368+
let result = await testUtils.cli(args, '.');
369+
console.log('playground response stdout:', result.stdout);
370+
console.log('playground response stderr:', result.stderr);
371+
expect(result.code).toBe(0);
372+
if (process.env.CI) {
373+
expect(result.stderr).toContain('Running in CI/CD environment, no Share URL generated');
374+
} else {
375+
expect(result.stdout).toContain('🌐');
376+
expect(result.stdout).toContain('playground.openapi-format.com');
377+
}
352378
});
353379

354380
it('should use the sortComponentsFile', async () => {

test/__utils__/test-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
4242
};
4343
}
4444

45-
function cli(args, cwd) {
45+
function cli(args, cwd, envOverrides = {}) {
4646
return new Promise(resolve => {
47-
const env = {...process.env};
47+
const env = {...process.env, ...envOverrides};
4848
if (env.FORCE_COLOR && env.NO_COLOR) {
4949
delete env.NO_COLOR;
5050
}

0 commit comments

Comments
 (0)