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: introduced asyncapi start preview command #1645

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
151 changes: 151 additions & 0 deletions src/commands/start/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import Command from '../../core/base';
import { start as startStudio } from '../../core/models/Studio';
import { load } from '../../core/models/SpecificationFile';
import bundle from '@asyncapi/bundler';
import path from 'path';
import fs from 'fs';
import { Args } from '@oclif/core';
import chokidar from 'chokidar';
import { previewFlags } from '../../core/flags/start/preview.flags';
import * as yaml from 'js-yaml';
import { NO_CONTEXTS_SAVED } from '../../core/errors/context-error';

interface LocalRef {
filePath: string;
pointer: string | null;
}

class StartPreview extends Command {
static description =
'Starts Studio in preview mode with local reference files bundled and hot reloading enabled.';

static examples = [
'asyncapi start preview',
'asyncapi start preview ./asyncapi.yaml',
'asyncapi start preview CONTEXT_NAME'
];

static flags = previewFlags();

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

parseRef(ref: string): LocalRef {
const [filePath, pointer] = ref.split('#');
return {
filePath: filePath || '',
pointer: pointer || null,
};
}

findLocalRefFiles(obj: any, basePath: string, files: Set<string>): void {

Check warning on line 45 in src/commands/start/preview.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Refactor this function to reduce its Cognitive Complexity from 18 to the 15 allowed
if (typeof obj === 'object' && obj !== null) {
for (const [key, value] of Object.entries(obj)) {
if (
key === '$ref' &&
typeof value === 'string' &&
(value.startsWith('.') || value.startsWith('./') || value.startsWith('../'))
) {
const { filePath } = this.parseRef(value);
const resolvedPath = path.resolve(basePath, filePath);

if (fs.existsSync(resolvedPath)) {
files.add(resolvedPath);
const referencedFile = yaml.load(
fs.readFileSync(resolvedPath, 'utf8')
);
this.findLocalRefFiles(referencedFile, path.dirname(resolvedPath), files);
} else {
this.error(`Missing local reference: ${value}`);
}
} else {
this.findLocalRefFiles(value, basePath, files);
}
}
} else if (Array.isArray(obj)) {
for (const item of obj) {
this.findLocalRefFiles(item, basePath, files);
}
}
}

async updateBundledFile(
AsyncAPIFile: string,
outputFormat: string,
bundledFilePath: string
) {
try {
const document = await bundle(AsyncAPIFile);
const fileContent =
outputFormat === '.yaml' || outputFormat === '.yml'
? document.yml()
: JSON.stringify(document.json());
fs.writeFileSync(bundledFilePath, fileContent, { encoding: 'utf-8' });
} catch (error: any) {
throw new Error(`Error bundling files: ${error.message}`);
}
}

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

this.specFile = await load(filePath);
this.metricsMetadata.port = port;

const AsyncAPIFile = this.specFile.getFilePath();

if (!AsyncAPIFile) {
this.error(NO_CONTEXTS_SAVED);
}

const outputFormat = path.extname(AsyncAPIFile);
if (!outputFormat) {
this.error(
'Unable to determine file format from the provided AsyncAPI file.'
);
}

const bundledFilePath = `./asyncapi-bundled${outputFormat}`;
const basePath = path.dirname(path.resolve(AsyncAPIFile));
const filesToWatch = new Set<string>();

filesToWatch.add(this.specFile.getFilePath()!);

Check warning on line 118 in src/commands/start/preview.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Forbidden non-null assertion
const asyncapiDocument = yaml.load(
fs.readFileSync(this.specFile.getFilePath()!, 'utf8')

Check warning on line 120 in src/commands/start/preview.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Forbidden non-null assertion
);
this.findLocalRefFiles(asyncapiDocument, basePath, filesToWatch);

const watcher = chokidar.watch(Array.from(filesToWatch), {
persistent: true,
});

await this.updateBundledFile(
AsyncAPIFile,
outputFormat,
bundledFilePath
);

watcher.on('change', async (changedPath) => {
this.log(`File changed: ${changedPath}`);
try {
await this.updateBundledFile(
AsyncAPIFile,
outputFormat,
bundledFilePath
);
} catch (error: any) {
this.error(`Error updating bundled file: ${error.message}`);
}
});

startStudio(bundledFilePath, port);
}
}

export default StartPreview;
8 changes: 8 additions & 0 deletions src/core/flags/start/preview.flags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Flags } from '@oclif/core';

export const previewFlags = () => {
return {
help: Flags.help({ char: 'h' ,description: 'Show CLI help'}),
port: Flags.integer({ char: 'p', description: 'port in which to start Studio' }),
};
};
Loading