Skip to content

Commit

Permalink
feat: migrate oclif/core to V3 (asyncapi#1228)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amzani authored May 15, 2024
1 parent 9b5332f commit b98eb61
Show file tree
Hide file tree
Showing 20 changed files with 11,370 additions and 60,101 deletions.
1 change: 0 additions & 1 deletion bin/dev
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ oclif.settings.debug = true;

// Start the CLI
oclif.run().then(oclif.flush).catch(oclif.Errors.handle)

1 change: 0 additions & 1 deletion bin/run
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,3 @@ oclif.run()
const oclifHandler = require('@oclif/core/handle');
return oclifHandler(err.message);
});

1 change: 0 additions & 1 deletion bin/run_bin
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ oclif.run()
const oclifHandler = require('@oclif/core/handle');
return oclifHandler(err.message);
});

71,264 changes: 11,285 additions & 59,979 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"@asyncapi/raml-dt-schema-parser": "^4.0.22",
"@asyncapi/studio": "^0.20.0",
"@clack/prompts": "^0.7.0",
"@oclif/core": "^1.26.2",
"@oclif/core": "^3",
"@oclif/errors": "^1.3.6",
"@oclif/plugin-not-found": "^2.3.22",
"@smoya/asyncapi-adoption-metrics": "^2.4.7",
Expand Down Expand Up @@ -168,6 +168,7 @@
"release": "semantic-release",
"test": "npm run test:unit",
"test:unit": "cross-env NODE_ENV=development TEST=1 CUSTOM_CONTEXT_FILENAME=\"test.asyncapi-cli\" CUSTOM_CONTEXT_FILE_LOCATION=\"\" nyc --extension .ts mocha --require ts-node/register --require test/helpers/init.js --reporter spec --timeout 100000 \"test/**/*.test.ts\"",
"test:one": "cross-env NODE_ENV=development TEST=1 CUSTOM_CONTEXT_FILENAME=\"test.asyncapi-cli\" CUSTOM_CONTEXT_FILE_LOCATION=\"\" nyc --extension .ts mocha --require ts-node/register --require test/helpers/init.js --reporter spec --timeout 100000",
"get-version": "echo $npm_package_version",
"createhook": "oclif generate hook myhook --event=command_not_found",
"createhookinit": "oclif generate hook inithook --event=init"
Expand Down
10 changes: 5 additions & 5 deletions src/commands/bundle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Flags } from '@oclif/core';
import { Example } from '@oclif/core/lib/interfaces';
import Command from '../base';
import bundle from '@asyncapi/bundler';
import { promises } from 'fs';
Expand All @@ -13,7 +12,8 @@ export default class Bundle extends Command {
static readonly description = 'Bundle one or multiple AsyncAPI Documents and their references together.';
static strict = false;

static examples: Example[] = [
static examples = [
'asyncapi bundle ./asyncapi.yaml > final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml --output final-asyncapi.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml',
'asyncapi bundle ./asyncapi.yaml ./features.yaml --base ./main.yaml',
Expand All @@ -32,8 +32,8 @@ export default class Bundle extends Command {
async run() {
const { argv, flags } = await this.parse(Bundle);
const output = flags.output;
const outputFormat = path.extname(argv[0]);
const AsyncAPIFiles = argv;
const outputFormat = path.extname(argv[0] as string);
const AsyncAPIFiles = argv as string[];

this.metricsMetadata.files = AsyncAPIFiles.length;

Expand All @@ -44,7 +44,7 @@ export default class Bundle extends Command {
xOrigin: flags.xOrigin,
}
);

await this.collectMetricsData(document);

if (!output) {
Expand Down
14 changes: 5 additions & 9 deletions src/commands/config/context/add.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../../../base';
import { addContext, setCurrentContext } from '../../../models/Context';
import {
Expand All @@ -18,14 +18,10 @@ export default class ContextAdd extends Command {
})
};

static args = [
{ name: 'context-name', description: 'context name', required: true },
{
name: 'spec-file-path',
description: 'file path of the spec file',
required: true,
},
];
static args = {
'context-name': Args.string({description: 'context name', required: true}),
'spec-file-path': Args.string({description: 'file path of the spec file', required: true}),
};

async run() {
const { args, flags } = await this.parse(ContextAdd);
Expand Down
15 changes: 5 additions & 10 deletions src/commands/config/context/edit.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../../../base';
import { editContext, CONTEXT_FILE_PATH } from '../../../models/Context';
import {
Expand All @@ -13,15 +13,10 @@ export default class ContextEdit extends Command {
help: Flags.help({ char: 'h' }),
};

static args = [
{ name: 'context-name', description: 'context name', required: true },
{
name: 'new-spec-file-path',
description: 'new file path of the spec file',
required: true,
},
];

static args = {
'context-name': Args.string({description: 'context name', required: true}),
'new-spec-file-path': Args.string({description: 'file path of the spec file', required: true}),
};
async run() {
const { args } = await this.parse(ContextEdit);
const contextName = args['context-name'];
Expand Down
14 changes: 5 additions & 9 deletions src/commands/config/context/init.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../../../base';
import { initContext } from '../../../models/Context';

Expand All @@ -13,19 +13,15 @@ export default class ContextInit extends Command {
- root of current repository : asyncapi config context init ./
- user's home directory : asyncapi config context init ~`;

static args = [
{
name: 'context-file-path',
description: `${ContextInit.contextFilePathMessage}`,
required: false,
},
];
static args = {
'context-file-path': Args.string({description: `${ContextInit.contextFilePathMessage}`, required: false})
};

async run() {
const { args } = await this.parse(ContextInit);
const contextFilePath = args['context-file-path'];

const contextWritePath = await initContext(contextFilePath);
const contextWritePath = await initContext(contextFilePath as string);
this.log(`Initialized context ${contextWritePath}`);
}
}
12 changes: 4 additions & 8 deletions src/commands/config/context/remove.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../../../base';
import { removeContext, CONTEXT_FILE_PATH } from '../../../models/Context';
import {
Expand All @@ -13,13 +13,9 @@ export default class ContextRemove extends Command {
help: Flags.help({ char: 'h' }),
};

static args = [
{
name: 'context-name',
description: 'Name of the context to delete',
required: true,
},
];
static args = {
'context-name': Args.string({description: 'Name of the context to delete', required: true}),
};

async run() {
const { args } = await this.parse(ContextRemove);
Expand Down
12 changes: 4 additions & 8 deletions src/commands/config/context/use.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../../../base';
import { setCurrentContext, CONTEXT_FILE_PATH } from '../../../models/Context';
import {
Expand All @@ -13,13 +13,9 @@ export default class ContextUse extends Command {
help: Flags.help({ char: 'h' }),
};

static args = [
{
name: 'context-name',
description: 'name of the saved context',
required: true,
},
];
static args = {
'context-name': Args.string({description: 'name of the saved context', required: true}),
};

async run() {
const { args } = await this.parse(ContextUse);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/convert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { promises as fPromises } from 'fs';
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../base';
import { ValidationError } from '../errors/validation-error';
import { load } from '../models/SpecificationFile';
Expand All @@ -23,9 +23,9 @@ export default class Convert extends Command {
'target-version': Flags.string({ char: 't', description: 'asyncapi version to convert to', default: latestVersion })
};

static args = [
{ name: 'spec-file', description: 'spec path, url, or context-name', required: false },
];
static args = {
'spec-file': Args.string({description: 'spec path, url, or context-name', required: false}),
};

async run() {
const { args, flags } = await this.parse(Convert);
Expand Down
20 changes: 6 additions & 14 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import * as diff from '@asyncapi/diff';
import AsyncAPIDiff from '@asyncapi/diff/lib/asyncapidiff';
import { promises as fs } from 'fs';
Expand Down Expand Up @@ -54,18 +54,10 @@ export default class Diff extends Command {
...validationFlags({ logDiagnostics: false }),
};

static args = [
{
name: 'old',
description: 'old spec path, URL or context-name',
required: true,
},
{
name: 'new',
description: 'new spec path, URL or context-name',
required: true,
},
];
static args = {
old: Args.string({description: 'old spec path, URL or context-name', required: true}),
new: Args.string({description: 'new spec path, URL or context-name', required: true}),
};

/* eslint-disable sonarjs/cognitive-complexity */
async run() {
Expand All @@ -82,7 +74,7 @@ export default class Diff extends Command {
let firstDocument: Specification, secondDocument: Specification;

checkAndWarnFalseFlag(outputFormat, markdownSubtype);
markdownSubtype = setDefaultMarkdownSubtype(outputFormat, markdownSubtype);
markdownSubtype = setDefaultMarkdownSubtype(outputFormat, markdownSubtype) as string;

try {
firstDocument = await load(firstDocumentPath);
Expand Down
20 changes: 10 additions & 10 deletions src/commands/generate/fromTemplate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import Command from '../../base';
// eslint-disable-next-line
// @ts-ignore
Expand All @@ -12,7 +12,6 @@ import { isLocalTemplate, Watcher } from '../../utils/generator';
import { ValidationError } from '../../errors/validation-error';
import { GeneratorError } from '../../errors/generator-error';
import { Parser } from '@asyncapi/parser';
import type { Example } from '@oclif/core/lib/interfaces';
import { intro, isCancel, spinner, text } from '@clack/prompts';
import { inverse, yellow, magenta, green, red } from 'picocolors';
import fetch from 'node-fetch';
Expand Down Expand Up @@ -53,7 +52,7 @@ function verifyTemplateSupportForV3(template: string) {
export default class Template extends Command {
static description = 'Generates whatever you want using templates compatible with AsyncAPI Generator.';

static examples: Example[] = [
static examples = [
'asyncapi generate fromTemplate asyncapi.yaml @asyncapi/html-template --param version=1.0.0 singleFile=true --output ./docs --force-write'
];

Expand Down Expand Up @@ -112,10 +111,11 @@ export default class Template extends Command {
description: 'The npm registry authentication token, that can be passed instead of base64 encoded username and password'
})
};
static args = [
{ name: 'asyncapi', description: '- Local path, url or context-name pointing to AsyncAPI file' },
{ name: 'template', description: '- Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template' },
];

static args = {
asyncapi: Args.string({description: '- Local path, url or context-name pointing to AsyncAPI file', required: true}),
template: Args.string({description: '- Name of the generator template like for example @asyncapi/html-template or https://github.com/asyncapi/html-template', required: true}),
};

parser = new Parser();

Expand Down Expand Up @@ -150,7 +150,7 @@ export default class Template extends Command {
}
};
const asyncapiInput = (await load(asyncapi)) || (await load());

this.specFile = asyncapiInput;
this.metricsMetadata.template = template;

Expand Down Expand Up @@ -193,7 +193,7 @@ export default class Template extends Command {
});
}

if (isCancel(asyncapi)) {
if (isCancel(asyncapi)) {
this.error(cancellationMessage, { exit: 1 });
}

Expand Down Expand Up @@ -233,7 +233,7 @@ export default class Template extends Command {
mapBaseUrlToFolder: this.mapBaseURLParser(mapBaseUrl),
registryURLValidation: this.registryURLParser(registryUrl),
registryAuthentication: this.registryValidation(registryUrl, registryAuth, registryToken)

} as ParsedFlags;
}

Expand Down
27 changes: 12 additions & 15 deletions src/commands/generate/models.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CSharpFileGenerator, JavaFileGenerator, JavaScriptFileGenerator, TypeScriptFileGenerator, GoFileGenerator, Logger, DartFileGenerator, PythonFileGenerator, RustFileGenerator, TS_COMMON_PRESET, TS_JSONBINPACK_PRESET, CSHARP_DEFAULT_PRESET, CSHARP_NEWTONSOFT_SERIALIZER_PRESET, CSHARP_COMMON_PRESET, CSHARP_JSON_SERIALIZER_PRESET, KotlinFileGenerator, TS_DESCRIPTION_PRESET, PhpFileGenerator, CplusplusFileGenerator, JAVA_CONSTRAINTS_PRESET, JAVA_JACKSON_PRESET, JAVA_COMMON_PRESET, JAVA_DESCRIPTION_PRESET } from '@asyncapi/modelina';
import { Flags } from '@oclif/core';
import { Flags, Args } from '@oclif/core';
import { ConvertDocumentParserAPIVersion } from '@smoya/multi-parser';
import Command from '../../base';
import { load } from '../../models/SpecificationFile';
import { formatOutput, parse, validationFlags } from '../../parser';
import { formatOutput, parse, validationFlags, ValidateOptions } from '../../parser';

import { select, text, spinner, isCancel, cancel, intro } from '@clack/prompts';
import { green, inverse } from 'picocolors';
Expand All @@ -27,14 +27,11 @@ const possibleLanguageValues = Object.values(Languages).join(', ');

export default class Models extends Command {
static description = 'Generates typed models';
static args = [
{
name: 'language',
description: 'The language you want the typed models generated for.',
options: Object.keys(Languages),
},
{ name: 'file', description: 'Path or URL to the AsyncAPI document, or context-name' },
];

static args = {
language: Args.string({description: 'The language you want the typed models generated for.', options: Object.keys(Languages), required: true}),
file: Args.string({description: 'Path or URL to the AsyncAPI document, or context-name', required: true}),
};

static flags = {
help: Flags.help({ char: 'h' }),
Expand Down Expand Up @@ -194,14 +191,14 @@ export default class Models extends Command {
if (inputFile.isAsyncAPI3()) {
this.error('Generate Models command does not support AsyncAPI v3 yet, please checkout https://github.com/asyncapi/modelina/issues/1376');
}
const { document, diagnostics ,status } = await parse(this, inputFile, flags);
const { document, diagnostics ,status } = await parse(this, inputFile, flags as ValidateOptions);
if (!document || status === 'invalid') {
const severityErrors = diagnostics.filter((obj) => obj.severity === 0);
this.log(`Input is not a correct AsyncAPI document so it cannot be processed.${formatOutput(severityErrors,'stylish','error')}`);
return;
}
// Modelina, atm, is not using @asyncapi/[email protected] but @asyncapi/[email protected], so it still uses Parser-API v1.0.0.

// Modelina, atm, is not using @asyncapi/[email protected] but @asyncapi/[email protected], so it still uses Parser-API v1.0.0.
// This call converts the parsed document object using @asyncapi/[email protected] (Parser-API v2) to a document compatible with the Parser-API version in use in @asyncapi/[email protected] (v1)
// This is needed until https://github.com/asyncapi/modelina/issues/1493 gets fixed.
const convertedDoc = ConvertDocumentParserAPIVersion(document.json(), 1);
Expand Down Expand Up @@ -394,7 +391,7 @@ export default class Models extends Command {
if (!language) {
language = await select({
message: 'Select the language you want to generate models for',
options: Object.keys(Languages).map((key) =>
options: Object.keys(Languages).map((key) =>
({ value: key, label: key, hint: Languages[key as keyof typeof Languages] })
),
});
Expand Down Expand Up @@ -434,7 +431,7 @@ export default class Models extends Command {
cancel(operationCancelled);
this.exit();
}

return { language, file, output: output || 'stdout' };
}
}
Loading

0 comments on commit b98eb61

Please sign in to comment.