slopcode 0.0.2 → 0.0.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 slopcode
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/slopcode 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
+ const envPath = process.env.SLOPCODE_BIN_PATH
21
+ if (envPath) {
22
+ run(envPath)
23
+ }
24
+
25
+ const scriptPath = fs.realpathSync(__filename)
26
+ const scriptDir = path.dirname(scriptPath)
27
+
28
+ //
29
+ const cached = path.join(scriptDir, ".slopcode")
30
+ if (fs.existsSync(cached)) {
31
+ run(cached)
32
+ }
33
+
34
+ const platformMap = {
35
+ darwin: "darwin",
36
+ linux: "linux",
37
+ win32: "windows",
38
+ }
39
+ const archMap = {
40
+ x64: "x64",
41
+ arm64: "arm64",
42
+ arm: "arm",
43
+ }
44
+
45
+ let platform = platformMap[os.platform()]
46
+ if (!platform) {
47
+ platform = os.platform()
48
+ }
49
+ let arch = archMap[os.arch()]
50
+ if (!arch) {
51
+ arch = os.arch()
52
+ }
53
+ const binary = platform === "windows" ? "slopcode.exe" : "slopcode"
54
+
55
+ function supportsAvx2() {
56
+ if (arch !== "x64") return false
57
+
58
+ if (platform === "linux") {
59
+ try {
60
+ return /(^|\s)avx2(\s|$)/i.test(fs.readFileSync("/proc/cpuinfo", "utf8"))
61
+ } catch {
62
+ return false
63
+ }
64
+ }
65
+
66
+ if (platform === "darwin") {
67
+ try {
68
+ const result = childProcess.spawnSync("sysctl", ["-n", "hw.optional.avx2_0"], {
69
+ encoding: "utf8",
70
+ timeout: 1500,
71
+ })
72
+ if (result.status !== 0) return false
73
+ return (result.stdout || "").trim() === "1"
74
+ } catch {
75
+ return false
76
+ }
77
+ }
78
+
79
+ if (platform === "windows") {
80
+ const cmd =
81
+ '(Add-Type -MemberDefinition "[DllImport(""kernel32.dll"")] public static extern bool IsProcessorFeaturePresent(int ProcessorFeature);" -Name Kernel32 -Namespace Win32 -PassThru)::IsProcessorFeaturePresent(40)'
82
+
83
+ for (const exe of ["powershell.exe", "pwsh.exe", "pwsh", "powershell"]) {
84
+ try {
85
+ const result = childProcess.spawnSync(exe, ["-NoProfile", "-NonInteractive", "-Command", cmd], {
86
+ encoding: "utf8",
87
+ timeout: 3000,
88
+ windowsHide: true,
89
+ })
90
+ if (result.status !== 0) continue
91
+ const out = (result.stdout || "").trim().toLowerCase()
92
+ if (out === "true" || out === "1") return true
93
+ if (out === "false" || out === "0") return false
94
+ } catch {
95
+ continue
96
+ }
97
+ }
98
+
99
+ return false
100
+ }
101
+
102
+ return false
103
+ }
104
+
105
+ function names(prefix) {
106
+ const base = `${prefix}-${platform}-${arch}`
107
+ const avx2 = supportsAvx2()
108
+ const baseline = arch === "x64" && !avx2
109
+
110
+ if (platform === "linux") {
111
+ const musl = (() => {
112
+ try {
113
+ if (fs.existsSync("/etc/alpine-release")) return true
114
+ } catch {
115
+ // ignore
116
+ }
117
+
118
+ try {
119
+ const result = childProcess.spawnSync("ldd", ["--version"], { encoding: "utf8" })
120
+ const text = ((result.stdout || "") + (result.stderr || "")).toLowerCase()
121
+ if (text.includes("musl")) return true
122
+ } catch {
123
+ // ignore
124
+ }
125
+
126
+ return false
127
+ })()
128
+
129
+ if (musl) {
130
+ if (arch === "x64") {
131
+ if (baseline) return [`${base}-baseline-musl`, `${base}-musl`, `${base}-baseline`, base]
132
+ return [`${base}-musl`, `${base}-baseline-musl`, base, `${base}-baseline`]
133
+ }
134
+ return [`${base}-musl`, base]
135
+ }
136
+
137
+ if (arch === "x64") {
138
+ if (baseline) return [`${base}-baseline`, base, `${base}-baseline-musl`, `${base}-musl`]
139
+ return [base, `${base}-baseline`, `${base}-musl`, `${base}-baseline-musl`]
140
+ }
141
+ return [base, `${base}-musl`]
142
+ }
143
+
144
+ if (arch === "x64") {
145
+ if (baseline) return [`${base}-baseline`, base]
146
+ return [base, `${base}-baseline`]
147
+ }
148
+ return [base]
149
+ }
150
+
151
+ const resolvedNames = Array.from(new Set([...names("slopcode-bin"), ...names("slopcode")]))
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 resolvedNames) {
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 slopcode CLI for your platform. You can try manually installing " +
175
+ resolvedNames.map((n) => `\"${n}\"`).join(" or ") +
176
+ " package",
177
+ )
178
+ process.exit(1)
179
+ }
180
+
181
+ run(resolved)
package/package.json CHANGED
@@ -1,17 +1,24 @@
1
1
  {
2
2
  "name": "slopcode",
3
- "version": "0.0.2",
4
- "description": "Reserved package name for SlopCode",
5
- "main": "index.js",
3
+ "bin": {
4
+ "slopcode": "./bin/slopcode"
5
+ },
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "postinstall": "bun ./postinstall.mjs || node ./postinstall.mjs"
8
8
  },
