-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathpreview-changeset-versions.mjs
More file actions
129 lines (110 loc) · 3.48 KB
/
preview-changeset-versions.mjs
File metadata and controls
129 lines (110 loc) · 3.48 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
#!/usr/bin/env node
/**
* Uses `@changesets/get-release-plan` to get the version bumps and formats it as markdown.
*/
import { writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { parseArgs } from 'node:util'
import getReleasePlan from '@changesets/get-release-plan'
const GITHUB_WORKSPACE = resolve(
process.env.CHANGESET_WORKSPACE || process.env.GITHUB_WORKSPACE,
)
console.log(`Using workspace: ${GITHUB_WORKSPACE}`)
function reasonRank(reason) {
return reason === 'Changeset' ? 2 : 1
}
async function main() {
const { values } = parseArgs({
args: process.argv.slice(2),
options: {
output: { type: 'string', short: 'o' },
},
strict: true,
allowPositionals: false,
})
const releasePlan = await getReleasePlan(GITHUB_WORKSPACE)
const releases = releasePlan.releases
if (releases.length === 0) {
const msg =
'No changeset entries found. Merging this PR will not cause a version bump for any packages.\n'
process.stdout.write(msg)
if (values.output) {
writeFileSync(values.output, msg)
process.stdout.write(`Written to ${values.output}\n`)
}
return
}
// 6. Diff
const bumps = []
for (const release of releases) {
if (release.oldVersion === release.newVersion) continue
const reason = release.changesets.length !== 0 ? 'Changeset' : 'Dependent'
bumps.push({ ...release, reason })
}
// Order by reason and name
bumps.sort(
(a, b) =>
reasonRank(b.reason) - reasonRank(a.reason) ||
a.name.localeCompare(b.name),
)
// 7. Build markdown
const lines = []
if (bumps.length === 0) {
lines.push(
'No version changes detected. Merging this PR will not cause a version bump for any packages.',
)
} else {
const majorBumps = bumps.filter((b) => b.type === 'major')
const minorBumps = bumps.filter((b) => b.type === 'minor')
const patchBumps = bumps.filter((b) => b.type === 'patch')
const directBumps = bumps.filter((b) => b.reason === 'Changeset')
const indirectBumps = bumps.filter((b) => b.reason === 'Dependent')
lines.push(
`**${directBumps.length}** package(s) bumped directly, **${indirectBumps.length}** bumped as dependents.`,
)
lines.push('')
if (majorBumps.length > 0) {
lines.push('### 🟥 Major bumps')
lines.push('')
lines.push('| Package | Version | Reason |')
lines.push('| --- | --- | --- |')
for (const b of majorBumps) {
lines.push(
`| \`${b.name}\` | ${b.oldVersion} → ${b.newVersion} | ${b.reason} |`,
)
}
lines.push('')
}
if (minorBumps.length > 0) {
lines.push('### 🟨 Minor bumps')
lines.push('')
lines.push('| Package | Version | Reason |')
lines.push('| --- | --- | --- |')
for (const b of minorBumps) {
lines.push(
`| \`${b.name}\` | ${b.oldVersion} → ${b.newVersion} | ${b.reason} |`,
)
}
lines.push('')
}
if (patchBumps.length > 0) {
lines.push('### 🟩 Patch bumps')
lines.push('')
lines.push('| Package | Version | Reason |')
lines.push('| --- | --- | --- |')
for (const b of patchBumps) {
lines.push(
`| \`${b.name}\` | ${b.oldVersion} → ${b.newVersion} | ${b.reason} |`,
)
}
}
}
lines.push('')
const md = lines.join('\n')
process.stdout.write(md)
if (values.output) {
writeFileSync(values.output, md)
process.stdout.write(`Written to ${values.output}\n`)
}
}
main()