From ad2a6861763077b90514b30de2f7ec2c6eb5fd1d Mon Sep 17 00:00:00 2001 From: Samir AMZANI Date: Fri, 15 Mar 2024 13:20:36 +0100 Subject: [PATCH] feat: big bang reorg --- .../api/controllers/generator.controller.ts | 8 +- .../api/controllers/ping.controller.ts | 2 +- src/adapters/api/{ => core}/app.ts | 2 +- src/adapters/api/{ => core}/constants.ts | 0 .../exceptions/problem.exception.ts | 0 src/adapters/api/{ => core}/interfaces.ts | 0 .../middlewares/problem.middleware.ts | 2 +- src/adapters/api/{ => core}/server-api.d.ts | 0 src/adapters/cli/commands/bundle.ts | 14 +- .../cli/commands/config/context/add.ts | 17 +-- .../cli/commands/config/context/current.ts | 10 +- .../cli/commands/config/context/edit.ts | 10 +- .../cli/commands/config/context/index.ts | 2 +- .../cli/commands/config/context/init.ts | 10 +- .../cli/commands/config/context/list.ts | 10 +- .../cli/commands/config/context/remove.ts | 10 +- .../cli/commands/config/context/use.ts | 10 +- src/adapters/cli/commands/config/index.ts | 2 +- src/adapters/cli/commands/config/versions.ts | 8 +- src/adapters/cli/commands/convert.ts | 12 +- src/adapters/cli/commands/diff.ts | 43 +----- .../cli/commands/generate/fromTemplate.ts | 51 +------ src/adapters/cli/commands/generate/index.ts | 2 +- src/adapters/cli/commands/generate/models.ts | 139 +----------------- src/adapters/cli/commands/new/file.ts | 15 +- src/adapters/cli/commands/new/glee.ts | 31 +--- src/adapters/cli/commands/optimize.ts | 24 +-- src/adapters/cli/commands/start/api.ts | 11 +- src/adapters/cli/commands/start/index.ts | 2 +- src/adapters/cli/commands/start/studio.ts | 14 +- src/adapters/cli/commands/validate.ts | 19 +-- src/adapters/cli/{ => core}/base.ts | 0 src/adapters/cli/core/flags/bundle.flags.ts | 10 ++ .../cli/core/flags/config/context.flags.ts | 14 ++ src/adapters/cli/core/flags/convert.flags.ts | 9 ++ src/adapters/cli/core/flags/diff.flags.ts | 35 +++++ .../core/flags/generate/fromTemplate.flags.ts | 45 ++++++ .../cli/core/flags/generate/models.flags.ts | 135 +++++++++++++++++ .../{flags.ts => core/flags/global.flags.ts} | 7 + src/adapters/cli/core/flags/new/file.flags.ts | 12 ++ src/adapters/cli/core/flags/new/glee.flags.ts | 27 ++++ src/adapters/cli/core/flags/optimize.flags.ts | 22 +++ .../cli/core/flags/start/api.flags.ts | 8 + .../cli/core/flags/start/studio.flags.ts | 9 ++ src/adapters/cli/core/flags/validate.flags.ts | 11 ++ src/adapters/cli/{ => core}/global.d.ts | 0 src/adapters/cli/{ => core}/globals.ts | 2 +- .../hooks/command_not_found/myhook.ts | 0 .../cli/core}/models/Context.ts | 2 +- .../cli/core}/models/SpecificationFile.ts | 4 +- .../cli/core}/models/Studio.ts | 2 +- src/adapters/cli/{ => core}/parser.ts | 2 +- src/api.ts | 2 +- .../services}/archiver.service.ts | 2 +- .../services}/generator.service.ts | 4 +- test/helpers/index.ts | 4 +- test/integration/api.test.ts | 2 +- test/integration/context.test.ts | 2 +- test/integration/optimize.test.ts | 2 +- 59 files changed, 455 insertions(+), 399 deletions(-) rename src/adapters/api/{ => core}/app.ts (98%) rename src/adapters/api/{ => core}/constants.ts (100%) rename src/adapters/api/{ => core}/exceptions/problem.exception.ts (100%) rename src/adapters/api/{ => core}/interfaces.ts (100%) rename src/adapters/api/{ => core}/middlewares/problem.middleware.ts (95%) rename src/adapters/api/{ => core}/server-api.d.ts (100%) rename src/adapters/cli/{ => core}/base.ts (100%) create mode 100644 src/adapters/cli/core/flags/bundle.flags.ts create mode 100644 src/adapters/cli/core/flags/config/context.flags.ts create mode 100644 src/adapters/cli/core/flags/convert.flags.ts create mode 100644 src/adapters/cli/core/flags/diff.flags.ts create mode 100644 src/adapters/cli/core/flags/generate/fromTemplate.flags.ts create mode 100644 src/adapters/cli/core/flags/generate/models.flags.ts rename src/adapters/cli/{flags.ts => core/flags/global.flags.ts} (70%) create mode 100644 src/adapters/cli/core/flags/new/file.flags.ts create mode 100644 src/adapters/cli/core/flags/new/glee.flags.ts create mode 100644 src/adapters/cli/core/flags/optimize.flags.ts create mode 100644 src/adapters/cli/core/flags/start/api.flags.ts create mode 100644 src/adapters/cli/core/flags/start/studio.flags.ts create mode 100644 src/adapters/cli/core/flags/validate.flags.ts rename src/adapters/cli/{ => core}/global.d.ts (100%) rename src/adapters/cli/{ => core}/globals.ts (95%) rename src/adapters/cli/{ => core}/hooks/command_not_found/myhook.ts (100%) rename src/{internal => adapters/cli/core}/models/Context.ts (99%) rename src/{internal => adapters/cli/core}/models/SpecificationFile.ts (97%) rename src/{internal => adapters/cli/core}/models/Studio.ts (97%) rename src/adapters/cli/{ => core}/parser.ts (98%) rename src/{internal => core/services}/archiver.service.ts (94%) rename src/{internal => core/services}/generator.service.ts (86%) diff --git a/src/adapters/api/controllers/generator.controller.ts b/src/adapters/api/controllers/generator.controller.ts index 5f47142c99f..81af45ce560 100644 --- a/src/adapters/api/controllers/generator.controller.ts +++ b/src/adapters/api/controllers/generator.controller.ts @@ -1,8 +1,8 @@ import { Router, Request, Response, NextFunction } from 'express'; -import { Controller } from '../interfaces'; -import { ArchiverService } from '../../../internal/archiver.service'; -import { GeneratorService } from '../../../internal/generator.service'; -import { ProblemException } from '../exceptions/problem.exception'; +import { Controller } from '../core/interfaces'; +import { ArchiverService } from '../../../core/services/archiver.service'; +import { GeneratorService } from '../../../core/services/generator.service'; +import { ProblemException } from '../core/exceptions/problem.exception'; // import { ProblemException } from '../exceptions/problem.exception'; export class GeneratorController implements Controller { diff --git a/src/adapters/api/controllers/ping.controller.ts b/src/adapters/api/controllers/ping.controller.ts index f9a5fee6b65..4769f6d07a0 100644 --- a/src/adapters/api/controllers/ping.controller.ts +++ b/src/adapters/api/controllers/ping.controller.ts @@ -1,5 +1,5 @@ import { Router, Request, Response, NextFunction } from 'express'; -import { Controller } from '../interfaces'; +import { Controller } from '../core/interfaces'; // import { ProblemException } from '../exceptions/problem.exception'; export class PingController implements Controller { diff --git a/src/adapters/api/app.ts b/src/adapters/api/core/app.ts similarity index 98% rename from src/adapters/api/app.ts rename to src/adapters/api/core/app.ts index 007fb699925..b2854d0e0ca 100644 --- a/src/adapters/api/app.ts +++ b/src/adapters/api/core/app.ts @@ -6,7 +6,7 @@ import cors from 'cors'; // @ts-ignore import helmet from 'helmet'; import { problemMiddleware } from './middlewares/problem.middleware'; -import { logger } from '../../utils/logger'; +import { logger } from '../../../utils/logger'; import { API_VERSION, CORS_ORIGIN, CORS_CREDENTIALS, diff --git a/src/adapters/api/constants.ts b/src/adapters/api/core/constants.ts similarity index 100% rename from src/adapters/api/constants.ts rename to src/adapters/api/core/constants.ts diff --git a/src/adapters/api/exceptions/problem.exception.ts b/src/adapters/api/core/exceptions/problem.exception.ts similarity index 100% rename from src/adapters/api/exceptions/problem.exception.ts rename to src/adapters/api/core/exceptions/problem.exception.ts diff --git a/src/adapters/api/interfaces.ts b/src/adapters/api/core/interfaces.ts similarity index 100% rename from src/adapters/api/interfaces.ts rename to src/adapters/api/core/interfaces.ts diff --git a/src/adapters/api/middlewares/problem.middleware.ts b/src/adapters/api/core/middlewares/problem.middleware.ts similarity index 95% rename from src/adapters/api/middlewares/problem.middleware.ts rename to src/adapters/api/core/middlewares/problem.middleware.ts index 91e8492724e..4cb0d4852da 100644 --- a/src/adapters/api/middlewares/problem.middleware.ts +++ b/src/adapters/api/core/middlewares/problem.middleware.ts @@ -1,7 +1,7 @@ import { NextFunction, Request, Response } from 'express'; import { ProblemException } from '../exceptions/problem.exception'; -import { logger } from '../../../utils/logger'; +import { logger } from '../../../../utils/logger'; /** * Catch problem exception, log it and serialize error to human readable form. diff --git a/src/adapters/api/server-api.d.ts b/src/adapters/api/core/server-api.d.ts similarity index 100% rename from src/adapters/api/server-api.d.ts rename to src/adapters/api/core/server-api.d.ts diff --git a/src/adapters/cli/commands/bundle.ts b/src/adapters/cli/commands/bundle.ts index 833304f5d4d..e7de0236162 100644 --- a/src/adapters/cli/commands/bundle.ts +++ b/src/adapters/cli/commands/bundle.ts @@ -1,10 +1,10 @@ -import { Flags } from '@oclif/core'; import { Example } from '@oclif/core/lib/interfaces'; -import Command from '../base'; +import Command from '../core/base'; import bundle from '@asyncapi/bundler'; import { promises } from 'fs'; import path from 'path'; -import { Specification, load } from '../../../internal/models/SpecificationFile'; +import { Specification, load } from '../core/models/SpecificationFile'; +import { bundleFlags } from '../core/flags/bundle.flags'; const { writeFile } = promises; @@ -19,13 +19,7 @@ export default class Bundle extends Command { 'asyncapi bundle ./asyncapi.yaml ./features.yaml --base ./asyncapi.yaml --reference-into-components' ]; - static flags = { - help: Flags.help({ char: 'h' }), - output: Flags.string({ char: 'o', description: 'The output file name. Omitting this flag the result will be printed in the console.' }), - 'reference-into-components': Flags.boolean({ char: 'r', description: 'Bundle the message $refs into components object.' }), - base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }), - }; - + static flags = bundleFlags(); async run() { const { argv, flags } = await this.parse(Bundle); const output = flags.output; diff --git a/src/adapters/cli/commands/config/context/add.ts b/src/adapters/cli/commands/config/context/add.ts index 6dc074062b9..37c927ffeab 100644 --- a/src/adapters/cli/commands/config/context/add.ts +++ b/src/adapters/cli/commands/config/context/add.ts @@ -1,23 +1,14 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; -import { addContext, setCurrentContext } from '../../../../../internal/models/Context'; +import Command from '../../../core/base'; +import { addContext, setCurrentContext } from '../../../core/models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, } from '../../../../../errors/context-error'; +import { addFlags } from '../../../core/flags/config/context.flags'; export default class ContextAdd extends Command { static description = 'Add a context to the store'; - static flags = { - help: Flags.help({ char: 'h' }), - 'set-current': Flags.boolean({ - char: 's', - description: 'Set context being added as the current context', - default: false, - required: false, - }) - }; - + static flags = addFlags(); static args = [ { name: 'context-name', description: 'context name', required: true }, { diff --git a/src/adapters/cli/commands/config/context/current.ts b/src/adapters/cli/commands/config/context/current.ts index 9a583c22393..6a84d6cb98c 100644 --- a/src/adapters/cli/commands/config/context/current.ts +++ b/src/adapters/cli/commands/config/context/current.ts @@ -1,18 +1,16 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; -import { getCurrentContext, CONTEXT_FILE_PATH } from '../../../../../internal/models/Context'; +import Command from '../../../core/base'; +import { getCurrentContext, CONTEXT_FILE_PATH } from '../../../core/models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, ContextFileEmptyError, ContextNotFoundError, } from '../../../../../errors/context-error'; +import { helpFlag } from '../../../core/flags/global.flags'; export default class ContextCurrent extends Command { static description = 'Shows the current context that is being used'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); async run() { let fileContent; diff --git a/src/adapters/cli/commands/config/context/edit.ts b/src/adapters/cli/commands/config/context/edit.ts index 7e9705d243b..0fe94af3acd 100644 --- a/src/adapters/cli/commands/config/context/edit.ts +++ b/src/adapters/cli/commands/config/context/edit.ts @@ -1,17 +1,15 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; -import { editContext, CONTEXT_FILE_PATH } from '../../../../../internal/models/Context'; +import Command from '../../../core/base'; +import { editContext, CONTEXT_FILE_PATH } from '../../../core/models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, ContextFileEmptyError, } from '../../../../../errors/context-error'; +import { helpFlag } from '../../../core/flags/global.flags'; export default class ContextEdit extends Command { static description = 'Edit a context in the store'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); static args = [ { name: 'context-name', description: 'context name', required: true }, diff --git a/src/adapters/cli/commands/config/context/index.ts b/src/adapters/cli/commands/config/context/index.ts index 0abf5da0b03..3ba767b80a3 100644 --- a/src/adapters/cli/commands/config/context/index.ts +++ b/src/adapters/cli/commands/config/context/index.ts @@ -1,5 +1,5 @@ import { loadHelpClass } from '@oclif/core'; -import Command from '../../../base'; +import Command from '../../../core/base'; export default class Context extends Command { static description = diff --git a/src/adapters/cli/commands/config/context/init.ts b/src/adapters/cli/commands/config/context/init.ts index b73ed228b74..5ba57f31072 100644 --- a/src/adapters/cli/commands/config/context/init.ts +++ b/src/adapters/cli/commands/config/context/init.ts @@ -1,12 +1,10 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; -import { initContext } from '../../../../../internal/models/Context'; +import Command from '../../../core/base'; +import { initContext } from '../../../core/models/Context'; +import { helpFlag } from '../../../core/flags/global.flags'; export default class ContextInit extends Command { static description = 'Initialize context'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); static contextFilePathMessage = `Specify directory in which context file should be created: - current directory : asyncapi config context init . (default) diff --git a/src/adapters/cli/commands/config/context/list.ts b/src/adapters/cli/commands/config/context/list.ts index cec431cd4d2..c4150830d60 100644 --- a/src/adapters/cli/commands/config/context/list.ts +++ b/src/adapters/cli/commands/config/context/list.ts @@ -1,20 +1,18 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; +import Command from '../../../core/base'; import { loadContextFile, isContextFileEmpty, CONTEXT_FILE_PATH, -} from '../../../../../internal/models/Context'; +} from '../../../core/models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, } from '../../../../../errors/context-error'; +import { helpFlag } from '../../../core/flags/global.flags'; export default class ContextList extends Command { static description = 'List all the stored contexts in the store'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); async run() { try { diff --git a/src/adapters/cli/commands/config/context/remove.ts b/src/adapters/cli/commands/config/context/remove.ts index e6d6adf8a2e..0a4ceb69cda 100644 --- a/src/adapters/cli/commands/config/context/remove.ts +++ b/src/adapters/cli/commands/config/context/remove.ts @@ -1,17 +1,15 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; -import { removeContext, CONTEXT_FILE_PATH } from '../../../../../internal/models/Context'; +import Command from '../../../core/base'; +import { removeContext, CONTEXT_FILE_PATH } from '../../../core/models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, ContextFileEmptyError, } from '../../../../../errors/context-error'; +import { helpFlag } from '../../../core/flags/global.flags'; export default class ContextRemove extends Command { static description = 'Delete a context from the store'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); static args = [ { diff --git a/src/adapters/cli/commands/config/context/use.ts b/src/adapters/cli/commands/config/context/use.ts index 4f376597be9..ce9e2da5274 100644 --- a/src/adapters/cli/commands/config/context/use.ts +++ b/src/adapters/cli/commands/config/context/use.ts @@ -1,17 +1,15 @@ -import { Flags } from '@oclif/core'; -import Command from '../../../base'; -import { setCurrentContext, CONTEXT_FILE_PATH } from '../../../../../internal/models/Context'; +import Command from '../../../core/base'; +import { setCurrentContext, CONTEXT_FILE_PATH } from '../../../core/models/Context'; import { MissingContextFileError, ContextFileWrongFormatError, ContextFileEmptyError, } from '../../../../../errors/context-error'; +import { helpFlag } from '../../../core/flags/global.flags'; export default class ContextUse extends Command { static description = 'Set a context as current'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); static args = [ { diff --git a/src/adapters/cli/commands/config/index.ts b/src/adapters/cli/commands/config/index.ts index f90430108de..ac5694a022f 100644 --- a/src/adapters/cli/commands/config/index.ts +++ b/src/adapters/cli/commands/config/index.ts @@ -1,4 +1,4 @@ -import Command from '../../base'; +import Command from '../../core/base'; import {loadHelpClass} from '@oclif/core'; export default class Config extends Command { diff --git a/src/adapters/cli/commands/config/versions.ts b/src/adapters/cli/commands/config/versions.ts index 76ce1953328..915f83ccb3f 100644 --- a/src/adapters/cli/commands/config/versions.ts +++ b/src/adapters/cli/commands/config/versions.ts @@ -1,12 +1,10 @@ -import { Flags } from '@oclif/core'; -import Command from '../../base'; +import Command from '../../core/base'; +import { helpFlag } from '../../core/flags/global.flags'; export default class Versions extends Command { static description = 'Show versions of AsyncAPI tools used'; - static flags = { - help: Flags.help({ char: 'h' }), - }; + static flags = helpFlag(); async run() { const dependencies: string[] = []; diff --git a/src/adapters/cli/commands/convert.ts b/src/adapters/cli/commands/convert.ts index 31740dbf030..da224d3f630 100644 --- a/src/adapters/cli/commands/convert.ts +++ b/src/adapters/cli/commands/convert.ts @@ -1,9 +1,8 @@ /* eslint-disable @typescript-eslint/ban-ts-comment */ import { promises as fPromises } from 'fs'; -import { Flags } from '@oclif/core'; -import Command from '../base'; +import Command from '../core/base'; import { ValidationError } from '../../../errors/validation-error'; -import { load } from '../../../internal/models/SpecificationFile'; +import { load } from '../core/models/SpecificationFile'; import { SpecificationFileNotFound } from '../../../errors/specification-file'; import { convert } from '@asyncapi/converter'; @@ -11,17 +10,14 @@ import type { ConvertVersion } from '@asyncapi/converter'; // @ts-ignore import specs from '@asyncapi/specs'; +import { convertFlags } from '../core/flags/convert.flags'; const latestVersion = Object.keys(specs.schemas).pop() as string; export default class Convert extends Command { static description = 'Convert asyncapi documents older to newer versions'; - static flags = { - help: Flags.help({ char: 'h' }), - output: Flags.string({ char: 'o', description: 'path to the file where the result is saved' }), - 'target-version': Flags.string({ char: 't', description: 'asyncapi version to convert to', default: latestVersion }) - }; + static flags = convertFlags(latestVersion); static args = [ { name: 'spec-file', description: 'spec path, url, or context-name', required: false }, diff --git a/src/adapters/cli/commands/diff.ts b/src/adapters/cli/commands/diff.ts index 96d23fece0c..9eb2e64fa57 100644 --- a/src/adapters/cli/commands/diff.ts +++ b/src/adapters/cli/commands/diff.ts @@ -1,11 +1,10 @@ /* eslint-disable sonarjs/no-duplicate-string */ -import { Flags } from '@oclif/core'; import * as diff from '@asyncapi/diff'; import AsyncAPIDiff from '@asyncapi/diff/lib/asyncapidiff'; import { promises as fs } from 'fs'; import chalk from 'chalk'; -import { load, Specification } from '../../../internal/models/SpecificationFile'; -import Command from '../base'; +import { load, Specification } from '../core/models/SpecificationFile'; +import Command from '../core/base'; import { ValidationError } from '../../../errors/validation-error'; import { SpecificationFileNotFound } from '../../../errors/specification-file'; import { @@ -13,46 +12,18 @@ import { DiffOverrideFileError, DiffOverrideJSONError, } from '../../../errors/diff-error'; -import { specWatcher } from '../globals'; -import { watchFlag } from '../flags'; -import { validationFlags, parse, convertToOldAPI } from '../parser'; +import { specWatcher } from '../core/globals'; +import { parse, convertToOldAPI } from '../core/parser'; -import type { SpecWatcherParams } from '../globals'; +import type { SpecWatcherParams } from '../core/globals'; +import { diffFlags } from '../core/flags/diff.flags'; const { readFile } = fs; export default class Diff extends Command { static description = 'Find diff between two asyncapi files'; - static flags = { - help: Flags.help({ char: 'h' }), - format: Flags.string({ - char: 'f', - description: 'format of the output', - default: 'yaml', - options: ['json', 'yaml', 'yml', 'md'], - }), - type: Flags.string({ - char: 't', - description: 'type of the output', - default: 'all', - options: ['breaking', 'non-breaking', 'unclassified', 'all'], - }), - markdownSubtype: Flags.string({ - description: 'the format of changes made to AsyncAPI document. It works only when diff is generated using md type. For example, when you specify subtype as json, then diff information in markdown is dumped as json structure.', - default: undefined, - options: ['json', 'yaml', 'yml'] - }), - overrides: Flags.string({ - char: 'o', - description: 'path to JSON file containing the override properties', - }), - 'no-error': Flags.boolean({ - description: 'don\'t show error on breaking changes', - }), - watch: watchFlag(), - ...validationFlags({ logDiagnostics: false }), - }; + static flags = diffFlags(); static args = [ { diff --git a/src/adapters/cli/commands/generate/fromTemplate.ts b/src/adapters/cli/commands/generate/fromTemplate.ts index 60e3d189d4f..566c977cf0f 100644 --- a/src/adapters/cli/commands/generate/fromTemplate.ts +++ b/src/adapters/cli/commands/generate/fromTemplate.ts @@ -1,17 +1,17 @@ -import { Flags, CliUx } from '@oclif/core'; -import Command from '../../base'; +import { CliUx } from '@oclif/core'; +import Command from '../../core/base'; // eslint-disable-next-line // @ts-ignore import path from 'path'; import fs from 'fs'; -import { load, Specification } from '../../../../internal/models/SpecificationFile'; -import { watchFlag } from '../../flags'; +import { load, Specification } from '../../core/models/SpecificationFile'; import { isLocalTemplate, Watcher } from '../../../../utils/generator'; import { ValidationError } from '../../../../errors/validation-error'; import { GeneratorError } from '../../../../errors/generator-error'; import type { Example } from '@oclif/core/lib/interfaces'; -import { GeneratorService } from '../../../../internal/generator.service'; +import { GeneratorService } from '../../../../core/services/generator.service'; +import { fromTemplateFlags } from '../../core/flags/generate/fromTemplate.flags'; const red = (text: string) => `\x1b[31m${text}\x1b[0m`; const magenta = (text: string) => `\x1b[35m${text}\x1b[0m`; @@ -62,46 +62,7 @@ export default class Template extends Command { static generatorService = new GeneratorService(); - static flags = { - help: Flags.help({ char: 'h' }), - 'disable-hook': Flags.string({ - char: 'd', - description: 'Disable a specific hook type or hooks from a given hook type', - multiple: true - }), - install: Flags.boolean({ - char: 'i', - default: false, - description: 'Installs the template and its dependencies (defaults to false)' - }), - debug: Flags.boolean({ - description: 'Enable more specific errors in the console' - }), - 'no-overwrite': Flags.string({ - char: 'n', - multiple: true, - description: 'Glob or path of the file(s) to skip when regenerating' - }), - output: Flags.string({ - char: 'o', - description: 'Directory where to put the generated files (defaults to current directory)', - }), - 'force-write': Flags.boolean({ - default: false, - description: 'Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)' - }), - watch: watchFlag( - 'Watches the template directory and the AsyncAPI document, and re-generate the files when changes occur. Ignores the output directory.' - ), - param: Flags.string({ - char: 'p', - description: 'Additional param to pass to templates', - multiple: true - }), - 'map-base-url': Flags.string({ - description: 'Maps all schema references from base url to local folder' - }), - }; + static flags = fromTemplateFlags(); static args = [ { name: 'asyncapi', description: '- Local path, url or context-name pointing to AsyncAPI file', required: true }, diff --git a/src/adapters/cli/commands/generate/index.ts b/src/adapters/cli/commands/generate/index.ts index fd719df01ee..643bca0df0c 100644 --- a/src/adapters/cli/commands/generate/index.ts +++ b/src/adapters/cli/commands/generate/index.ts @@ -1,4 +1,4 @@ -import Command from '../../base'; +import Command from '../../core/base'; import { Help } from '@oclif/core'; export default class Generate extends Command { diff --git a/src/adapters/cli/commands/generate/models.ts b/src/adapters/cli/commands/generate/models.ts index 86190cd6430..48a5df29a03 100644 --- a/src/adapters/cli/commands/generate/models.ts +++ b/src/adapters/cli/commands/generate/models.ts @@ -1,11 +1,11 @@ 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 { ConvertDocumentParserAPIVersion } from '@smoya/multi-parser'; -import Command from '../../base'; -import { load } from '../../../../internal/models/SpecificationFile'; -import { formatOutput, parse, validationFlags } from '../../parser'; +import Command from '../../core/base'; +import { load } from '../../core/models/SpecificationFile'; +import { formatOutput, parse } from '../../core/parser'; import type { AbstractGenerator, AbstractFileGenerator } from '@asyncapi/modelina'; +import { modelsFlags } from '../../core/flags/generate/models.flags'; enum Languages { typescript = 'typescript', @@ -34,136 +34,7 @@ export default class Models extends Command { { name: 'file', description: 'Path or URL to the AsyncAPI document, or context-name', required: true }, ]; - static flags = { - help: Flags.help({ char: 'h' }), - output: Flags.string({ - char: 'o', - description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.', - required: false - }), - /** - * TypeScript specific options - */ - tsModelType: Flags.string({ - type: 'option', - options: ['class', 'interface'], - description: 'TypeScript specific, define which type of model needs to be generated.', - required: false, - default: 'class', - }), - tsEnumType: Flags.string({ - type: 'option', - options: ['enum', 'union'], - description: 'TypeScript specific, define which type of enums needs to be generated.', - required: false, - default: 'enum', - }), - tsModuleSystem: Flags.string({ - type: 'option', - options: ['ESM', 'CJS'], - description: 'TypeScript specific, define the module system to be used.', - required: false, - default: 'ESM', - - }), - tsIncludeComments: Flags.boolean({ - description: 'TypeScript specific, if enabled add comments while generating models.', - required: false, - default: false, - }), - tsExportType: Flags.string({ - type: 'option', - options: ['default', 'named'], - description: 'TypeScript specific, define which type of export needs to be generated.', - required: false, - default: 'default', - }), - tsJsonBinPack: Flags.boolean({ - description: 'TypeScript specific, define basic support for serializing to and from binary with jsonbinpack.', - required: false, - default: false, - }), - tsMarshalling: Flags.boolean({ - description: 'TypeScript specific, generate the models with marshalling functions.', - required: false, - default: false, - }), - tsExampleInstance: Flags.boolean({ - description: 'Typescript specific, generate example of the model', - required: false, - default: false, - }), - /** - * Go and Java specific package name to use for the generated models - */ - packageName: Flags.string({ - description: 'Go, Java and Kotlin specific, define the package to use for the generated models. This is required when language is `go`, `java` or `kotlin`.', - required: false - }), - /** - * Java specific options - */ - javaIncludeComments: Flags.boolean({ - description: 'Java specific, if enabled add comments while generating models.', - required: false, - default: false - }), - javaJackson: Flags.boolean({ - description: 'Java specific, generate the models with Jackson serialization support', - required: false, - default: false - }), - javaConstraints: Flags.boolean({ - description: 'Java specific, generate the models with constraints', - required: false, - default: false - }), - - /** - * C++ and C# and PHP specific namespace to use for the generated models - */ - namespace: Flags.string({ - description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.', - required: false - }), - - /** - * C# specific options - */ - csharpAutoImplement: Flags.boolean({ - description: 'C# specific, define whether to generate auto-implemented properties or not.', - required: false, - default: false - }), - csharpNewtonsoft: Flags.boolean({ - description: 'C# specific, generate the models with newtonsoft serialization support', - required: false, - default: false - }), - csharpArrayType: Flags.string({ - type: 'option', - description: 'C# specific, define which type of array needs to be generated.', - options: ['Array', 'List'], - required: false, - default: 'Array' - }), - csharpHashcode: Flags.boolean({ - description: 'C# specific, generate the models with the GetHashCode method overwritten', - required: false, - default: false - }), - csharpEqual: Flags.boolean({ - description: 'C# specific, generate the models with the Equal method overwritten', - required: false, - default: false - }), - csharpSystemJson: Flags.boolean({ - description: 'C# specific, generate the models with System.Text.Json serialization support', - required: false, - default: false - }), - ...validationFlags({ logDiagnostics: false }), - }; + static flags = modelsFlags(); /* eslint-disable sonarjs/cognitive-complexity */ async run() { diff --git a/src/adapters/cli/commands/new/file.ts b/src/adapters/cli/commands/new/file.ts index 1651e2a0e5a..20401c29335 100644 --- a/src/adapters/cli/commands/new/file.ts +++ b/src/adapters/cli/commands/new/file.ts @@ -1,10 +1,10 @@ -import {Flags} from '@oclif/core'; import { promises as fPromises, readFileSync } from 'fs'; -import Command from '../../base'; +import Command from '../../core/base'; import * as inquirer from 'inquirer'; -import { start as startStudio, DEFAULT_PORT } from '../../../../internal/models/Studio'; +import { start as startStudio, DEFAULT_PORT } from '../../core/models/Studio'; import { runApi } from '../../../../api'; import { resolve } from 'path'; +import { fileFlags } from '../../core/flags/new/file.flags'; const { writeFile, readFile } = fPromises; const DEFAULT_ASYNCAPI_FILE_NAME = 'asyncapi.yaml'; @@ -32,14 +32,7 @@ function getExamplesFlagDescription (): string { export default class NewFile extends Command { static description = 'Creates a new asyncapi file'; - static flags = { - help: Flags.help({ char: 'h' }), - 'file-name': Flags.string({ char: 'n', description: 'name of the file' }), - example: Flags.string({ char: 'e', description: getExamplesFlagDescription() }), - studio: Flags.boolean({ char: 's', description: 'open in Studio' }), - port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }), - 'no-tty': Flags.boolean({ description: 'do not use an interactive terminal' }), - }; + static flags = fileFlags(getExamplesFlagDescription()); static examples = [ 'asyncapi new\t - start creation of a file in interactive mode', diff --git a/src/adapters/cli/commands/new/glee.ts b/src/adapters/cli/commands/new/glee.ts index 0886c14dcd0..d2367ae64dd 100644 --- a/src/adapters/cli/commands/new/glee.ts +++ b/src/adapters/cli/commands/new/glee.ts @@ -1,43 +1,20 @@ -import { Flags } from '@oclif/core'; import { promises as fPromises } from 'fs'; -import Command from '../../base'; +import Command from '../../core/base'; import path, { resolve, join } from 'path'; import fs from 'fs-extra'; -import { Specification, load } from '../../../../internal/models/SpecificationFile'; +import { Specification, load } from '../../core/models/SpecificationFile'; import yaml from 'js-yaml'; import { prompt } from 'inquirer'; // eslint-disable-next-line // @ts-ignore import Generator from '@asyncapi/generator'; +import { gleeFlags } from '../../core/flags/new/glee.flags'; export default class NewGlee extends Command { static description = 'Creates a new Glee project'; protected commandName = 'glee'; - static flags = { - help: Flags.help({ char: 'h' }), - name: Flags.string({ - char: 'n', - description: 'Name of the Project', - default: 'project', - }), - template: Flags.string({ - char: 't', - description: 'Name of the Template', - default: 'default', - }), - file: Flags.string({ - char: 'f', - description: - 'The path to the AsyncAPI file for generating a Glee project.', - }), - 'force-write': Flags.boolean({ - default: false, - description: - 'Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)', - }), - }; - + static flags = gleeFlags(); async getFilteredServers(serversObject: any) { console.log({ serversObject }); const servers = Object.keys(serversObject); diff --git a/src/adapters/cli/commands/optimize.ts b/src/adapters/cli/commands/optimize.ts index 2097e03bb62..43f7f088167 100644 --- a/src/adapters/cli/commands/optimize.ts +++ b/src/adapters/cli/commands/optimize.ts @@ -1,25 +1,14 @@ -import { Flags } from '@oclif/core'; import { Optimizer, Output, Report, ReportElement } from '@asyncapi/optimizer'; -import Command from '../base'; +import Command from '../core/base'; import { ValidationError } from '../../../errors/validation-error'; -import { load, Specification } from '../../../internal/models/SpecificationFile'; +import { load, Specification } from '../core/models/SpecificationFile'; import * as inquirer from 'inquirer'; import chalk from 'chalk'; import { promises } from 'fs'; import { Example } from '@oclif/core/lib/interfaces'; +import { optimizeFlags, Optimizations, Outputs } from '../core/flags/optimize.flags'; const { writeFile } = promises; -export enum Optimizations { - REMOVE_COMPONENTS='remove-components', - REUSE_COMPONENTS='reuse-components', - MOVE_TO_COMPONETS='move-to-components' -} - -export enum Outputs { - TERMINAL='terminal', - NEW_FILE='new-file', - OVERWRITE='overwrite' -} export default class Optimize extends Command { static description = 'optimize asyncapi specification file'; isInteractive = false; @@ -33,12 +22,7 @@ export default class Optimize extends Command { 'asyncapi optimize ./asyncapi.yaml --optimization=remove-components,reuse-components,move-to-components --output=terminal --no-tty', ]; - static flags = { - help: Flags.help({ char: 'h' }), - optimization: Flags.string({char: 'p', default: Object.values(Optimizations), options: Object.values(Optimizations), multiple: true, description: 'select the type of optimizations that you want to apply.'}), - output: Flags.string({char: 'o', default: Outputs.TERMINAL, options: Object.values(Outputs), description: 'select where you want the output.'}), - 'no-tty': Flags.boolean({ description: 'do not use an interactive terminal', default: false }), - }; + static flags = optimizeFlags(); static args = [ { name: 'spec-file', description: 'spec path, url, or context-name', required: false }, diff --git a/src/adapters/cli/commands/start/api.ts b/src/adapters/cli/commands/start/api.ts index e9c4aacf731..4182cf7e78f 100644 --- a/src/adapters/cli/commands/start/api.ts +++ b/src/adapters/cli/commands/start/api.ts @@ -1,14 +1,13 @@ -import {Flags} from '@oclif/core'; -import Command from '../../base'; +import Command from '../../core/base'; import { runApi } from '../../../../api'; +import { apiFlags } from '../../core/flags/start/api.flags'; + +const defaultPort = 3001; export default class StartApi extends Command { static description = 'starts a new local instance of AsyncAPI'; - static flags = { - help: Flags.help({ char: 'h' }), - port: Flags.integer({ char: 'p', description: 'port in which to start API', default: 3001 }), - }; + static flags = apiFlags(defaultPort); static args = []; diff --git a/src/adapters/cli/commands/start/index.ts b/src/adapters/cli/commands/start/index.ts index 2641a297aed..9060d443653 100644 --- a/src/adapters/cli/commands/start/index.ts +++ b/src/adapters/cli/commands/start/index.ts @@ -1,4 +1,4 @@ -import Command from '../../base'; +import Command from '../../core/base'; import { Help } from '@oclif/core'; export default class Start extends Command { diff --git a/src/adapters/cli/commands/start/studio.ts b/src/adapters/cli/commands/start/studio.ts index 20bfff4caa1..0a9b7b76302 100644 --- a/src/adapters/cli/commands/start/studio.ts +++ b/src/adapters/cli/commands/start/studio.ts @@ -1,16 +1,12 @@ -import {Flags} from '@oclif/core'; -import Command from '../../base'; -import { start as startStudio } from '../../../../internal/models/Studio'; -import { load } from '../../../../internal/models/SpecificationFile'; +import Command from '../../core/base'; +import { start as startStudio } from '../../core/models/Studio'; +import { load } from '../../core/models/SpecificationFile'; +import { studioFlags } from '../../core/flags/start/studio.flags'; export default class StartStudio extends Command { static description = 'starts a new local instance of Studio'; - static flags = { - help: Flags.help({ char: 'h' }), - file: Flags.string({ char: 'f', description: 'path to the AsyncAPI file to link with Studio' }), - port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }), - }; + static flags = studioFlags(); static args = []; diff --git a/src/adapters/cli/commands/validate.ts b/src/adapters/cli/commands/validate.ts index 31c7d595e1e..e65814b0276 100644 --- a/src/adapters/cli/commands/validate.ts +++ b/src/adapters/cli/commands/validate.ts @@ -1,19 +1,12 @@ -import { Flags } from '@oclif/core'; - -import Command from '../base'; -import { validate, validationFlags } from '../parser'; -import { load } from '../../../internal/models/SpecificationFile'; -import { specWatcher } from '../globals'; -import { watchFlag } from '../flags'; - +import Command from '../core/base'; +import { validate } from '../core/parser'; +import { load } from '../core/models/SpecificationFile'; +import { specWatcher } from '../core/globals'; +import { validateFlags } from '../core/flags/validate.flags'; export default class Validate extends Command { static description = 'validate asyncapi file'; - static flags = { - help: Flags.help({ char: 'h' }), - watch: watchFlag(), - ...validationFlags(), - }; + static flags = validateFlags(); static args = [ { name: 'spec-file', description: 'spec path, url, or context-name', required: false }, diff --git a/src/adapters/cli/base.ts b/src/adapters/cli/core/base.ts similarity index 100% rename from src/adapters/cli/base.ts rename to src/adapters/cli/core/base.ts diff --git a/src/adapters/cli/core/flags/bundle.flags.ts b/src/adapters/cli/core/flags/bundle.flags.ts new file mode 100644 index 00000000000..669ed5b00aa --- /dev/null +++ b/src/adapters/cli/core/flags/bundle.flags.ts @@ -0,0 +1,10 @@ +import { Flags } from '@oclif/core'; + +export const bundleFlags = () => { + return { + help: Flags.help({ char: 'h' }), + output: Flags.string({ char: 'o', description: 'The output file name. Omitting this flag the result will be printed in the console.' }), + 'reference-into-components': Flags.boolean({ char: 'r', description: 'Bundle the message $refs into components object.' }), + base: Flags.string({ char: 'b', description: 'Path to the file which will act as a base. This is required when some properties are to needed to be overwritten.' }), + }; +}; diff --git a/src/adapters/cli/core/flags/config/context.flags.ts b/src/adapters/cli/core/flags/config/context.flags.ts new file mode 100644 index 00000000000..ce8db51ce12 --- /dev/null +++ b/src/adapters/cli/core/flags/config/context.flags.ts @@ -0,0 +1,14 @@ +import { Flags } from '@oclif/core'; + +export const addFlags = () => { + return { + help: Flags.help({ char: 'h' }), + 'set-current': Flags.boolean({ + char: 's', + description: 'Set context being added as the current context', + default: false, + required: false, + }) + }; +}; + diff --git a/src/adapters/cli/core/flags/convert.flags.ts b/src/adapters/cli/core/flags/convert.flags.ts new file mode 100644 index 00000000000..98c7bd2c7e9 --- /dev/null +++ b/src/adapters/cli/core/flags/convert.flags.ts @@ -0,0 +1,9 @@ +import { Flags } from '@oclif/core'; + +export const convertFlags = (latestVersion: string) => { + return { + help: Flags.help({ char: 'h' }), + output: Flags.string({ char: 'o', description: 'path to the file where the result is saved' }), + 'target-version': Flags.string({ char: 't', description: 'asyncapi version to convert to', default: latestVersion }) + }; +}; diff --git a/src/adapters/cli/core/flags/diff.flags.ts b/src/adapters/cli/core/flags/diff.flags.ts new file mode 100644 index 00000000000..ca29bc54c44 --- /dev/null +++ b/src/adapters/cli/core/flags/diff.flags.ts @@ -0,0 +1,35 @@ +import { Flags } from '@oclif/core'; +import { validationFlags } from '../parser'; +import { watchFlag } from './global.flags'; + +export const diffFlags = () => { + return { + help: Flags.help({ char: 'h' }), + format: Flags.string({ + char: 'f', + description: 'format of the output', + default: 'yaml', + options: ['json', 'yaml', 'yml', 'md'], + }), + type: Flags.string({ + char: 't', + description: 'type of the output', + default: 'all', + options: ['breaking', 'non-breaking', 'unclassified', 'all'], + }), + markdownSubtype: Flags.string({ + description: 'the format of changes made to AsyncAPI document. It works only when diff is generated using md type. For example, when you specify subtype as json, then diff information in markdown is dumped as json structure.', + default: undefined, + options: ['json', 'yaml', 'yml'] + }), + overrides: Flags.string({ + char: 'o', + description: 'path to JSON file containing the override properties', + }), + 'no-error': Flags.boolean({ + description: 'don\'t show error on breaking changes', + }), + watch: watchFlag(), + ...validationFlags({ logDiagnostics: false }), + }; +}; diff --git a/src/adapters/cli/core/flags/generate/fromTemplate.flags.ts b/src/adapters/cli/core/flags/generate/fromTemplate.flags.ts new file mode 100644 index 00000000000..ec9ac427e40 --- /dev/null +++ b/src/adapters/cli/core/flags/generate/fromTemplate.flags.ts @@ -0,0 +1,45 @@ +import { Flags } from '@oclif/core'; +import { watchFlag } from '../global.flags'; + +export const fromTemplateFlags = () => { + return { + help: Flags.help({ char: 'h' }), + 'disable-hook': Flags.string({ + char: 'd', + description: 'Disable a specific hook type or hooks from a given hook type', + multiple: true + }), + install: Flags.boolean({ + char: 'i', + default: false, + description: 'Installs the template and its dependencies (defaults to false)' + }), + debug: Flags.boolean({ + description: 'Enable more specific errors in the console' + }), + 'no-overwrite': Flags.string({ + char: 'n', + multiple: true, + description: 'Glob or path of the file(s) to skip when regenerating' + }), + output: Flags.string({ + char: 'o', + description: 'Directory where to put the generated files (defaults to current directory)', + }), + 'force-write': Flags.boolean({ + default: false, + description: 'Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)' + }), + watch: watchFlag( + 'Watches the template directory and the AsyncAPI document, and re-generate the files when changes occur. Ignores the output directory.' + ), + param: Flags.string({ + char: 'p', + description: 'Additional param to pass to templates', + multiple: true + }), + 'map-base-url': Flags.string({ + description: 'Maps all schema references from base url to local folder' + }), + }; +}; diff --git a/src/adapters/cli/core/flags/generate/models.flags.ts b/src/adapters/cli/core/flags/generate/models.flags.ts new file mode 100644 index 00000000000..c06c94d734a --- /dev/null +++ b/src/adapters/cli/core/flags/generate/models.flags.ts @@ -0,0 +1,135 @@ +import { Flags } from '@oclif/core'; +import { validationFlags } from '../../parser'; + +export const modelsFlags = () => { + return { + help: Flags.help({ char: 'h' }), + output: Flags.string({ + char: 'o', + description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.', + required: false + }), + /** + * TypeScript specific options + */ + tsModelType: Flags.string({ + type: 'option', + options: ['class', 'interface'], + description: 'TypeScript specific, define which type of model needs to be generated.', + required: false, + default: 'class', + }), + tsEnumType: Flags.string({ + type: 'option', + options: ['enum', 'union'], + description: 'TypeScript specific, define which type of enums needs to be generated.', + required: false, + default: 'enum', + }), + tsModuleSystem: Flags.string({ + type: 'option', + options: ['ESM', 'CJS'], + description: 'TypeScript specific, define the module system to be used.', + required: false, + default: 'ESM', + + }), + tsIncludeComments: Flags.boolean({ + description: 'TypeScript specific, if enabled add comments while generating models.', + required: false, + default: false, + }), + tsExportType: Flags.string({ + type: 'option', + options: ['default', 'named'], + description: 'TypeScript specific, define which type of export needs to be generated.', + required: false, + default: 'default', + }), + tsJsonBinPack: Flags.boolean({ + description: 'TypeScript specific, define basic support for serializing to and from binary with jsonbinpack.', + required: false, + default: false, + }), + tsMarshalling: Flags.boolean({ + description: 'TypeScript specific, generate the models with marshalling functions.', + required: false, + default: false, + }), + tsExampleInstance: Flags.boolean({ + description: 'Typescript specific, generate example of the model', + required: false, + default: false, + }), + /** + * Go and Java specific package name to use for the generated models + */ + packageName: Flags.string({ + description: 'Go, Java and Kotlin specific, define the package to use for the generated models. This is required when language is `go`, `java` or `kotlin`.', + required: false + }), + /** + * Java specific options + */ + javaIncludeComments: Flags.boolean({ + description: 'Java specific, if enabled add comments while generating models.', + required: false, + default: false + }), + javaJackson: Flags.boolean({ + description: 'Java specific, generate the models with Jackson serialization support', + required: false, + default: false + }), + javaConstraints: Flags.boolean({ + description: 'Java specific, generate the models with constraints', + required: false, + default: false + }), + + /** + * C++ and C# and PHP specific namespace to use for the generated models + */ + namespace: Flags.string({ + description: 'C#, C++ and PHP specific, define the namespace to use for the generated models. This is required when language is `csharp`,`c++` or `php`.', + required: false + }), + + /** + * C# specific options + */ + csharpAutoImplement: Flags.boolean({ + description: 'C# specific, define whether to generate auto-implemented properties or not.', + required: false, + default: false + }), + csharpNewtonsoft: Flags.boolean({ + description: 'C# specific, generate the models with newtonsoft serialization support', + required: false, + default: false + }), + csharpArrayType: Flags.string({ + type: 'option', + description: 'C# specific, define which type of array needs to be generated.', + options: ['Array', 'List'], + required: false, + default: 'Array' + }), + csharpHashcode: Flags.boolean({ + description: 'C# specific, generate the models with the GetHashCode method overwritten', + required: false, + default: false + }), + csharpEqual: Flags.boolean({ + description: 'C# specific, generate the models with the Equal method overwritten', + required: false, + default: false + }), + csharpSystemJson: Flags.boolean({ + description: 'C# specific, generate the models with System.Text.Json serialization support', + required: false, + default: false + }), + ...validationFlags({ logDiagnostics: false }), + }; +}; diff --git a/src/adapters/cli/flags.ts b/src/adapters/cli/core/flags/global.flags.ts similarity index 70% rename from src/adapters/cli/flags.ts rename to src/adapters/cli/core/flags/global.flags.ts index 9ca21cff9f5..cf53c4a472f 100644 --- a/src/adapters/cli/flags.ts +++ b/src/adapters/cli/core/flags/global.flags.ts @@ -7,3 +7,10 @@ export const watchFlag = (description?: string) => { description: description ?? 'Enable watch mode', }); }; + +export const helpFlag = () => { + return { + help: Flags.help({ char: 'h' }) + }; +}; + diff --git a/src/adapters/cli/core/flags/new/file.flags.ts b/src/adapters/cli/core/flags/new/file.flags.ts new file mode 100644 index 00000000000..7c0c24ff718 --- /dev/null +++ b/src/adapters/cli/core/flags/new/file.flags.ts @@ -0,0 +1,12 @@ +import { Flags } from '@oclif/core'; + +export const fileFlags = (exampleFlagDescription: string) => { + return { + help: Flags.help({ char: 'h' }), + 'file-name': Flags.string({ char: 'n', description: 'name of the file' }), + example: Flags.string({ char: 'e', description: exampleFlagDescription }), + studio: Flags.boolean({ char: 's', description: 'open in Studio' }), + port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }), + 'no-tty': Flags.boolean({ description: 'do not use an interactive terminal' }), + }; +}; diff --git a/src/adapters/cli/core/flags/new/glee.flags.ts b/src/adapters/cli/core/flags/new/glee.flags.ts new file mode 100644 index 00000000000..9139b35b0fa --- /dev/null +++ b/src/adapters/cli/core/flags/new/glee.flags.ts @@ -0,0 +1,27 @@ +import { Flags } from '@oclif/core'; + +export const gleeFlags = () => { + return { + help: Flags.help({ char: 'h' }), + name: Flags.string({ + char: 'n', + description: 'Name of the Project', + default: 'project', + }), + template: Flags.string({ + char: 't', + description: 'Name of the Template', + default: 'default', + }), + file: Flags.string({ + char: 'f', + description: + 'The path to the AsyncAPI file for generating a Glee project.', + }), + 'force-write': Flags.boolean({ + default: false, + description: + 'Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir (defaults to false)', + }), + }; +}; diff --git a/src/adapters/cli/core/flags/optimize.flags.ts b/src/adapters/cli/core/flags/optimize.flags.ts new file mode 100644 index 00000000000..289483f8081 --- /dev/null +++ b/src/adapters/cli/core/flags/optimize.flags.ts @@ -0,0 +1,22 @@ +import { Flags } from '@oclif/core'; + +export enum Optimizations { + REMOVE_COMPONENTS='remove-components', + REUSE_COMPONENTS='reuse-components', + MOVE_TO_COMPONETS='move-to-components' +} + +export enum Outputs { + TERMINAL='terminal', + NEW_FILE='new-file', + OVERWRITE='overwrite' +} + +export const optimizeFlags = () => { + return { + help: Flags.help({ char: 'h' }), + optimization: Flags.string({char: 'p', default: Object.values(Optimizations), options: Object.values(Optimizations), multiple: true, description: 'select the type of optimizations that you want to apply.'}), + output: Flags.string({char: 'o', default: Outputs.TERMINAL, options: Object.values(Outputs), description: 'select where you want the output.'}), + 'no-tty': Flags.boolean({ description: 'do not use an interactive terminal', default: false }), + }; +}; diff --git a/src/adapters/cli/core/flags/start/api.flags.ts b/src/adapters/cli/core/flags/start/api.flags.ts new file mode 100644 index 00000000000..ac7317f2afe --- /dev/null +++ b/src/adapters/cli/core/flags/start/api.flags.ts @@ -0,0 +1,8 @@ +import { Flags } from '@oclif/core'; + +export const apiFlags = (defaultPort: number) => { + return { + help: Flags.help({ char: 'h' }), + port: Flags.integer({ char: 'p', description: 'port in which to start API', default: defaultPort }), + }; +}; diff --git a/src/adapters/cli/core/flags/start/studio.flags.ts b/src/adapters/cli/core/flags/start/studio.flags.ts new file mode 100644 index 00000000000..acc4a175713 --- /dev/null +++ b/src/adapters/cli/core/flags/start/studio.flags.ts @@ -0,0 +1,9 @@ +import { Flags } from '@oclif/core'; + +export const studioFlags = () => { + return { + help: Flags.help({ char: 'h' }), + file: Flags.string({ char: 'f', description: 'path to the AsyncAPI file to link with Studio' }), + port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }), + }; +}; diff --git a/src/adapters/cli/core/flags/validate.flags.ts b/src/adapters/cli/core/flags/validate.flags.ts new file mode 100644 index 00000000000..b86de912be6 --- /dev/null +++ b/src/adapters/cli/core/flags/validate.flags.ts @@ -0,0 +1,11 @@ +import { Flags } from '@oclif/core'; +import { validationFlags } from '../parser'; +import { watchFlag } from './global.flags'; + +export const validateFlags = () => { + return { + help: Flags.help({ char: 'h' }), + watch: watchFlag(), + ...validationFlags(), + }; +}; diff --git a/src/adapters/cli/global.d.ts b/src/adapters/cli/core/global.d.ts similarity index 100% rename from src/adapters/cli/global.d.ts rename to src/adapters/cli/core/global.d.ts diff --git a/src/adapters/cli/globals.ts b/src/adapters/cli/core/globals.ts similarity index 95% rename from src/adapters/cli/globals.ts rename to src/adapters/cli/core/globals.ts index 7ff345af81b..47c5acd910f 100644 --- a/src/adapters/cli/globals.ts +++ b/src/adapters/cli/core/globals.ts @@ -1,7 +1,7 @@ import chokidar from 'chokidar'; import chalk from 'chalk'; import Command from './base'; -import { Specification } from '../../internal/models/SpecificationFile'; +import { Specification } from './models/SpecificationFile'; const GreenLog = chalk.hex('#00FF00'); const OrangeLog = chalk.hex('#FFA500'); diff --git a/src/adapters/cli/hooks/command_not_found/myhook.ts b/src/adapters/cli/core/hooks/command_not_found/myhook.ts similarity index 100% rename from src/adapters/cli/hooks/command_not_found/myhook.ts rename to src/adapters/cli/core/hooks/command_not_found/myhook.ts diff --git a/src/internal/models/Context.ts b/src/adapters/cli/core/models/Context.ts similarity index 99% rename from src/internal/models/Context.ts rename to src/adapters/cli/core/models/Context.ts index f2f346d9a82..66736168dca 100644 --- a/src/internal/models/Context.ts +++ b/src/adapters/cli/core/models/Context.ts @@ -10,7 +10,7 @@ import { ContextAlreadyExistsError, ContextFileEmptyError, ContextFileWriteError, -} from '../../errors/context-error'; +} from '../../../../errors/context-error'; const { readFile, writeFile } = fs; diff --git a/src/internal/models/SpecificationFile.ts b/src/adapters/cli/core/models/SpecificationFile.ts similarity index 97% rename from src/internal/models/SpecificationFile.ts rename to src/adapters/cli/core/models/SpecificationFile.ts index 9afdaeafffd..c049123035a 100644 --- a/src/internal/models/SpecificationFile.ts +++ b/src/adapters/cli/core/models/SpecificationFile.ts @@ -4,8 +4,8 @@ import { URL } from 'url'; import fetch from 'node-fetch'; import yaml from 'js-yaml'; import { loadContext } from './Context'; -import { ErrorLoadingSpec } from '../../errors/specification-file'; -import { MissingContextFileError } from '../../errors/context-error'; +import { ErrorLoadingSpec } from '../../../../errors/specification-file'; +import { MissingContextFileError } from '../../../../errors/context-error'; const { readFile, lstat } = fs; const allowedFileNames: string[] = [ diff --git a/src/internal/models/Studio.ts b/src/adapters/cli/core/models/Studio.ts similarity index 97% rename from src/internal/models/Studio.ts rename to src/adapters/cli/core/models/Studio.ts index d4ab904c1dc..35825e71745 100644 --- a/src/internal/models/Studio.ts +++ b/src/adapters/cli/core/models/Studio.ts @@ -1,5 +1,5 @@ import { existsSync, promises as fPromises } from 'fs'; -import { SpecificationFileNotFound } from '../../errors/specification-file'; +import { SpecificationFileNotFound } from '../../../../errors/specification-file'; import { createServer } from 'http'; import serveHandler from 'serve-handler'; import { WebSocketServer } from 'ws'; diff --git a/src/adapters/cli/parser.ts b/src/adapters/cli/core/parser.ts similarity index 98% rename from src/adapters/cli/parser.ts rename to src/adapters/cli/core/parser.ts index faeca974ed8..8ac302c466a 100644 --- a/src/adapters/cli/parser.ts +++ b/src/adapters/cli/core/parser.ts @@ -10,7 +10,7 @@ import { html, json, junit, pretty, stylish, teamcity, text } from '@stoplight/s import type { Diagnostic } from '@asyncapi/parser/cjs'; import type Command from './base'; -import type { Specification } from '../../internal/models/SpecificationFile'; +import type { Specification } from './models/SpecificationFile'; export type SeveritytKind = 'error' | 'warn' | 'info' | 'hint'; diff --git a/src/api.ts b/src/api.ts index bfd2bbf88b8..2780dcf698b 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,4 +1,4 @@ -import { App } from './adapters/api/app'; +import { App } from './adapters/api/core/app'; import { PingController } from './adapters/api/controllers/ping.controller'; import { GeneratorController } from './adapters/api/controllers/generator.controller'; diff --git a/src/internal/archiver.service.ts b/src/core/services/archiver.service.ts similarity index 94% rename from src/internal/archiver.service.ts rename to src/core/services/archiver.service.ts index efbaaed0460..ef09355d59f 100644 --- a/src/internal/archiver.service.ts +++ b/src/core/services/archiver.service.ts @@ -1,7 +1,7 @@ import archiver, { Archiver } from 'archiver'; import { Response } from 'express'; -import { createTempDirectory, removeTempDirectory } from '../utils/temp-dir'; +import { createTempDirectory, removeTempDirectory } from '../../utils/temp-dir'; /** * Service wrapping the `archiver` module: diff --git a/src/internal/generator.service.ts b/src/core/services/generator.service.ts similarity index 86% rename from src/internal/generator.service.ts rename to src/core/services/generator.service.ts index 0b110e04a4c..c3710a91cd7 100644 --- a/src/internal/generator.service.ts +++ b/src/core/services/generator.service.ts @@ -1,8 +1,8 @@ // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore import AsyncAPIGenerator from '@asyncapi/generator'; -import { IGenerator } from '../ports/generator.interface'; -import { GeneratorError } from '../errors/generator-error'; +import { IGenerator } from '../../ports/generator.interface'; +import { GeneratorError } from '../../errors/generator-error'; import path from 'path'; import os from 'os'; diff --git a/test/helpers/index.ts b/test/helpers/index.ts index 0d4cc8387ec..24dec158af3 100644 --- a/test/helpers/index.ts +++ b/test/helpers/index.ts @@ -1,7 +1,7 @@ import { existsSync, writeFileSync, unlinkSync,rmdirSync, mkdirSync , promises as fs } from 'fs'; import * as path from 'path'; -import { IContextFile, CONTEXT_FILE_PATH } from '../../src/internal/models/Context'; -import SpecificationFile from '../../src/internal/models/SpecificationFile'; +import { IContextFile, CONTEXT_FILE_PATH } from '../../src/adapters/cli/core/models/Context'; +import SpecificationFile from '../../src/adapters/cli/core/models/SpecificationFile'; import http from 'http'; import rimraf from 'rimraf'; diff --git a/test/integration/api.test.ts b/test/integration/api.test.ts index ed015c3a260..2ed25fd1023 100644 --- a/test/integration/api.test.ts +++ b/test/integration/api.test.ts @@ -1,7 +1,7 @@ // api.test.js import { expect, test } from '@oclif/test'; import supertest from 'supertest'; -import { App } from '../../src/adapters/api/app'; +import { App } from '../../src/adapters/api/core/app'; import { PingController } from '../../src/adapters/api/controllers/ping.controller'; import { GeneratorController } from '../../src/adapters/api/controllers/generator.controller'; diff --git a/test/integration/context.test.ts b/test/integration/context.test.ts index 69beb27959f..b9542167873 100644 --- a/test/integration/context.test.ts +++ b/test/integration/context.test.ts @@ -2,7 +2,7 @@ import path from 'path'; import { expect, test } from '@oclif/test'; import TestHelper from '../helpers'; -import { CONTEXT_FILE_PATH } from '../../src/internal/models/Context'; +import { CONTEXT_FILE_PATH } from '../../src/adapters/cli/core/models/Context'; const testHelper = new TestHelper(); diff --git a/test/integration/optimize.test.ts b/test/integration/optimize.test.ts index 2e695629267..fca889448b1 100644 --- a/test/integration/optimize.test.ts +++ b/test/integration/optimize.test.ts @@ -2,7 +2,7 @@ import path from 'path'; import { test } from '@oclif/test'; import TestHelper, { createMockServer, stopMockServer } from '../helpers'; import inquirer from 'inquirer'; -import {Optimizations, Outputs} from '../../src/adapters/cli/commands/optimize'; +import {Optimizations, Outputs} from '../../src/adapters/cli/core/flags/optimize.flags'; import { expect } from '@oclif/test'; const testHelper = new TestHelper();