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
4 changes: 2 additions & 2 deletions apps/cli/src/command/convert.command.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { OpenAPIConverter } from '@api7/adc-converter-openapi';
import * as ADCSDK from '@api7/adc-sdk';
import OpenAPIParser from '@readme/openapi-parser';
import { dump } from 'js-yaml';
import { Listr } from 'listr2';
import { cloneDeep } from 'lodash';
import { existsSync, writeFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { OpenAPIV3 } from 'openapi-types';
import { stringify } from 'yaml';

import { SignaleRenderer } from '../utils/listr';
import { TaskContext } from './diff.command';
Expand Down Expand Up @@ -103,7 +103,7 @@ const OpenAPICommand = new BaseConvertCommand('openapi')
title: 'Write converted OpenAPI file',
task: (ctx, task) => {
ctx.local.services = ctx.buffer.flatMap((item) => item.services);
const yamlStr = stringify(ctx.local, {});
const yamlStr = dump(ctx.local, { noRefs: true, sortKeys: true });
writeFileSync(opts.output, yamlStr);
task.title = `Converted OpenAPI file to "${resolve(
opts.output,
Expand Down
10 changes: 7 additions & 3 deletions apps/cli/src/command/diff.command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as ADCSDK from '@api7/adc-sdk';
import YAML from 'js-yaml';
import { Listr } from 'listr2';
import { readFile, writeFile } from 'node:fs/promises';
import YAML from 'yaml';
import { writeFile } from 'node:fs/promises';

import {
DiffResourceTask,
Expand Down Expand Up @@ -82,7 +82,11 @@ export const DiffCommand = new BackendCommand<DiffOptions>(
{
title: 'Write detail diff result to file',
task: async (ctx, task) => {
await writeFile('./diff.yaml', YAML.stringify(ctx.diff), {});
await writeFile(
'./diff.yaml',
YAML.dump(ctx.diff, { noRefs: true }),
{ encoding: 'utf-8' },
);
task.output = 'Detail diff result has been wrote to diff.yaml';
},
},
Expand Down
31 changes: 4 additions & 27 deletions apps/cli/src/command/dump.command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dump } from 'js-yaml';
import { Listr } from 'listr2';
import { writeFile } from 'node:fs/promises';
import path from 'node:path';
import { Scalar, stringify } from 'yaml';

import { LoadRemoteConfigurationTask } from '../tasks';
import { InitializeBackendTask } from '../tasks/init_backend';
Expand Down Expand Up @@ -69,32 +69,9 @@ export const DumpCommand = new BackendCommand<DumpOptions>(
task: async (ctx, task) => {
await writeFile(
opts.output,
stringify(resortConfiguration(ctx.remote), {
sortMapEntries: (a, b) => {
const nameKey = 'name';
const descKey = 'description';
const labelKey = 'labels';
const consumerUsernameKey = 'username';
const aKey = (a.key as Scalar)?.value;
const bKey = (b.key as Scalar)?.value;

// make sure the metadata is always at the top
if (aKey && bKey) {
if (aKey === nameKey || bKey === nameKey)
return aKey === nameKey ? -1 : 1;
if (
aKey === consumerUsernameKey ||
bKey === consumerUsernameKey
)
return aKey === consumerUsernameKey ? -1 : 1;
if (aKey === descKey || bKey === descKey)
return aKey === descKey ? -1 : 1;
if (aKey === labelKey || bKey === labelKey)
return aKey === labelKey ? -1 : 1;
}

return a.key > b.key ? 1 : a.key < b.key ? -1 : 0;
},
dump(resortConfiguration(ctx.remote), {
noRefs: true,
sortKeys: true,
}),
{ encoding: 'utf8' },
);
Expand Down
4 changes: 2 additions & 2 deletions apps/cli/src/tasks/experimental.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { readFile } from 'fs/promises';
import YAML from 'yaml';
import YAML from 'js-yaml';

export const ExperimentalRemoteStateFileTask = (file: string) => ({
task: async (ctx) => {
const fileContent =
(await readFile(file, {
encoding: 'utf-8',
})) ?? '';
ctx.remote = YAML.parse(fileContent);
ctx.remote = YAML.load(fileContent);
},
});
10 changes: 8 additions & 2 deletions apps/cli/src/tasks/load_local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as ADCSDK from '@api7/adc-sdk';
import { existsSync } from 'fs';
import { readFile } from 'fs/promises';
import { globSync } from 'glob';
import YAML from 'js-yaml';
import { ListrTask } from 'listr2';
import path from 'node:path';
import YAML from 'yaml';

import {
fillLabels,
Expand Down Expand Up @@ -51,7 +51,13 @@ export const LoadLocalConfigurationTask = (
const fileContent =
(await readFile(filePath, { encoding: 'utf-8' })) ?? '';

subCtx.configurations[filePath] = YAML.parse(fileContent) ?? {};
const ext = path.extname(filePath).toLowerCase();
if (ext === '.json') {
subCtx.configurations[filePath] =
JSON.parse(fileContent) ?? {};
return;
}
subCtx.configurations[filePath] = YAML.load(fileContent) ?? {};
},
};
}),
Expand Down
8 changes: 4 additions & 4 deletions libs/converter-openapi/test/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import { OpenAPIV3 } from 'openapi-types';
import { parse } from 'yaml';
import { load } from 'js-yaml';

import { OpenAPIConverter } from '../src';
import { loadAsset, runTask } from './utils';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parse = (content: string): any => load(content);

describe('Basic', () => {
it('case 1 (single path)', async () => {
const oas = parse(loadAsset('basic-1.yaml'));
Expand Down
5 changes: 4 additions & 1 deletion libs/converter-openapi/test/extension.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { parse } from 'yaml';
import { load } from 'js-yaml';

import { OpenAPIConverter } from '../src';
import { loadAsset, runTask } from './utils';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const parse = (content: string): any => load(content);

describe('Extension', () => {
it('case 1 (override resource name)', async () => {
const oas = parse(loadAsset('extension-1.yaml'));
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"dotenv": "^16.4.5",
"glob": "^11.0.0",
"immer": "^10.1.1",
"js-yaml": "^4.1.0",
"json-schema": "^0.4.0",
"listr2": "^8.2.1",
"lodash": "^4.17.21",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.