Skip to content

Commit 9ca7496

Browse files
committed
Fix semver comparison (#5)
1 parent ca13871 commit 9ca7496

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

main.js

+13-17
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ const cp = require("child_process");
88

99
// If the semver string a is greater than b, return 1. If the semver string b is greater than a, return -1. If a equals b, return 0;
1010
function semverCompare(a, b) {
11-
const pa = a.split('.');
12-
const pb = b.split('.');
13-
for (let i = 0; i < 3; i++) {
14-
const na = Number(pa[i]);
15-
const nb = Number(pb[i]);
16-
if (na > nb) return 1;
17-
if (nb > na) return -1;
18-
if (!isNaN(na) && isNaN(nb)) return 1;
19-
if (isNaN(na) && !isNaN(nb)) return -1;
20-
}
21-
return 0;
22-
};
11+
const pa = a.split(".");
12+
const pb = b.split(".");
2313

24-
// https://semver.org
25-
const SEMVER_REGEX = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$/;
14+
for (let i = 0; i < 3; i++) {
15+
const na = Number(pa[i]);
16+
const nb = Number(pb[i]);
17+
if (na > nb) return 1;
18+
if (nb > na) return -1;
19+
if (!isNaN(na) && isNaN(nb)) return 1;
20+
if (isNaN(na) && !isNaN(nb)) return -1;
21+
}
22+
return 0;
23+
}
2624

2725
// Input parameters. See action.yaml
2826
const { INPUT_PATH, INPUT_TOKEN, INPUT_FORMAT } = process.env;
@@ -56,9 +54,7 @@ if (base.name == head.name) {
5654
process.exit(1);
5755
}
5856

59-
const from = base.version.match(SEMVER_REGEX);
60-
const to = head.version.match(SEMVER_REGEX);
61-
const versionDiffResult = semverCompare(from, to);
57+
const versionDiffResult = semverCompare(base.version, head.version);
6258

6359
if (versionDiffResult === 1 || versionDiffResult === 0) {
6460
console.log(

0 commit comments

Comments
 (0)