This repository was archived by the owner on Feb 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrigger-deployments.ts
More file actions
151 lines (137 loc) · 4.15 KB
/
trigger-deployments.ts
File metadata and controls
151 lines (137 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
import { parseArgs } from 'util'
import { getBenchmarkProjects } from './util.ts'
import constants from './constants.json' with { type: 'json' }
import { Vercel } from '@vercel/sdk'
import { readFile } from 'fs/promises'
import { join } from 'path'
import assert from 'assert'
const {
registries: REGISTRIES,
teamId: TEAM_ID,
projectSettings: PROJECT_SETTINGS,
} = constants
const VERCEL_TOKEN = process.env.VERCEL_TOKEN
assert(VERCEL_TOKEN, 'VERCEL_TOKEN is not set in .env')
const vercel = new Vercel({ bearerToken: VERCEL_TOKEN })
type RegistryConfig = {
key: string
registry: string
npmrc?: string
}
async function triggerBenchmark({
full = false,
limit = '100',
filter = [],
registry = ['npm', 'vsr', 'aws'],
// We are limited to 120 deployments before being rate limited by Vercel
// so we can only run one variant at a time.
variant: variants = [/* 'lockfile', */ 'no-lockfile'],
}: {
full?: boolean
limit?: string
filter?: string[]
registry?: string[]
variant?: string[]
}) {
const projects = await getBenchmarkProjects(vercel, {
limit,
filters: filter,
})
assert(projects.length, 'No projects found')
console.error(
`Found ${projects.length} projects: ${projects.map((p) => p.name).join(', ')}`,
)
const registries: RegistryConfig[] = (
registry.length
? Object.entries(REGISTRIES).filter(([key]) => registry.includes(key))
: Object.entries(REGISTRIES)
).map(([key, value]) => ({
key,
...(value as Omit<RegistryConfig, 'key'>),
}))
const deploymentsToCreate = await Promise.all(
projects.map(async (project) => {
const readProjectFile = (file: string) =>
readFile(
join(
process.cwd(),
'packages',
project.name.replace('benchmark-', ''),
file,
),
'utf-8',
)
const [pkgJson, pkgLock] = await Promise.all([
readProjectFile('package.json'),
readProjectFile('package-lock.json'),
])
return variants.flatMap((variant) =>
registries.map((registry) => {
return {
teamId: TEAM_ID,
requestBody: {
name: project.name,
target: 'production',
files: [
{ file: 'package.json', data: pkgJson },
...(variant === 'lockfile'
? [{ file: 'package-lock.json', data: pkgLock }]
: []),
...(registry.npmrc
? [{ file: '.npmrc', data: registry.npmrc }]
: []),
],
projectSettings: {
...PROJECT_SETTINGS,
installCommand:
`${registry.registry ? `NPM_CONFIG_REGISTRY=${registry.registry}` : ''} ${PROJECT_SETTINGS.installCommand}`.trim(),
},
},
}
}),
)
}),
).then((deployments) => deployments.flat())
console.error(`Creating ${deploymentsToCreate.length} deployments...`)
const createdDeployments = await Promise.all(
deploymentsToCreate.map(
async (deployment) =>
[
deployment,
await vercel.deployments.createDeployment(deployment),
] as const,
),
)
return createdDeployments.map(([request, deployment]) => ({
status: deployment.status,
name: deployment.name,
id: deployment.id,
inspectorUrl: deployment.inspectorUrl,
isPackageLock: request.requestBody.files?.some(
(f) => f.file === 'package-lock.json',
),
files: request.requestBody.files.map((f) => f.file),
registry:
request.requestBody.projectSettings.installCommand?.match(
/NPM_CONFIG_REGISTRY=([^\s]+)/,
)?.[1] ?? REGISTRIES.npm.registry,
...(full ? { deployment } : {}),
}))
}
triggerBenchmark(
parseArgs({
options: {
full: { type: 'boolean' },
limit: { type: 'string' },
filter: { type: 'string', multiple: true },
registry: { type: 'string', multiple: true },
variant: { type: 'string', multiple: true },
},
}).values,
)
.then((r) => console.log(JSON.stringify(r, null, 2)))
.catch((error) => {
console.error(error)
process.exit(1)
})