-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcheck-release-version.ts
More file actions
144 lines (123 loc) · 3.58 KB
/
Copy pathcheck-release-version.ts
File metadata and controls
144 lines (123 loc) · 3.58 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
import { appendFileSync } from 'node:fs';
import {
desktopReleasePackageSources,
normalizeReleaseVersion,
readPackageVersion,
type PackageVersionSource,
} from './desktop-release-version.ts';
interface ParsedArgs {
githubOutput?: string;
githubSummary?: string;
version?: string;
}
function parseArgs(argv: string[]): ParsedArgs {
const args: ParsedArgs = {
githubOutput: process.env.GITHUB_OUTPUT || undefined,
githubSummary: process.env.GITHUB_STEP_SUMMARY || undefined,
version: process.env.RELEASE_VERSION || undefined,
};
for (let i = 0; i < argv.length; i += 1) {
const arg = argv[i];
const next = argv[i + 1];
if (arg === '--version' || arg === '-v') {
if (!next) throw new Error(`${arg} requires a value.`);
args.version = next;
i += 1;
continue;
}
if (arg === '--github-output') {
if (!next) throw new Error(`${arg} requires a value.`);
args.githubOutput = next;
i += 1;
continue;
}
if (arg === '--github-summary') {
if (!next) throw new Error(`${arg} requires a value.`);
args.githubSummary = next;
i += 1;
continue;
}
throw new Error(`Unknown argument: ${arg}`);
}
return args;
}
function appendGithubOutput(
outputPath: string | undefined,
outputs: Record<string, string>,
): void {
if (!outputPath) return;
const lines = Object.entries(outputs).map(([key, value]) => `${key}=${value}`);
appendFileSync(outputPath, `${lines.join('\n')}\n`);
}
function appendGithubSummary(
summaryPath: string | undefined,
params: {
mismatches: { actual: string; source: PackageVersionSource }[];
packageVersions: { source: PackageVersionSource; version: string }[];
tag: string;
version: string;
},
): void {
if (!summaryPath) return;
const lines = [
'## Desktop release version',
'',
`Version: ${params.version}`,
`Release tag: ${params.tag}`,
'',
'| Source | Version |',
'| --- | --- |',
...params.packageVersions.map(
({ source, version }) => `| ${source.path} | ${version} |`,
),
];
if (params.mismatches.length > 0) {
lines.push('', 'Version mismatch detected. Update source versions first.');
}
appendFileSync(summaryPath, `${lines.join('\n')}\n`);
}
function main(): void {
const args = parseArgs(process.argv.slice(2));
if (!args.version) {
throw new Error(
'Release version is required. Pass --version or RELEASE_VERSION.',
);
}
const { tag, version } = normalizeReleaseVersion(args.version);
const packageVersions = desktopReleasePackageSources.map((source) => ({
source,
version: readPackageVersion(source.path),
}));
const mismatches = packageVersions
.filter((entry) => entry.version !== version)
.map((entry) => ({
actual: entry.version,
source: entry.source,
}));
appendGithubSummary(args.githubSummary, {
mismatches,
packageVersions,
tag,
version,
});
if (mismatches.length > 0) {
const details = mismatches
.map(({ actual, source }) => ` - ${source.path}: ${actual}`)
.join('\n');
throw new Error(
[
`Release version mismatch. Requested ${version}, but source versions differ:`,
details,
'Update package.json, apps/electron/package.json, and packages/shared/package.json before releasing.',
].join('\n'),
);
}
appendGithubOutput(args.githubOutput, { tag, version });
console.log(`Release version OK: ${version} (${tag})`);
}
try {
main();
} catch (error) {
console.error(error instanceof Error ? error.message : error);
process.exit(1);
}