-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathget-version.js
More file actions
98 lines (87 loc) · 3 KB
/
get-version.js
File metadata and controls
98 lines (87 loc) · 3 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
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
#!/usr/bin/env node
/**
* Outputs the next or current version of the SDK package based on the latest
* published version and provided version increment type.
*
* Usage:
*
* node scripts/get-version.js [current|current-prerelease|latest|prerelease|unstable]
*
* Outputs the version to stdout.
*/
import { execSync } from "child_process";
import * as semver from "semver";
async function getLatestVersion(tag) {
try {
const result = execSync(
`npm view @github/copilot-sdk@${tag} version --registry=https://registry.npmjs.org`,
{ encoding: "utf-8", stdio: ["pipe", "pipe", "pipe"] }
);
const version = result.trim();
if (!semver.valid(version)) {
console.error(`Invalid version returned from npm for tag "${tag}": "${version}"`);
process.exit(1);
}
return version;
} catch {
// Tag doesn't exist yet
return null;
}
}
async function main() {
const command = process.argv[2];
const validCommands = ["current", "current-prerelease", "latest", "prerelease", "unstable"];
if (!validCommands.includes(command)) {
console.error(
`Invalid argument, must be one of: ${validCommands.join(", ")}, got: "${command}"`
);
process.exit(1);
}
const latest = await getLatestVersion("latest");
if (!latest) {
console.error("No latest version found. Publish an initial version first.");
process.exit(1);
}
// Output the current latest version to stdout
if (command === "current") {
console.log(latest);
return;
}
const prerelease = await getLatestVersion("prerelease");
// 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) {
console.error(
`Failed to compare versions "${latest}" and "${prerelease}": ${err.message}`
);
process.exit(1);
}
}
// Output the most recent version including prerelease versions to stdout
if (command === "current-prerelease") {
console.log(higherVersion);
return;
}
if (command === "unstable") {
const unstable = await getLatestVersion("unstable");
if (unstable && semver.gt(unstable, higherVersion)) {
higherVersion = unstable;
}
}
const increment = command === "latest" ? "patch" : "prerelease";
const prereleaseIdentifier =
command === "prerelease" ? "preview" : command === "unstable" ? "unstable" : undefined;
const nextVersion = semver.inc(higherVersion, increment, prereleaseIdentifier);
if (!nextVersion) {
console.error(`Failed to increment version "${higherVersion}" with "${increment}"`);
process.exit(1);
}
// Output the next version to stdout
console.log(nextVersion);
}
void main();