Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fair-partner-placement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@tanstack/cli': patch
'@tanstack/create': patch
---

Order partner integrations by sponsorship tier and rotate integrations within each tier for every CLI run.
74 changes: 74 additions & 0 deletions packages/cli/src/partner-placement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { randomUUID } from 'node:crypto'

import type { AddOn } from '@tanstack/create'

type PartnerTier = NonNullable<AddOn['partner']>['tier']

const partnerTierOrder = {
gold: 0,
silver: 1,
bronze: 2,
} satisfies Record<PartnerTier, number>

const cliSessionSeed = randomUUID()

function hashString(value: string) {
let hash = 2166136261

for (let index = 0; index < value.length; index++) {
hash ^= value.charCodeAt(index)
hash = Math.imul(hash, 16777619)
}

return hash >>> 0
}

function compareIdentity(left: AddOn, right: AddOn) {
const nameComparison = left.name.localeCompare(right.name)
return nameComparison || left.id.localeCompare(right.id)
}

function compareSeededPartnerOrder(left: AddOn, right: AddOn, seed: string) {
const leftWeight = left.partner!.placementWeight ?? 1
const rightWeight = right.partner!.placementWeight ?? 1
const leftRandom =
(hashString(`${seed}:${left.partner!.id}`) + 1) / 4294967297
const rightRandom =
(hashString(`${seed}:${right.partner!.id}`) + 1) / 4294967297
const seededComparison =
-Math.log(leftRandom) / leftWeight + Math.log(rightRandom) / rightWeight

return seededComparison || compareIdentity(left, right)
}

export function orderAddOnsForPartnerPlacement(
addOns: Array<AddOn>,
surface: string,
sessionSeed = cliSessionSeed,
) {
const rotationSeed = `${surface}:${sessionSeed}`
const partners = addOns
.filter((addOn) => addOn.partner)
.sort((left, right) => {
const tierComparison =
partnerTierOrder[left.partner!.tier] -
partnerTierOrder[right.partner!.tier]

if (tierComparison !== 0) {
return tierComparison
}

if (surface === 'deployment' && left.partner!.id === 'cloudflare') {
return -1
}

if (surface === 'deployment' && right.partner!.id === 'cloudflare') {
return 1
}

return compareSeededPartnerOrder(left, right, rotationSeed)
})
const nonPartners = addOns.filter((addOn) => !addOn.partner)

return [...partners, ...nonPartners]
}
34 changes: 19 additions & 15 deletions packages/cli/src/ui-prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
isCurrentDirectoryProjectNameInput,
validateProjectName,
} from './utils.js'
import { orderAddOnsForPartnerPlacement } from './partner-placement.js'
import type { AddOn, PackageManager } from '@tanstack/create'

import type { Framework } from '@tanstack/create/dist/types/types.js'
Expand Down Expand Up @@ -154,8 +155,9 @@ export async function selectAddOns(
}

