-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
publish.ts
1111 lines (962 loc) · 34.8 KB
/
publish.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import slugify from '@sindresorhus/slugify'
import { IncomingWebhook } from '@slack/webhook'
import arg from 'arg'
import topo from 'batching-toposort'
import execa from 'execa'
import fs from 'fs'
import globby from 'globby'
import { blue, bold, cyan, dim, magenta, red, underline } from 'kleur/colors'
import fetch from 'node-fetch'
import pRetry from 'p-retry'
import path from 'path'
import redis from 'redis'
import semver from 'semver'
import { promisify } from 'util'
const onlyPackages = process.env.ONLY_PACKAGES ? process.env.ONLY_PACKAGES.split(',') : null
const skipPackages = process.env.SKIP_PACKAGES ? process.env.SKIP_PACKAGES.split(',') : null
async function getLatestCommitHash(dir: string): Promise<string> {
if (process.env.GITHUB_CONTEXT) {
const context = JSON.parse(process.env.GITHUB_CONTEXT)
return context.sha
}
const result = await runResult(dir, 'git log --pretty=format:"%ad %H %P" --date=iso-strict -n 1')
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [date, hash] = result.split(' ')
return hash
}
/**
* Runs a command and returns the resulting stdout in a Promise.
* @param cwd cwd for running the command
* @param cmd command to run
*/
async function runResult(cwd: string, cmd: string): Promise<string> {
try {
const result = await execa.command(cmd, {
cwd,
stdio: 'pipe',
shell: true,
})
return result.stdout
} catch (_e) {
const e = _e as execa.ExecaError
throw new Error(red(`Error running ${bold(cmd)} in ${underline(cwd)}:`) + (e.stderr || e.stack || e.message))
}
}
/**
* Runs a command and pipes the stdout & stderr to the current process.
* @param cwd cwd for running the command
* @param cmd command to run
*/
async function run(cwd: string, cmd: string, dry = false, hidden = false): Promise<void> {
const args = [underline('./' + cwd).padEnd(20), bold(cmd)]
if (dry) {
args.push(dim('(dry)'))
}
if (!hidden) {
console.log(...args)
}
if (dry) {
return
}
try {
await execa.command(cmd, {
cwd,
stdio: 'inherit',
shell: true,
env: {
...process.env,
PRISMA_SKIP_POSTINSTALL_GENERATE: 'true',
},
})
} catch (_e) {
const e = _e as execa.ExecaError
throw new Error(red(`Error running ${bold(cmd)} in ${underline(cwd)}:`) + (e.stderr || e.stack || e.message))
}
}
type RawPackage = {
path: string
packageJson: any
}
type RawPackages = { [packageName: string]: RawPackage }
export async function getPackages(): Promise<RawPackages> {
const packagePaths = await globby(['packages/*/package.json'], {
ignore: ['**/node_modules/**', '**/examples/**', '**/fixtures/**'],
})
const packages = await Promise.all(
packagePaths.map(async (p) => ({
path: p,
packageJson: JSON.parse(await fs.promises.readFile(p, 'utf-8')),
})),
)
return packages.reduce<RawPackages>((acc, p) => {
if (p.packageJson.name) {
acc[p.packageJson.name] = p
}
return acc
}, {})
}
interface Package {
name: string
path: string
version: string
usedBy: string[]
usedByDev: string[]
uses: string[]
usesDev: string[]
packageJson: any
}
type Packages = { [packageName: string]: Package }
export function getPackageDependencies(packages: RawPackages): Packages {
const packageCache = Object.entries(packages).reduce<Packages>((acc, [name, pkg]) => {
const usesDev = getPrismaDependencies(pkg.packageJson.devDependencies)
acc[name] = {
version: pkg.packageJson.version,
name,
path: pkg.path,
usedBy: [],
usedByDev: [],
uses: getPrismaDependencies(pkg.packageJson.dependencies),
usesDev,
packageJson: pkg.packageJson,
}
return acc
}, {})
for (const pkg of Object.values(packageCache)) {
for (const dependency of pkg.uses) {
if (packageCache[dependency]) {
packageCache[dependency].usedBy.push(pkg.name)
} else {
console.info(`Skipping ${dependency} as it's not in this workspace`)
}
}
for (const devDependency of pkg.usesDev) {
if (packageCache[devDependency]) {
packageCache[devDependency].usedByDev.push(pkg.name)
} else {
console.info(`Skipping ${devDependency} as it's not in this workspace`)
}
}
}
return packageCache
}
function getPrismaDependencies(dependencies?: { [name: string]: string }): string[] {
if (!dependencies) {
return []
}
return Object.keys(dependencies).filter((d) => d.startsWith('@prisma') && !d.startsWith('@prisma/studio'))
}
function getCircularDependencies(packages: Packages): string[][] {
const circularDeps = [] as string[][]
for (const pkg of Object.values(packages)) {
const uses = [...pkg.uses, ...pkg.usesDev]
const usedBy = [...pkg.usedBy, ...pkg.usedByDev]
const circles = intersection(uses, usedBy)
if (circles.length > 0) {
circularDeps.push(circles)
}
}
return circularDeps
}
export function getPublishOrder(packages: Packages): string[][] {
const dag: { [pkg: string]: string[] } = Object.values(packages).reduce((acc, curr) => {
acc[curr.name] = [...curr.usedBy, ...curr.usedByDev]
return acc
}, {})
return topo(dag) // TODO: this is now done by pnpm (top sort), can we remove?
}
function zeroOutPatch(version: string): string {
const parts = version.split('.')
parts[parts.length - 1] = '0'
return parts.join('.')
}
/**
* Takes the max dev version + 1
* For now supporting X.Y.Z-dev.#
* @param packages Local package definitions
*/
async function getNewDevVersion(packages: Packages): Promise<string> {
const before = Math.round(performance.now())
console.log('\nCalculating new dev version...')
// Why are we calling zeroOutPatch?
// Because here we're only interested in the 2.5.0 <- the next minor stable version
// If the current version would be 2.4.7, we would end up with 2.5.7
const nextStable = zeroOutPatch((await getNextMinorStable())!)
console.log(`getNewDevVersion: Next minor stable: ${nextStable}`)
const versions = await getAllVersionsPublishedFor(packages, 'dev', nextStable + '-dev')
const maxDev = getMaxDevVersionIncrement(versions)
const version = `${nextStable}-dev.${maxDev + 1}`
console.log(`Got ${version} in ${Math.round(performance.now()) - before}ms`)
return version
}
/**
* Takes the max dev version + 1
* For now supporting X.Y.Z-dev.#
* @param packages Local package definitions
*/
async function getNewIntegrationVersion(packages: Packages, branch: string): Promise<string> {
const before = Math.round(performance.now())
console.log('\nCalculating new integration version...')
// Why are we calling zeroOutPatch?
// Because here we're only interested in the 2.5.0 <- the next minor stable version
// If the current version would be 2.4.7, we would end up with 2.5.7
const nextStable = zeroOutPatch((await getNextMinorStable())!)
console.log(`getNewIntegrationVersion: Next minor stable: ${nextStable}`)
const branchWithoutPrefix = branch.replace(/^integration\//, '')
const versionNameSlug = `${nextStable}-integration-${slugify(branchWithoutPrefix)}`
const versions = await getAllVersionsPublishedFor(packages, 'integration', versionNameSlug)
const maxIntegration = getMaxIntegrationVersionIncrement(versions)
const version = `${versionNameSlug}.${maxIntegration + 1}`
// TODO: can we remove this?
console.log(`Got ${version} in ${Math.round(performance.now()) - before}ms`)
return version
}
// This function gets the current "patchMajorMinor" (major and minor of the patch branch),
// then retrieves the current versions of @prisma/client from npm,
// and filters that array down to the major and minor of the patch branch
// to figure out what the current highest patch number there currently is
async function getCurrentPatchForPatchVersions(patchMajorMinor: { major: number; minor: number }): Promise<number> {
// TODO: could we add the name of the branch, as well as the relevant versions => faster
// $ npm view '@prisma/client@3.0.x' version --json
// [
// "3.0.1",
// "3.0.2"
// ]
// We retry a few times if it fails
// npm can have some hiccups
const remoteVersionsString = await pRetry(
async () => {
return await runResult('.', 'npm view @prisma/client@* version --json')
},
{
retries: 6,
onFailedAttempt: (e) => {
console.error(e)
},
},
)
let versions = JSON.parse(remoteVersionsString)
// inconsistent npm api
if (!Array.isArray(versions)) {
versions = [versions]
}
const relevantVersions: Array<{
major: number
minor: number
patch: number
}> = versions
.map((v) => {
const match = semverRegex.exec(v)
if (match?.groups) {
return {
major: Number(match.groups.major),
minor: Number(match.groups.minor),
patch: Number(match.groups.patch),
}
}
return null
})
.filter((group) => group && group.minor === patchMajorMinor.minor && group.major === patchMajorMinor.major)
if (relevantVersions.length === 0) {
return 0
}
// sort descending by patch
relevantVersions.sort((a, b) => {
return a.patch < b.patch ? 1 : -1
})
return relevantVersions[0].patch
}
async function getNewPatchDevVersion(packages: Packages, patchBranch: string): Promise<string> {
const patchMajorMinor = getSemverFromPatchBranch(patchBranch)
if (!patchMajorMinor) {
throw new Error(`Could not get major and minor for ${patchBranch}`)
}
const currentPatch = await getCurrentPatchForPatchVersions(patchMajorMinor)
const newPatch = currentPatch + 1
const newVersion = `${patchMajorMinor.major}.${patchMajorMinor.minor}.${newPatch}`
const versions = [...(await getAllVersionsPublishedFor(packages, 'dev', newVersion))]
const maxIncrement = getMaxPatchVersionIncrement(versions)
return `${newVersion}-dev.${maxIncrement + 1}`
}
function getMaxDevVersionIncrement(versions: string[]): number {
const regex = /\d+\.\d+\.\d+-dev\.(\d+)/
const increments = versions
.filter((v) => v.trim().length > 0)
.map((v) => {
const match = regex.exec(v)
if (match) {
return Number(match[1])
}
return 0
})
.filter((v) => v)
return Math.max(...increments, 0)
}
function getMaxIntegrationVersionIncrement(versions: string[]): number {
const regex = /\d+\.\d+\.\d+-integration.*\.(\d+)/
const increments = versions
.filter((v) => v.trim().length > 0)
.map((v) => {
const match = regex.exec(v)
if (match) {
return Number(match[1])
}
return 0
})
.filter((v) => v)
return Math.max(...increments, 0)
}
// TODO: Adjust this for stable releases
function getMaxPatchVersionIncrement(versions: string[]): number {
const regex = /\d+\.\d+\.\d+-dev\.(\d+)/
const increments = versions
.filter((v) => v.trim().length > 0)
.map((v) => {
const match = regex.exec(v)
if (match && match[1]) {
return Number(match[1])
}
return 0
})
.filter((v) => v)
return Math.max(...increments, 0)
}
/**
* @param pkgs
* @param channel
* @param prefix
* @returns All versions published on npm for a given channel and prefix
*/
export async function getAllVersionsPublishedFor(pkgs: Packages, channel: string, prefix: string): Promise<string[]> {
// We check the versions for the `@prisma/debug` package
// Why?
// Because `@prisma/debug` is the first package that will be published
// So if npm fails to publish one of the packages,
// we cannot republish on the same version on a next run
const pkg = pkgs['@prisma/debug']
const values = async (pkg: Package) => {
const pkgVersions = [] as string[]
if (pkg.version.startsWith(prefix)) {
pkgVersions.push(pkg.version)
}
// We retry a few times if it fails
// npm can have some hiccups
const remoteVersionsString = await pRetry(
async () => {
return await runResult('.', `npm info ${pkg.name} versions --json`)
},
{
retries: 6,
onFailedAttempt: (e) => {
console.error(e)
},
},
)
const remoteVersions: string = JSON.parse(remoteVersionsString)
for (const remoteVersion of remoteVersions) {
if (remoteVersion.includes(channel) && remoteVersion.startsWith(prefix) && !pkgVersions.includes(remoteVersion)) {
pkgVersions.push(remoteVersion)
}
}
return pkgVersions
}
return [...new Set(await values(pkg))]
}
/**
* Only used when publishing to the `dev` and `integration` npm channels
* (see `getNewDevVersion()` and `getNewIntegrationVersion()`)
* @returns The next minor version for the `latest` channel
* Example: If latest is `4.9.0` it will return `4.10.0`
*/
async function getNextMinorStable() {
// We check the Prisma CLI `latest` version
// We retry a few times if it fails
// npm can have some hiccups
const remoteVersionString = await pRetry(
async () => {
return await runResult('.', `npm info prisma version`)
},
{
retries: 6,
onFailedAttempt: (e) => {
console.error(e)
},
},
)
return increaseMinor(remoteVersionString)
}
// TODO: could probably use the semver package
function getSemverFromPatchBranch(version: string) {
// the branch name must match
// number.number.x like 3.0.x or 2.29.x
// as an exact match, no character before or after
const regex = /^(\d+)\.(\d+)\.x$/
const match = regex.exec(version)
if (match) {
return {
major: Number(match[1]),
minor: Number(match[2]),
}
}
return undefined
}
// TODO: name this to main
async function publish() {
const args = arg({
'--publish': Boolean,
'--dry-run': Boolean,
'--release': String, // TODO What does that do? Can we remove this? probably
'--test': Boolean,
})
if (!process.env.BUILDKITE_BRANCH) {
throw new Error(`Missing env var BUILDKITE_BRANCH`)
}
if (process.env.DRY_RUN) {
console.log(blue(bold(`\nThe DRY_RUN env var is set, so we'll do a dry run!\n`)))
args['--dry-run'] = true
}
const dryRun = args['--dry-run'] ?? false
// TODO: rename BUILDKITE_TAG
if (args['--publish'] && process.env.BUILDKITE_TAG) {
if (args['--release']) {
throw new Error(`Can't provide env var BUILDKITE_TAG and --release at the same time`)
}
console.log(`Setting --release to BUILDKITE_TAG = ${process.env.BUILDKITE_TAG}`)
args['--release'] = process.env.BUILDKITE_TAG // TODO: rename this var to RELEASE_VERSION
// TODO: put this into a global variable VERSION
// and then replace the args['--release'] with it
}
if (!args['--test'] && !args['--publish'] && !dryRun) {
throw new Error('Please either provide --test or --publish or --dry-run')
}
if (args['--release']) {
if (!semver.valid(args['--release'])) {
throw new Error(`New release version ${bold(underline(args['--release']))} is not a valid semver version.`)
}
// TODO: this can probably be replaced by semver lib
const releaseRegex = /\d{1,2}\.\d{1,2}\.\d{1,2}/
if (!releaseRegex.test(args['--release'])) {
throw new Error(
`New release version ${bold(underline(args['--release']))} does not follow the stable naming scheme: ${bold(
underline('x.y.z'),
)}`,
)
}
// If there is --release, it's always also --publish
args['--publish'] = true
}
// makes sure that only have 1 publish job running at a time
let unlock: undefined | (() => void)
if (process.env.BUILDKITE && args['--publish']) {
console.info(`Let's try to acquire a lock before continuing. (to avoid concurrent publishing)`)
const before = Math.round(performance.now())
// TODO: problem lock might not work for more than 2 jobs
unlock = await acquireLock(process.env.BUILDKITE_BRANCH)
const after = Math.round(performance.now())
console.log(`Acquired lock after ${after - before}ms`)
}
try {
// TODO: does this make more sense to be in our tests? or in the monorepo postinstall?
const rawPackages = await getPackages()
const packages = getPackageDependencies(rawPackages)
const circles = getCircularDependencies(packages)
if (circles.length > 0) {
// TODO this can be done by esbuild
throw new Error(`Oops, there are circular dependencies: ${circles}`)
}
let prismaVersion: undefined | string
let tag: undefined | string
let tagForEcosystemTestsCheck: undefined | string
const patchBranch = getPatchBranch()
console.log({ patchBranch })
// TODO: can be refactored into one branch utility
const branch = await getPrismaBranch()
console.log({ branch })
// For branches that are named "integration/" we publish to the integration npm tag
if (branch && (process.env.FORCE_INTEGRATION_RELEASE || branch.startsWith('integration/'))) {
prismaVersion = await getNewIntegrationVersion(packages, branch)
tag = 'integration'
}
// Is it a patch branch? (Like 2.20.x)
else if (patchBranch) {
prismaVersion = await getNewPatchDevVersion(packages, patchBranch)
tag = 'patch-dev'
if (args['--release']) {
tagForEcosystemTestsCheck = 'patch-dev' //?
prismaVersion = args['--release']
tag = 'latest'
}
} else if (args['--release']) {
// TODO:Where each patch branch goes
prismaVersion = args['--release']
tag = 'latest'
tagForEcosystemTestsCheck = 'dev'
} else {
prismaVersion = await getNewDevVersion(packages)
tag = 'dev'
}
console.log({
patchBranch,
tag,
tagForEcosystemTestsCheck,
prismaVersion,
})
if (typeof process.env.GITHUB_OUTPUT == 'string' && process.env.GITHUB_OUTPUT.length > 0) {
fs.appendFileSync(process.env.GITHUB_OUTPUT, `patchBranch=${patchBranch}\n`)
fs.appendFileSync(process.env.GITHUB_OUTPUT, `tag=${tag}\n`)
fs.appendFileSync(process.env.GITHUB_OUTPUT, `tagForEcosystemTestsCheck=${tagForEcosystemTestsCheck}\n`)
fs.appendFileSync(process.env.GITHUB_OUTPUT, `prismaVersion=${prismaVersion}\n`)
}
if (!dryRun && args['--test']) {
if (onlyPackages || skipPackages) {
console.log(bold('\nTesting all packages was skipped because onlyPackages or skipPackages is set.'))
} else {
console.log(bold('\nTesting all packages...'))
await testPackages(packages, getPublishOrder(packages))
}
}
if (args['--publish'] || dryRun) {
if (args['--release']) {
if (!tagForEcosystemTestsCheck) {
throw new Error(`tagForEcosystemTestsCheck missing`)
}
const passing = await areEcosystemTestsPassing(tagForEcosystemTestsCheck)
if (!passing && !process.env.SKIP_ECOSYSTEMTESTS_CHECK) {
throw new Error(`We can't release, as the ecosystem-tests are not passing for the ${tag} npm tag!
Check them out at https://github.com/prisma/ecosystem-tests/actions?query=workflow%3Atest+branch%3A${tag}`)
}
}
const publishOrder = filterPublishOrder(getPublishOrder(packages), ['@prisma/integration-tests'])
if (!dryRun) {
console.log(`Let's first do a dry run!`)
await publishPackages(packages, publishOrder, true, prismaVersion, tag, args['--release'])
console.log(`Waiting 5 sec so you can check it out first...`)
await new Promise((r) => setTimeout(r, 5_000))
}
await publishPackages(packages, publishOrder, dryRun, prismaVersion, tag, args['--release'])
const enginesCommitHash = await getEnginesCommitHash()
const enginesCommitInfo = await getCommitInfo('prisma-engines', enginesCommitHash)
const prismaCommitHash = await getLatestCommitHash('.')
const prismaCommitInfo = await getCommitInfo('prisma', prismaCommitHash)
if (typeof process.env.GITHUB_OUTPUT == 'string' && process.env.GITHUB_OUTPUT.length > 0) {
fs.appendFileSync(process.env.GITHUB_OUTPUT, `enginesCommitHash=${enginesCommitHash}\n`)
fs.appendFileSync(process.env.GITHUB_OUTPUT, `prismaCommitHash=${prismaCommitHash}\n`)
}
try {
await sendSlackMessage({
version: prismaVersion,
enginesCommitInfo,
prismaCommitInfo,
})
} catch (e) {
console.error(e)
}
if (!args['--dry-run']) {
try {
await tagEnginesRepo(prismaVersion, enginesCommitHash, patchBranch, dryRun)
} catch (e) {
console.error(e)
}
}
}
} catch (e) {
if (unlock) {
unlock()
unlock = undefined
}
throw e
} finally {
if (unlock) {
unlock()
unlock = undefined
}
}
}
async function getEnginesCommitHash(): Promise<string> {
const prismaPath = path.resolve(process.cwd(), './packages/engines/package.json')
const pkg = JSON.parse(await fs.promises.readFile(prismaPath, 'utf-8'))
// const engineVersion = pkg.prisma.version
const engineVersion = pkg.devDependencies['@prisma/engines-version']?.split('.').slice(-1)[0]
return engineVersion
}
async function tagEnginesRepo(
prismaVersion: string,
engineVersion: string,
patchBranch: string | null,
dryRun = false,
) {
let previousTag: string
console.log(`Going to tag the engines repo with "${prismaVersion}", patchBranch: ${patchBranch}, dryRun: ${dryRun}`)
/** Get ready */
await cloneOrPull('prisma-engines', dryRun)
// 3.2.x
if (patchBranch) {
// 3.2
const [major, minor] = patchBranch.split('.')
const majorMinor = [major, minor].join('.')
// ['3.2.0', '3.2.1']
const patchesPublished: string | string[] = JSON.parse(
// TODO this line is useful for retrieving versions
await runResult('.', `npm view @prisma/client@${majorMinor} version --json`),
)
console.log({ patchesPublished })
if (typeof patchesPublished === 'string') {
previousTag = patchesPublished
} else if (patchesPublished.length > 0) {
// 3.2.0
previousTag = patchesPublished.pop() as string
} else {
console.warn('No version found for this patch branch')
return
}
} else {
/** Get previous tag */
previousTag = await runResult('prisma-engines', `git describe --tags --abbrev=0`)
}
/** Get commits between previous tag and engines sha1 */
const changelog = await runResult(
'prisma-engines',
`git log ${previousTag}..${engineVersion} --pretty=format:' * %h - %s - by %an' --`,
)
// TODO remove later
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const changelogSanitized = changelog.replace(/"/gm, '\\"').replace(/`/gm, '\\`')
if (typeof process.env.GITHUB_OUTPUT == 'string' && process.env.GITHUB_OUTPUT.length > 0) {
// fs.appendFileSync(process.env.GITHUB_OUTPUT, `changelogSanitized=${changelogSanitized}\n`)
}
}
/**
* Tests packages in "publishOrder"
* @param packages Packages
* @param publishOrder string[][]
*/
async function testPackages(packages: Packages, publishOrder: string[][]): Promise<void> {
const order = flatten(publishOrder)
// If parallelism is set in build-kite we split the testing
// Job 0 - Node-API Library
// PRISMA_CLIENT_ENGINE_TYPE="library"
// PRISMA_CLI_QUERY_ENGINE_TYPE="library"
// Job 1 - Binary
// PRISMA_CLIENT_ENGINE_TYPE="binary"
// PRISMA_CLI_QUERY_ENGINE_TYPE="binary"
if (process.env.BUILDKITE_PARALLEL_JOB === '0') {
console.log('BUILDKITE_PARALLEL_JOB === 0 - Node-API Library')
} else if (process.env.BUILDKITE_PARALLEL_JOB === '1') {
console.log('BUILDKITE_PARALLEL_JOB === 1 - Binary')
}
console.log(bold(`\nRun ${cyan('tests')}. Testing order:`))
console.log(order)
for (const pkgName of order) {
const pkg = packages[pkgName]
if (pkg.packageJson.scripts.test) {
console.log(`\nTesting ${magenta(pkg.name)}`)
// Sets ENV to override engines
if (process.env.BUILDKITE_PARALLEL_JOB === '0') {
await run(
path.dirname(pkg.path),
'PRISMA_CLIENT_ENGINE_TYPE="library" PRISMA_CLI_QUERY_ENGINE_TYPE="library" pnpm run test --silent',
)
} else if (process.env.BUILDKITE_PARALLEL_JOB === '1') {
await run(
path.dirname(pkg.path),
'PRISMA_CLIENT_ENGINE_TYPE="binary" PRISMA_CLI_QUERY_ENGINE_TYPE="binary" pnpm run test --silent',
)
} else {
await run(path.dirname(pkg.path), 'pnpm run test')
}
} else {
console.log(`\nSkipping ${magenta(pkg.name)}, as it doesn't have tests`)
}
}
}
function flatten<T>(arr: T[][]): T[] {
return arr.reduce((acc, val) => acc.concat(val), [])
}
function intersection<T>(arr1: T[], arr2: T[]): T[] {
return arr1.filter((value) => arr2.includes(value))
}
// Thanks 🙏 to https://github.com/semver/semver/issues/232#issuecomment-405596809
const semverRegex =
/^(?<major>0|[1-9]\d*)\.(?<minor>0|[1-9]\d*)\.(?<patch>0|[1-9]\d*)(?:-(?<prerelease>(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+(?<buildmetadata>[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/
function increaseMinor(version: string) {
const match = semverRegex.exec(version)
if (match?.groups) {
return `${match.groups.major}.${Number(match.groups.minor) + 1}.${match.groups.patch}`
}
return undefined
}
function filterPublishOrder(publishOrder: string[][], packages: string[]): string[][] {
return publishOrder.reduce<string[][]>((acc, curr) => {
if (Array.isArray(curr)) {
curr = curr.filter((pkg) => !packages.includes(pkg))
if (curr.length > 0) {
acc.push(curr)
}
} else if (!packages.includes(curr)) {
acc.push(curr)
}
return acc
}, [])
}
async function publishPackages(
packages: Packages,
// TODO: pnpm can calculate this for us when using `pnpm -r publish`
publishOrder: string[][],
dryRun: boolean,
prismaVersion: string,
tag: string,
releaseVersion?: string,
): Promise<void> {
// we need to release a new `prisma` CLI in all cases.
// if there is a change in prisma-client-js, it will also use this new version
const publishStr = dryRun ? `${bold('Dry publish')} ` : releaseVersion ? 'Releasing ' : 'Publishing '
if (releaseVersion) {
console.log(red(bold(`RELEASE. This will release ${underline(releaseVersion)} on latest!!!`)))
if (dryRun) {
console.log(red(bold(`But it's only a dry run, so don't worry.`)))
}
}
console.log(
blue(
`\n${bold(underline(prismaVersion))}: ${publishStr}(all) ${bold(
String(Object.values(packages).length),
)} packages. Publish order:`,
),
)
console.log(blue(publishOrder.map((o, i) => ` ${i + 1}. ${o.join(', ')}`).join('\n')))
if (releaseVersion) {
console.log(
red(
bold(
`\nThis will ${underline('release')} a new version of Prisma packages on latest: ${underline(prismaVersion)}`,
),
),
)
if (!dryRun) {
console.log(red('Are you sure you want to do this? We wait for 10s just in case...'))
await new Promise((r) => {
setTimeout(r, 10_000)
})
}
} else if (!dryRun) {
// For dev releases
console.log(`\nGiving you 5s to review the changes...`)
await new Promise((r) => {
setTimeout(r, 5_000)
})
}
for (const currentBatch of publishOrder) {
for (const pkgName of currentBatch) {
const pkg = packages[pkgName]
if (pkg.name === '@prisma/integration-tests') {
continue
}
// @prisma/engines-version is published outside of this script
const packagesNotToPublish = ['@prisma/engines-version']
if (packagesNotToPublish.includes(pkgName)) {
continue
}
const pkgDir = path.dirname(pkg.path)
const newVersion = prismaVersion
console.log(`\nPublishing ${magenta(`${pkgName}@${newVersion}`)} ${dim(`on ${tag}`)}`)
// Why is this needed?
// Was introduced in the first version of this script on Apr 14, 2020
// https://github.com/prisma/prisma/commit/7d6a26c1777c59ee945356687673102de4b1fe55#diff-51cd3eaba5264dc956e45fabcc02d5d21d8a8c473bd1bd00a297f9f4550c115bR790-R797
const prismaDeps = [...pkg.uses, ...pkg.usesDev]
if (prismaDeps.length > 0) {
await pRetry(
async () => {
await run(pkgDir, `pnpm update ${prismaDeps.join(' ')} --filter "${pkgName}"`, dryRun)
},
{
retries: 6,
onFailedAttempt: (e) => {
console.error(e)
},
},
)
}
// set the version in package.json for current package
await writeVersion(pkgDir, newVersion, dryRun)
// For package `prisma`, get latest commit hash (that is being released)
// and put into `prisma.prismaCommit` in `package.json` before publishing
if (pkgName === 'prisma') {
const latestCommitHash = await getLatestCommitHash('.')
await writeToPkgJson(pkgDir, (pkg) => {
pkg.prisma.prismaCommit = latestCommitHash
})
}
if (!isSkipped(pkgName)) {
/*
* About `--no-git-checks`
* By default, `pnpm publish` will make some checks before actually publishing a new version of your package.
* The next checks will happen:
* - The current branch is your publish branch. The publish branch is `master` by default. This is configurable through the `publish-branch` setting.
* - Your working directory is clean (there are no uncommitted changes).
* - The branch is up-to-date.
*/
await run(pkgDir, `pnpm publish --no-git-checks --access public --tag ${tag}`, dryRun)
}
}
}
}
function isSkipped(pkgName) {
if (skipPackages && skipPackages.includes(pkgName)) {
return true
}
if (onlyPackages && !onlyPackages.includes(pkgName)) {
return true
}
return false
}
async function acquireLock(branch: string): Promise<() => void> {
const before = Math.round(performance.now())
if (!process.env.REDIS_URL) {
console.log(bold(red(`REDIS_URL missing. Setting dummy lock`)))
return () => {
console.log(`Lock removed after ${Math.round(performance.now()) - before}ms`)
}
}
const client = redis.createClient({
url: process.env.REDIS_URL,
retry_strategy: () => {
return 1000
},
})
const lock = promisify(require('redis-lock')(client))
// get a lock of max 15 min
// the lock is specific to the branch name
const cb = await lock(`prisma-release-${branch}`, 15 * 60 * 1000)
return async () => {
cb()
const after = Math.round(performance.now())
console.log(`Lock removed after ${after - before}ms`)
await new Promise((r) => setTimeout(r, 200))
client.quit()
}
}
async function writeToPkgJson(pkgDir, cb: (pkg: any) => any, dryRun?: boolean) {
const pkgJsonPath = path.join(pkgDir, 'package.json')
const file = await fs.promises.readFile(pkgJsonPath, 'utf-8')
let packageJson = JSON.parse(file)
if (dryRun) {
console.log(`Would write to ${pkgJsonPath} from ${packageJson.version} now`)
} else {
const result = cb(packageJson)
if (result) {
packageJson = result
}
await fs.promises.writeFile(pkgJsonPath, JSON.stringify(packageJson, null, 2))
}
}
async function writeVersion(pkgDir: string, version: string, dryRun?: boolean) {
const pkgJsonPath = path.join(pkgDir, 'package.json')
const file = await fs.promises.readFile(pkgJsonPath, 'utf-8')
const packageJson = JSON.parse(file)
if (dryRun) {
console.log(`Would update ${pkgJsonPath} from ${packageJson.version} to ${version} now ${dim('(dry)')}`)
} else {
packageJson.version = version
await fs.promises.writeFile(pkgJsonPath, JSON.stringify(packageJson, null, 2))
}
}
async function getPrismaBranch(): Promise<string | undefined> {
if (process.env.BUILDKITE_BRANCH) {
return process.env.BUILDKITE_BRANCH
}
try {
// TODO: this can probably be simplified, we don't publish locally, remove?
return await runResult('.', 'git rev-parse --symbolic-full-name --abbrev-ref HEAD')
} catch (e) {}
return undefined
}
async function areEcosystemTestsPassing(tag: string): Promise<boolean> {