FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
opencode/packages/opencode/script/postinstall.mjs at dev · argszero/opencode · GitHub
argszero
/
opencode
Public
forked from
anomalyco/opencode
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
opencode
/
packages
/
opencode
/
script
/
postinstall.mjs
Copy path
More file actions
More file actions
Latest commit
History
History
History
189 lines (164 loc) · 5.37 KB
Breadcrumbs
opencode
/
packages
/
opencode
/
script
/
postinstall.mjs
Copy path
File metadata and controls
189 lines (164 loc) · 5.37 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env node
import
childProcess
from
"child_process"
import
fs
from
"fs"
import
os
from
"os"
import
path
from
"path"
import
{
createRequire
}
from
"module"
import
{
fileURLToPath
}
from
"url"
const
__dirname
=
path
.
dirname
(
fileURLToPath
(
import
.
meta
.
url
)
)
const
require
=
createRequire
(
import
.
meta
.
url
)
const
packageJson
=
JSON
.
parse
(
fs
.
readFileSync
(
path
.
join
(
__dirname
,
"package.json"
)
,
"utf8"
)
)
const
platformMap
=
{
darwin
:
"darwin"
,
linux
:
"linux"
,
win32
:
"windows"
,
}
const
archMap
=
{
x64
:
"x64"
,
arm64
:
"arm64"
,
arm
:
"arm"
,
}
const
platform
=
platformMap
[
os
.
platform
(
)
]
??
os
.
platform
(
)
const
arch
=
archMap
[
os
.
arch
(
)
]
??
os
.
arch
(
)
const
base
=
`opencode-
${
platform
}
-
${
arch
}
`
const
sourceBinary
=
platform
===
"windows"
?
"opencode.exe"
:
"opencode"
const
targetBinary
=
path
.
join
(
__dirname
,
"bin"
,
"opencode.exe"
)
function
supportsAvx2
(
)
{
if
(
arch
!==
"x64"
)
return
false
if
(
platform
===
"linux"
)
{
try
{
return
/
(
^
|
\s
)
a
v
x
2
(
\s
|
$
)
/
i
.
test
(
fs
.
readFileSync
(
"/proc/cpuinfo"
,
"utf8"
)
)
}
catch
{
return
false
}
}
if
(
platform
===
"darwin"
)
{
try
{
const
result
=
childProcess
.
spawnSync
(
"sysctl"
,
[
"-n"
,
"hw.optional.avx2_0"
]
,
{
encoding
:
"utf8"
,
timeout
:
1500
,
}
)
if
(
result
.
status
!==
0
)
return
false
return
(
result
.
stdout
||
""
)
.
trim
(
)
===
"1"
}
catch
{
return
false
}
}
if
(
platform
===
"windows"
)
{
const
command
=
'(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
for
(
const
executable
of
[
"powershell.exe"
,
"pwsh.exe"
,
"pwsh"
,
"powershell"
]
)
{
try
{
const
result
=
childProcess
.
spawnSync
(
executable
,
[
"-NoProfile"
,
"-NonInteractive"
,
"-Command"
,
command
]
,
{
encoding
:
"utf8"
,
timeout
:
3000
,
windowsHide
:
true
,
}
)
if
(
result
.
status
!==
0
)
continue
const
output
=
(
result
.
stdout
||
""
)
.
trim
(
)
.
toLowerCase
(
)
if
(
output
===
"true"
||
output
===
"1"
)
return
true
if
(
output
===
"false"
||
output
===
"0"
)
return
false
}
catch
{
continue
}
}
}
return
false
}
function
isMusl
(
)
{
if
(
platform
!==
"linux"
)
return
false
try
{
if
(
fs
.
existsSync
(
"/etc/alpine-release"
)
)
return
true
}
catch
{
// Ignore filesystem probes that are blocked by the host.
}
try
{
const
result
=
childProcess
.
spawnSync
(
"ldd"
,
[
"--version"
]
,
{
encoding
:
"utf8"
}
)
return
`
${
result
.
stdout
||
""
}
${
result
.
stderr
||
""
}
`
.
toLowerCase
(
)
.
includes
(
"musl"
)
}
catch
{
return
false
}
}
function
packageNames
(
)
{
const
baseline
=
arch
===
"x64"
&&
!
supportsAvx2
(
)
if
(
platform
===
"linux"
)
{
if
(
isMusl
(
)
)
{
if
(
arch
===
"x64"
)
return
baseline
?
[
`
${
base
}
-baseline-musl`
,
`
${
base
}
-musl`
,
`
${
base
}
-baseline`
,
base
]
:
[
`
${
base
}
-musl`
,
`
${
base
}
-baseline-musl`
,
base
,
`
${
base
}
-baseline`
]
return
[
`
${
base
}
-musl`
,
base
]
}
if
(
arch
===
"x64"
)
return
baseline
?
[
`
${
base
}
-baseline`
,
base
,
`
${
base
}
-baseline-musl`
,
`
${
base
}
-musl`
]
:
[
base
,
`
${
base
}
-baseline`
,
`
${
base
}
-musl`
,
`
${
base
}
-baseline-musl`
]
return
[
base
,
`
${
base
}
-musl`
]
}
if
(
arch
===
"x64"
)
return
baseline
?
[
`
${
base
}
-baseline`
,
base
]
:
[
base
,
`
${
base
}
-baseline`
]
return
[
base
]
}
function
resolveBinary
(
name
)
{
const
packageJsonPath
=
require
.
resolve
(
`
${
name
}
/package.json`
)
const
binaryPath
=
path
.
join
(
path
.
dirname
(
packageJsonPath
)
,
"bin"
,
sourceBinary
)
if
(
!
fs
.
existsSync
(
binaryPath
)
)
throw
new
Error
(
`Binary not found at
${
binaryPath
}
`
)
return
binaryPath
}
function
installPackage
(
name
)
{
const
version
=
packageJson
.
optionalDependencies
?.
[
name
]
if
(
!
version
)
return
const
temp
=
fs
.
mkdtempSync
(
path
.
join
(
os
.
tmpdir
(
)
,
"opencode-install-"
)
)
try
{
const
result
=
childProcess
.
spawnSync
(
"npm"
,
[
"install"
,
"--ignore-scripts"
,
"--no-save"
,
"--loglevel=error"
,
"--prefix"
,
temp
,
`
${
name
}
@
${
version
}
`
]
,
{
stdio
:
"inherit"
,
windowsHide
:
true
}
,
)
if
(
result
.
status
!==
0
)
return
const
packageDir
=
path
.
join
(
temp
,
"node_modules"
,
name
)
copyBinary
(
path
.
join
(
packageDir
,
"bin"
,
sourceBinary
)
,
targetBinary
)
return
true
}
finally
{
fs
.
rmSync
(
temp
,
{
recursive
:
true
,
force
:
true
}
)
}
}
function
copyBinary
(
source
,
target
)
{
if
(
!
fs
.
existsSync
(
source
)
)
throw
new
Error
(
`Binary not found at
${
source
}
`
)
fs
.
mkdirSync
(
path
.
dirname
(
target
)
,
{
recursive
:
true
}
)
if
(
fs
.
existsSync
(
target
)
)
fs
.
unlinkSync
(
target
)
try
{
fs
.
linkSync
(
source
,
target
)
}
catch
{
fs
.
copyFileSync
(
source
,
target
)
}
fs
.
chmodSync
(
target
,
0o755
)
}
function
verifyBinary
(
)
{
const
result
=
childProcess
.
spawnSync
(
targetBinary
,
[
"--version"
]
,
{
encoding
:
"utf8"
,
stdio
:
"ignore"
,
windowsHide
:
true
,
}
)
return
result
.
status
===
0
}
function
main
(
)
{
for
(
const
name
of
packageNames
(
)
)
{
try
{
copyBinary
(
resolveBinary
(
name
)
,
targetBinary
)
if
(
verifyBinary
(
)
)
return
}
catch
{
if
(
installPackage
(
name
)
&&
verifyBinary
(
)
)
return
}
}
throw
new
Error
(
`It seems your package manager failed to install the right opencode CLI package. Try manually installing
${
packageNames
(
)
.
map
(
(
name
)
=>
JSON
.
stringify
(
name
)
)
.
join
(
" or "
)
}
.`
,
)
}
try
{
main
(
)
}
catch
(
error
)
{
console
.
error
(
error
.
message
)
process
.
exit
(
1
)
}
Back
|
FazBrowse Home
|
New Git URL