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 pathget-latest-deployments.ts
More file actions
185 lines (160 loc) · 5.49 KB
/
get-latest-deployments.ts
File metadata and controls
185 lines (160 loc) · 5.49 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/env node
import { parseArgs } from 'util'
import {
getBenchmarkProjects,
createCombinations,
shortestCommonPrefix,
uniqBy,
} from './util.ts'
import constants from './constants.json' with { type: 'json' }
import { Vercel } from '@vercel/sdk'
import type { Deployments } from '@vercel/sdk/models/getdeploymentsop.js'
import assert from 'assert'
const { teamId: TEAM_ID } = 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 })
const processBuildLogs = async (deployment: Deployments) => {
const buildLogs = await vercel.deployments.getDeploymentEvents({
idOrUrl: deployment.uid,
teamId: TEAM_ID,
})
assert(Array.isArray(buildLogs), 'Build logs is not an array')
const registries = new Map<string, number>()
const fetchTiming: [string, number][] = []
let npmTime: number | null = null
for (const log of buildLogs) {
const { text } = log as any
const isNpmHttpFetch = text.match(
/^npm http fetch GET \d+ ([^\s]+) (\d+)ms /,
)
if (isNpmHttpFetch) {
const registry = isNpmHttpFetch[1]
const duration = +isNpmHttpFetch[2]
fetchTiming.push([registry, duration])
registries.set(registry, (registries.get(registry) ?? 0) + 1)
continue
}
const isTotal = text.match(/^npm timing npm Completed in (\d+)ms$/)
if (isTotal) {
npmTime = +isTotal[1]
continue
}
}
assert(npmTime !== null, 'NPM time is not set for ' + deployment.name)
const registry = shortestCommonPrefix([...registries.keys()])
assert(registry, 'Registry URL is not set')
return { fetchTiming, npmTime, registry }
}
const processDeploymentFiles = async (deployment: Deployments) => {
const deploymentFiles = await vercel.deployments.listDeploymentFiles({
id: deployment.uid,
teamId: TEAM_ID,
})
const isPackageLock = !!deploymentFiles[0]?.children?.some(
(file) => file.name === 'package-lock.json',
)
return { isPackageLock }
}
async function getLatestDeployments({
full = false,
limit = '100',
filter = [],
fetchTiming: includeFetchTiming = false,
registry = ['npm', 'vsr', 'aws'],
variant: variants = ['lockfile', 'no-lockfile'],
}: {
full?: boolean
limit?: string
filter?: string[]
fetchTiming?: boolean
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 combos = createCombinations([registry, variants])
assert(combos.length, 'No combinations found')
console.error(
`Looking for ${combos.length} combinations:${JSON.stringify(combos)}`,
)
const deployments = await Promise.all(
projects.map(async (project) => {
const deploymentsData = await vercel.deployments.getDeployments({
limit: combos.length,
projectId: project.id,
teamId: TEAM_ID,
target: 'production',
})
assert(deploymentsData.deployments.length, 'No deployments found')
const deployments = await Promise.all(
deploymentsData.deployments.map(async (deployment) => {
const [{ fetchTiming, npmTime, registry }, { isPackageLock }] =
await Promise.all([
processBuildLogs(deployment),
processDeploymentFiles(deployment),
])
const name = project.name
return {
id: `${name.replace('benchmark-', '')}-${isPackageLock ? 'lockfile' : 'no-lockfile'}-${registry}`,
name,
registry,
isPackageLock,
state: deployment.state,
buildDuration:
deployment.ready && deployment.buildingAt
? deployment.ready - deployment.buildingAt
: null,
queueDuration: deployment.buildingAt
? deployment.buildingAt - deployment.created
: null,
createdTime: new Date(deployment.created).toISOString(),
buildStartTime: deployment.buildingAt
? new Date(deployment.buildingAt).toISOString()
: null,
readyTime: deployment.ready
? new Date(deployment.ready).toISOString()
: null,
npmTime,
fetchTiming: includeFetchTiming ? fetchTiming : null,
...(full ? { deployment } : {}),
}
}),
)
const uniqueDeployments = uniqBy(deployments, (d) => d.id)
assert(
deployments.length === uniqueDeployments.length,
[
'Duplicate deployments found.',
'This is probably because the combinations of the latest triggered deployments do not match the combinations you are looking for.',
'Check the --registry and --variant flags against the latest deployments.',
].join('\n'),
)
return deployments
}),
)
return deployments.flat().sort((a, b) => `${a.id}`.localeCompare(`${b.id}`))
}
getLatestDeployments(
parseArgs({
options: {
full: { type: 'boolean' },
limit: { type: 'string' },
filter: { type: 'string', multiple: true },
fetchTiming: { type: 'boolean' },
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)
})