This repository was archived by the owner on Sep 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.cjs
More file actions
88 lines (77 loc) · 2.5 KB
/
Copy pathinstall.cjs
File metadata and controls
88 lines (77 loc) · 2.5 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
"use strict";
const { chmodSync, copyFileSync, existsSync, renameSync } = require("node:fs");
const { dirname, join } = require("node:path");
const { spawnSync } = require("node:child_process");
const PLATFORM_PACKAGES = {
"darwin-arm64": {
binary: "bin/trce",
packageName: "@trce/cli-darwin-arm64",
},
"darwin-x64": {
binary: "bin/trce",
packageName: "@trce/cli-darwin-x64",
},
"linux-arm64": {
binary: "bin/trce",
packageName: "@trce/cli-linux-arm64",
},
"linux-x64": {
binary: "bin/trce",
packageName: "@trce/cli-linux-x64",
},
"win32-arm64": {
binary: "bin/trce.exe",
packageName: "@trce/cli-win32-arm64",
},
"win32-x64": {
binary: "bin/trce.exe",
packageName: "@trce/cli-win32-x64",
},
};
function platformPackage(platform = process.platform, arch = process.arch) {
return PLATFORM_PACKAGES[`${platform}-${arch}`] ?? null;
}
function install() {
// The source checkout builds platform packages explicitly. Published npm
// tarballs do not contain .git and must always install their native binary.
if (existsSync(join(__dirname, ".git"))) {
return;
}
const target = platformPackage();
if (target === null) {
throw new Error(`Unsupported platform: ${process.platform} ${process.arch}`);
}
const packageJson = require.resolve(`${target.packageName}/package.json`);
const packageVersion = require(packageJson).version;
const rootVersion = require(join(__dirname, "package.json")).version;
if (packageVersion !== rootVersion) {
throw new Error(
`Native package version mismatch: @trce/cli ${rootVersion}, ${target.packageName} ${packageVersion}`,
);
}
const source = join(dirname(packageJson), target.binary);
const destination = join(__dirname, "bin", "trce.exe");
if (!existsSync(source)) {
throw new Error(`Native trce binary is missing from ${target.packageName}`);
}
const smoke = spawnSync(source, ["help"], { encoding: "utf8" });
if (smoke.status !== 0) {
throw new Error(smoke.stderr || smoke.stdout || `Could not run ${source}`);
}
try {
renameSync(source, destination);
} catch {
copyFileSync(source, destination);
}
chmodSync(destination, 0o755);
}
module.exports = { install, platformPackage };
if (require.main === module) {
try {
install();
} catch (error) {
console.error(`trce install failed: ${error instanceof Error ? error.message : String(error)}`);
console.error("Reinstall without --ignore-scripts or --omit=optional.");
process.exit(1);
}
}