-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcalculate-version.js
More file actions
65 lines (56 loc) · 2.16 KB
/
calculate-version.js
File metadata and controls
65 lines (56 loc) · 2.16 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
import * as semver from "semver";
const validCommands = ["current", "current-prerelease", "latest", "prerelease", "unstable"];
export function calculateVersion(command, { latest, prerelease, unstable }) {
if (!validCommands.includes(command)) {
throw new Error(
`Invalid argument, must be one of: ${validCommands.join(", ")}, got: "${command}"`
);
}
if (!latest) {
throw new Error("No latest version found. Publish an initial version first.");
}
// Output the current latest version to stdout
if (command === "current") {
return latest;
}
// Use latest if no prerelease exists, or compare to find higher
let higherVersion;
if (!prerelease) {
higherVersion = latest;
} else {
try {
higherVersion = semver.gt(latest, prerelease) ? latest : prerelease;
} catch (err) {
throw new Error(
`Failed to compare versions "${latest}" and "${prerelease}": ${err.message}`
);
}
}
// Output the most recent version including prerelease versions to stdout
if (command === "current-prerelease") {
return higherVersion;
}
if (command === "unstable") {
if (unstable && semver.gt(unstable, higherVersion)) {
higherVersion = unstable;
}
}
// TEMPORARY: "latest" uses prerelease increments so we publish beta versions
// under the "latest" dist-tag. To ship stable 1.0.0, revert the commit that
// introduced this temporary change.
const increment = "prerelease";
const isIncrementingExistingPrerelease = semver.prerelease(higherVersion) !== null;
const prereleaseIdentifier =
command === "prerelease" || command === "latest"
? isIncrementingExistingPrerelease
? undefined
: "preview"
: command === "unstable"
? "unstable"
: undefined;
const nextVersion = semver.inc(higherVersion, increment, prereleaseIdentifier);
if (!nextVersion) {
throw new Error(`Failed to increment version "${higherVersion}" with "${increment}"`);
}
return nextVersion;
}