From 08653aeee223a06954f81cbf5ee4462976d6ede6 Mon Sep 17 00:00:00 2001 From: Dan Goodman Date: Thu, 14 Oct 2021 17:14:22 -0400 Subject: [PATCH] Add waiting for created droplets and throw error if error on creation --- lib/commands/instance/init.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lib/commands/instance/init.ts b/lib/commands/instance/init.ts index 1c8e94f1..8ebd5ada 100644 --- a/lib/commands/instance/init.ts +++ b/lib/commands/instance/init.ts @@ -227,6 +227,31 @@ export default class InstanceInitCmd extends Command { 'content-type': 'application/json' } }) + responseBody = await res.json() + + const visuals = getVisuals(); + const spinner = new visuals.Spinner( + `Waiting for droplets to be created`, + ); + + spinner.start(); + // God tier code incoming + await Promise.all(responseBody.links.actions.map(async (action: any) => { + let respBody: any + do { + await new Promise((r) => setTimeout(() => r(null), 2000)) // Sleep for 2 seconds + const waitResp = await fetch(action.href, { + headers: { + authorization: `Bearer ${options.apiKey}` + } + }) + respBody = await waitResp.json() + if (respBody.action.status === 'errored') { + throw new Error('Error creating droplet') + } + } while (respBody.action.status !== 'completed') + })) + spinner.stop(); console.log('Done! The device should appear in your Dashboard in a few minutes!')