-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added new command for asyncapi start preview
- Loading branch information
Showing
4 changed files
with
140 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// test/commands/start/preview.test.ts | ||
import { expect, test } from '@oclif/test'; | ||
import { promises as fs } from 'fs'; | ||
import path from 'path'; | ||
import { bundleInMemory } from '../../../src/commands/bundle'; | ||
import { startPreview } from '../../../src/commands/start/preview'; // | ||
|
||
const asyncAPISpecPath = './test/fixtures/spec/asyncapi.yaml'; | ||
const asyncAPIWithRefsPath = './test/fixtures/spec/asyncapi-with-refs.yaml'; | ||
|
||
async function setupTestSpecFiles() { | ||
await fs.mkdir(path.dirname(asyncAPISpecPath), { recursive: true }); | ||
await fs.writeFile( | ||
asyncAPISpecPath, | ||
'asyncapi: "2.0.0"\ninfo:\n title: Test API\n version: "1.0.0"\n' | ||
); | ||
await fs.writeFile( | ||
asyncAPIWithRefsPath, | ||
'asyncapi: "2.0.0"\ninfo:\n title: Test API with Refs\n version: "1.0.0"\ncomponents:\n schemas:\n example: \n $ref: ./example-schema.yaml\n' | ||
); | ||
} | ||
|
||
describe('start preview', () => { | ||
before(async () => { | ||
await setupTestSpecFiles(); | ||
}); | ||
|
||
after(async () => { | ||
await fs.rm('./test/fixtures', { recursive: true, force: true }); | ||
}); | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.do(async () => { | ||
const bundledDoc = await bundleInMemory([asyncAPISpecPath], {}); | ||
await startPreview(bundledDoc.string(), { readOnly: true }); | ||
}) | ||
.it('should start preview with a bundled AsyncAPI document', (ctx) => { | ||
expect(ctx.stdout).to.contain('Preview started'); | ||
}); | ||
|
||
test | ||
.stdout() | ||
.stderr() | ||
.do(async () => { | ||
const bundledDoc = await bundleInMemory([asyncAPIWithRefsPath], {}); | ||
await startPreview(bundledDoc.string(), { readOnly: true }); | ||
}) | ||
.it('should handle references correctly during preview', (ctx) => { | ||
expect(ctx.stdout).to.contain('Preview started'); | ||
}); | ||
|
||
test | ||
.stderr() | ||
.do(async () => { | ||
try { | ||
await startPreview('', { readOnly: true }); | ||
} catch (error) { | ||
expect(error.message).to.contain('Invalid AsyncAPI document'); | ||
} | ||
}) | ||
.it('should throw an error for invalid or empty AsyncAPI documents'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { Command } from '@oclif/core'; | ||
import chokidar from 'chokidar'; | ||
import { bundle } from '../bundler'; | ||
import { startStudio } from '../studio'; | ||
import path from 'path'; | ||
import fs from 'fs'; | ||
|
||
export default class StartPreview extends Command { | ||
static description = 'Start a live preview of your AsyncAPI document with references resolved.'; | ||
|
||
static args = [ | ||
{ name: 'file', required: true, description: 'Path to the AsyncAPI file' }, | ||
]; | ||
|
||
async run() { | ||
const { args } = await this.parse(StartPreview); | ||
|
||
const filePath = path.resolve(args.file); | ||
if (!fs.existsSync(filePath)) { | ||
this.error(`File not found: ${filePath}`); | ||
} | ||
|
||
const bundleAndPreview = async () => { | ||
try { | ||
this.log('Bundling AsyncAPI file...'); | ||
const bundledDocument = await bundle(filePath); | ||
this.log('Starting Studio in preview mode...'); | ||
await startStudio(bundledDocument, { readOnly: true }); | ||
} catch (error) { | ||
this.error(`Error bundling AsyncAPI file: ${error.message}`); | ||
} | ||
}; | ||
|
||
await bundleAndPreview(); | ||
|
||
const watcher = chokidar.watch(filePath, { ignoreInitial: true }); | ||
watcher.on('change', async () => { | ||
this.log('File changed. Reloading preview...'); | ||
await bundleAndPreview(); | ||
}); | ||
|
||
this.log('Watching for file changes. Press Ctrl+C to stop.'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import open from 'open'; | ||
|
||
export async function startStudio(bundledDocument: string, options: { readOnly: boolean }) { | ||
const studioUrl = `https://studio.asyncapi.com/?readOnly=${options.readOnly}&document=${encodeURIComponent( | ||
bundledDocument | ||
)}`; | ||
|
||
await open(studioUrl); | ||
} |