-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathpackage-sdk.ts
More file actions
74 lines (71 loc) · 3.06 KB
/
Copy pathpackage-sdk.ts
File metadata and controls
74 lines (71 loc) · 3.06 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
import { execFileSync } from "node:child_process";
import { mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { COPILOT_CLI_VERSION } from "../src/cliVersion.js";
import {
getRuntimePackageName,
materializeRuntimeBundle,
RUNTIME_PLATFORMS,
} from "../src/runtimeArtifacts.js";
import { ensureCopilotPackage } from "./releaseArtifacts.js";
const nodeRoot = join(dirname(fileURLToPath(import.meta.url)), "..");
const packagePath = join(nodeRoot, "package.json");
const originalPackage = readFileSync(packagePath, "utf8");
const packageJson = JSON.parse(originalPackage);
const sdkVersion = packageJson.version;
const npmCliPath = process.env.npm_execpath;
if (!npmCliPath) {
throw new Error("package-sdk.ts must be run through an npm script");
}
const requestedPlatforms = process.env.COPILOT_SDK_RUNTIME_PLATFORMS?.split(",").filter(Boolean);
const platforms = requestedPlatforms ?? [...RUNTIME_PLATFORMS];
const stagingRoot = mkdtempSync(join(tmpdir(), "copilot-sdk-platform-packages-"));
try {
const optionalDependencies: Record<string, string> = {};
for (const platform of platforms) {
if (!(RUNTIME_PLATFORMS as readonly string[]).includes(platform)) {
throw new Error(`Unsupported runtime platform: ${platform}`);
}
const releasePackage = await ensureCopilotPackage(COPILOT_CLI_VERSION, { platform });
const runtimeWrapper = materializeRuntimeBundle(
{ packageRoot: releasePackage, platform },
stagingRoot,
platform
);
const runtimeRoot = resolve(dirname(runtimeWrapper), "..", "..");
const packageName = getRuntimePackageName(platform);
const [osName, cpu] = platform.replace("linuxmusl", "linux").split("-");
const runtimePackage = {
name: packageName,
version: sdkVersion,
description: `Platform runtime for @github/copilot-sdk (${platform})`,
repository: packageJson.repository,
license: "MIT",
os: [osName],
cpu: [cpu],
...(platform.startsWith("linux")
? { libc: [platform.startsWith("linuxmusl") ? "musl" : "glibc"] }
: {}),
};
writeFileSync(
join(runtimeRoot, "package.json"),
`${JSON.stringify(runtimePackage, null, 4)}\n`
);
execFileSync(process.execPath, [npmCliPath, "pack", runtimeRoot, "--pack-destination", nodeRoot], {
stdio: "inherit",
});
optionalDependencies[packageName] = sdkVersion;
}
packageJson.optionalDependencies = optionalDependencies;
writeFileSync(packagePath, `${JSON.stringify(packageJson, null, 4)}\n`);
execFileSync(
process.execPath,
[npmCliPath, "pack", nodeRoot, "--pack-destination", nodeRoot],
{ stdio: "inherit" }
);
} finally {
writeFileSync(packagePath, originalPackage);
rmSync(stagingRoot, { recursive: true, force: true });
}