Skip to content

Commit

Permalink
use the specFile
Browse files Browse the repository at this point in the history
  • Loading branch information
KhudaDad414 committed Apr 2, 2024
1 parent 61b377b commit 2a05e2c
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export default abstract class extends Command {
parser = new Parser();
metricsMetadata: MetricMetadata = {};
specFile: Specification | undefined;
specFilePath: string | undefined;

async init(): Promise<void> {
await super.init();
Expand Down Expand Up @@ -81,10 +80,10 @@ export default abstract class extends Command {
}

setSource() {
if (this.specFilePath) {
const stats = statSync(this.specFilePath);
this.metricsMetadata['file_creation_timestamp'] = stats.birthtimeMs;
}
const specFilePath = this.specFile?.getFilePath();
if (!specFilePath) { return; }
const stats = statSync(specFilePath);
this.metricsMetadata['file_creation_timestamp'] = stats.birthtimeMs;
}
async finally(error: Error | undefined): Promise<any> {
await super.finally(error);
Expand Down
4 changes: 2 additions & 2 deletions src/commands/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class Bundle extends Command {

this.metricsMetadata.files = AsyncAPIFiles.length;

if (flags.base) {baseFile = (await load(this,flags.base)).text();}
if (flags.base) {baseFile = (await load(flags.base)).text();}

const fileContents = AsyncAPIFiles.map((file) => file.text());

Expand Down Expand Up @@ -90,7 +90,7 @@ export default class Bundle extends Command {
async loadFiles(filepaths: string[]): Promise<Specification[]> {
const files = [];
for (const filepath of filepaths) {
const file = await load(this,filepath);
const file = await load(filepath);
files.push(file);
}
return files;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default class Convert extends Command {

try {
// LOAD FILE
this.specFile = await load(this,filePath);
this.specFile = await load(filePath);
this.metricsMetadata.to_version = flags['target-version'];

// CONVERSION
Expand Down
4 changes: 2 additions & 2 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class Diff extends Command {
markdownSubtype = setDefaultMarkdownSubtype(outputFormat, markdownSubtype);

try {
firstDocument = await load(this,firstDocumentPath);
firstDocument = await load(firstDocumentPath);

if (firstDocument.isAsyncAPI3()) {
this.error('Diff command does not support AsyncAPI v3 yet which was your first document, please checkout https://github.com/asyncapi/diff/issues/154');
Expand All @@ -111,7 +111,7 @@ export default class Diff extends Command {
}

try {
secondDocument = await load(this,secondDocumentPath);
secondDocument = await load(secondDocumentPath);

if (secondDocument.isAsyncAPI3()) {
this.error('Diff command does not support AsyncAPI v3 yet which was your second document, please checkout https://github.com/asyncapi/diff/issues/154');
Expand Down
6 changes: 3 additions & 3 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export default class Template extends Command {
mapBaseUrlToFolder: parsedFlags.mapBaseUrlToFolder,
disabledHooks: parsedFlags.disableHooks,
};
const asyncapiInput = (await load(this,asyncapi)) || (await load(this));
const asyncapiInput = (await load(asyncapi)) || (await load());

this.specFile = asyncapiInput;
this.metricsMetadata.template = template;
Expand Down Expand Up @@ -207,7 +207,7 @@ export default class Template extends Command {
private async generate(asyncapi: string | undefined, template: string, output: string, options: any, genOption: any) {
let specification: Specification;
try {
specification = await load(this,asyncapi);
specification = await load(asyncapi);
} catch (err: any) {
return this.error(
new ValidationError({
Expand All @@ -231,7 +231,7 @@ export default class Template extends Command {
}

private async runWatchMode(asyncapi: string | undefined, template: string, output: string, watchHandler: ReturnType<typeof this.watcherHandler>) {
const specification = await load(this,asyncapi);
const specification = await load(asyncapi);

const watchDir = path.resolve(template);
const outputPath = path.resolve(watchDir, output);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/generate/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class Models extends Command {
const { args, flags } = await this.parse(Models);
const { tsModelType, tsEnumType, tsIncludeComments, tsModuleSystem, tsExportType, tsJsonBinPack, tsMarshalling, tsExampleInstance, namespace, csharpAutoImplement, csharpArrayType, csharpNewtonsoft, csharpHashcode, csharpEqual, csharpSystemJson, packageName, javaIncludeComments, javaJackson, javaConstraints, output } = flags;
const { language, file } = args;
const inputFile = (await load(this,file)) || (await load(this));
const inputFile = (await load(file)) || (await load());
if (inputFile.isAsyncAPI3()) {
this.error('Generate Models command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/modelina/issues/1376');
}
Expand Down
2 changes: 1 addition & 1 deletion src/commands/new/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export default class NewFile extends Command {
}
await writeFile(fileNameToWriteToDisk, asyncApiFile, { encoding: 'utf8' });
console.log(`Created file ${fileNameToWriteToDisk}...`);
this.specFile = await load(this,fileNameToWriteToDisk);
this.specFile = await load(fileNameToWriteToDisk);
this.metricsMetadata.selected_template = selectedTemplate;
}
}
2 changes: 1 addition & 1 deletion src/commands/new/glee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export default class NewGlee extends Command {

try {
console.log(file);
const asyncapiInput = (await load(this,file)) || (await load(this));
const asyncapiInput = (await load(file)) || (await load());
console.log(asyncapiInput);

const serversObject = asyncapiInput.toJson()?.servers;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/optimize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class Optimize extends Command {
const filePath = args['spec-file'];

try {
this.specFile = await load(this,filePath);
this.specFile = await load(filePath);
} catch (err) {
this.error(
new ValidationError({
Expand Down
2 changes: 1 addition & 1 deletion src/commands/start/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default class StartStudio extends Command {

async run() {
const { flags } = await this.parse(StartStudio);
const filePath = flags.file || (await load(this)).getFilePath();
const filePath = flags.file || (await load()).getFilePath();
const port = flags.port;

startStudio(filePath as string, port);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export default class Validate extends Command {
const filePath = args['spec-file'];
const watchMode = flags.watch;

this.specFile = await load(this,filePath);
this.specFile = await load(filePath);
if (watchMode) {
specWatcher({ spec: this.specFile, handler: this, handlerName: 'validate' });
}
Expand Down
3 changes: 1 addition & 2 deletions src/models/SpecificationFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ interface LoadType {
}

/* eslint-disable sonarjs/cognitive-complexity */
export async function load(command?: Command, filePathOrContextName?: string, loadType?: LoadType): Promise<Specification> { // NOSONAR
export async function load(filePathOrContextName?: string, loadType?: LoadType): Promise<Specification> { // NOSONAR
if (filePathOrContextName) {
if (loadType?.file) { return Specification.fromFile(filePathOrContextName); }
if (loadType?.context) { return loadFromContext(filePathOrContextName); }
Expand All @@ -137,7 +137,6 @@ export async function load(command?: Command, filePathOrContextName?: string, lo
return Specification.fromURL(filePathOrContextName);
}
await fileExists(filePathOrContextName);
if (command) {command.specFilePath = filePathOrContextName;}
return Specification.fromFile(filePathOrContextName);
}

Expand Down

0 comments on commit 2a05e2c

Please sign in to comment.