forked from onejs/one
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release.ts
381 lines (326 loc) · 10.4 KB
/
release.ts
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* eslint-disable no-console */
import * as proc from 'node:child_process'
import { join } from 'node:path'
import { promisify } from 'node:util'
import path from 'path'
import fs, { writeJSON } from 'fs-extra'
import pMap from 'p-map'
import prompts from 'prompts'
import { exec } from './exec'
// avoid emitter error
process.setMaxListeners(0)
// --resume would be cool here where it stores the last failed step somewhere and tries resuming
// for failed publishes that need to re-run
const confirmFinalPublish = process.argv.includes('--confirm-final-publish')
const reRun = process.argv.includes('--rerun')
const rePublish = reRun || process.argv.includes('--republish')
const finish = process.argv.includes('--finish')
const canary = process.argv.includes('--canary')
const skipVersion = finish || rePublish || process.argv.includes('--skip-version')
const patch = process.argv.includes('--patch')
const dirty = process.argv.includes('--dirty')
const skipPublish = process.argv.includes('--skip-publish')
const skipTest =
rePublish ||
process.argv.includes('--skip-test') ||
process.argv.includes('--skip-tests')
const skipBuild = rePublish || process.argv.includes('--skip-build')
const dryRun = process.argv.includes('--dry-run')
const isCI = process.argv.includes('--ci')
const curVersion = fs.readJSONSync('./packages/vxrn/package.json').version
const nextVersion = (() => {
if (rePublish) {
return curVersion
}
const plusVersion = skipVersion ? 0 : 1
const curPatch = +curVersion.split('.')[2] || 0
const patchVersion = patch ? curPatch + plusVersion : 0
const curMinor = +curVersion.split('.')[1] || 0
const minorVersion = curMinor + (patch || canary ? 0 : plusVersion)
const next = `0.${minorVersion}.${patchVersion}`
if (canary) {
return `${next}-${Date.now()}`
}
return next
})()
const sleep = (ms) => {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`Sleeping ${ms}ms`)
return new Promise((res) => setTimeout(res, ms))
}
if (!finish) {
if (!skipVersion) {
console.log('Publishing version:', nextVersion, '\n')
} else {
console.log(`Re-publishing ${curVersion}`)
}
}
async function run() {
try {
let version = curVersion
// ensure we are up to date
// ensure we are on main
if (!canary) {
if ((await exec(`git rev-parse --abbrev-ref HEAD`)).trim() !== 'main') {
throw new Error(`Not on main`)
}
if (!dirty && !rePublish) {
await exec(`git pull --rebase origin main`)
}
}
const workspaces = (await exec(`yarn workspaces list --json`)).trim().split('\n')
const packagePaths = workspaces.map((p) => JSON.parse(p)) as {
name: string
location: string
}[]
const allPackageJsons = (
await Promise.all(
packagePaths
.filter((i) => i.location !== '.' && !i.name.startsWith('@takeout'))
.map(async ({ name, location }) => {
const cwd = path.join(process.cwd(), location)
const json = await fs.readJSON(path.join(cwd, 'package.json'))
return {
name,
cwd,
json,
path: path.join(cwd, 'package.json'),
directory: location,
}
})
)
).filter((x) => !x.json['publish-skip'])
const packageJsons = allPackageJsons
.filter((x) => {
return !x.json.private
})
// slow things last
.sort((a, b) => {
if (a.name.includes('font-') || a.name.includes('-icons')) {
return 1
}
return -1
})
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`Publishing in order:\n\n${packageJsons.map((x) => x.name).join('\n')}`)
async function checkDistDirs() {
await Promise.all(
packageJsons.map(async ({ cwd, json }) => {
const distDir = join(cwd, 'dist')
if (!json.scripts || json.scripts.build === 'true') {
return
}
if (!(await fs.pathExists(distDir))) {
console.warn('no dist dir!', distDir)
process.exit(1)
}
})
)
}
const answer =
isCI || skipVersion
? { version: nextVersion }
: await prompts({
type: 'text',
name: 'version',
message: 'Version?',
initial: nextVersion,
})
version = answer.version
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('install and build')
if (!rePublish) {
await exec(`yarn install`)
}
if (!skipBuild) {
await exec(`yarn build`)
await checkDistDirs()
}
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('run checks')
if (!finish) {
if (!skipTest) {
await exec(`yarn fix`)
await exec(`yarn lint`)
await exec(`yarn check`)
await exec(`yarn test`)
}
}
if (!dirty && !dryRun && !rePublish) {
const out = await exec(`git status --porcelain`)
if (out.stdout) {
throw new Error(`Has unsaved git changes: ${out.stdout}`)
}
}
if (!skipVersion && !finish) {
await Promise.all(
allPackageJsons.map(async ({ json, path }) => {
const next = { ...json }
next.version = version
for (const field of [
'dependencies',
'devDependencies',
'optionalDependencies',
'peerDependencies',
]) {
const nextDeps = next[field]
if (!nextDeps) continue
for (const depName in nextDeps) {
if (packageJsons.some((p) => p.name === depName)) {
nextDeps[depName] = version
}
}
}
await writeJSON(path, next, { spaces: 2 })
})
)
}
if (!finish && dryRun) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`Dry run, exiting before publish`)
return
}
if (!finish && !rePublish) {
await exec(`git diff`)
}
if (!isCI) {
const { confirmed } = await prompts({
type: 'confirm',
name: 'confirmed',
message: 'Ready to publish?',
})
if (!confirmed) {
process.exit(0)
}
}
if (!finish && !skipPublish && !rePublish) {
const erroredPackages: { name: string }[] = []
// publish with tag
await pMap(
packageJsons,
async (pkg) => {
const { cwd, name } = pkg
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`Publish ${name}`)
// check if already published first as its way faster for re-runs
let versionsOut = ''
try {
versionsOut = await exec(`npm view ${name} versions --json`, {
avoidLog: true,
})
if (versionsOut) {
const allVersions = JSON.parse(versionsOut.trim().replaceAll(`\n`, ''))
const latest = allVersions[allVersions.length - 1]
if (latest === nextVersion) {
console.log(`Already published, skipping`)
return
}
}
} catch (err) {
if (`${err}`.includes(`404`)) {
// fails if never published before, ok
} else {
if (`${err}`.includes(`Unexpected token`)) {
console.log(`Bad JSON? ${versionsOut}`)
}
throw err
}
}
try {
await exec(`npm publish --tag prepub --access public`, {
cwd,
avoidLog: true,
})
console.log(` 📢 pre-published ${name}`)
} catch (err: any) {
// @ts-ignore
if (err.includes(`403`)) {
console.log('Already published, skipping')
return
}
console.log(`Error publishing!`, `${err}`)
}
},
{
concurrency: 5,
}
)
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(
`✅ Published under dist-tag "prepub" (${erroredPackages.length} errors)\n`
)
}
if (!finish) {
if (confirmFinalPublish) {
const { confirmed } = await prompts({
type: 'confirm',
name: 'confirmed',
message: 'Ready to publish?',
})
if (!confirmed) {
console.log(`Not confirmed, can re-run with --republish to try again`)
process.exit(0)
}
}
}
if (rePublish) {
// if all successful, re-tag as latest
await pMap(
packageJsons,
async ({ name, cwd }) => {
const tag = canary ? ` --tag canary` : ''
console.log(`Publishing ${name}${tag}`)
await exec(`npm publish${tag}`, {
cwd,
}).catch((err) => console.error(err))
},
{
concurrency: 15,
}
)
} else {
const distTag = canary ? 'canary' : 'latest'
// if all successful, re-tag as latest (try and be fast)
await pMap(
packageJsons,
async ({ name, cwd }) => {
await exec(`npm dist-tag add ${name}@${version} ${distTag}`, {
cwd,
}).catch((err) => console.error(err))
},
{
concurrency: 20,
}
)
}
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`✅ Published\n`)
// then git tag, commit, push
if (!finish) {
await exec(`yarn fix`)
await exec(`yarn install`)
}
const tagPrefix = canary ? 'canary' : 'v'
const gitTag = `${tagPrefix}${version}`
if (!rePublish || reRun || finish) {
await exec(`git add -A`)
await exec(`git commit -m ${gitTag}`)
await exec(`git tag ${gitTag}`)
if (!dirty) {
// pull once more before pushing so if there was a push in interim we get it
await exec(`git pull --rebase origin main`)
}
await exec(`git push origin head`)
await exec(`git push origin ${gitTag}`)
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`✅ Pushed and versioned\n`)
}
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(`✅ Done\n`)
} catch (err) {
// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log('\nError:\n', err)
process.exit(1)
}
}
run()