From b2900add69b6973f8217d47f6ba33e7b0c166833 Mon Sep 17 00:00:00 2001 From: Paul B Date: Thu, 5 Oct 2023 13:58:12 -0300 Subject: [PATCH] =?UTF-8?q?preview:=20fix=20(again)=20the=20=E2=80=9Clive?= =?UTF-8?q?=20preview=E2=80=9D=20feature?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit makes sure to only create one preview (the previous tentative fix on live previews would create 1 preview, then a 2nd preview, and then update that 2nd preview). Follow-up to #456 (cf #182) --- src/commands/preview.ts | 13 ++++++------- test/commands/preview.test.ts | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 7 deletions(-) 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