@sdc-libs/morpher-cli 0.0.0-alpha.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/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/morpher ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env node
2
+
3
+ const childProcess = require("child_process")
4
+ const fs = require("fs")
5
+ const path = require("path")
6
+
7
+ const packageName = "@sdc-libs/morpher-cli"
8
+ const binName = "morpher"
9
+ const target = path.join(__dirname, `${binName}.exe`)
10
+
11
+ if (!fs.existsSync(target)) {
12
+ console.error(`Error: ${packageName}'s postinstall script was not run.`)
13
+ console.error("")
14
+ console.error("Reinstall without --ignore-scripts, or run the postinstall script manually:")
15
+ console.error(` cd node_modules/${packageName} && node postinstall.mjs`)
16
+ process.exit(1)
17
+ }
18
+
19
+ const result = childProcess.spawnSync(target, process.argv.slice(2), { stdio: "inherit", windowsHide: true })
20
+ if (result.error) {
21
+ console.error(result.error.message)
22
+ process.exit(1)
23
+ }
24
+ process.exit(result.status ?? 1)
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@sdc-libs/morpher-cli",
3
+ "version": "0.0.0-alpha.0",
4
+ "description": "morpher CLI/TUI npm package",
5
+ "license": "MIT",
6
+ "bin": {
7
+ "morpher": "bin/morpher"
8
+ },
9
+ "scripts": {
10
+ "postinstall": "node ./postinstall.mjs"
11
+ },
12
+ "os": [
13
+ "darwin",
14
+ "linux",
15
+ "win32"
16
+ ],
17
+ "cpu": [
18
+ "arm64",
19
+ "x64"
20
+ ],
21
+ "optionalDependencies": {
22
+ "@sdc-libs/morpher-cli-darwin-arm64": "0.0.0-alpha.0",
23
+ "@sdc-libs/morpher-cli-linux-x64": "0.0.0-alpha.0",
24
+ "@sdc-libs/morpher-cli-linux-arm64": "0.0.0-alpha.0",
25
+ "@sdc-libs/morpher-cli-windows-x64": "0.0.0-alpha.0",
26
+ "@sdc-libs/morpher-cli-windows-arm64": "0.0.0-alpha.0"
27
+ },
28
+ "files": [
29
+ "bin",
30
+ "postinstall.mjs",
31
+ "LICENSE"
32
+ ]
33
+ }
@@ -0,0 +1,125 @@
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
+ const binName = Object.keys(packageJson.bin ?? {})[0]
14
+
15
+ const platformMap = {
16
+ darwin: "darwin",
17
+ linux: "linux",
18
+ win32: "windows",
19
+ }
20
+ const archMap = {
21
+ x64: "x64",
22
+ arm64: "arm64",
23
+ }
24
+
25
+ function platformPackageName() {
26
+ const platform = platformMap[os.platform()]
27
+ const arch = archMap[os.arch()]
28
+ if (!binName) throw new Error(`${packageJson.name} does not declare a bin entry.`)
29
+ if (!platform || !arch) throw new Error(`${packageJson.name} does not support ${os.platform()}-${os.arch()}.`)
30
+ if (platform === "darwin" && arch !== "arm64") throw new Error(`${packageJson.name} supports macOS arm64 only.`)
31
+
32
+ if (packageJson.name.startsWith("@")) {
33
+ const parts = packageJson.name.split("/")
34
+ return `${parts[0]}/${parts[1]}-${platform}-${arch}`
35
+ }
36
+ return `${packageJson.name}-${platform}-${arch}`
37
+ }
38
+
39
+ function resolveBinary(name) {
40
+ const packageJsonPath = require.resolve(`${name}/package.json`)
41
+ return path.join(
42
+ path.dirname(packageJsonPath),
43
+ "bin",
44
+ platformMap[os.platform()] === "windows" ? `${binName}.exe` : binName,
45
+ )
46
+ }
47
+
48
+ function copyBinary(source, target) {
49
+ if (!fs.existsSync(source)) throw new Error(`Binary not found at ${source}`)
50
+ fs.mkdirSync(path.dirname(target), { recursive: true })
51
+ if (fs.existsSync(target)) fs.unlinkSync(target)
52
+ try {
53
+ fs.linkSync(source, target)
54
+ } catch {
55
+ fs.copyFileSync(source, target)
56
+ }
57
+ if (os.platform() !== "win32") fs.chmodSync(target, 0o755)
58
+ }
59
+
60
+ function installPackage(name) {
61
+ const version = packageJson.optionalDependencies?.[name]
62
+ if (!version) return false
63
+
64
+ const temp = fs.mkdtempSync(path.join(os.tmpdir(), `${binName}-install-`))
65
+ try {
66
+ const result = childProcess.spawnSync(
67
+ "npm",
68
+ ["install", "--ignore-scripts", "--no-save", "--loglevel=error", "--prefix", temp, `${name}@${version}`],
69
+ { stdio: "inherit", windowsHide: true },
70
+ )
71
+ if (result.status !== 0) return false
72
+ copyBinary(
73
+ path.join(
74
+ temp,
75
+ "node_modules",
76
+ name,
77
+ "bin",
78
+ platformMap[os.platform()] === "windows" ? `${binName}.exe` : binName,
79
+ ),
80
+ targetBinary(),
81
+ )
82
+ return true
83
+ } finally {
84
+ fs.rmSync(temp, { recursive: true, force: true })
85
+ }
86
+ }
87
+
88
+ function targetBinary() {
89
+ return path.join(__dirname, "bin", `${binName}.exe`)
90
+ }
91
+
92
+ function verifyBinary() {
93
+ return (
94
+ childProcess.spawnSync(targetBinary(), ["--version"], {
95
+ encoding: "utf8",
96
+ stdio: "ignore",
97
+ windowsHide: true,
98
+ }).status === 0
99
+ )
100
+ }
101
+
102
+ function main() {
103
+ const name = platformPackageName()
104
+ if (!packageJson.optionalDependencies?.[name]) {
105
+ throw new Error(`${packageJson.name} does not ship a ${os.platform()}-${os.arch()} binary package.`)
106
+ }
107
+
108
+ try {
109
+ copyBinary(resolveBinary(name), targetBinary())
110
+ if (verifyBinary()) return
111
+ } catch {}
112
+
113
+ if (installPackage(name) && verifyBinary()) return
114
+
115
+ throw new Error(
116
+ `Failed to install ${name}. Try installing ${name}@${packageJson.optionalDependencies[name]} manually.`,
117
+ )
118
+ }
119
+
120
+ try {
121
+ main()
122
+ } catch (error) {
123
+ console.error(error.message)
124
+ process.exit(1)
125
+ }