Skip to content

Commit

Permalink
feat(cli): prompt to use default plan when wrong coupon or plan
Browse files Browse the repository at this point in the history
* feat(cli): prompt to use default plan when wrong coupon or plan

* feat(cli): Add telemetry and 404 validation
  • Loading branch information
indrekkarner authored Feb 2, 2024
1 parent 5b1d51c commit 8e202c7
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 16 deletions.
20 changes: 14 additions & 6 deletions packages/@sanity/cli/src/__telemetry__/init.telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ interface CreateOrSelectProjectStep {
selectedOption: 'create' | 'select' | 'none'
}

interface CreateOrSelectProjectStep {
step: 'createOrSelectProject'
projectId: string
selectedOption: 'create' | 'select' | 'none'
}

interface CreateOrSelectDatasetStep {
step: 'createOrSelectDataset'
datasetName: string
selectedOption: 'create' | 'select' | 'none'
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'
Expand Down Expand Up @@ -70,6 +76,8 @@ type InitStepResult =
| SendCommunityInviteStep
| SelectPackageManagerStep
| SelectTemplateStep
| UseDefaultPlanCoupon
| UseDefaultPlanId

export const CLIInitStepCompleted = defineTrace<InitStepResult>({
name: 'CLI Init Step Completed',
Expand Down
72 changes: 62 additions & 10 deletions packages/@sanity/cli/src/actions/init-project/initProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<string> {
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(
Expand Down

0 comments on commit 8e202c7

Please sign in to comment.