From 8e202c74be8f12cfaf7aac2eb468652d91547ddd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Indrek=20K=C3=A4rner?= <152283155+indrekkarner@users.noreply.github.com> Date: Fri, 2 Feb 2024 16:22:55 +0200 Subject: [PATCH] feat(cli): prompt to use default plan when wrong coupon or plan * feat(cli): prompt to use default plan when wrong coupon or plan * feat(cli): Add telemetry and 404 validation --- .../cli/src/__telemetry__/init.telemetry.ts | 20 ++++-- .../src/actions/init-project/initProject.ts | 72 ++++++++++++++++--- 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/packages/@sanity/cli/src/__telemetry__/init.telemetry.ts b/packages/@sanity/cli/src/__telemetry__/init.telemetry.ts index 765729b92d9..5055795a8e4 100644 --- a/packages/@sanity/cli/src/__telemetry__/init.telemetry.ts +++ b/packages/@sanity/cli/src/__telemetry__/init.telemetry.ts @@ -16,12 +16,6 @@ interface CreateOrSelectProjectStep { selectedOption: 'create' | 'select' | 'none' } -interface CreateOrSelectProjectStep { - step: 'createOrSelectProject' - projectId: string - selectedOption: 'create' | 'select' | 'none' -} - interface CreateOrSelectDatasetStep { step: 'createOrSelectDataset' datasetName: string @@ -29,6 +23,18 @@ interface CreateOrSelectDatasetStep { visibility: 'private' | 'public' } +interface UseDefaultPlanCoupon { + step: 'useDefaultPlanCoupon' + selectedOption: 'yes' | 'no' + coupon?: string +} + +interface UseDefaultPlanId { + step: 'useDefaultPlanId' + selectedOption: 'yes' | 'no' + planId?: string +} + interface UseDetectedFrameworkStep { step: 'useDetectedFramework' selectedOption: 'yes' | 'no' @@ -70,6 +76,8 @@ type InitStepResult = | SendCommunityInviteStep | SelectPackageManagerStep | SelectTemplateStep + | UseDefaultPlanCoupon + | UseDefaultPlanId export const CLIInitStepCompleted = defineTrace({ name: 'CLI Init Step Completed', diff --git a/packages/@sanity/cli/src/actions/init-project/initProject.ts b/packages/@sanity/cli/src/actions/init-project/initProject.ts index b474468506e..82d27b98255 100644 --- a/packages/@sanity/cli/src/actions/init-project/initProject.ts +++ b/packages/@sanity/cli/src/actions/init-project/initProject.ts @@ -166,10 +166,50 @@ export default async function initSanity( selectedPlan = await getPlanFromCoupon(apiClient, intendedCoupon) print(`Coupon "${intendedCoupon}" validated!\n`) } catch (err) { - throw new Error(`Unable to validate coupon code "${intendedCoupon}":\n\n${err.message}`) + if (err.statusCode == '404') { + const useDefaultPlan = await prompt.single({ + type: 'confirm', + message: `Coupon "${intendedCoupon}" is not available, use default plan instead?`, + default: true, + }) + trace.log({ + step: 'useDefaultPlanCoupon', + selectedOption: useDefaultPlan ? 'yes' : 'no', + coupon: intendedCoupon, + }) + if (useDefaultPlan) { + print(`Using default plan.`) + } else { + throw new Error(`Coupon "${intendedCoupon}" does not exist`) + } + } else { + throw new Error(`Unable to validate coupon, please try again later:\n\n${err.message}`) + } } } else if (intendedPlan) { - selectedPlan = intendedPlan + try { + selectedPlan = await getPlanFromId(apiClient, intendedPlan) + } catch (err) { + if (err.statusCode == '404') { + const useDefaultPlan = await prompt.single({ + type: 'confirm', + message: `Project plan "${intendedPlan}" does not exist, use default plan instead?`, + default: true, + }) + trace.log({ + step: 'useDefaultPlanId', + selectedOption: useDefaultPlan ? 'yes' : 'no', + planId: intendedPlan, + }) + if (useDefaultPlan) { + print(`Using default plan.`) + } else { + throw new Error(`Plan id "${intendedPlan}" does not exist`) + } + } else { + throw new Error(`Unable to validate plan, please try again later:\n\n${err.message}`) + } + } } if (reconfigure) { @@ -1245,15 +1285,27 @@ async function getPlanFromCoupon(apiClient: CliApiClient, couponCode: string): P uri: `plans/coupon/${couponCode}`, }) - try { - const planId = response[0].id - if (!planId) { - throw new Error('Unable to find a plan from coupon code') - } - return planId - } catch (err) { - throw err + const planId = response[0].id + if (!planId) { + throw new Error('Unable to find a plan from coupon code') + } + return planId +} + +async function getPlanFromId(apiClient: CliApiClient, planId: string): Promise { + const response = await apiClient({ + requireUser: false, + requireProject: false, + }).request({ + method: 'GET', + uri: `plans/${planId}`, + }) + + const id = response[0].id + if (!id) { + throw new Error(`Unable to find a plan with id ${planId}`) } + return id } function getImportCommand(