From 031870a268dfc29585784a7ac53a1ba328195341 Mon Sep 17 00:00:00 2001 From: Paribesh01 Date: Sat, 18 Jan 2025 00:51:19 +0530 Subject: [PATCH 1/4] feat: added new command preview --- src/commands/start/preview.ts | 66 +++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/commands/start/preview.ts diff --git a/src/commands/start/preview.ts b/src/commands/start/preview.ts new file mode 100644 index 00000000000..d98743e4d0f --- /dev/null +++ b/src/commands/start/preview.ts @@ -0,0 +1,66 @@ +import Command from '../../core/base'; +import bundle from '@asyncapi/bundler'; +import path from 'path'; +import open from 'open'; +import { bundleFlags } from '../../core/flags/bundle.flags'; +import { createServer } from 'http'; +import serveHandler from 'serve-handler'; +import { version as studioVersion } from '@asyncapi/studio/package.json'; + +const port = 3210; + +class preview extends Command { + static description = + 'Preview AsyncAPI document with local references in Studio.'; + static strict = false; + + static flags = bundleFlags(); + + async run() { + const { argv, flags } = await this.parse(preview); + const AsyncAPIFiles = argv as string[]; + + console.log('Got flags: ', flags, argv); + this.startServer(AsyncAPIFiles, flags); + } + + async startServer(AsyncAPIFiles: string[], flags: any) { + const server = createServer(async (request, response) => { + const indexLocation = require.resolve( + '@asyncapi/studio/build/index.html', + ); + const hostFolder = indexLocation.substring( + 0, + indexLocation.lastIndexOf(path.sep), + ); + + if (request.url && request.url.includes('/file.yml')) { + response.setHeader('Content-Type', 'application/x-yaml'); + const bundledDocument = await bundle(AsyncAPIFiles, { + base: flags.base, + baseDir: flags.baseDir, + xOrigin: flags.xOrigin, + }); + + const yamlContent = bundledDocument.yml(); + + response.end(yamlContent); + } else { + return serveHandler(request, response, { + public: hostFolder, + }); + } + }); + + server.listen(port, () => { + const url = `http://localhost:${port}?liveServer=${port}&studio-version=${studioVersion}&url=file.yml&readOnly=true`; + console.log(`Studio is now running at ${url}`); + console.log( + 'You can open this URL in your web browser, and if needed, press Ctrl + C to stop the process.', + ); + open(url); + }); + } +} + +export default preview; From 3ea5673321f49b28028d3866a856fedf01700fe4 Mon Sep 17 00:00:00 2001 From: Paribesh01 Date: Sat, 18 Jan 2025 00:53:56 +0530 Subject: [PATCH 2/4] fix:minor change --- src/commands/start/preview.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commands/start/preview.ts b/src/commands/start/preview.ts index d98743e4d0f..12fe8bb9c85 100644 --- a/src/commands/start/preview.ts +++ b/src/commands/start/preview.ts @@ -20,7 +20,6 @@ class preview extends Command { const { argv, flags } = await this.parse(preview); const AsyncAPIFiles = argv as string[]; - console.log('Got flags: ', flags, argv); this.startServer(AsyncAPIFiles, flags); } From 462c461cb84a59be7ee4f4fef4daab0cf8ca4c55 Mon Sep 17 00:00:00 2001 From: Paribesh01 Date: Sat, 18 Jan 2025 02:02:42 +0530 Subject: [PATCH 3/4] fix:minor changes --- src/commands/start/preview.ts | 6 ++++-- src/core/flags/start/preview.flags.ts | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 src/core/flags/start/preview.flags.ts diff --git a/src/commands/start/preview.ts b/src/commands/start/preview.ts index 12fe8bb9c85..1bd9b219080 100644 --- a/src/commands/start/preview.ts +++ b/src/commands/start/preview.ts @@ -18,12 +18,14 @@ class preview extends Command { async run() { const { argv, flags } = await this.parse(preview); + const port = flags.port; + const AsyncAPIFiles = argv as string[]; - this.startServer(AsyncAPIFiles, flags); + this.startServer(AsyncAPIFiles, flags, port | 3210); } - async startServer(AsyncAPIFiles: string[], flags: any) { + async startServer(AsyncAPIFiles: string[], flags: any, port: number) { const server = createServer(async (request, response) => { const indexLocation = require.resolve( '@asyncapi/studio/build/index.html', diff --git a/src/core/flags/start/preview.flags.ts b/src/core/flags/start/preview.flags.ts new file mode 100644 index 00000000000..361395a9d39 --- /dev/null +++ b/src/core/flags/start/preview.flags.ts @@ -0,0 +1,15 @@ +import { Flags } from '@oclif/core'; + +export const previewFlags = () => { + 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', + }), + }; +}; From 90d1cb73fe6a807d14a0bdfd325b8feb2acbae44 Mon Sep 17 00:00:00 2001 From: Paribesh01 Date: Sat, 18 Jan 2025 12:10:32 +0530 Subject: [PATCH 4/4] fix:minor changes --- docs/usage.md | 82 ++++++++++++++++++++++------------- src/commands/start/preview.ts | 8 ++-- 2 files changed, 56 insertions(+), 34 deletions(-) diff --git a/docs/usage.md b/docs/usage.md index 9e261882a78..019a0d93919 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -3,9 +3,9 @@ title: 'Usage' weight: 40 --- - The AsyncAPI CLI makes it easier to work with AsyncAPI documents. + # Usage + ```sh-session $ npm install -g @asyncapi/cli $ asyncapi COMMAND @@ -33,38 +35,40 @@ USAGE $ asyncapi COMMAND ... ``` + # Commands -* [`asyncapi bundle`](#asyncapi-bundle) -* [`asyncapi config`](#asyncapi-config) -* [`asyncapi config analytics`](#asyncapi-config-analytics) -* [`asyncapi config context`](#asyncapi-config-context) -* [`asyncapi config context add CONTEXT-NAME SPEC-FILE-PATH`](#asyncapi-config-context-add-context-name-spec-file-path) -* [`asyncapi config context current`](#asyncapi-config-context-current) -* [`asyncapi config context edit CONTEXT-NAME NEW-SPEC-FILE-PATH`](#asyncapi-config-context-edit-context-name-new-spec-file-path) -* [`asyncapi config context init [CONTEXT-FILE-PATH]`](#asyncapi-config-context-init-context-file-path) -* [`asyncapi config context list`](#asyncapi-config-context-list) -* [`asyncapi config context remove CONTEXT-NAME`](#asyncapi-config-context-remove-context-name) -* [`asyncapi config context use CONTEXT-NAME`](#asyncapi-config-context-use-context-name) -* [`asyncapi config versions`](#asyncapi-config-versions) -* [`asyncapi convert [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-convert-spec-file-proxyhost-proxyport) -* [`asyncapi diff OLD NEW`](#asyncapi-diff-old-new) -* [`asyncapi format [SPEC-FILE]`](#asyncapi-format-spec-file) -* [`asyncapi generate`](#asyncapi-generate) -* [`asyncapi generate fromTemplate ASYNCAPI TEMPLATE`](#asyncapi-generate-fromtemplate-asyncapi-template) -* [`asyncapi generate models LANGUAGE FILE`](#asyncapi-generate-models-language-file) -* [`asyncapi new`](#asyncapi-new) -* [`asyncapi new file`](#asyncapi-new-file) -* [`asyncapi new glee`](#asyncapi-new-glee) -* [`asyncapi new template`](#asyncapi-new-template) -* [`asyncapi optimize [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-optimize-spec-file-proxyhost-proxyport) -* [`asyncapi pretty SPEC-FILE`](#asyncapi-pretty-spec-file) -* [`asyncapi start`](#asyncapi-start) -* [`asyncapi start studio`](#asyncapi-start-studio) -* [`asyncapi validate [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-validate-spec-file-proxyhost-proxyport) + +- [`asyncapi bundle`](#asyncapi-bundle) +- [`asyncapi config`](#asyncapi-config) +- [`asyncapi config analytics`](#asyncapi-config-analytics) +- [`asyncapi config context`](#asyncapi-config-context) +- [`asyncapi config context add CONTEXT-NAME SPEC-FILE-PATH`](#asyncapi-config-context-add-context-name-spec-file-path) +- [`asyncapi config context current`](#asyncapi-config-context-current) +- [`asyncapi config context edit CONTEXT-NAME NEW-SPEC-FILE-PATH`](#asyncapi-config-context-edit-context-name-new-spec-file-path) +- [`asyncapi config context init [CONTEXT-FILE-PATH]`](#asyncapi-config-context-init-context-file-path) +- [`asyncapi config context list`](#asyncapi-config-context-list) +- [`asyncapi config context remove CONTEXT-NAME`](#asyncapi-config-context-remove-context-name) +- [`asyncapi config context use CONTEXT-NAME`](#asyncapi-config-context-use-context-name) +- [`asyncapi config versions`](#asyncapi-config-versions) +- [`asyncapi convert [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-convert-spec-file-proxyhost-proxyport) +- [`asyncapi diff OLD NEW`](#asyncapi-diff-old-new) +- [`asyncapi format [SPEC-FILE]`](#asyncapi-format-spec-file) +- [`asyncapi generate`](#asyncapi-generate) +- [`asyncapi generate fromTemplate ASYNCAPI TEMPLATE`](#asyncapi-generate-fromtemplate-asyncapi-template) +- [`asyncapi generate models LANGUAGE FILE`](#asyncapi-generate-models-language-file) +- [`asyncapi new`](#asyncapi-new) +- [`asyncapi new file`](#asyncapi-new-file) +- [`asyncapi new glee`](#asyncapi-new-glee) +- [`asyncapi new template`](#asyncapi-new-template) +- [`asyncapi optimize [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-optimize-spec-file-proxyhost-proxyport) +- [`asyncapi pretty SPEC-FILE`](#asyncapi-pretty-spec-file) +- [`asyncapi start`](#asyncapi-start) +- [`asyncapi start studio`](#asyncapi-start-studio) +- [`asyncapi validate [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-validate-spec-file-proxyhost-proxyport) ## `asyncapi bundle` @@ -799,8 +803,27 @@ DESCRIPTION _See code: [src/commands/start/studio.ts](https://github.com/asyncapi/cli/blob/v2.14.1/src/commands/start/studio.ts)_ +## `asyncapi start preview` + +starts a new local instance of studio in readonly mode + +``` +USAGE + $ asyncapi start studio [-h] [-f ] [-p ] + +FLAGS + -f, --file= path to the AsyncAPI file to link with Studio + -h, --help Show CLI help. + -p, --port= port in which to start Studio + +DESCRIPTION + starts a new local instance of studio in readonly mode +``` + ## `asyncapi validate [SPEC-FILE] [PROXYHOST] [PROXYPORT]` +_See code: [src/commands/start/preview.ts](https://github.com/asyncapi/cli/blob/v2.14.1/src/commands/start/preview.ts)_ + validate asyncapi file ``` @@ -834,4 +857,5 @@ DESCRIPTION ``` _See code: [src/commands/validate.ts](https://github.com/asyncapi/cli/blob/v2.14.1/src/commands/validate.ts)_ + diff --git a/src/commands/start/preview.ts b/src/commands/start/preview.ts index 1bd9b219080..d08251cc006 100644 --- a/src/commands/start/preview.ts +++ b/src/commands/start/preview.ts @@ -2,19 +2,17 @@ import Command from '../../core/base'; import bundle from '@asyncapi/bundler'; import path from 'path'; import open from 'open'; -import { bundleFlags } from '../../core/flags/bundle.flags'; import { createServer } from 'http'; import serveHandler from 'serve-handler'; import { version as studioVersion } from '@asyncapi/studio/package.json'; - -const port = 3210; +import { previewFlags } from '../../core/flags/start/preview.flags'; class preview extends Command { static description = 'Preview AsyncAPI document with local references in Studio.'; static strict = false; - static flags = bundleFlags(); + static flags = previewFlags(); async run() { const { argv, flags } = await this.parse(preview); @@ -22,7 +20,7 @@ class preview extends Command { const AsyncAPIFiles = argv as string[]; - this.startServer(AsyncAPIFiles, flags, port | 3210); + this.startServer(AsyncAPIFiles, flags, port ? port : 3210); } async startServer(AsyncAPIFiles: string[], flags: any, port: number) {