-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathinject-cli-version.mjs
More file actions
56 lines (48 loc) · 1.94 KB
/
Copy pathinject-cli-version.mjs
File metadata and controls
56 lines (48 loc) · 1.94 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
#!/usr/bin/env node
/**
* inject-cli-version.mjs
*
* Reads the pinned @github/copilot version from nodejs/package-lock.json and
* writes it into python/copilot/_cli_version.py, replacing the `CLI_VERSION = None`
* sentinel with the concrete version string.
*
* Run from the repository root:
* node python/scripts/inject-cli-version.mjs
*/
import { readFileSync, writeFileSync } from "fs";
import { join, dirname } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const repoRoot = join(__dirname, "..", "..");
// Read version from nodejs/package-lock.json
const lockPath = join(repoRoot, "nodejs", "package-lock.json");
const lock = JSON.parse(readFileSync(lockPath, "utf-8"));
// The version is in packages["node_modules/@github/copilot"].version
const copilotPkg = lock.packages?.["node_modules/@github/copilot"];
if (!copilotPkg?.version) {
console.error(
"Error: Could not find @github/copilot version in nodejs/package-lock.json"
);
process.exit(1);
}
const version = copilotPkg.version;
console.log(`Injecting CLI_VERSION = "${version}"`);
// Patch _cli_version.py
const versionFile = join(__dirname, "..", "copilot", "_cli_version.py");
let content = readFileSync(versionFile, "utf-8");
const sentinel = 'CLI_VERSION: str | None = None';
const replacement = `CLI_VERSION: str | None = "${version}"`;
if (!content.includes(sentinel)) {
// Check if already injected
if (content.includes(`CLI_VERSION: str | None = "`)) {
console.log("CLI_VERSION already injected, updating...");
content = content.replace(/CLI_VERSION: str \| None = ".*?"/, `CLI_VERSION: str | None = "${version}"`);
} else {
console.error(`Error: Could not find sentinel '${sentinel}' in _cli_version.py`);
process.exit(1);
}
} else {
content = content.replace(sentinel, replacement);
}
writeFileSync(versionFile, content);
console.log(`Done. _cli_version.py now has CLI_VERSION = "${version}"`);