Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove -f flag from asyncapi start command #1638

Merged
merged 3 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 33 additions & 29 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,35 @@ USAGE
# Commands

<!-- 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)
- [Usage](#usage)
- [Commands](#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`

Expand Down Expand Up @@ -786,10 +788,12 @@ starts a new local instance of Studio

```
USAGE
$ asyncapi start studio [-h] [-f <value>] [-p <value>]
$ asyncapi start studio [SPEC_FILE] [-h] [-p <value>]

ARGUMENTS
SPEC-FILE spec path, url, or context-name

FLAGS
-f, --file=<value> path to the AsyncAPI file to link with Studio
-h, --help Show CLI help.
-p, --port=<value> port in which to start Studio

Expand Down
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 23 additions & 5 deletions src/commands/start/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@ 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';
import { Args } from '@oclif/core';

export default class StartStudio extends Command {
static description = 'starts a new local instance of Studio';

static flags = studioFlags();

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

async run() {
const { flags } = await this.parse(StartStudio);
const filePath = flags.file || (await load()).getFilePath();
const { args, flags } = await this.parse(StartStudio);
let filePath = args['spec-file'];
const port = flags.port;

this.specFile = await load(filePath);
if (!filePath) {
try {
filePath = ((await load()).getFilePath());
this.log(`Loaded specification from: ${filePath}`);
} catch (error) {
filePath = '';
this.log('No file specified.');
}
}
try {
this.specFile = await load(filePath);
} catch (error) {
if (filePath) {
this.error(error as Error);
}
}
this.metricsMetadata.port = port;

startStudio(filePath as string, port);
}
}
1 change: 0 additions & 1 deletion src/core/flags/start/studio.flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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' }),
};
};
66 changes: 41 additions & 25 deletions src/core/models/Studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,33 @@
return existsSync(filePath);
}

export function start(filePath: string, port: number = DEFAULT_PORT): void {

Check warning on line 23 in src/core/models/Studio.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 23 to the 15 allowed
if (!isValidFilePath(filePath)) {
if (filePath && !isValidFilePath(filePath)) {
throw new SpecificationFileNotFound(filePath);
}
chokidar.watch(filePath).on('all', (event, path) => {
switch (event) {
case 'add':
case 'change':
getFileContent(path).then((code:string) => {
if (filePath) {
chokidar.watch(filePath).on('all', (event, path) => {
switch (event) {
case 'add':
case 'change':
getFileContent(path).then((code: string) => {
messageQueue.push(JSON.stringify({
type: 'file:changed',
code,
}));
sendQueuedMessages();
});
break;
case 'unlink':
messageQueue.push(JSON.stringify({
type: 'file:changed',
code,
type: 'file:deleted',
filePath,
}));
sendQueuedMessages();
});
break;
case 'unlink':
messageQueue.push(JSON.stringify({
type: 'file:deleted',
filePath,
}));
sendQueuedMessages();
break;
}
});
break;
}
});
}

const server = createServer((request, response) => {
//not all CLI users use npm. Some package managers put dependencies in different weird places
Expand All @@ -71,18 +73,26 @@

wsServer.on('connection', (socket: any) => {
sockets.push(socket);
getFileContent(filePath).then((code: string) => {
if (filePath) {
getFileContent(filePath).then((code: string) => {
messageQueue.push(JSON.stringify({
type: 'file:loaded',
code,
}));
sendQueuedMessages();
});
} else {
messageQueue.push(JSON.stringify({
type: 'file:loaded',
code,
code: '',
}));
sendQueuedMessages();
});
}

socket.on('message', (event: string) => {
try {
const json:any = JSON.parse(event);
if (json.type === 'file:update') {
const json: any = JSON.parse(event);
if (filePath && json.type === 'file:update') {
saveFileContent(filePath, json.code);
} else {
console.warn('Live Server: An unknown event has been received. See details:');
Expand All @@ -102,7 +112,13 @@
const url = `http://localhost:${port}?liveServer=${port}&studio-version=${studioVersion}`;
console.log(`Studio is now running at ${url}.`);
console.log(`You can open this URL in your web browser, and if needed, press ${gray('Ctrl + C')} to stop the process.`);
console.log(`Watching changes on file ${filePath}`);
if (filePath) {
console.log(`Watching changes on file ${filePath}`);
} else {
console.log(
'Hint : No file was provided, and we couldn\'t find a default file (like "asyncapi.yaml" or "asyncapi.json") in the current folder. Starting Studio with a blank workspace.'
);
}
open(url);
});
}
Expand Down
Loading