diff --git a/src/commands/preview.ts b/src/commands/preview.ts index 6c121575..d8f83443 100644 --- a/src/commands/preview.ts +++ b/src/commands/preview.ts @@ -31,10 +31,10 @@ export default class Preview extends Command { async run(): Promise { const { args, flags } = this.parse(Preview); + const currentPreview: PreviewResponse = await this.preview(args.FILE, flags.open); - await this.preview(args.FILE, flags.open); if (flags.live) { - await this.waitForChanges(args.FILE, flags.open); + await this.waitForChanges(args.FILE, currentPreview); } return; @@ -76,20 +76,19 @@ export default class Preview extends Command { return response.data; } - async waitForChanges(file: string, open: boolean): Promise { + async waitForChanges(file: string, preview: PreviewResponse): Promise { const mutex = new Mutex(); - let currentPreview: PreviewResponse | undefined = undefined; + let currentPreview: PreviewResponse = preview; cli.action.start(`Waiting for changes on file ${file}...`); watch(file, async () => { if (!mutex.isLocked()) { const release = await mutex.acquire(); - const firstOpen = !currentPreview && open; - this.preview(file, firstOpen, currentPreview) + this.preview(file, false, currentPreview) .then((preview) => { - currentPreview = currentPreview || preview; + currentPreview = preview; cli.action.start(`Waiting for changes on file ${file}`); }) .catch((err) => { diff --git a/test/commands/preview.test.ts b/test/commands/preview.test.ts index 1ad3f9e0..7f19806c 100644 --- a/test/commands/preview.test.ts +++ b/test/commands/preview.test.ts @@ -23,6 +23,25 @@ describe('preview subcommand', () => { expect(stdout).to.match(/https:\/\/bump.sh\/preview\/123abc-cba321/); }); + test + .nock('https://bump.sh', (api) => + api.post('/api/v1/previews').reply(201, { + id: '123abc-cba321', + expires_at: new Date(), + public_url: 'https://bump.sh/preview/123abc-cba321', + }), + ) + .stdout() + .stderr() + .command(['preview', '--live', 'examples/valid/openapi.v3.json']) + .it('Creates a live preview and waits for file update', ({ stdout, stderr }) => { + expect(stderr).to.match(/Let's render a preview on Bump... done/); + + expect(stdout).to.match(/preview is visible at/); + expect(stdout).to.match(/https:\/\/bump.sh\/preview\/123abc-cba321/); + expect(stderr).to.match(/Waiting for changes on file/); + }); + test .nock('http://example.org', (api) => { api