Skip to content

Commit 0e8e3c1

Browse files
committed
feat: made the start studio command inteactive along with addition of a flag to disable prompt.
1 parent d83caa2 commit 0e8e3c1

File tree

3 files changed

+60
-6
lines changed

3 files changed

+60
-6
lines changed

docs/usage.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -745,15 +745,16 @@ starts a new local instance of Studio
745745

746746
```
747747
USAGE
748-
$ asyncapi start studio [SPEC-FILE] [-h] [-f <value>] [-p <value>]
748+
$ asyncapi start studio [SPEC-FILE] [-h] [-f <value>] [-p <value>] [--no-interactive]
749749
750750
ARGUMENTS
751751
SPEC-FILE spec path, url, or context-name
752752
753753
FLAGS
754-
-f, --file=<value> path to the AsyncAPI file to link with Studio
755-
-h, --help Show CLI help.
756-
-p, --port=<value> port in which to start Studio
754+
-f, --file=<value> path to the AsyncAPI file to link with Studio
755+
-h, --help Show CLI help.
756+
-p, --port=<value> port in which to start Studio
757+
--no-interactive disable prompts for this command which asks for file path if not passed via the arguments.
757758
758759
DESCRIPTION
759760
starts a new local instance of Studio

src/commands/start/studio.ts

+50-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { start as startStudio } from '../../core/models/Studio';
33
import { load } from '../../core/models/SpecificationFile';
44
import { studioFlags } from '../../core/flags/start/studio.flags';
55
import { Args } from '@oclif/core';
6+
import { isCancel, text, cancel } from '@clack/prompts';
67

78
export default class StartStudio extends Command {
89
static description = 'starts a new local instance of Studio';
@@ -16,13 +17,22 @@ export default class StartStudio extends Command {
1617
async run() {
1718
const { args, flags } = await this.parse(StartStudio);
1819

20+
let filePath = args['spec-file'] ?? flags.file;
21+
22+
let port = flags.port;
23+
1924
if (flags.file) {
2025
this.warn('The file flag has been removed and is being replaced by the argument spec-file. Please pass the filename directly like `asyncapi start studio asyncapi.yml`');
2126
}
2227

23-
let filePath = args['spec-file'] ?? flags.file;
28+
const isInteractive = !flags['no-interactive'];
29+
30+
if (isInteractive && !filePath) {
31+
const parsedArgs = await this.parseArgs({ filePath }, port?.toString());
32+
filePath = parsedArgs.filePath;
33+
port = parseInt(parsedArgs.port,10);
34+
}
2435

25-
const port = flags.port;
2636
if (!filePath) {
2737
try {
2838
filePath = ((await load()).getFilePath());
@@ -42,4 +52,42 @@ export default class StartStudio extends Command {
4252
this.metricsMetadata.port = port;
4353
startStudio(filePath as string, port);
4454
}
55+
56+
private async parseArgs(args:Record<string,any>,port?:string) {
57+
const operationCancelled = 'Operation cancelled by the user.';
58+
let askForPort = false;
59+
let {filePath} = args;
60+
if (!filePath) {
61+
filePath = await text({
62+
message: 'Enter the path to the AsyncAPI document',
63+
defaultValue: 'asyncapi.yaml',
64+
placeholder: 'asyncapi.yaml',
65+
validate: (value) => {
66+
if (!value) {return 'The path to the AsyncAPI document is required';}
67+
},
68+
});
69+
askForPort = true;
70+
}
71+
72+
if (isCancel(filePath)) {
73+
cancel(operationCancelled);
74+
this.exit();
75+
}
76+
77+
if (!port && askForPort) {
78+
port = await text({
79+
message: 'Enter the port in which to start Studio',
80+
defaultValue: '3210',
81+
placeholder: '3210',
82+
validate: (value) => (!value ? 'The port number is required' : undefined),
83+
}) as string;
84+
}
85+
86+
if (isCancel(port)) {
87+
cancel(operationCancelled);
88+
this.exit();
89+
}
90+
91+
return { filePath, port: port ?? '3210' };
92+
}
4593
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,10 @@ export const studioFlags = () => {
55
help: Flags.help({ char: 'h' }),
66
file: Flags.string({ char: 'f', description: 'path to the AsyncAPI file to link with Studio', deprecated: true }),
77
port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }),
8+
'no-interactive': Flags.boolean({
9+
description: 'disable prompts for this command which asks for file path if not passed via the arguments.',
10+
required: false,
11+
default: false,
12+
}),
813
};
914
};

0 commit comments

Comments
 (0)