-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathgenerate-version.ts
More file actions
43 lines (34 loc) · 1.38 KB
/
generate-version.ts
File metadata and controls
43 lines (34 loc) · 1.38 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
import { readFile, writeFile } from 'node:fs/promises';
import path from 'node:path';
interface PackageJson {
version: string;
iOSTemplateVersion: string;
macOSTemplateVersion: string;
}
const VERSION_REGEX = /^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z0-9.\-]+)?(\+[a-zA-Z0-9.\-]+)?$/;
function validateVersion(name: string, value: string): void {
if (!VERSION_REGEX.test(value)) {
throw new Error(
`Invalid ${name} in package.json: ${JSON.stringify(value)}. Expected a version string.`,
);
}
}
async function main(): Promise<void> {
const repoRoot = process.cwd();
const packagePath = path.join(repoRoot, 'package.json');
const versionPath = path.join(repoRoot, 'src', 'version.ts');
const raw = await readFile(packagePath, 'utf8');
const pkg = JSON.parse(raw) as PackageJson;
validateVersion('version', pkg.version);
validateVersion('iOSTemplateVersion', pkg.iOSTemplateVersion);
validateVersion('macOSTemplateVersion', pkg.macOSTemplateVersion);
const content =
`export const version = ${JSON.stringify(pkg.version)};\n` +
`export const iOSTemplateVersion = ${JSON.stringify(pkg.iOSTemplateVersion)};\n` +
`export const macOSTemplateVersion = ${JSON.stringify(pkg.macOSTemplateVersion)};\n`;
await writeFile(versionPath, content, 'utf8');
}
main().catch((error) => {
console.error('Failed to generate src/version.ts:', error);
process.exit(1);
});