Skip to content

Commit a97003a

Browse files
authored
build: sort versions in breaking changes script and remove custom parsing (#32234)
Removes the custom version parsing from the breaking changes script in favor of the `semver` module and adds sorting so the message is easier to read.
1 parent 31e9100 commit a97003a

File tree

1 file changed

+13
-38
lines changed

1 file changed

+13
-38
lines changed

scripts/breaking-changes.mts

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {join, relative} from 'path';
22
import {readFileSync} from 'fs';
3+
import semver from 'semver';
34
import chalk from 'chalk';
45
import ts from 'typescript';
56

@@ -8,7 +9,7 @@ const projectRoot = process.cwd();
89
const packageJson = JSON.parse(readFileSync(join(projectRoot, 'package.json'), 'utf8'));
910

1011
// Current version from the package.json. Splits it on the dash to ignore pre-release labels.
11-
const packageVersion = packageJson.version.split('-')[0];
12+
const packageVersion = semver.parse(packageJson.version.split('-')[0])!;
1213

1314
// Regex used to extract versions from a string.
1415
const versionRegex = /\d+\.\d+\.\d+/;
@@ -63,15 +64,17 @@ parsedConfig.fileNames.forEach((fileName: string) => {
6364
});
6465

6566
// Go through the summary and log out all of the breaking changes.
66-
Object.keys(summary).forEach(version => {
67-
const isExpired = hasExpired(packageVersion, version);
68-
const status = isExpired ? chalk.red('(expired)') : chalk.green('(not expired)');
69-
const header = chalk.bold(`Breaking changes for ${version} ${status}:`);
70-
const messages = summary[version].join('\n');
71-
72-
console.log(isExpired ? chalk.red(header) : header);
73-
console.log(isExpired ? chalk.red(messages) : messages, '\n');
74-
});
67+
Object.keys(summary)
68+
.sort(semver.compare)
69+
.forEach(version => {
70+
const isExpired = packageVersion.compare(version) > -1;
71+
const status = isExpired ? chalk.red('(expired)') : chalk.green('(not expired)');
72+
const header = chalk.bold(`Breaking changes for ${version} ${status}:`);
73+
const messages = summary[version].join('\n');
74+
75+
console.log(isExpired ? chalk.red(header) : header);
76+
console.log(isExpired ? chalk.red(messages) : messages, '\n');
77+
});
7578

7679
/**
7780
* Formats a message to be logged out in the breaking changes summary.
@@ -92,31 +95,3 @@ function formatMessage(comment: string, position: number, lineStarts: readonly n
9295

9396
return `Line ${lineNumber}, ${cleanMessage || 'No message'}`;
9497
}
95-
96-
/** Converts a version string into an object. */
97-
function parseVersion(version: string) {
98-
const [major = 0, minor = 0, patch = 0] = version.split('.').map(segment => parseInt(segment));
99-
return {major, minor, patch};
100-
}
101-
102-
/**
103-
* Checks whether a version has expired, based on the current version.
104-
* @param currentVersion Current version of the package.
105-
* @param breakingChange Version that is being checked.
106-
*/
107-
function hasExpired(currentVersion: string, breakingChange: string) {
108-
if (currentVersion === breakingChange) {
109-
return true;
110-
}
111-
112-
const current = parseVersion(currentVersion);
113-
const target = parseVersion(breakingChange);
114-
115-
return (
116-
target.major < current.major ||
117-
(target.major === current.major && target.minor < current.minor) ||
118-
(target.major === current.major &&
119-
target.minor === current.minor &&
120-
target.patch < current.patch)
121-
);
122-
}

0 commit comments

Comments
 (0)