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: new command preview #1633

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
65 changes: 65 additions & 0 deletions src/commands/start/preview.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import Command from '../../core/base';
import bundle from '@asyncapi/bundler';
import path from 'path';
import open from 'open';
import { bundleFlags } from '../../core/flags/bundle.flags';
import { createServer } from 'http';
import serveHandler from 'serve-handler';
import { version as studioVersion } from '@asyncapi/studio/package.json';

const port = 3210;

class preview extends Command {
static description =
'Preview AsyncAPI document with local references in Studio.';
static strict = false;

static flags = bundleFlags();

async run() {
const { argv, flags } = await this.parse(preview);
const AsyncAPIFiles = argv as string[];

this.startServer(AsyncAPIFiles, flags);
}

async startServer(AsyncAPIFiles: string[], flags: any) {
const server = createServer(async (request, response) => {
const indexLocation = require.resolve(
'@asyncapi/studio/build/index.html',
);
const hostFolder = indexLocation.substring(
0,
indexLocation.lastIndexOf(path.sep),
);

if (request.url && request.url.includes('/file.yml')) {
response.setHeader('Content-Type', 'application/x-yaml');
const bundledDocument = await bundle(AsyncAPIFiles, {
base: flags.base,
baseDir: flags.baseDir,
xOrigin: flags.xOrigin,
});

const yamlContent = bundledDocument.yml();

response.end(yamlContent);
} else {
return serveHandler(request, response, {
public: hostFolder,
});
}
});

server.listen(port, () => {
const url = `http://localhost:${port}?liveServer=${port}&studio-version=${studioVersion}&url=file.yml&readOnly=true`;
console.log(`Studio is now running at ${url}`);
console.log(
'You can open this URL in your web browser, and if needed, press Ctrl + C to stop the process.',
);
open(url);
});
}
}

export default preview;
Loading