Skip to content

Commit c2af61e

Browse files
committed
feat: improve CLI playground URL handling
1 parent a8b8f72 commit c2af61e

3 files changed

Lines changed: 57 additions & 18 deletions

File tree

bin/cli.js

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

421421
const cliOut = [];
422422
cliLog.unusedActions.forEach(action => {
423-
const description = action.description || 'No description provided';
424423
cliOut.push(
425424
`- Target: ${action.target}\n Type: ${action.update ? 'update' : action.remove ? 'remove' : 'unknown'}`
426425
);
@@ -441,7 +440,10 @@ async function run(oaFile, options) {
441440

442441
if (options?.playground) {
443442
try {
444-
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+
];
445447
const config = {};
446448

447449
if (options.sortSet !== undefined) config.sortSet = await stringify(options.sortSet);
@@ -458,14 +460,20 @@ async function run(oaFile, options) {
458460
};
459461

460462
if (!process.env.CI) {
461-
const response = await fetch(playgroundEndpoint, {
462-
method: 'POST',
463-
headers: {
464-
'Content-Type': 'application/json',
465-
Source: 'openapi-format-cli'
466-
},
467-
body: JSON.stringify(payload)
468-
});
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+
}
469477

470478
if (!response.ok) {
471479
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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ 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-
exec(`node ${path.resolve('./bin/cli')} ${args.join(' ')}`, {cwd}, (error, stderr, stdout) => {
47+
const env = {...process.env, ...envOverrides};
48+
if (env.FORCE_COLOR && env.NO_COLOR) {
49+
delete env.NO_COLOR;
50+
}
51+
52+
exec(`node ${path.resolve('./bin/cli')} ${args.join(' ')}`, {cwd, env}, (error, stderr, stdout) => {
4853
resolve({
4954
code: error && error.code ? error.code : 0,
5055
error,

0 commit comments

Comments
 (0)