@costrict/cs 0.0.0-refactor-workflow-202603060823

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 (4) hide show
  1. package/LICENSE +21 -0
  2. package/bin/cs +115 -0
  3. package/package.json +14 -0
  4. package/postinstall.mjs +141 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 opencode
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/bin/cs ADDED
@@ -0,0 +1,115 @@
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 DEBUG = process.env.DEBUG === "cs"
10
+ if (DEBUG) {
11
+ console.error(`[DEBUG] Executing: ${target} ${process.argv.slice(2).join(' ')}`)
12
+ console.error(`[DEBUG] target exists: ${fs.existsSync(target)}`)
13
+ }
14
+ if (!fs.existsSync(target)) {
15
+ console.error(`[ERROR] Binary not found: ${target}`)
16
+ process.exit(1)
17
+ }
18
+ const result = childProcess.spawnSync(target, process.argv.slice(2), {
19
+ stdio: "inherit",
20
+ shell: process.platform === "win32",
21
+ })
22
+ if (result.error) {
23
+ console.error(`[ERROR] Execution failed: ${result.error.message}`)
24
+ console.error(`[ERROR] Error code: ${result.error.code}`)
25
+ process.exit(1)
26
+ }
27
+ const code = typeof result.status === "number" ? result.status : 0
28
+ if (DEBUG) console.error(`[DEBUG] Exit code: ${code}`)
29
+ process.exit(code)
30
+ }
31
+
32
+ const envPath = process.env.COSTRICT_BIN_PATH
33
+ if (envPath) {
34
+ run(envPath)
35
+ }
36
+
37
+ const scriptPath = fs.realpathSync(__filename)
38
+ const scriptDir = path.dirname(scriptPath)
39
+
40
+ const platformMap = {
41
+ darwin: "darwin",
42
+ linux: "linux",
43
+ win32: "windows",
44
+ }
45
+ const archMap = {
46
+ x64: "x64",
47
+ arm64: "arm64",
48
+ arm: "arm",
49
+ }
50
+
51
+ let platform = platformMap[os.platform()]
52
+ if (!platform) {
53
+ platform = os.platform()
54
+ }
55
+ let arch = archMap[os.arch()]
56
+ if (!arch) {
57
+ arch = os.arch()
58
+ }
59
+ const base = "@costrict/cs-" + platform + "-" + arch + (platform === "windows" || (platform === "linux" && arch === "x64") ? "-baseline" : "")
60
+ const binary = platform === "windows" ? "cs.exe" : "cs"
61
+
62
+ function findBinary(startDir) {
63
+ let current = startDir
64
+ for (;;) {
65
+ const modules = path.join(current, "node_modules")
66
+ if (fs.existsSync(modules)) {
67
+ const entries = fs.readdirSync(modules)
68
+ for (const entry of entries) {
69
+ // Handle scoped packages (e.g., @costrict/cs-darwin-arm64)
70
+ if (entry.startsWith("@")) {
71
+ const scopeDir = path.join(modules, entry)
72
+ if (fs.existsSync(scopeDir)) {
73
+ const scopedPackages = fs.readdirSync(scopeDir)
74
+ for (const pkg of scopedPackages) {
75
+ const fullName = entry + "/" + pkg
76
+ if (!fullName.startsWith(base)) {
77
+ continue
78
+ }
79
+ const candidate = path.join(scopeDir, pkg, "bin", binary)
80
+ if (fs.existsSync(candidate)) {
81
+ return candidate
82
+ }
83
+ }
84
+ }
85
+ } else {
86
+ // Handle non-scoped packages
87
+ if (!entry.startsWith(base)) {
88
+ continue
89
+ }
90
+ const candidate = path.join(modules, entry, "bin", binary)
91
+ if (fs.existsSync(candidate)) {
92
+ return candidate
93
+ }
94
+ }
95
+ }
96
+ }
97
+ const parent = path.dirname(current)
98
+ if (parent === current) {
99
+ return
100
+ }
101
+ current = parent
102
+ }
103
+ }
104
+
105
+ const resolved = findBinary(scriptDir)
106
+ if (!resolved) {
107
+ console.error(
108
+ 'It seems that your package manager failed to install the right version of the CoStrict CLI for your platform. You can try manually installing the "' +
109
+ base +
110
+ '" package',
111
+ )
112
+ process.exit(1)
113
+ }
114
+
115
+ run(resolved)
package/package.json ADDED
@@ -0,0 +1,14 @@
1
+ {
2
+ "name": "@costrict/cs",
3
+ "bin": {
4
+ "cs": "./bin/cs"
5
+ },
6
+ "scripts": {
7
+ "postinstall": "bun ./postinstall.mjs || node ./postinstall.mjs"
8
+ },
9
+ "version": "0.0.0-refactor-workflow-202603060823",
10
+ "license": "MIT",
11
+ "optionalDependencies": {
12
+ "@costrict/cs-linux-x64": "0.0.0-refactor-workflow-202603060823"
13
+ }
14
+ }
@@ -0,0 +1,141 @@
1
+ #!/usr/bin/env node
2
+
3
+ import fs from "fs"
4
+ import path from "path"
5
+ import os from "os"
6
+ import { fileURLToPath } from "url"
7
+ import { createRequire } from "module"
8
+
9
+ const __dirname = path.dirname(fileURLToPath(import.meta.url))
10
+ const require = createRequire(import.meta.url)
11
+
12
+ function detectPlatformAndArch() {
13
+ // Map platform names
14
+ let platform
15
+ switch (os.platform()) {
16
+ case "darwin":
17
+ platform = "darwin"
18
+ break
19
+ case "linux":
20
+ platform = "linux"
21
+ break
22
+ case "win32":
23
+ platform = "windows"
24
+ break
25
+ default:
26
+ platform = os.platform()
27
+ break
28
+ }
29
+
30
+ // Map architecture names
31
+ let arch
32
+ switch (os.arch()) {
33
+ case "x64":
34
+ arch = "x64"
35
+ break
36
+ case "arm64":
37
+ arch = "arm64"
38
+ break
39
+ case "arm":
40
+ arch = "arm"
41
+ break
42
+ default:
43
+ arch = os.arch()
44
+ break
45
+ }
46
+
47
+ return { platform, arch }
48
+ }
49
+
50
+ function findBinary() {
51
+ const { platform, arch } = detectPlatformAndArch()
52
+ const packageName = `@costrict/cs-${platform}-${arch}`
53
+ const binaryName = platform === "windows" ? "cs.exe" : "cs"
54
+
55
+ try {
56
+ // Use require.resolve to find the package
57
+ const packageJsonPath = require.resolve(`${packageName}/package.json`)
58
+ const packageDir = path.dirname(packageJsonPath)
59
+ const binaryPath = path.join(packageDir, "bin", binaryName)
60
+
61
+ if (!fs.existsSync(binaryPath)) {
62
+ throw new Error(`Binary not found at ${binaryPath}`)
63
+ }
64
+
65
+ return { binaryPath, binaryName }
66
+ } catch (error) {
67
+ throw new Error(`Could not find package ${packageName}: ${error.message}`)
68
+ }
69
+ }
70
+
71
+ function prepareBinDirectory(binaryName) {
72
+ const binDir = path.join(__dirname, "bin")
73
+ const targetPath = path.join(binDir, binaryName)
74
+
75
+ // Ensure bin directory exists
76
+ if (!fs.existsSync(binDir)) {
77
+ fs.mkdirSync(binDir, { recursive: true })
78
+ }
79
+
80
+ // Remove existing binary/symlink if it exists
81
+ if (fs.existsSync(targetPath)) {
82
+ fs.unlinkSync(targetPath)
83
+ }
84
+
85
+ return { binDir, targetPath }
86
+ }
87
+
88
+ function symlinkBinary(sourcePath, binaryName) {
89
+ const { targetPath } = prepareBinDirectory(binaryName)
90
+
91
+ fs.symlinkSync(sourcePath, targetPath)
92
+ console.log(`cs binary symlinked: ${targetPath} -> ${sourcePath}`)
93
+
94
+ // Verify the file exists after operation
95
+ if (!fs.existsSync(targetPath)) {
96
+ throw new Error(`Failed to symlink binary to ${targetPath}`)
97
+ }
98
+ }
99
+
100
+ async function main() {
101
+ try {
102
+ console.log(`[DEBUG] Platform: ${os.platform()}, Arch: ${os.arch()}`)
103
+
104
+ if (os.platform() === "win32") {
105
+ // On Windows, verify the platform-specific package is installed
106
+ try {
107
+ const { binaryPath } = findBinary()
108
+ console.log(`[DEBUG] Windows binary found at: ${binaryPath}`)
109
+ console.log("Windows: Platform binary verified successfully")
110
+ } catch (error) {
111
+ console.error(`[ERROR] Failed to find Windows binary: ${error.message}`)
112
+ console.error("[ERROR] This usually means @costrict/cs-windows-x64 was not installed")
113
+ console.error("[ERROR] Try: npm install @costrict/cs-windows-x64 --save-optional")
114
+ // Don't exit with error for Windows - let the wrapper handle it
115
+ }
116
+ return
117
+ }
118
+
119
+ // On non-Windows platforms, just verify the binary package exists
120
+ // Don't replace the wrapper script - it handles binary execution
121
+ const { binaryPath } = findBinary()
122
+ const target = path.join(__dirname, "bin", ".opencode")
123
+ if (fs.existsSync(target)) fs.unlinkSync(target)
124
+ try {
125
+ fs.linkSync(binaryPath, target)
126
+ } catch {
127
+ fs.copyFileSync(binaryPath, target)
128
+ }
129
+ fs.chmodSync(target, 0o755)
130
+ } catch (error) {
131
+ console.error("Failed to setup cs binary:", error.message)
132
+ process.exit(1)
133
+ }
134
+ }
135
+
136
+ try {
137
+ main()
138
+ } catch (error) {
139
+ console.error("Postinstall script error:", error.message)
140
+ process.exit(0)
141
+ }