agent-facets 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.package.json.bak +3 -2
- package/CHANGELOG.md +17 -0
- package/bin/facet +181 -0
- package/bin/package.json +3 -0
- package/dist/facet +0 -0
- package/package.json +5 -4
- package/scripts/postinstall.mjs +210 -0
- package/src/__tests__/launcher.test.ts +106 -0
- package/src/__tests__/postinstall.test.ts +196 -0
package/.package.json.bak
CHANGED
|
@@ -5,13 +5,14 @@
|
|
|
5
5
|
"url": "https://github.com/agent-facets/facets",
|
|
6
6
|
"directory": "packages/cli"
|
|
7
7
|
},
|
|
8
|
-
"version": "0.
|
|
8
|
+
"version": "0.3.0",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"bin": {
|
|
11
|
-
"facet": "./
|
|
11
|
+
"facet": "./bin/facet"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "bun build src/index.ts --compile --outfile dist/facet",
|
|
15
|
+
"postinstall": "node scripts/postinstall.mjs",
|
|
15
16
|
"prepack": "bun ../../scripts/prepack.ts",
|
|
16
17
|
"postpack": "bun ../../scripts/postpack.ts",
|
|
17
18
|
"types": "tsc --noEmit",
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# agent-facets
|
|
2
2
|
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#51](https://github.com/agent-facets/facets/pull/51) [`8280bba`](https://github.com/agent-facets/facets/commit/8280bba66d5ab6a132e1b6792bcccce03037a6de) Thanks [@eXamadeus](https://github.com/eXamadeus)! - Support 12 platform binaries (linux, windows, mac and common variants)
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- [#51](https://github.com/agent-facets/facets/pull/51) [`8280bba`](https://github.com/agent-facets/facets/commit/8280bba66d5ab6a132e1b6792bcccce03037a6de) Thanks [@eXamadeus](https://github.com/eXamadeus)! - Support dev platform "dev" mode via `bun dev` removing the complex build -> link flow
|
|
12
|
+
|
|
13
|
+
## 0.2.2
|
|
14
|
+
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- [#39](https://github.com/agent-facets/facets/pull/39) [`f380b7b`](https://github.com/agent-facets/facets/commit/f380b7bc5115acec1f974ef1401eba199a2f90fb) Thanks [@eXamadeus](https://github.com/eXamadeus)! - Ensure release CI works in isolation
|
|
18
|
+
- [#46](https://github.com/agent-facets/facets/pull/46) [`a5cbb89`](https://github.com/agent-facets/facets/commit/a5cbb89a46e14e2f79749ea7eafb5aebbd3504b7) Thanks [@eXamadeus](https://github.com/eXamadeus)! - Ensure all CI runs and provenance is managed correctly across packages
|
|
19
|
+
|
|
3
20
|
## 0.2.1
|
|
4
21
|
|
|
5
22
|
### Patch Changes
|
package/bin/facet
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
const childProcess = require("child_process")
|
|
4
|
+
const fs = require("fs")
|
|
5
|
+
const path = require("path")
|
|
6
|
+
const os = require("os")
|
|
7
|
+
|
|
8
|
+
function run(target) {
|
|
9
|
+
const result = childProcess.spawnSync(target, process.argv.slice(2), {
|
|
10
|
+
stdio: "inherit",
|
|
11
|
+
})
|
|
12
|
+
if (result.error) {
|
|
13
|
+
console.error(result.error.message)
|
|
14
|
+
process.exit(1)
|
|
15
|
+
}
|
|
16
|
+
const code = typeof result.status === "number" ? result.status : 0
|
|
17
|
+
process.exit(code)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Resolution #1: Environment variable override
|
|
21
|
+
const envPath = process.env.FACET_BIN_PATH
|
|
22
|
+
if (envPath) {
|
|
23
|
+
run(envPath)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Resolution #2: Cached hard-link from postinstall
|
|
27
|
+
const scriptPath = fs.realpathSync(__filename)
|
|
28
|
+
const scriptDir = path.dirname(scriptPath)
|
|
29
|
+
|
|
30
|
+
const cached = path.join(scriptDir, ".facet")
|
|
31
|
+
if (fs.existsSync(cached)) {
|
|
32
|
+
run(cached)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Resolution #3: Platform package lookup
|
|
36
|
+
const platformMap = {
|
|
37
|
+
darwin: "darwin",
|
|
38
|
+
linux: "linux",
|
|
39
|
+
win32: "windows",
|
|
40
|
+
}
|
|
41
|
+
const archMap = {
|
|
42
|
+
x64: "x64",
|
|
43
|
+
arm64: "arm64",
|
|
44
|
+
arm: "arm",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let platform = platformMap[os.platform()]
|
|
48
|
+
if (!platform) {
|
|
49
|
+
platform = os.platform()
|
|
50
|
+
}
|
|
51
|
+
let arch = archMap[os.arch()]
|
|
52
|
+
if (!arch) {
|
|
53
|
+
arch = os.arch()
|
|
54
|
+
}
|
|
55
|
+
const base = "agent-facets-" + platform + "-" + arch
|
|
56
|
+
const binary = platform === "windows" ? "facet.exe" : "facet"
|
|
57
|
+
|
|
58
|
+
function supportsAvx2() {
|
|
59
|
+
if (arch !== "x64") return false
|
|
60
|
+
|
|
61
|
+
if (platform === "linux") {
|
|
62
|
+
try {
|
|
63
|
+
return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
|
|
64
|
+
} catch {
|
|
65
|
+
return false
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (platform === "darwin") {
|
|
70
|
+
try {
|
|
71
|
+
const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
|
|
72
|
+
encoding: "utf8",
|
|
73
|
+
timeout: 1500,
|
|
74
|
+
})
|
|
75
|
+
if (result.status !== 0) return false
|
|
76
|
+
return (result.stdout || "").trim() === "1"
|
|
77
|
+
} catch {
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (platform === "windows") {
|
|
83
|
+
const cmd =
|
|
84
|
+
'(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
|
|
85
|
+
|
|
86
|
+
for (const exe of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
|
|
87
|
+
try {
|
|
88
|
+
const result = childProcess.spawnSync(exe, ["-NoProfile", "-NonInteractive", "-Command", cmd], {
|
|
89
|
+
encoding: "utf8",
|
|
90
|
+
timeout: 3000,
|
|
91
|
+
windowsHide: true,
|
|
92
|
+
})
|
|
93
|
+
if (result.status !== 0) continue
|
|
94
|
+
const out = (result.stdout || "").trim().toLowerCase()
|
|
95
|
+
if (out === "true" || out === "1") return true
|
|
96
|
+
if (out === "false" || out === "0") return false
|
|
97
|
+
} catch {
|
|
98
|
+
continue
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return false
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return false
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const names = (() => {
|
|
109
|
+
const avx2 = supportsAvx2()
|
|
110
|
+
const baseline = arch === "x64" && !avx2
|
|
111
|
+
|
|
112
|
+
if (platform === "linux") {
|
|
113
|
+
const musl = (() => {
|
|
114
|
+
try {
|
|
115
|
+
if (fs.existsSync("/etc/alpine-release")) return true
|
|
116
|
+
} catch {
|
|
117
|
+
// ignore
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
try {
|
|
121
|
+
const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
|
|
122
|
+
const text = ((result.stdout || "") + (result.stderr || "")).toLowerCase()
|
|
123
|
+
if (text.includes("musl")) return true
|
|
124
|
+
} catch {
|
|
125
|
+
// ignore
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return false
|
|
129
|
+
})()
|
|
130
|
+
|
|
131
|
+
if (musl) {
|
|
132
|
+
if (arch === "x64") {
|
|
133
|
+
if (baseline) return [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
|
|
134
|
+
return [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
|
|
135
|
+
}
|
|
136
|
+
return [`${base}-musl`, base]
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (arch === "x64") {
|
|
140
|
+
if (baseline) return [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
|
|
141
|
+
return [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
|
|
142
|
+
}
|
|
143
|
+
return [base, `${base}-musl`]
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if (arch === "x64") {
|
|
147
|
+
if (baseline) return [`${base}-baseline`, base]
|
|
148
|
+
return [base, `${base}-baseline`]
|
|
149
|
+
}
|
|
150
|
+
return [base]
|
|
151
|
+
})()
|
|
152
|
+
|
|
153
|
+
function findBinary(startDir) {
|
|
154
|
+
let current = startDir
|
|
155
|
+
for (;;) {
|
|
156
|
+
const modules = path.join(current, "node_modules")
|
|
157
|
+
if (fs.existsSync(modules)) {
|
|
158
|
+
for (const name of names) {
|
|
159
|
+
const candidate = path.join(modules, name, "bin", binary)
|
|
160
|
+
if (fs.existsSync(candidate)) return candidate
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const parent = path.dirname(current)
|
|
164
|
+
if (parent === current) {
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
current = parent
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
const resolved = findBinary(scriptDir)
|
|
172
|
+
if (!resolved) {
|
|
173
|
+
console.error(
|
|
174
|
+
"It seems that your package manager failed to install the right version of the facet CLI for your platform. You can try manually installing " +
|
|
175
|
+
names.map((n) => `"${n}"`).join(" or ") +
|
|
176
|
+
" package",
|
|
177
|
+
)
|
|
178
|
+
process.exit(1)
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
run(resolved)
|
package/bin/package.json
ADDED
package/dist/facet
CHANGED
|
Binary file
|
package/package.json
CHANGED
|
@@ -5,13 +5,14 @@
|
|
|
5
5
|
"url": "https://github.com/agent-facets/facets",
|
|
6
6
|
"directory": "packages/cli"
|
|
7
7
|
},
|
|
8
|
-
"version": "0.
|
|
8
|
+
"version": "0.3.0",
|
|
9
9
|
"type": "module",
|
|
10
10
|
"bin": {
|
|
11
|
-
"facet": "./
|
|
11
|
+
"facet": "./bin/facet"
|
|
12
12
|
},
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "bun build src/index.ts --compile --outfile dist/facet",
|
|
15
|
+
"postinstall": "node scripts/postinstall.mjs",
|
|
15
16
|
"prepack": "bun ../../scripts/prepack.ts",
|
|
16
17
|
"postpack": "bun ../../scripts/postpack.ts",
|
|
17
18
|
"types": "tsc --noEmit",
|
|
@@ -29,8 +30,8 @@
|
|
|
29
30
|
"react-devtools-core": "7.0.1"
|
|
30
31
|
},
|
|
31
32
|
"devDependencies": {
|
|
32
|
-
"@agent-facets/brand": "0.2.
|
|
33
|
-
"@agent-facets/core": "0.2.
|
|
33
|
+
"@agent-facets/brand": "0.2.1",
|
|
34
|
+
"@agent-facets/core": "0.2.1",
|
|
34
35
|
"@types/bun": "1.3.10",
|
|
35
36
|
"ink-testing-library": "4.0.0"
|
|
36
37
|
},
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Postinstall script for agent-facets.
|
|
3
|
+
*
|
|
4
|
+
* Detects the current platform, architecture, AVX2 support, and musl libc,
|
|
5
|
+
* then hard-links the optimal binary to bin/.facet for fast launcher resolution.
|
|
6
|
+
*
|
|
7
|
+
* Silent failure — if anything goes wrong, the launcher's fallback logic handles it.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { spawnSync } from 'node:child_process'
|
|
11
|
+
import { chmodSync, copyFileSync, existsSync, linkSync, mkdirSync, readFileSync, unlinkSync } from 'node:fs'
|
|
12
|
+
import { createRequire } from 'node:module'
|
|
13
|
+
import { arch as osArch, platform as osPlatform } from 'node:os'
|
|
14
|
+
import { dirname, join, resolve } from 'node:path'
|
|
15
|
+
import { fileURLToPath } from 'node:url'
|
|
16
|
+
|
|
17
|
+
const __dirname = dirname(fileURLToPath(import.meta.url))
|
|
18
|
+
const require = createRequire(import.meta.url)
|
|
19
|
+
|
|
20
|
+
// ---------------------------------------------------------------------------
|
|
21
|
+
// Platform detection
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
function detectPlatform() {
|
|
25
|
+
const platformMap = { darwin: 'darwin', linux: 'linux', win32: 'windows' }
|
|
26
|
+
const archMap = { x64: 'x64', arm64: 'arm64', arm: 'arm' }
|
|
27
|
+
const platform = platformMap[osPlatform()] || osPlatform()
|
|
28
|
+
const arch = archMap[osArch()] || osArch()
|
|
29
|
+
return { platform, arch }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
// AVX2 detection
|
|
34
|
+
// ---------------------------------------------------------------------------
|
|
35
|
+
|
|
36
|
+
function supportsAvx2(platform) {
|
|
37
|
+
if (platform === 'linux') {
|
|
38
|
+
try {
|
|
39
|
+
return /(^|\s)avx2(\s|$)/i.test(readFileSync('/proc/cpuinfo', 'utf8'))
|
|
40
|
+
} catch {
|
|
41
|
+
return false
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (platform === 'darwin') {
|
|
46
|
+
try {
|
|
47
|
+
const result = spawnSync('sysctl', ['-n', 'hw.optional.avx2_0'], {
|
|
48
|
+
encoding: 'utf8',
|
|
49
|
+
timeout: 1500,
|
|
50
|
+
})
|
|
51
|
+
if (result.status !== 0) return false
|
|
52
|
+
return (result.stdout || '').trim() === '1'
|
|
53
|
+
} catch {
|
|
54
|
+
return false
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (platform === 'windows') {
|
|
59
|
+
const cmd =
|
|
60
|
+
'(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
|
|
61
|
+
|
|
62
|
+
for (const exe of ['powershell.exe', 'pwsh.exe', 'pwsh', 'powershell']) {
|
|
63
|
+
try {
|
|
64
|
+
const result = spawnSync(exe, ['-NoProfile', '-NonInteractive', '-Command', cmd], {
|
|
65
|
+
encoding: 'utf8',
|
|
66
|
+
timeout: 3000,
|
|
67
|
+
windowsHide: true,
|
|
68
|
+
})
|
|
69
|
+
if (result.status !== 0) continue
|
|
70
|
+
const out = (result.stdout || '').trim().toLowerCase()
|
|
71
|
+
if (out === 'true' || out === '1') return true
|
|
72
|
+
if (out === 'false' || out === '0') return false
|
|
73
|
+
} catch {}
|
|
74
|
+
}
|
|
75
|
+
return false
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return false
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------------------------------------------------------------------------
|
|
82
|
+
// musl detection (Linux only)
|
|
83
|
+
// ---------------------------------------------------------------------------
|
|
84
|
+
|
|
85
|
+
function isMusl() {
|
|
86
|
+
try {
|
|
87
|
+
if (existsSync('/etc/alpine-release')) return true
|
|
88
|
+
} catch {
|
|
89
|
+
// ignore
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
const result = spawnSync('ldd', ['--version'], { encoding: 'utf8' })
|
|
94
|
+
const text = ((result.stdout || '') + (result.stderr || '')).toLowerCase()
|
|
95
|
+
if (text.includes('musl')) return true
|
|
96
|
+
} catch {
|
|
97
|
+
// ignore
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return false
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ---------------------------------------------------------------------------
|
|
104
|
+
// Build priority-ordered candidate list
|
|
105
|
+
// ---------------------------------------------------------------------------
|
|
106
|
+
|
|
107
|
+
function buildCandidates(platform, arch, opts = {}) {
|
|
108
|
+
const base = `agent-facets-${platform}-${arch}`
|
|
109
|
+
const avx2 = opts.avx2 !== undefined ? opts.avx2 : arch === 'x64' ? supportsAvx2(platform) : false
|
|
110
|
+
const baseline = arch === 'x64' && !avx2
|
|
111
|
+
|
|
112
|
+
if (platform === 'linux') {
|
|
113
|
+
const musl = opts.musl !== undefined ? opts.musl : isMusl()
|
|
114
|
+
|
|
115
|
+
if (musl) {
|
|
116
|
+
if (arch === 'x64') {
|
|
117
|
+
if (baseline) return [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
|
|
118
|
+
return [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
|
|
119
|
+
}
|
|
120
|
+
return [`${base}-musl`, base]
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (arch === 'x64') {
|
|
124
|
+
if (baseline) return [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
|
|
125
|
+
return [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
|
|
126
|
+
}
|
|
127
|
+
return [base, `${base}-musl`]
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (arch === 'x64') {
|
|
131
|
+
if (baseline) return [`${base}-baseline`, base]
|
|
132
|
+
return [base, `${base}-baseline`]
|
|
133
|
+
}
|
|
134
|
+
return [base]
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------------------------------------------------------------------------
|
|
138
|
+
// Find the binary via require.resolve
|
|
139
|
+
// ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
function findBinary(candidates) {
|
|
142
|
+
const binaryName = osPlatform() === 'win32' ? 'facet.exe' : 'facet'
|
|
143
|
+
|
|
144
|
+
for (const candidate of candidates) {
|
|
145
|
+
try {
|
|
146
|
+
const pkgPath = require.resolve(`${candidate}/package.json`)
|
|
147
|
+
const pkgDir = dirname(pkgPath)
|
|
148
|
+
const binaryPath = join(pkgDir, 'bin', binaryName)
|
|
149
|
+
if (existsSync(binaryPath)) return binaryPath
|
|
150
|
+
} catch {
|
|
151
|
+
// Package not installed — try next candidate
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return undefined
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ---------------------------------------------------------------------------
|
|
159
|
+
// Main
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
|
|
162
|
+
function main() {
|
|
163
|
+
// Windows: no-op — the .exe is used directly
|
|
164
|
+
if (osPlatform() === 'win32') return
|
|
165
|
+
|
|
166
|
+
const { platform, arch } = detectPlatform()
|
|
167
|
+
const candidates = buildCandidates(platform, arch)
|
|
168
|
+
const binaryPath = findBinary(candidates)
|
|
169
|
+
|
|
170
|
+
if (!binaryPath) return // No binary found — launcher fallback handles it
|
|
171
|
+
|
|
172
|
+
const binDir = join(__dirname, '..', 'bin')
|
|
173
|
+
const targetPath = join(binDir, '.facet')
|
|
174
|
+
|
|
175
|
+
// Ensure bin directory exists
|
|
176
|
+
mkdirSync(binDir, { recursive: true })
|
|
177
|
+
|
|
178
|
+
// Remove existing cached binary
|
|
179
|
+
try {
|
|
180
|
+
unlinkSync(targetPath)
|
|
181
|
+
} catch {
|
|
182
|
+
// Doesn't exist — fine
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// Hard-link (copy fallback on cross-device)
|
|
186
|
+
try {
|
|
187
|
+
linkSync(binaryPath, targetPath)
|
|
188
|
+
} catch {
|
|
189
|
+
copyFileSync(binaryPath, targetPath)
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
chmodSync(targetPath, 0o755)
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Guard: only run main() when executed directly, not when imported by tests
|
|
196
|
+
const scriptPath = fileURLToPath(import.meta.url)
|
|
197
|
+
const isDirectExecution = process.argv[1] && resolve(process.argv[1]) === scriptPath
|
|
198
|
+
|
|
199
|
+
if (isDirectExecution) {
|
|
200
|
+
try {
|
|
201
|
+
main()
|
|
202
|
+
} catch (e) {
|
|
203
|
+
// Log the error but exit 0 — launcher fallback will handle binary resolution.
|
|
204
|
+
// We don't want a postinstall failure to break `npm install`.
|
|
205
|
+
console.error('[agent-facets postinstall] failed to cache platform binary:', e.message || e)
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Exports for testing — these are no-ops when run as a script
|
|
210
|
+
export { buildCandidates, detectPlatform }
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
|
|
2
|
+
import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises'
|
|
3
|
+
import { tmpdir } from 'node:os'
|
|
4
|
+
import { join, resolve } from 'node:path'
|
|
5
|
+
|
|
6
|
+
const LAUNCHER_PATH = resolve(import.meta.dir, '..', '..', 'bin', 'facet')
|
|
7
|
+
|
|
8
|
+
interface ExecResult {
|
|
9
|
+
stdout: string
|
|
10
|
+
stderr: string
|
|
11
|
+
exitCode: number
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
async function runLauncher(args: string[] = [], env: Record<string, string> = {}): Promise<ExecResult> {
|
|
15
|
+
const proc = Bun.spawn(['node', LAUNCHER_PATH, ...args], {
|
|
16
|
+
env: { ...process.env, ...env },
|
|
17
|
+
stdout: 'pipe',
|
|
18
|
+
stderr: 'pipe',
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
const [stdout, stderr] = await Promise.all([new Response(proc.stdout).text(), new Response(proc.stderr).text()])
|
|
22
|
+
const exitCode = await proc.exited
|
|
23
|
+
|
|
24
|
+
return { stdout, stderr, exitCode }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
describe('launcher — FACET_BIN_PATH override', () => {
|
|
28
|
+
let tmpDir: string
|
|
29
|
+
let mockBinaryPath: string
|
|
30
|
+
|
|
31
|
+
beforeAll(async () => {
|
|
32
|
+
tmpDir = await mkdtemp(join(tmpdir(), 'launcher-test-'))
|
|
33
|
+
mockBinaryPath = join(tmpDir, 'mock-facet')
|
|
34
|
+
await writeFile(mockBinaryPath, '#!/bin/sh\necho "mock-facet: $@"\n')
|
|
35
|
+
await chmod(mockBinaryPath, 0o755)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
afterAll(async () => {
|
|
39
|
+
await rm(tmpDir, { recursive: true, force: true })
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
test('runs the binary at FACET_BIN_PATH', async () => {
|
|
43
|
+
const result = await runLauncher(['--version'], { FACET_BIN_PATH: mockBinaryPath })
|
|
44
|
+
expect(result.stdout).toContain('mock-facet:')
|
|
45
|
+
expect(result.exitCode).toBe(0)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
test('forwards arguments to the target binary', async () => {
|
|
49
|
+
const result = await runLauncher(['build', '--force', 'my-dir'], { FACET_BIN_PATH: mockBinaryPath })
|
|
50
|
+
expect(result.stdout).toContain('build --force my-dir')
|
|
51
|
+
})
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
describe('launcher — forwards exit code', () => {
|
|
55
|
+
let tmpDir: string
|
|
56
|
+
let exitingBinaryPath: string
|
|
57
|
+
|
|
58
|
+
beforeAll(async () => {
|
|
59
|
+
tmpDir = await mkdtemp(join(tmpdir(), 'launcher-exit-'))
|
|
60
|
+
exitingBinaryPath = join(tmpDir, 'exit-42')
|
|
61
|
+
await writeFile(exitingBinaryPath, '#!/bin/sh\nexit 42\n')
|
|
62
|
+
await chmod(exitingBinaryPath, 0o755)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
afterAll(async () => {
|
|
66
|
+
await rm(tmpDir, { recursive: true, force: true })
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
test('exits with the same code as the target binary', async () => {
|
|
70
|
+
const result = await runLauncher([], { FACET_BIN_PATH: exitingBinaryPath })
|
|
71
|
+
expect(result.exitCode).toBe(42)
|
|
72
|
+
})
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
describe('launcher — no binary found', () => {
|
|
76
|
+
let tmpDir: string
|
|
77
|
+
let isolatedLauncherPath: string
|
|
78
|
+
|
|
79
|
+
beforeAll(async () => {
|
|
80
|
+
// Create an isolated copy of the launcher in a directory with no node_modules
|
|
81
|
+
// and no .facet, so resolution falls through to the error path.
|
|
82
|
+
tmpDir = await mkdtemp(join(tmpdir(), 'launcher-nobin-'))
|
|
83
|
+
isolatedLauncherPath = join(tmpDir, 'facet')
|
|
84
|
+
await Bun.write(isolatedLauncherPath, await Bun.file(LAUNCHER_PATH).text())
|
|
85
|
+
await chmod(isolatedLauncherPath, 0o755)
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
afterAll(async () => {
|
|
89
|
+
await rm(tmpDir, { recursive: true, force: true })
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
test('prints error with candidate package names and exits 1', async () => {
|
|
93
|
+
const proc = Bun.spawn(['node', isolatedLauncherPath], {
|
|
94
|
+
env: { ...process.env, FACET_BIN_PATH: undefined },
|
|
95
|
+
stdout: 'pipe',
|
|
96
|
+
stderr: 'pipe',
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
const stderr = await new Response(proc.stderr).text()
|
|
100
|
+
const exitCode = await proc.exited
|
|
101
|
+
|
|
102
|
+
expect(exitCode).toBe(1)
|
|
103
|
+
expect(stderr).toContain('agent-facets-')
|
|
104
|
+
expect(stderr).toContain('package manager failed')
|
|
105
|
+
})
|
|
106
|
+
})
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { describe, expect, test } from 'bun:test'
|
|
2
|
+
import { resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
// Import the pure functions from the postinstall script
|
|
5
|
+
const postinstallPath = resolve(import.meta.dir, '..', '..', 'scripts', 'postinstall.mjs')
|
|
6
|
+
const { buildCandidates, detectPlatform } = await import(postinstallPath)
|
|
7
|
+
|
|
8
|
+
describe('detectPlatform', () => {
|
|
9
|
+
test('returns an object with platform and arch strings', () => {
|
|
10
|
+
const result = detectPlatform()
|
|
11
|
+
expect(typeof result.platform).toBe('string')
|
|
12
|
+
expect(typeof result.arch).toBe('string')
|
|
13
|
+
expect(result.platform.length).toBeGreaterThan(0)
|
|
14
|
+
expect(result.arch.length).toBeGreaterThan(0)
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('maps darwin correctly', () => {
|
|
18
|
+
// We're running on macOS in this project
|
|
19
|
+
const result = detectPlatform()
|
|
20
|
+
if (process.platform === 'darwin') {
|
|
21
|
+
expect(result.platform).toBe('darwin')
|
|
22
|
+
}
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('buildCandidates', () => {
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Linux glibc
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
test('linux x64 avx2 glibc', () => {
|
|
32
|
+
const result = buildCandidates('linux', 'x64', { avx2: true, musl: false })
|
|
33
|
+
expect(result).toEqual([
|
|
34
|
+
'agent-facets-linux-x64',
|
|
35
|
+
'agent-facets-linux-x64-baseline',
|
|
36
|
+
'agent-facets-linux-x64-musl',
|
|
37
|
+
'agent-facets-linux-x64-baseline-musl',
|
|
38
|
+
])
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
test('linux x64 no-avx2 glibc', () => {
|
|
42
|
+
const result = buildCandidates('linux', 'x64', { avx2: false, musl: false })
|
|
43
|
+
expect(result).toEqual([
|
|
44
|
+
'agent-facets-linux-x64-baseline',
|
|
45
|
+
'agent-facets-linux-x64',
|
|
46
|
+
'agent-facets-linux-x64-baseline-musl',
|
|
47
|
+
'agent-facets-linux-x64-musl',
|
|
48
|
+
])
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
test('linux arm64 glibc', () => {
|
|
52
|
+
const result = buildCandidates('linux', 'arm64', { avx2: false, musl: false })
|
|
53
|
+
expect(result).toEqual(['agent-facets-linux-arm64', 'agent-facets-linux-arm64-musl'])
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
// Linux musl
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
test('linux x64 avx2 musl', () => {
|
|
61
|
+
const result = buildCandidates('linux', 'x64', { avx2: true, musl: true })
|
|
62
|
+
expect(result).toEqual([
|
|
63
|
+
'agent-facets-linux-x64-musl',
|
|
64
|
+
'agent-facets-linux-x64-baseline-musl',
|
|
65
|
+
'agent-facets-linux-x64',
|
|
66
|
+
'agent-facets-linux-x64-baseline',
|
|
67
|
+
])
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
test('linux x64 no-avx2 musl', () => {
|
|
71
|
+
const result = buildCandidates('linux', 'x64', { avx2: false, musl: true })
|
|
72
|
+
expect(result).toEqual([
|
|
73
|
+
'agent-facets-linux-x64-baseline-musl',
|
|
74
|
+
'agent-facets-linux-x64-musl',
|
|
75
|
+
'agent-facets-linux-x64-baseline',
|
|
76
|
+
'agent-facets-linux-x64',
|
|
77
|
+
])
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
test('linux arm64 musl', () => {
|
|
81
|
+
const result = buildCandidates('linux', 'arm64', { avx2: false, musl: true })
|
|
82
|
+
expect(result).toEqual(['agent-facets-linux-arm64-musl', 'agent-facets-linux-arm64'])
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
// ---------------------------------------------------------------------------
|
|
86
|
+
// Non-Linux
|
|
87
|
+
// ---------------------------------------------------------------------------
|
|
88
|
+
|
|
89
|
+
test('darwin arm64', () => {
|
|
90
|
+
const result = buildCandidates('darwin', 'arm64', { avx2: false })
|
|
91
|
+
expect(result).toEqual(['agent-facets-darwin-arm64'])
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
test('darwin x64 avx2', () => {
|
|
95
|
+
const result = buildCandidates('darwin', 'x64', { avx2: true })
|
|
96
|
+
expect(result).toEqual(['agent-facets-darwin-x64', 'agent-facets-darwin-x64-baseline'])
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test('darwin x64 no-avx2', () => {
|
|
100
|
+
const result = buildCandidates('darwin', 'x64', { avx2: false })
|
|
101
|
+
expect(result).toEqual(['agent-facets-darwin-x64-baseline', 'agent-facets-darwin-x64'])
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
test('windows x64 avx2', () => {
|
|
105
|
+
const result = buildCandidates('windows', 'x64', { avx2: true })
|
|
106
|
+
expect(result).toEqual(['agent-facets-windows-x64', 'agent-facets-windows-x64-baseline'])
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test('windows arm64', () => {
|
|
110
|
+
const result = buildCandidates('windows', 'arm64', { avx2: false })
|
|
111
|
+
expect(result).toEqual(['agent-facets-windows-arm64'])
|
|
112
|
+
})
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// ---------------------------------------------------------------------------
|
|
116
|
+
// Consistency: launcher and postinstall must produce identical candidate lists
|
|
117
|
+
// ---------------------------------------------------------------------------
|
|
118
|
+
|
|
119
|
+
describe('consistency — launcher and postinstall candidate lists match', () => {
|
|
120
|
+
/**
|
|
121
|
+
* Reimplements the launcher's candidate-building logic as a pure function.
|
|
122
|
+
* This is intentionally a direct translation of the CommonJS code in bin/facet
|
|
123
|
+
* so the test catches any drift between the two implementations.
|
|
124
|
+
*/
|
|
125
|
+
function launcherCandidates(platform: string, arch: string, opts: { avx2: boolean; musl?: boolean }): string[] {
|
|
126
|
+
const base = `agent-facets-${platform}-${arch}`
|
|
127
|
+
const baseline = arch === 'x64' && !opts.avx2
|
|
128
|
+
|
|
129
|
+
if (platform === 'linux') {
|
|
130
|
+
const musl = !!opts.musl
|
|
131
|
+
|
|
132
|
+
if (musl) {
|
|
133
|
+
if (arch === 'x64') {
|
|
134
|
+
if (baseline) return [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
|
|
135
|
+
return [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
|
|
136
|
+
}
|
|
137
|
+
return [`${base}-musl`, base]
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
if (arch === 'x64') {
|
|
141
|
+
if (baseline) return [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
|
|
142
|
+
return [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
|
|
143
|
+
}
|
|
144
|
+
return [base, `${base}-musl`]
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (arch === 'x64') {
|
|
148
|
+
if (baseline) return [`${base}-baseline`, base]
|
|
149
|
+
return [base, `${base}-baseline`]
|
|
150
|
+
}
|
|
151
|
+
return [base]
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const cases: Array<{ platform: string; arch: string; avx2: boolean; musl?: boolean }> = [
|
|
155
|
+
// Linux glibc
|
|
156
|
+
{ platform: 'linux', arch: 'x64', avx2: true, musl: false },
|
|
157
|
+
{ platform: 'linux', arch: 'x64', avx2: false, musl: false },
|
|
158
|
+
{ platform: 'linux', arch: 'arm64', avx2: false, musl: false },
|
|
159
|
+
// Linux musl
|
|
160
|
+
{ platform: 'linux', arch: 'x64', avx2: true, musl: true },
|
|
161
|
+
{ platform: 'linux', arch: 'x64', avx2: false, musl: true },
|
|
162
|
+
{ platform: 'linux', arch: 'arm64', avx2: false, musl: true },
|
|
163
|
+
// Darwin
|
|
164
|
+
{ platform: 'darwin', arch: 'arm64', avx2: false },
|
|
165
|
+
{ platform: 'darwin', arch: 'x64', avx2: true },
|
|
166
|
+
{ platform: 'darwin', arch: 'x64', avx2: false },
|
|
167
|
+
// Windows
|
|
168
|
+
{ platform: 'windows', arch: 'arm64', avx2: false },
|
|
169
|
+
{ platform: 'windows', arch: 'x64', avx2: true },
|
|
170
|
+
{ platform: 'windows', arch: 'x64', avx2: false },
|
|
171
|
+
]
|
|
172
|
+
|
|
173
|
+
for (const c of cases) {
|
|
174
|
+
const label = `${c.platform}/${c.arch} avx2=${c.avx2}${c.musl !== undefined ? ` musl=${c.musl}` : ''}`
|
|
175
|
+
test(label, () => {
|
|
176
|
+
const fromPostinstall = buildCandidates(c.platform, c.arch, { avx2: c.avx2, musl: c.musl })
|
|
177
|
+
const fromLauncher = launcherCandidates(c.platform, c.arch, { avx2: c.avx2, musl: c.musl })
|
|
178
|
+
expect(fromPostinstall).toEqual(fromLauncher)
|
|
179
|
+
})
|
|
180
|
+
}
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
// ---------------------------------------------------------------------------
|
|
184
|
+
// Silent failure
|
|
185
|
+
// ---------------------------------------------------------------------------
|
|
186
|
+
|
|
187
|
+
describe('postinstall — silent failure', () => {
|
|
188
|
+
test('exits 0 when no platform packages are installed', async () => {
|
|
189
|
+
const proc = Bun.spawn(['node', resolve(import.meta.dir, '..', '..', 'scripts', 'postinstall.mjs')], {
|
|
190
|
+
stdout: 'pipe',
|
|
191
|
+
stderr: 'pipe',
|
|
192
|
+
})
|
|
193
|
+
const exitCode = await proc.exited
|
|
194
|
+
expect(exitCode).toBe(0)
|
|
195
|
+
})
|
|
196
|
+
})
|