9
- "keywords": [],
10
- "author": "",
9
+ "version": "0.0.3",
11
10
  "license": "MIT",
12
- "homepage": "http://github.com/grappeggia/slopcode",
13
- "repository": {
14
- "type": "git",
15
- "url": "http://github.com/grappeggia/slopcode"
11
+ "optionalDependencies": {
12
+ "slopcode-bin-linux-x64-baseline-musl": "0.0.3",
13
+ "slopcode-bin-darwin-x64-baseline": "0.0.3",
14
+ "slopcode-bin-linux-arm64-musl": "0.0.3",
15
+ "slopcode-bin-darwin-x64": "0.0.3",
16
+ "slopcode-bin-windows-x64": "0.0.3",
17
+ "slopcode-bin-windows-x64-baseline": "0.0.3",
18
+ "slopcode-bin-linux-x64-baseline": "0.0.3",
19
+ "slopcode-bin-linux-x64-musl": "0.0.3",
20
+ "slopcode-bin-linux-arm64": "0.0.3",
21
+ "slopcode-bin-darwin-arm64": "0.0.3",
22
+ "slopcode-bin-linux-x64": "0.0.3"
16
23
  }
17
- }
24
+ }
@@ -0,0 +1,136 @@
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 binaryName = platform === "windows" ? "slopcode.exe" : "slopcode"
53
+ const names = [`slopcode-bin-${platform}-${arch}`, `slopcode-${platform}-${arch}`]
54
+
55
+ for (const name of names) {
56
+ try {
57
+ const packageJsonPath = require.resolve(`${name}/package.json`)
58
+ const packageDir = path.dirname(packageJsonPath)
59
+ const binaryPath = path.join(packageDir, "bin", binaryName)
60
+ if (fs.existsSync(binaryPath)) {
61
+ return { binaryPath, binaryName }
62
+ }
63
+ } catch {
64
+ continue
65
+ }
66
+ }
67
+
68
+ return
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(`slopcode 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
+ if (os.platform() === "win32") {
103
+ // On Windows, the .exe is already included in the package and bin field points to it
104
+ // No postinstall setup needed
105
+ console.log("Windows detected: binary setup not needed (using packaged .exe)")
106
+ return
107
+ }
108
+
109
+ // On non-Windows platforms, just verify the binary package exists
110
+ // Don't replace the wrapper script - it handles binary execution
111
+ const found = findBinary()
112
+ if (!found) {
113
+ console.log("No platform binary package detected during postinstall; runtime resolver will handle it")
114
+ return
115
+ }
116
+ const binaryPath = found.binaryPath
117
+ const target = path.join(__dirname, "bin", ".slopcode")
118
+ if (fs.existsSync(target)) fs.unlinkSync(target)
119
+ try {
120
+ fs.linkSync(binaryPath, target)
121
+ } catch {
122
+ fs.copyFileSync(binaryPath, target)
123
+ }
124
+ fs.chmodSync(target, 0o755)
125
+ } catch (error) {
126
+ console.error("Failed to setup slopcode binary cache:", error.message)
127
+ process.exit(0)
128
+ }
129
+ }
130
+
131
+ try {
132
+ main()
133
+ } catch (error) {
134
+ console.error("Postinstall script error:", error.message)
135
+ process.exit(0)
136
+ }