-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion-bump.ts
101 lines (85 loc) · 4.11 KB
/
version-bump.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
import fs from 'fs';
import { execSync } from 'child_process';
import semver, { ReleaseType } from 'semver';
import { createTextGeneration } from './src/util/ai';
async function generateChangelog(commitMessages: string[]): Promise<string> {
const systemPrompt = 'You are a helpful assistant generating a changelog based on commit messages.';
const userPrompt = `Create a concise changelog summary for the following commits:\n\n${commitMessages.join('\n')}`;
const changelog = await createTextGeneration(systemPrompt, userPrompt);
return changelog || 'Error generating changelog';
}
function bumpVersion(increment: ReleaseType): string {
const packageJsonPath = './package.json';
const readmePath = './README.md';
const mainFilePath = './src/main.ts'; // Define path to main.ts
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
const currentVersion = packageJson.version;
const newVersion = semver.inc(currentVersion, increment) || currentVersion;
packageJson.version = newVersion;
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log(`Version bumped from ${currentVersion} to ${newVersion}`);
// Update version badge in README.md
const readmeContent = fs.readFileSync(readmePath, 'utf8');
const updatedReadmeContent = readmeContent.replace(
/!\[Version\]\(https:\/\/img\.shields\.io\/badge\/version-[\d.]+-blue\)/,
``,
);
fs.writeFileSync(readmePath, updatedReadmeContent);
console.log(`README.md updated with new version badge ${newVersion}`);
// Update version in main.ts
const mainFileContent = fs.readFileSync(mainFilePath, 'utf8');
const updatedMainFileContent = mainFileContent.replace(
/(program\.name\('evergit'\)\.description\('Automate your Evergreen ILS git workflow'\)\.version\(')([\d.]+)('\);)/,
`$1${newVersion}$3`,
);
fs.writeFileSync(mainFilePath, updatedMainFileContent);
console.log(`src/main.ts updated to new version ${newVersion}`);
return newVersion;
}
function createIncrementBadge(increment: ReleaseType): string {
switch (increment) {
case 'major':
return '';
case 'premajor':
return '';
case 'minor':
return '';
case 'preminor':
return '';
case 'patch':
return '';
case 'prepatch':
return '';
case 'prerelease':
return '';
default:
throw new Error('Invalid increment type');
}
}
function updateChangelog(increment: ReleaseType, version: string, changelog: string): void {
const date = new Date().toISOString().split('T')[0];
const incrementBadge = createIncrementBadge(increment);
const changelogContent = `## [${version}] - ${date}\n\n${incrementBadge}\n\n${changelog}\n\n`;
fs.appendFileSync('CHANGELOG.md', `\n${changelogContent}`);
console.log(`Changelog updated for version ${version}`);
}
function getBranchCommits(): string[] {
try {
execSync('git fetch origin main');
const commits = execSync('git log origin/main..HEAD --pretty=format:"%s"').toString().split('\n');
return commits;
} catch (error) {
console.error('Error fetching branch-specific commits:', error);
return [];
}
}
async function main() {
const increment: ReleaseType = (process.argv[2] as ReleaseType) || 'patch';
const newVersion = bumpVersion(increment);
const commitMessages = getBranchCommits();
const changelog = await generateChangelog(commitMessages);
updateChangelog(increment, newVersion, changelog);
execSync('npm install');
console.log(changelog);
}
main();