Skip to content
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## unreleased

- feat: improve CLI playground URL handling

## [1.29.5] - 2026-02-28

- Filter: combine inverseTags and inverseFlag (#192)
Expand Down
28 changes: 18 additions & 10 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,6 @@ async function run(oaFile, options) {

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

if (options?.playground) {
try {
const playgroundEndpoint = 'https://openapi-format-playground.vercel.app/api/share';
const playgroundEndpoints = [
'https://playground.openapi-format.com/api/share',
'https://openapi-format-playground.vercel.app/api/share'
];
const config = {};

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

if (!process.env.CI) {
const response = await fetch(playgroundEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Source: 'openapi-format-cli'
},
body: JSON.stringify(payload)
});
let response;
for (const playgroundEndpoint of playgroundEndpoints) {
response = await fetch(playgroundEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Source: 'openapi-format-cli'
},
body: JSON.stringify(payload)
});

if (response.ok) break;
if (response.status !== 405) break;
}

if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
Expand Down
38 changes: 32 additions & 6 deletions bin/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,17 +338,43 @@ describe('openapi-format CLI command', () => {
fs.unlinkSync(outputPath);
});

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

let result = await testUtils.cli([inputFile, `--filterFile ${setting}`, `--playground`], '.');
// console.log('result', result)
let result = await testUtils.cli([inputFile, `--filterFile ${setting}`, `--playground`], '.', {CI: 'true'});
expect(result.code).toBe(0);
expect(result.stderr).toContain('Running in CI/CD environment, no Share URL generated');
});

it('should handle -p in CI mode', async () => {
const path = `test/yaml-filter-custom`;
const inputFile = `${path}/input.yaml`;
const setting = `${path}/customFilter.yaml`;

let result = await testUtils.cli([inputFile, `--filterFile ${setting}`, `-p`], '.', {CI: 'true'});
expect(result.code).toBe(0);
expect(result.stdout).toContain('🌐');
expect(result.stderr).toContain('Running in CI/CD environment, no Share URL generated');
});

it('should handle playground share URL generation based on environment', async () => {
const path = `test/yaml-filter-custom`;
const inputFile = `${path}/input.yaml`;
const setting = `${path}/customFilter.yaml`;
const args = [inputFile, `--filterFile ${setting}`, `--playground`];

console.log('playground request args:', args);
let result = await testUtils.cli(args, '.');
console.log('playground response stdout:', result.stdout);
console.log('playground response stderr:', result.stderr);
expect(result.code).toBe(0);
if (process.env.CI) {
expect(result.stderr).toContain('Running in CI/CD environment, no Share URL generated');
} else {
expect(result.stdout).toContain('🌐');
expect(result.stdout).toContain('playground.openapi-format.com');
}
});

it('should use the sortComponentsFile', async () => {
Expand Down
9 changes: 7 additions & 2 deletions test/__utils__/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@ async function loadTest(folder, inputType = 'yaml', outType = 'yaml') {
};
}

function cli(args, cwd) {
function cli(args, cwd, envOverrides = {}) {
return new Promise(resolve => {
exec(`node ${path.resolve('./bin/cli')} ${args.join(' ')}`, {cwd}, (error, stderr, stdout) => {
const env = {...process.env, ...envOverrides};
if (env.FORCE_COLOR && env.NO_COLOR) {
delete env.NO_COLOR;
}

exec(`node ${path.resolve('./bin/cli')} ${args.join(' ')}`, {cwd, env}, (error, stderr, stdout) => {
resolve({
code: error && error.code ? error.code : 0,
error,
Expand Down
Loading