Skip to content

Commit

Permalink
feat: Remove -f flag from asyncapi start command
Browse files Browse the repository at this point in the history
  • Loading branch information
Adi-204 committed Jan 20, 2025
1 parent 5635245 commit c3dc0d9
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 65 deletions.
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.

25 changes: 20 additions & 5 deletions src/commands/start/studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,35 @@ 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 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) {
this.log('No file specified. Starting with an empty Studio.');
}
}
try {
this.specFile = await load(filePath);
} catch (error) {
filePath = "";
}
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' }),
};
};
74 changes: 46 additions & 28 deletions src/core/models/Studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,32 @@ function isValidFilePath(filePath: string): boolean {
}

export function start(filePath: string, port: number = DEFAULT_PORT): void {
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) => {
messageQueue.push(JSON.stringify({
type: 'file:changed',
code,
}));
sendQueuedMessages();
});
break;
case 'unlink':
messageQueue.push(JSON.stringify({
type: 'file:deleted',
filePath,
}));
sendQueuedMessages();
break;
}
});
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:deleted',
filePath,
}));
sendQueuedMessages();
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,27 @@ export function start(filePath: string, port: number = DEFAULT_PORT): void {

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 +113,14 @@ export function start(filePath: string, port: number = DEFAULT_PORT): void {
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

0 comments on commit c3dc0d9

Please sign in to comment.