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: added interactive steps to using asyncapi start studio command #1736

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 54 additions & 13 deletions src/commands/start/studio.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Command from '../../core/base';
import fs from 'fs';
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';
import { isCancel, confirm, text } from '@clack/prompts';

export default class StartStudio extends Command {
static description = 'starts a new local instance of Studio';
Expand All @@ -15,31 +17,70 @@ export default class StartStudio extends Command {

async run() {
const { args, flags } = await this.parse(StartStudio);
const cancellationMessage = 'Operation cancelled';

if (flags.file) {
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`');
}
const filePath = await this.getSpecFile(args['spec-file'] ?? flags.file, cancellationMessage);
try {
this.specFile = await load(filePath as string | undefined);
} catch (error) {
if (filePath) {
this.error(error as Error);
}
}

const port = await this.getPort(flags.port, cancellationMessage);
this.metricsMetadata.port = port;
startStudio(filePath as string, port);
}

let filePath = args['spec-file'] ?? flags.file;

const port = flags.port;
if (!filePath) {
private async getSpecFile(filePath: string | undefined, cancellationMessage: string) {
if (filePath) { return filePath; }

const response = await confirm({ message: 'Do you want to start AsyncAPI Studio with any reference? [y/n]' });
if (isCancel(response)) {
this.error(cancellationMessage, { exit: 1 });
}
if (!response) {
try {
filePath = ((await load()).getFilePath());
const filePath = (await load()).getFilePath();
this.log(`Loaded specification from: ${filePath}`);
return filePath;
} catch (error) {
filePath = '';
this.error('No file specified.');
}
}
try {
this.specFile = await load(filePath);
} catch (error) {
if (filePath) {
this.error(error as Error);
const asyncapi = await text({
message: 'Please provide the path to the AsyncAPI document',
placeholder: 'asyncapi.yaml',
defaultValue: 'asyncapi.yaml',
validate: (value) => {
if (!value) {return 'The path to the AsyncAPI document is required';}
if (!fs.existsSync(value)) {return 'The file does not exist';}
}
});

if (isCancel(asyncapi)) {
this.error(cancellationMessage, { exit: 1 });
}
this.metricsMetadata.port = port;
startStudio(filePath as string, port);
return asyncapi;
}

private async getPort(port: number | undefined, cancellationMessage: string) {
if (port) { return port; }

const portInput = await text({
message: 'Which port do you want to start AsyncAPI Studio on',
placeholder: '3210',
defaultValue: '3210',
validate: (value) => (!value ? 'The port number is required' : undefined),
});

if (isCancel(portInput)) {
this.error(cancellationMessage, { exit: 1 });
}
return Number.parseInt(portInput, 10);
}
}
Loading