Skip to content

Commit 9f847e2

Browse files
authored
feat: remove -f flag from asyncapi start command (#1638)
1 parent 5635245 commit 9f847e2

File tree

5 files changed

+100
-62
lines changed

5 files changed

+100
-62
lines changed

docs/usage.md

+33-29
Original file line numberDiff line numberDiff line change
@@ -38,33 +38,35 @@ USAGE
3838
# Commands
3939

4040
<!-- commands -->
41-
* [`asyncapi bundle`](#asyncapi-bundle)
42-
* [`asyncapi config`](#asyncapi-config)
43-
* [`asyncapi config analytics`](#asyncapi-config-analytics)
44-
* [`asyncapi config context`](#asyncapi-config-context)
45-
* [`asyncapi config context add CONTEXT-NAME SPEC-FILE-PATH`](#asyncapi-config-context-add-context-name-spec-file-path)
46-
* [`asyncapi config context current`](#asyncapi-config-context-current)
47-
* [`asyncapi config context edit CONTEXT-NAME NEW-SPEC-FILE-PATH`](#asyncapi-config-context-edit-context-name-new-spec-file-path)
48-
* [`asyncapi config context init [CONTEXT-FILE-PATH]`](#asyncapi-config-context-init-context-file-path)
49-
* [`asyncapi config context list`](#asyncapi-config-context-list)
50-
* [`asyncapi config context remove CONTEXT-NAME`](#asyncapi-config-context-remove-context-name)
51-
* [`asyncapi config context use CONTEXT-NAME`](#asyncapi-config-context-use-context-name)
52-
* [`asyncapi config versions`](#asyncapi-config-versions)
53-
* [`asyncapi convert [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-convert-spec-file-proxyhost-proxyport)
54-
* [`asyncapi diff OLD NEW`](#asyncapi-diff-old-new)
55-
* [`asyncapi format [SPEC-FILE]`](#asyncapi-format-spec-file)
56-
* [`asyncapi generate`](#asyncapi-generate)
57-
* [`asyncapi generate fromTemplate ASYNCAPI TEMPLATE`](#asyncapi-generate-fromtemplate-asyncapi-template)
58-
* [`asyncapi generate models LANGUAGE FILE`](#asyncapi-generate-models-language-file)
59-
* [`asyncapi new`](#asyncapi-new)
60-
* [`asyncapi new file`](#asyncapi-new-file)
61-
* [`asyncapi new glee`](#asyncapi-new-glee)
62-
* [`asyncapi new template`](#asyncapi-new-template)
63-
* [`asyncapi optimize [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-optimize-spec-file-proxyhost-proxyport)
64-
* [`asyncapi pretty SPEC-FILE`](#asyncapi-pretty-spec-file)
65-
* [`asyncapi start`](#asyncapi-start)
66-
* [`asyncapi start studio`](#asyncapi-start-studio)
67-
* [`asyncapi validate [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-validate-spec-file-proxyhost-proxyport)
41+
- [Usage](#usage)
42+
- [Commands](#commands)
43+
- [`asyncapi bundle`](#asyncapi-bundle)
44+
- [`asyncapi config`](#asyncapi-config)
45+
- [`asyncapi config analytics`](#asyncapi-config-analytics)
46+
- [`asyncapi config context`](#asyncapi-config-context)
47+
- [`asyncapi config context add CONTEXT-NAME SPEC-FILE-PATH`](#asyncapi-config-context-add-context-name-spec-file-path)
48+
- [`asyncapi config context current`](#asyncapi-config-context-current)
49+
- [`asyncapi config context edit CONTEXT-NAME NEW-SPEC-FILE-PATH`](#asyncapi-config-context-edit-context-name-new-spec-file-path)
50+
- [`asyncapi config context init [CONTEXT-FILE-PATH]`](#asyncapi-config-context-init-context-file-path)
51+
- [`asyncapi config context list`](#asyncapi-config-context-list)
52+
- [`asyncapi config context remove CONTEXT-NAME`](#asyncapi-config-context-remove-context-name)
53+
- [`asyncapi config context use CONTEXT-NAME`](#asyncapi-config-context-use-context-name)
54+
- [`asyncapi config versions`](#asyncapi-config-versions)
55+
- [`asyncapi convert [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-convert-spec-file-proxyhost-proxyport)
56+
- [`asyncapi diff OLD NEW`](#asyncapi-diff-old-new)
57+
- [`asyncapi format [SPEC-FILE]`](#asyncapi-format-spec-file)
58+
- [`asyncapi generate`](#asyncapi-generate)
59+
- [`asyncapi generate fromTemplate ASYNCAPI TEMPLATE`](#asyncapi-generate-fromtemplate-asyncapi-template)
60+
- [`asyncapi generate models LANGUAGE FILE`](#asyncapi-generate-models-language-file)
61+
- [`asyncapi new`](#asyncapi-new)
62+
- [`asyncapi new file`](#asyncapi-new-file)
63+
- [`asyncapi new glee`](#asyncapi-new-glee)
64+
- [`asyncapi new template`](#asyncapi-new-template)
65+
- [`asyncapi optimize [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-optimize-spec-file-proxyhost-proxyport)
66+
- [`asyncapi pretty SPEC-FILE`](#asyncapi-pretty-spec-file)
67+
- [`asyncapi start`](#asyncapi-start)
68+
- [`asyncapi start studio`](#asyncapi-start-studio)
69+
- [`asyncapi validate [SPEC-FILE] [PROXYHOST] [PROXYPORT]`](#asyncapi-validate-spec-file-proxyhost-proxyport)
6870

6971
## `asyncapi bundle`
7072

@@ -786,10 +788,12 @@ starts a new local instance of Studio
786788

787789
```
788790
USAGE
789-
$ asyncapi start studio [-h] [-f <value>] [-p <value>]
791+
$ asyncapi start studio [SPEC_FILE] [-h] [-p <value>]
792+
793+
ARGUMENTS
794+
SPEC-FILE spec path, url, or context-name
790795
791796
FLAGS
792-
-f, --file=<value> path to the AsyncAPI file to link with Studio
793797
-h, --help Show CLI help.
794798
-p, --port=<value> port in which to start Studio
795799

package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/commands/start/studio.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,38 @@ import Command from '../../core/base';
22
import { start as startStudio } from '../../core/models/Studio';
33
import { load } from '../../core/models/SpecificationFile';
44
import { studioFlags } from '../../core/flags/start/studio.flags';
5+
import { Args } from '@oclif/core';
56

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

910
static flags = studioFlags();
1011

12+
static readonly args = {
13+
'spec-file': Args.string({ description: 'spec path, url, or context-name', required: false }),
14+
};
15+
1116
async run() {
12-
const { flags } = await this.parse(StartStudio);
13-
const filePath = flags.file || (await load()).getFilePath();
17+
const { args, flags } = await this.parse(StartStudio);
18+
let filePath = args['spec-file'];
1419
const port = flags.port;
15-
16-
this.specFile = await load(filePath);
20+
if (!filePath) {
21+
try {
22+
filePath = ((await load()).getFilePath());
23+
this.log(`Loaded specification from: ${filePath}`);
24+
} catch (error) {
25+
filePath = '';
26+
this.log('No file specified.');
27+
}
28+
}
29+
try {
30+
this.specFile = await load(filePath);
31+
} catch (error) {
32+
if (filePath) {
33+
this.error(error as Error);
34+
}
35+
}
1736
this.metricsMetadata.port = port;
18-
1937
startStudio(filePath as string, port);
2038
}
2139
}

src/core/flags/start/studio.flags.ts

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Flags } from '@oclif/core';
33
export const studioFlags = () => {
44
return {
55
help: Flags.help({ char: 'h' }),
6-
file: Flags.string({ char: 'f', description: 'path to the AsyncAPI file to link with Studio' }),
76
port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }),
87
};
98
};

src/core/models/Studio.ts

+41-25
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,32 @@ function isValidFilePath(filePath: string): boolean {
2121
}
2222

2323
export function start(filePath: string, port: number = DEFAULT_PORT): void {
24-
if (!isValidFilePath(filePath)) {
24+
if (filePath && !isValidFilePath(filePath)) {
2525
throw new SpecificationFileNotFound(filePath);
2626
}
27-
chokidar.watch(filePath).on('all', (event, path) => {
28-
switch (event) {
29-
case 'add':
30-
case 'change':
31-
getFileContent(path).then((code:string) => {
27+
if (filePath) {
28+
chokidar.watch(filePath).on('all', (event, path) => {
29+
switch (event) {
30+
case 'add':
31+
case 'change':
32+
getFileContent(path).then((code: string) => {
33+
messageQueue.push(JSON.stringify({
34+
type: 'file:changed',
35+
code,
36+
}));
37+
sendQueuedMessages();
38+
});
39+
break;
40+
case 'unlink':
3241
messageQueue.push(JSON.stringify({
33-
type: 'file:changed',
34-
code,
42+
type: 'file:deleted',
43+
filePath,
3544
}));
3645
sendQueuedMessages();
37-
});
38-
break;
39-
case 'unlink':
40-
messageQueue.push(JSON.stringify({
41-
type: 'file:deleted',
42-
filePath,
43-
}));
44-
sendQueuedMessages();
45-
break;
46-
}
47-
});
46+
break;
47+
}
48+
});
49+
}
4850

4951
const server = createServer((request, response) => {
5052
//not all CLI users use npm. Some package managers put dependencies in different weird places
@@ -71,18 +73,26 @@ export function start(filePath: string, port: number = DEFAULT_PORT): void {
7173

7274
wsServer.on('connection', (socket: any) => {
7375
sockets.push(socket);
74-
getFileContent(filePath).then((code: string) => {
76+
if (filePath) {
77+
getFileContent(filePath).then((code: string) => {
78+
messageQueue.push(JSON.stringify({
79+
type: 'file:loaded',
80+
code,
81+
}));
82+
sendQueuedMessages();
83+
});
84+
} else {
7585
messageQueue.push(JSON.stringify({
7686
type: 'file:loaded',
77-
code,
87+
code: '',
7888
}));
7989
sendQueuedMessages();
80-
});
90+
}
8191

8292
socket.on('message', (event: string) => {
8393
try {
84-
const json:any = JSON.parse(event);
85-
if (json.type === 'file:update') {
94+
const json: any = JSON.parse(event);
95+
if (filePath && json.type === 'file:update') {
8696
saveFileContent(filePath, json.code);
8797
} else {
8898
console.warn('Live Server: An unknown event has been received. See details:');
@@ -102,7 +112,13 @@ export function start(filePath: string, port: number = DEFAULT_PORT): void {
102112
const url = `http://localhost:${port}?liveServer=${port}&studio-version=${studioVersion}`;
103113
console.log(`Studio is now running at ${url}.`);
104114
console.log(`You can open this URL in your web browser, and if needed, press ${gray('Ctrl + C')} to stop the process.`);
105-
console.log(`Watching changes on file ${filePath}`);
115+
if (filePath) {
116+
console.log(`Watching changes on file ${filePath}`);
117+
} else {
118+
console.log(
119+
'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.'
120+
);
121+
}
106122
open(url);
107123
});
108124
}

0 commit comments

Comments
 (0)