-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathset-cli-version.js
More file actions
71 lines (68 loc) · 2.37 KB
/
Copy pathset-cli-version.js
File metadata and controls
71 lines (68 loc) · 2.37 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
import { readFileSync, writeFileSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const [version, mode] = process.argv.slice(2);
if (!version || !/^\d+\.\d+\.\d+(?:-[0-9A-Za-z._-]+)?$/.test(version)) {
throw new Error("Usage: set-cli-version.js <semver> [--npm-package]");
}
if (mode !== undefined && mode !== "--npm-package") {
throw new Error(`Unknown option: ${mode}`);
}
const nodeRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
const runtimePlatforms = [
"darwin-arm64",
"darwin-x64",
"linux-arm64",
"linux-x64",
"linuxmusl-arm64",
"linuxmusl-x64",
"win32-arm64",
"win32-x64",
];
const cliAssets = [
"copilot-darwin-arm64.tar.gz",
"copilot-darwin-x64.tar.gz",
"copilot-linux-arm64.tar.gz",
"copilot-linux-x64.tar.gz",
"copilot-win32-arm64.zip",
"copilot-win32-x64.zip",
];
const useNpmPackage = mode === "--npm-package";
if (!useNpmPackage) {
const checksumsUrl = `https://github.com/github/copilot-cli/releases/download/v${version}/SHA256SUMS.txt`;
const response = await fetch(checksumsUrl);
if (!response.ok) {
throw new Error(
`Failed to download ${checksumsUrl}: ${response.status} ${response.statusText}`
);
}
const checksums = new Map(
(await response.text())
.split(/\r?\n/)
.map((line) => line.trim().split(/\s+/, 2))
.filter(([hash, name]) => /^[a-fA-F0-9]{64}$/.test(hash) && name)
.map(([hash, name]) => [name.replace(/^\*/, ""), hash.toLowerCase()])
);
for (const assetName of [
...runtimePlatforms.map((platform) => `github-copilot-${version}-${platform}.tgz`),
...cliAssets,
]) {
if (!checksums.has(assetName)) {
throw new Error(`SHA256SUMS.txt does not contain ${assetName}`);
}
}
}
const packagePath = join(nodeRoot, "package.json");
const packageJson = JSON.parse(readFileSync(packagePath, "utf8"));
packageJson.copilotCliVersion = version;
writeFileSync(packagePath, `${JSON.stringify(packageJson, null, 4)}\n`);
const sourcePath = join(nodeRoot, "src", "cliVersion.ts");
writeFileSync(
sourcePath,
[
`export const COPILOT_CLI_VERSION = ${JSON.stringify(version)};`,
"",
`export const COPILOT_CLI_USE_NPM_PACKAGE = ${useNpmPackage};`,
"",
].join("\n")
);