if (allowMultiple) {
const selectableAddOns = addOns.filter(
(addOn) => !forcedAddOns.includes(addOn.id),
const selectableAddOns = orderAddOnsForPartnerPlacement(
addOns.filter((addOn) => !forcedAddOns.includes(addOn.id)),
'add-ons',
)

if (selectableAddOns.length === 0) {
Expand Down Expand Up @@ -381,19 +383,21 @@ export async function selectDeployment(
): Promise<string | undefined> {
const deployments = new Set<AddOn>()
let initialValue: string | undefined = undefined
for (const addOn of framework
.getAddOns()
.sort((a, b) => a.name.localeCompare(b.name))) {
if (addOn.type === 'deployment') {
deployments.add(addOn)
if (deployment && addOn.id === deployment) {
return deployment
}
if (forcedDeployment && addOn.id === forcedDeployment) {
initialValue = addOn.id
} else if (!initialValue && addOn.default) {
initialValue = addOn.id
}
for (const addOn of orderAddOnsForPartnerPlacement(
framework
.getAddOns()
.filter((addOn) => addOn.type === 'deployment')
.sort((a, b) => a.name.localeCompare(b.name)),
'deployment',
)) {
deployments.add(addOn)
if (deployment && addOn.id === deployment) {
return deployment
}
if (forcedDeployment && addOn.id === forcedDeployment) {
initialValue = addOn.id
} else if (!initialValue && addOn.default) {
initialValue = addOn.id
}
}

Expand Down
76 changes: 76 additions & 0 deletions packages/cli/tests/partner-placement.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { describe, expect, it } from 'vitest'

import { orderAddOnsForPartnerPlacement } from '../src/partner-placement'

import type { AddOn } from '@tanstack/create'

function addOn(
id: string,
partner?: {
id: string
tier: 'gold' | 'silver' | 'bronze'
placementWeight?: number
},
) {
return { id, name: id, partner } as AddOn
}

describe('partner placement', () => {
it('orders partners by tier ahead of non-partners', () => {
const ordered = orderAddOnsForPartnerPlacement(
[
addOn('other'),
addOn('bronze', { id: 'bronze', tier: 'bronze' }),
addOn('gold', { id: 'gold', tier: 'gold' }),
addOn('silver', { id: 'silver', tier: 'silver' }),
],
'add-ons',
'test',
)

expect(ordered.map((item) => item.id)).toEqual([
'gold',
'silver',
'bronze',
'other',
])
})

it('rotates partners within a tier from the CLI session seed', () => {
const partners = [
addOn('clerk', { id: 'clerk', tier: 'silver' }),
addOn('workos', { id: 'workos', tier: 'silver' }),
]

expect(
orderAddOnsForPartnerPlacement(partners, 'add-ons', '0').map(
(item) => item.id,
),
).toEqual(['clerk', 'workos'])
expect(
orderAddOnsForPartnerPlacement(partners, 'add-ons', '4').map(
(item) => item.id,
),
).toEqual(['workos', 'clerk'])
})

it('reserves Cloudflare as the first deployment partner', () => {
const ordered = orderAddOnsForPartnerPlacement(
[
addOn('netlify', { id: 'netlify', tier: 'gold' }),
addOn('cloudflare', { id: 'cloudflare', tier: 'gold' }),
addOn('railway', { id: 'railway', tier: 'gold' }),
],
'deployment',
'test',
)

expect(ordered[0]?.id).toBe('cloudflare')
expect(
ordered
.slice(1)
.map((item) => item.id)
.sort(),
).toEqual(['netlify', 'railway'])
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"exclusive": ["auth"],
"color": "#6C47FF",
"priority": 150,
"partner": { "id": "clerk", "tier": "silver" },
"link": "https://clerk.com",
"routes": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
"exclusive": ["orm"],
"color": "#2D3748",
"priority": 120,
"partner": { "id": "prisma", "tier": "bronze" },
"link": "https://www.prisma.io/",
"modes": ["file-router"],

"postInitSpecialSteps": ["post-init-script"],
"options": {
"database": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"category": "monitoring",
"color": "#362D59",
"priority": 130,
"partner": { "id": "sentry", "tier": "bronze" },
"routes": [
{
"url": "/demo/sentry/testing",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"exclusive": ["auth"],
"color": "#6363F1",
"priority": 160,
"partner": { "id": "workos", "tier": "silver" },
"link": "https://workos.com",
"routes": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exclusive": ["deploy"],
"color": "#F38020",
"priority": 200,
"partner": { "id": "cloudflare", "tier": "gold" },
"integrations": [
{
"type": "vite-plugin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exclusive": ["deploy"],
"color": "#00C7B7",
"priority": 180,
"partner": { "id": "netlify", "tier": "gold" },
"integrations": [
{
"type": "vite-plugin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exclusive": ["deploy"],
"color": "#9B4DCA",
"priority": 160,
"partner": { "id": "railway", "tier": "gold" },
"integrations": [
{
"type": "vite-plugin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"type": "add-on",
"category": "monitoring",
"color": "#362D59",
"partner": { "id": "sentry", "tier": "bronze" },
"routes": [
{
"url": "/demo/sentry/bad-event-handler",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exclusive": ["deploy"],
"color": "#F38020",
"priority": 200,
"partner": { "id": "cloudflare", "tier": "gold" },
"integrations": [
{
"type": "vite-plugin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exclusive": ["deploy"],
"color": "#00C7B7",
"priority": 180,
"partner": { "id": "netlify", "tier": "gold" },
"integrations": [
{
"type": "vite-plugin",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"exclusive": ["deploy"],
"color": "#9B4DCA",
"priority": 160,
"partner": { "id": "railway", "tier": "gold" },
"integrations": [
{
"type": "vite-plugin",
Expand Down
7 changes: 7 additions & 0 deletions packages/create/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ export const AddOnBaseSchema = z.object({
.optional(),
color: z.string().optional(),
priority: z.number().optional(),
partner: z
.object({
id: z.string(),
tier: z.enum(['gold', 'silver', 'bronze']),
placementWeight: z.number().positive().optional(),
})
.optional(),
command: z
.object({
command: z.string(),
Expand Down
Loading