@deveco-test/deveco-code 0.1.0-TD.27.2 → 0.1.0-TD.28

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.
Files changed (6) hide show
  1. package/CHANGELOG.md +217 -217
  2. package/LICENSE +21 -21
  3. package/README.md +327 -327
  4. package/bin/deveco +206 -206
  5. package/package.json +5 -5
  6. package/postinstall.mjs +273 -273
package/postinstall.mjs CHANGED
@@ -1,273 +1,273 @@
1
- #!/usr/bin/env node
2
-
3
- import childProcess from "child_process"
4
- import fs from "fs"
5
- import os from "os"
6
- import path from "path"
7
- import { createRequire } from "module"
8
- import { fileURLToPath } from "url"
9
-
10
- const __dirname = path.dirname(fileURLToPath(import.meta.url))
11
- const require = createRequire(import.meta.url)
12
- const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8"))
13
-
14
- const platformMap = {
15
- darwin: "darwin",
16
- linux: "linux",
17
- win32: "windows",
18
- }
19
- const archMap = {
20
- x64: "x64",
21
- arm64: "arm64",
22
- arm: "arm",
23
- }
24
-
25
- const platform = platformMap[os.platform()] ?? os.platform()
26
- const arch = archMap[os.arch()] ?? os.arch()
27
- const scope = packageJson.name && packageJson.name.includes("/") ? packageJson.name.split("/")[0] : "@deveco-test"
28
- const base = `${scope}/deveco-code-${platform}-${arch}`
29
- const sourceBinary = platform === "windows" ? "deveco.exe" : "deveco"
30
- const targetBinary = path.join(__dirname, "bin", "deveco.exe")
31
- const devecoCliPackageName = "@deveco/deveco-cli"
32
-
33
- function resolveLocalDevecoCliEntry() {
34
- const packageJsonPath = require.resolve(`${devecoCliPackageName}/package.json`)
35
- const entry = path.join(path.dirname(packageJsonPath), "dist", "cli.js")
36
- if (fs.existsSync(entry)) return entry
37
- throw new Error(`Local dependency "${devecoCliPackageName}" has no CLI entry at ${entry}.`)
38
- }
39
-
40
- function writeDevecoCliEntryMarker(installRoot) {
41
- const marker = path.join(installRoot, "vendor", ".deveco-cli-entry")
42
- fs.mkdirSync(path.dirname(marker), { recursive: true })
43
- fs.writeFileSync(marker, `${resolveLocalDevecoCliEntry()}\n`, "utf8")
44
- }
45
-
46
- function supportsAvx2() {
47
- if (arch !== "x64") return false
48
-
49
- if (platform === "linux") {
50
- try {
51
- return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
52
- } catch {
53
- return false
54
- }
55
- }
56
-
57
- if (platform === "darwin") {
58
- try {
59
- const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
60
- encoding: "utf8",
61
- timeout: 1500,
62
- })
63
- if (result.status !== 0) return false
64
- return (result.stdout || "").trim() === "1"
65
- } catch {
66
- return false
67
- }
68
- }
69
-
70
- if (platform === "windows") {
71
- const command =
72
- '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
73
-
74
- for (const executable of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
75
- try {
76
- const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", command], {
77
- encoding: "utf8",
78
- timeout: 3000,
79
- windowsHide: true,
80
- })
81
- if (result.status !== 0) continue
82
- const output = (result.stdout || "").trim().toLowerCase()
83
- if (output === "true" || output === "1") return true
84
- if (output === "false" || output === "0") return false
85
- } catch {
86
- continue
87
- }
88
- }
89
- }
90
-
91
- return false
92
- }
93
-
94
- function isMusl() {
95
- if (platform !== "linux") return false
96
-
97
- try {
98
- if (fs.existsSync("/etc/alpine-release")) return true
99
- } catch {
100
- // Ignore filesystem probes that are blocked by the host.
101
- }
102
-
103
- try {
104
- const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
105
- return `${result.stdout || ""}${result.stderr || ""}`.toLowerCase().includes("musl")
106
- } catch {
107
- return false
108
- }
109
- }
110
-
111
- function packageNames() {
112
- const baseline = arch === "x64" && !supportsAvx2()
113
-
114
- if (platform === "linux") {
115
- if (isMusl()) {
116
- if (arch === "x64")
117
- return baseline
118
- ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
119
- : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
120
- return [`${base}-musl`, base]
121
- }
122
-
123
- if (arch === "x64")
124
- return baseline
125
- ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
126
- : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
127
- return [base, `${base}-musl`]
128
- }
129
-
130
- if (arch === "x64") return baseline ? [`${base}-baseline`, base] : [base, `${base}-baseline`]
131
- return [base]
132
- }
133
-
134
- function resolvePackageDir(name) {
135
- const packageJsonPath = require.resolve(`${name}/package.json`)
136
- return path.dirname(packageJsonPath)
137
- }
138
-
139
- function resolveBinary(name) {
140
- const binaryPath = path.join(resolvePackageDir(name), "bin", sourceBinary)
141
- if (!fs.existsSync(binaryPath)) throw new Error(`Binary not found at ${binaryPath}`)
142
- return binaryPath
143
- }
144
-
145
- function copyDir(src, dst) {
146
- if (!fs.existsSync(dst)) fs.mkdirSync(dst, { recursive: true })
147
- for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
148
- const srcPath = path.join(src, entry.name)
149
- const dstPath = path.join(dst, entry.name)
150
- if (entry.isDirectory()) {
151
- copyDir(srcPath, dstPath)
152
- } else {
153
- fs.copyFileSync(srcPath, dstPath)
154
- if (process.platform !== "win32") {
155
- fs.chmodSync(dstPath, fs.statSync(srcPath).mode)
156
- }
157
- }
158
- }
159
- }
160
-
161
- function copyVendor(packageDir) {
162
- const vendorSrc = path.join(packageDir, "vendor")
163
- const vendorDst = path.join(__dirname, "vendor")
164
- if (fs.existsSync(vendorSrc)) {
165
- copyDir(vendorSrc, vendorDst)
166
- // Windows packs often drop Unix +x; force it back on macOS/Linux install.
167
- ensureVendorExecutables(vendorDst)
168
- ensureVendorExecutables(path.join(packageDir, "vendor"))
169
- }
170
- }
171
-
172
- /** Restore execute bits lost when packages are built/published from Windows. */
173
- function ensureVendorExecutables(vendorRoot) {
174
- if (process.platform === "win32" || !fs.existsSync(vendorRoot)) return
175
-
176
- const stack = [vendorRoot]
177
- while (stack.length > 0) {
178
- const dir = stack.pop()
179
- let entries
180
- try {
181
- entries = fs.readdirSync(dir, { withFileTypes: true })
182
- } catch {
183
- continue
184
- }
185
- for (const entry of entries) {
186
- const full = path.join(dir, entry.name)
187
- if (entry.isDirectory()) {
188
- stack.push(full)
189
- continue
190
- }
191
- if (!entry.isFile()) continue
192
- // Top-level vendor/ripgrep/rg and nested @vscode/ripgrep*/**/rg
193
- if (entry.name === "rg") {
194
- try {
195
- fs.chmodSync(full, 0o755)
196
- } catch {
197
- // Best-effort; ignore files we cannot chmod.
198
- }
199
- }
200
- }
201
- }
202
- }
203
-
204
- function installPackage(name) {
205
- const version = packageJson.optionalDependencies?.[name]
206
- if (!version) return
207
-
208
- const temp = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-install-"))
209
- try {
210
- const result = childProcess.spawnSync(
211
- "npm",
212
- ["install", "--ignore-scripts", "--no-save", "--loglevel=error", "--prefix", temp, `${name}@${version}`],
213
- { stdio: "inherit", windowsHide: true },
214
- )
215
- if (result.status !== 0) return
216
- const packageDir = path.join(temp, "node_modules", name)
217
- copyBinary(path.join(packageDir, "bin", sourceBinary), targetBinary)
218
- copyVendor(packageDir)
219
- return true
220
- } finally {
221
- fs.rmSync(temp, { recursive: true, force: true })
222
- }
223
- }
224
-
225
- function copyBinary(source, target) {
226
- if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`)
227
- fs.mkdirSync(path.dirname(target), { recursive: true })
228
- if (fs.existsSync(target)) fs.unlinkSync(target)
229
- try {
230
- fs.linkSync(source, target)
231
- } catch {
232
- fs.copyFileSync(source, target)
233
- }
234
- fs.chmodSync(target, 0o755)
235
- }
236
-
237
- function verifyBinary() {
238
- const result = childProcess.spawnSync(targetBinary, ["--version"], {
239
- encoding: "utf8",
240
- stdio: "ignore",
241
- windowsHide: true,
242
- })
243
- return result.status === 0
244
- }
245
-
246
- function main() {
247
- writeDevecoCliEntryMarker(__dirname)
248
-
249
- for (const name of packageNames()) {
250
- try {
251
- const packageDir = resolvePackageDir(name)
252
- copyBinary(resolveBinary(name), targetBinary)
253
- copyVendor(packageDir)
254
- writeDevecoCliEntryMarker(packageDir)
255
- if (verifyBinary()) return
256
- } catch {
257
- if (installPackage(name) && verifyBinary()) return
258
- }
259
- }
260
-
261
- throw new Error(
262
- `It seems your package manager failed to install the right deveco-code package. Try manually installing ${packageNames()
263
- .map((name) => JSON.stringify(name))
264
- .join(" or ")}.`,
265
- )
266
- }
267
-
268
- try {
269
- main()
270
- } catch (error) {
271
- console.error(error.message)
272
- process.exit(1)
273
- }
1
+ #!/usr/bin/env node
2
+
3
+ import childProcess from "child_process"
4
+ import fs from "fs"
5
+ import os from "os"
6
+ import path from "path"
7
+ import { createRequire } from "module"
8
+ import { fileURLToPath } from "url"
9
+
10
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
11
+ const require = createRequire(import.meta.url)
12
+ const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, "package.json"), "utf8"))
13
+
14
+ const platformMap = {
15
+ darwin: "darwin",
16
+ linux: "linux",
17
+ win32: "windows",
18
+ }
19
+ const archMap = {
20
+ x64: "x64",
21
+ arm64: "arm64",
22
+ arm: "arm",
23
+ }
24
+
25
+ const platform = platformMap[os.platform()] ?? os.platform()
26
+ const arch = archMap[os.arch()] ?? os.arch()
27
+ const scope = packageJson.name && packageJson.name.includes("/") ? packageJson.name.split("/")[0] : "@deveco"
28
+ const base = `${scope}/deveco-code-${platform}-${arch}`
29
+ const sourceBinary = platform === "windows" ? "deveco.exe" : "deveco"
30
+ const targetBinary = path.join(__dirname, "bin", "deveco.exe")
31
+ const devecoCliPackageName = "@deveco/deveco-cli"
32
+
33
+ function resolveLocalDevecoCliEntry() {
34
+ const packageJsonPath = require.resolve(`${devecoCliPackageName}/package.json`)
35
+ const entry = path.join(path.dirname(packageJsonPath), "dist", "cli.js")
36
+ if (fs.existsSync(entry)) return entry
37
+ throw new Error(`Local dependency "${devecoCliPackageName}" has no CLI entry at ${entry}.`)
38
+ }
39
+
40
+ function writeDevecoCliEntryMarker(installRoot) {
41
+ const marker = path.join(installRoot, "vendor", ".deveco-cli-entry")
42
+ fs.mkdirSync(path.dirname(marker), { recursive: true })
43
+ fs.writeFileSync(marker, `${resolveLocalDevecoCliEntry()}\n`, "utf8")
44
+ }
45
+
46
+ function supportsAvx2() {
47
+ if (arch !== "x64") return false
48
+
49
+ if (platform === "linux") {
50
+ try {
51
+ return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
52
+ } catch {
53
+ return false
54
+ }
55
+ }
56
+
57
+ if (platform === "darwin") {
58
+ try {
59
+ const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
60
+ encoding: "utf8",
61
+ timeout: 1500,
62
+ })
63
+ if (result.status !== 0) return false
64
+ return (result.stdout || "").trim() === "1"
65
+ } catch {
66
+ return false
67
+ }
68
+ }
69
+
70
+ if (platform === "windows") {
71
+ const command =
72
+ '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
73
+
74
+ for (const executable of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
75
+ try {
76
+ const result = childProcess.spawnSync(executable, ["-NoProfile", "-NonInteractive", "-Command", command], {
77
+ encoding: "utf8",
78
+ timeout: 3000,
79
+ windowsHide: true,
80
+ })
81
+ if (result.status !== 0) continue
82
+ const output = (result.stdout || "").trim().toLowerCase()
83
+ if (output === "true" || output === "1") return true
84
+ if (output === "false" || output === "0") return false
85
+ } catch {
86
+ continue
87
+ }
88
+ }
89
+ }
90
+
91
+ return false
92
+ }
93
+
94
+ function isMusl() {
95
+ if (platform !== "linux") return false
96
+
97
+ try {
98
+ if (fs.existsSync("/etc/alpine-release")) return true
99
+ } catch {
100
+ // Ignore filesystem probes that are blocked by the host.
101
+ }
102
+
103
+ try {
104
+ const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
105
+ return `${result.stdout || ""}${result.stderr || ""}`.toLowerCase().includes("musl")
106
+ } catch {
107
+ return false
108
+ }
109
+ }
110
+
111
+ function packageNames() {
112
+ const baseline = arch === "x64" && !supportsAvx2()
113
+
114
+ if (platform === "linux") {
115
+ if (isMusl()) {
116
+ if (arch === "x64")
117
+ return baseline
118
+ ? [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
119
+ : [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
120
+ return [`${base}-musl`, base]
121
+ }
122
+
123
+ if (arch === "x64")
124
+ return baseline
125
+ ? [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
126
+ : [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
127
+ return [base, `${base}-musl`]
128
+ }
129
+
130
+ if (arch === "x64") return baseline ? [`${base}-baseline`, base] : [base, `${base}-baseline`]
131
+ return [base]
132
+ }
133
+
134
+ function resolvePackageDir(name) {
135
+ const packageJsonPath = require.resolve(`${name}/package.json`)
136
+ return path.dirname(packageJsonPath)
137
+ }
138
+
139
+ function resolveBinary(name) {
140
+ const binaryPath = path.join(resolvePackageDir(name), "bin", sourceBinary)
141
+ if (!fs.existsSync(binaryPath)) throw new Error(`Binary not found at ${binaryPath}`)
142
+ return binaryPath
143
+ }
144
+
145
+ function copyDir(src, dst) {
146
+ if (!fs.existsSync(dst)) fs.mkdirSync(dst, { recursive: true })
147
+ for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
148
+ const srcPath = path.join(src, entry.name)
149
+ const dstPath = path.join(dst, entry.name)
150
+ if (entry.isDirectory()) {
151
+ copyDir(srcPath, dstPath)
152
+ } else {
153
+ fs.copyFileSync(srcPath, dstPath)
154
+ if (process.platform !== "win32") {
155
+ fs.chmodSync(dstPath, fs.statSync(srcPath).mode)
156
+ }
157
+ }
158
+ }
159
+ }
160
+
161
+ function copyVendor(packageDir) {
162
+ const vendorSrc = path.join(packageDir, "vendor")
163
+ const vendorDst = path.join(__dirname, "vendor")
164
+ if (fs.existsSync(vendorSrc)) {
165
+ copyDir(vendorSrc, vendorDst)
166
+ // Windows packs often drop Unix +x; force it back on macOS/Linux install.
167
+ ensureVendorExecutables(vendorDst)
168
+ ensureVendorExecutables(path.join(packageDir, "vendor"))
169
+ }
170
+ }
171
+
172
+ /** Restore execute bits lost when packages are built/published from Windows. */
173
+ function ensureVendorExecutables(vendorRoot) {
174
+ if (process.platform === "win32" || !fs.existsSync(vendorRoot)) return
175
+
176
+ const stack = [vendorRoot]
177
+ while (stack.length > 0) {
178
+ const dir = stack.pop()
179
+ let entries
180
+ try {
181
+ entries = fs.readdirSync(dir, { withFileTypes: true })
182
+ } catch {
183
+ continue
184
+ }
185
+ for (const entry of entries) {
186
+ const full = path.join(dir, entry.name)
187
+ if (entry.isDirectory()) {
188
+ stack.push(full)
189
+ continue
190
+ }
191
+ if (!entry.isFile()) continue
192
+ // Top-level vendor/ripgrep/rg and nested @vscode/ripgrep*/**/rg
193
+ if (entry.name === "rg") {
194
+ try {
195
+ fs.chmodSync(full, 0o755)
196
+ } catch {
197
+ // Best-effort; ignore files we cannot chmod.
198
+ }
199
+ }
200
+ }
201
+ }
202
+ }
203
+
204
+ function installPackage(name) {
205
+ const version = packageJson.optionalDependencies?.[name]
206
+ if (!version) return
207
+
208
+ const temp = fs.mkdtempSync(path.join(os.tmpdir(), "opencode-install-"))
209
+ try {
210
+ const result = childProcess.spawnSync(
211
+ "npm",
212
+ ["install", "--ignore-scripts", "--no-save", "--loglevel=error", "--prefix", temp, `${name}@${version}`],
213
+ { stdio: "inherit", windowsHide: true },
214
+ )
215
+ if (result.status !== 0) return
216
+ const packageDir = path.join(temp, "node_modules", name)
217
+ copyBinary(path.join(packageDir, "bin", sourceBinary), targetBinary)
218
+ copyVendor(packageDir)
219
+ return true
220
+ } finally {
221
+ fs.rmSync(temp, { recursive: true, force: true })
222
+ }
223
+ }
224
+
225
+ function copyBinary(source, target) {
226
+ if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`)
227
+ fs.mkdirSync(path.dirname(target), { recursive: true })
228
+ if (fs.existsSync(target)) fs.unlinkSync(target)
229
+ try {
230
+ fs.linkSync(source, target)
231
+ } catch {
232
+ fs.copyFileSync(source, target)
233
+ }
234
+ fs.chmodSync(target, 0o755)
235
+ }
236
+
237
+ function verifyBinary() {
238
+ const result = childProcess.spawnSync(targetBinary, ["--version"], {
239
+ encoding: "utf8",
240
+ stdio: "ignore",
241
+ windowsHide: true,
242
+ })
243
+ return result.status === 0
244
+ }
245
+
246
+ function main() {
247
+ writeDevecoCliEntryMarker(__dirname)
248
+
249
+ for (const name of packageNames()) {
250
+ try {
251
+ const packageDir = resolvePackageDir(name)
252
+ copyBinary(resolveBinary(name), targetBinary)
253
+ copyVendor(packageDir)
254
+ writeDevecoCliEntryMarker(packageDir)
255
+ if (verifyBinary()) return
256
+ } catch {
257
+ if (installPackage(name) && verifyBinary()) return
258
+ }
259
+ }
260
+
261
+ throw new Error(
262
+ `It seems your package manager failed to install the right deveco-code package. Try manually installing ${packageNames()
263
+ .map((name) => JSON.stringify(name))
264
+ .join(" or ")}.`,
265
+ )
266
+ }
267
+
268
+ try {
269
+ main()
270
+ } catch (error) {
271
+ console.error(error.message)
272
+ process.exit(1)
273
+ }