|
6 | 6 | import * as gulp from 'gulp';
|
7 | 7 | import * as fs from 'fs';
|
8 | 8 | import * as path from 'path';
|
| 9 | +import * as os from 'os'; |
9 | 10 |
|
10 |
| -gulp.task('incrementVersionJson', async (): Promise<void> => { |
| 11 | +gulp.task('incrementVersion', async (): Promise<void> => { |
| 12 | + // Get the current version from version.json |
11 | 13 | const versionFilePath = path.join(path.resolve(__dirname, '..'), 'version.json');
|
12 | 14 | const file = fs.readFileSync(versionFilePath, 'utf8');
|
13 | 15 | const versionJson = JSON.parse(file);
|
14 | 16 |
|
| 17 | + // Increment the minor version |
15 | 18 | const version = versionJson.version as string;
|
16 | 19 | const split = version.split('.');
|
17 | 20 | const newVersion = `${split[0]}.${parseInt(split[1]) + 1}`;
|
18 | 21 |
|
19 | 22 | console.log(`Updating ${version} to ${newVersion}`);
|
20 | 23 |
|
| 24 | + // Write the new version back to version.json |
21 | 25 | versionJson.version = newVersion;
|
22 | 26 | const newJson = JSON.stringify(versionJson, null, 4);
|
23 | 27 | console.log(`New json: ${newJson}`);
|
24 | 28 |
|
25 | 29 | fs.writeFileSync(versionFilePath, newJson);
|
| 30 | + |
| 31 | + // Add a new changelog section for the new version. |
| 32 | + console.log('Adding new version header to changelog'); |
| 33 | + |
| 34 | + const changelogPath = path.join(path.resolve(__dirname, '..'), 'CHANGELOG.md'); |
| 35 | + const changelogContent = fs.readFileSync(changelogPath, 'utf8'); |
| 36 | + const changelogLines = changelogContent.split(os.EOL); |
| 37 | + |
| 38 | + // Find all the headers in the changelog (and their line numbers) |
| 39 | + const headerRegex = /^#+\s+.*$/gm; |
| 40 | + const matches = []; |
| 41 | + for (let i = 0; i < changelogLines.length; i++) { |
| 42 | + const line = changelogLines.at(i); |
| 43 | + const match = headerRegex.exec(line!); |
| 44 | + if (match) { |
| 45 | + matches.push({ line: i, text: match[0] }); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Find the known issues header, then find the next header after it. |
| 50 | + const knownIssuesHeader = matches.find((m) => m.text.includes('Known Issues')); |
| 51 | + if (knownIssuesHeader === undefined) { |
| 52 | + throw new Error('Could not find the known issues header in the changelog.'); |
| 53 | + } |
| 54 | + const knownIssuesIndex = matches.indexOf(knownIssuesHeader); |
| 55 | + if (knownIssuesIndex === -1) { |
| 56 | + throw new Error('Could not find the known issues index in the matches.'); |
| 57 | + } |
| 58 | + |
| 59 | + // Insert a new header for the new version after the known issues header but before the next header. |
| 60 | + const lineToInsertAt = matches[knownIssuesIndex + 1].line - 1; |
| 61 | + console.log(`Inserting new version header at line ${lineToInsertAt}`); |
| 62 | + const linesToInsert = ['', `# ${newVersion}.x`]; |
| 63 | + |
| 64 | + changelogLines.splice(lineToInsertAt, 0, ...linesToInsert); |
| 65 | + fs.writeFileSync(changelogPath, changelogLines.join(os.EOL)); |
26 | 66 | });
|
0 commit comments