Skip to content

Commit d87be93

Browse files
Refractor code base
1 parent ee75100 commit d87be93

File tree

7 files changed

+32
-46
lines changed

7 files changed

+32
-46
lines changed

src/apps/cli/commands/convert.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ import { proxyFlags } from '@cli/internal/flags/proxy.flags';
99
import specs from '@asyncapi/specs';
1010
import { convertFlags } from '@cli/internal/flags/convert.flags';
1111
import { ConversionService } from '@services/convert.service';
12+
import { applyProxyToPath } from '@utils/proxy';
1213

1314
const latestVersion = Object.keys(specs.schemas).pop() as string;
1415

1516
export default class Convert extends Command {
16-
static specFile: any;
17-
static metricsMetadata: any = {};
1817
static description =
1918
'Convert asyncapi documents older to newer versions or OpenAPI/postman-collection documents to AsyncAPI';
2019
private conversionService = new ConversionService();
@@ -32,18 +31,15 @@ export default class Convert extends Command {
3231

3332
async run() {
3433
const { args, flags } = await this.parse(Convert);
35-
let filePath = args['spec-file'];
36-
const proxyHost = flags['proxyHost'];
37-
const proxyPort = flags['proxyPort'];
38-
if (proxyHost && proxyPort) {
39-
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
40-
filePath = `${filePath}+${proxyUrl}`; // Update filePath with proxyUrl
41-
}
34+
const filePath = applyProxyToPath(
35+
args['spec-file'],
36+
flags['proxyHost'],
37+
flags['proxyPort']
38+
);
4239

4340
try {
4441
// LOAD FILE
4542
this.specFile = await load(filePath);
46-
// eslint-disable-next-line sonarjs/no-duplicate-string
4743
this.metricsMetadata.to_version = flags['target-version'];
4844
const conversionOptions = {
4945
format: flags.format as 'asyncapi' | 'openapi' | 'postman-collection',
@@ -76,20 +72,20 @@ export default class Convert extends Command {
7672
this.log(result.data.convertedDocument);
7773
}
7874
} catch (err) {
79-
this.handleError(err, filePath ?? 'unknown', flags);
75+
this.handleError(err, filePath ?? 'unknown', flags['target-version']);
8076
}
8177
}
8278

8379
// Helper function to handle errors
84-
private handleError(err: any, filePath: string, flags: any) {
80+
private handleError(err: unknown, filePath: string, targetVersion: string | undefined) {
8581
if (err instanceof SpecificationFileNotFound) {
8682
this.error(
8783
new ValidationError({
8884
type: 'invalid-file',
8985
filepath: filePath,
9086
}),
9187
);
92-
} else if (this.specFile?.toJson().asyncapi > flags['target-version']) {
88+
} else if (this.specFile?.toJson().asyncapi > (targetVersion ?? '')) {
9389
this.error(
9490
`The ${cyan(filePath)} file cannot be converted to an older version. Downgrading is not supported.`,
9591
);

src/apps/cli/commands/format.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @typescript-eslint/ban-ts-comment */
21
import { promises as fPromises } from 'fs';
32
import { Args } from '@oclif/core';
43
import Command from '@cli/internal/base';
@@ -16,9 +15,7 @@ import {
1615
fileFormat,
1716
} from '@cli/internal/flags/format.flags';
1817

19-
export default class Convert extends Command {
20-
static specFile: any;
21-
static metricsMetadata: any = {};
18+
export default class Format extends Command {
2219
static description =
2320
'Convert asyncapi documents from any format to yaml, yml or JSON';
2421

@@ -32,14 +29,13 @@ export default class Convert extends Command {
3229
};
3330

3431
async run() {
35-
const { args, flags } = await this.parse(Convert);
32+
const { args, flags } = await this.parse(Format);
3633
const filePath = args['spec-file'];
3734
const outputFileFormat = flags['format'] as fileFormat;
3835
let convertedFile;
3936
try {
4037
this.specFile = await load(filePath);
41-
// eslint-disable-next-line sonarjs/no-duplicate-string
42-
this.metricsMetadata.to_version = flags['target-version'];
38+
this.metricsMetadata.output_format = outputFileFormat;
4339

4440
const ff = retrieveFileFormat(this.specFile.text());
4541
const isSpecFileJson = ff === 'json';

src/apps/cli/commands/generate/models.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
ValidationStatus,
1616
} from '@/domains/services/validation.service';
1717
import { Diagnostic } from '@asyncapi/parser/cjs';
18+
import { applyProxyToPath } from '@utils/proxy';
1819

1920
export default class Models extends Command {
2021
static description = 'Generates typed models';
@@ -44,11 +45,8 @@ export default class Models extends Command {
4445
output = parsedArgs.output;
4546
}
4647

47-
if (proxyHost && proxyPort) {
48-
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
49-
file = `${file}+${proxyUrl}`;
50-
}
51-
const inputFile = (await load(file)) || (await load());
48+
const fileWithProxy = applyProxyToPath(file, proxyHost, proxyPort);
49+
const inputFile = (await load(fileWithProxy)) || (await load());
5250

5351
const result = await this.validationService.parseDocument(
5452
inputFile,

src/apps/cli/commands/optimize.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { promises } from 'fs';
99
import { Parser } from '@asyncapi/parser';
1010
import { optimizeFlags } from '@cli/internal/flags/optimize.flags';
1111
import { proxyFlags } from '@cli/internal/flags/proxy.flags';
12+
import { applyProxyToPath } from '@utils/proxy';
1213

1314
const { writeFile } = promises;
1415

@@ -59,13 +60,11 @@ export default class Optimize extends Command {
5960

6061
async run() {
6162
const { args, flags } = await this.parse(Optimize); //NOSONAR
62-
let filePath = args['spec-file'];
63-
const proxyHost = flags['proxyHost'];
64-
const proxyPort = flags['proxyPort'];
65-
if (proxyHost && proxyPort) {
66-
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
67-
filePath = `${filePath}+${proxyUrl}`; // Update filePath with proxyUrl
68-
}
63+
const filePath = applyProxyToPath(
64+
args['spec-file'],
65+
flags['proxyHost'],
66+
flags['proxyPort']
67+
);
6968
try {
7069
this.specFile = await load(filePath);
7170
} catch (err: any) {

src/apps/cli/commands/validate.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
ValidationService,
1414
ValidationStatus,
1515
} from '@services/validation.service';
16+
import { applyProxyToPath } from '@utils/proxy';
1617

1718
export default class Validate extends Command {
1819
static description = 'validate asyncapi file';
@@ -32,14 +33,11 @@ export default class Validate extends Command {
3233

3334
async run() {
3435
const { args, flags } = await this.parse(Validate); //NOSONAR
35-
let filePath = args['spec-file'];
36-
const proxyHost = flags['proxyHost'];
37-
const proxyPort = flags['proxyPort'];
38-
39-
if (proxyHost && proxyPort) {
40-
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
41-
filePath = `${filePath}+${proxyUrl}`; // Update filePath with proxyUrl
42-
}
36+
const filePath = applyProxyToPath(
37+
args['spec-file'],
38+
flags['proxyHost'],
39+
flags['proxyPort']
40+
);
4341

4442
this.specFile = await load(filePath);
4543
const watchMode = flags.watch;

src/apps/cli/internal/base/BaseGeneratorCommand.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { getMapBaseUrlToFolderResolver } from '@utils/generate/mapBaseUrl';
1515
import { promptForAsyncAPIPath, promptForOutputDir } from '@utils/generate/prompts';
1616
import { ParsedFlags } from '@models/generate/Flags';
1717
import { GeneratorService } from '@services/generator.service';
18+
import { applyProxyToPath } from '@utils/proxy';
1819

1920
export interface GeneratorOptions {
2021
forceWrite: boolean;
@@ -61,11 +62,7 @@ export abstract class BaseGeneratorCommand extends Command {
6162
}
6263

6364
protected applyProxyConfiguration(asyncapi: string, proxyHost?: string, proxyPort?: string): string {
64-
if (proxyHost && proxyPort) {
65-
const proxyUrl = `http://${proxyHost}:${proxyPort}`;
66-
return `${asyncapi}+${proxyUrl}`;
67-
}
68-
return asyncapi;
65+
return applyProxyToPath(asyncapi, proxyHost, proxyPort) ?? asyncapi;
6966
}
7067

7168
protected async handleWatchMode(

src/domains/models/Context.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,9 @@ async function getContextFilePath(): Promise<string | null> {
339339
CONTEXT_FILE_PATH = currentPathString;
340340
return CONTEXT_FILE_PATH;
341341
}
342-
} catch (e) {} // eslint-disable-line
342+
} catch {
343+
// Silently continue to parent directory if file doesn't exist or is invalid JSON
344+
}
343345

344346
currentPath.pop();
345347
}

0 commit comments

Comments
 (0)