-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbump-version.ts
More file actions
84 lines (71 loc) · 2.58 KB
/
Copy pathbump-version.ts
File metadata and controls
84 lines (71 loc) · 2.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
import fs from 'fs';
import path from 'path';
import { execSync } from 'child_process';
function readJSON(file: string) {
return JSON.parse(fs.readFileSync(file, 'utf8'));
}
function writeJSON(file: string, data: any) {
fs.writeFileSync(file, JSON.stringify(data, null, 2) + '\n');
}
function bumpVersion(version: string): string {
const parts = version.split('.').map(Number);
parts[2] += 1;
return parts.join('.');
}
function isValidVersion(version: string): boolean {
return /^\d+\.\d+\.\d+$/.test(version);
}
function updatePackageJson(newVersion: string) {
const pkgPath = path.join(__dirname, 'package.json');
const pkg = readJSON(pkgPath);
pkg.version = newVersion;
writeJSON(pkgPath, pkg);
console.log(`Updated package.json to version ${newVersion}`);
}
function updateConfigYaml(newVersion: string) {
const configPath = path.join(__dirname, 'ha_addon', 'config.yaml');
let config = fs.readFileSync(configPath, 'utf8');
// Between releases develop carries `version: "next"`, so this has to match a
// non-numeric version too.
config = config.replace(/^version: *["']?[^"'\n]+["']?/m, `version: "${newVersion}"`);
fs.writeFileSync(configPath, config);
console.log(`Updated ha_addon/config.yaml to version ${newVersion}`);
}
function updateChangelog(newVersion: string) {
const changelogPath = path.join(__dirname, 'CHANGELOG.md');
let changelog = fs.readFileSync(changelogPath, 'utf8');
const today = new Date().toISOString().slice(0, 10);
changelog = changelog.replace(
/^## \[(Next|Unreleased)\]/m,
`## [${newVersion}] - ${today}`
);
fs.writeFileSync(changelogPath, changelog);
console.log(`Updated CHANGELOG.md to version ${newVersion}`);
}
function runNpmInstall() {
console.log('Running npm install to update package-lock.json...');
execSync('npm install', { stdio: 'inherit' });
}
function main() {
const pkg = readJSON(path.join(__dirname, 'package.json'));
const currentVersion = pkg.version;
const argVersion = process.argv[2];
let newVersion: string;
if (argVersion) {
if (!isValidVersion(argVersion)) {
console.error(`Invalid version string: ${argVersion}`);
process.exit(1);
}
newVersion = argVersion;
console.log(`Using provided version: ${newVersion}`);
} else {
newVersion = bumpVersion(currentVersion);
console.log(`No version provided. Bumping patch: ${currentVersion} -> ${newVersion}`);
}
updatePackageJson(newVersion);
updateConfigYaml(newVersion);
updateChangelog(newVersion);
runNpmInstall();
console.log(`\nVersion bump complete: ${currentVersion} -> ${newVersion}`);
}
main();