@ziniao-open/cli 0.1.0-beta.175 → 0.1.0-beta.186
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/README.md +2 -0
- package/package.json +2 -5
- package/scripts/install.js +35 -27
- package/scripts/legacy-cli.js +179 -0
- package/scripts/open-eco-bin.js +0 -172
- package/scripts/run-open-eco.js +0 -32
- package/scripts/uninstall-open-eco-bin.js +0 -17
package/README.md
CHANGED
|
@@ -12,6 +12,8 @@ npm install -g @ziniao-open/cli
|
|
|
12
12
|
|
|
13
13
|
安装时会自动下载当前平台对应的预编译二进制(支持 macOS / Linux / Windows × amd64 / arm64),并做 SHA-256 校验。
|
|
14
14
|
|
|
15
|
+
全局安装或更新时会自动清理同一 npm 前缀内的旧包 `@ziniao-agent/cli`、`zn-open-eco` 入口,以及已识别的旧版紫鸟技能,并告知清理结果。同名新版技能会保留,后续统一使用 `ziniao-cli`。本地项目依赖安装不执行这项清理。
|
|
16
|
+
|
|
15
17
|
如果同时使用紫鸟桌面客户端,安装后可执行 `ziniao-cli doctor` 检查环境;PATH 异常时可用:
|
|
16
18
|
|
|
17
19
|
```bash
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ziniao-open/cli",
|
|
3
|
-
"version": "0.1.0-beta.
|
|
3
|
+
"version": "0.1.0-beta.186",
|
|
4
4
|
"description": "紫鸟开放平台命令行工具,为开发者和 AI Agent 提供统一的接口调用入口。",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"bin": {
|
|
@@ -8,15 +8,12 @@
|
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
10
|
"postinstall": "node scripts/install.js",
|
|
11
|
-
"preuninstall": "node scripts/uninstall-open-eco-bin.js",
|
|
12
11
|
"pack:local": "npm pack"
|
|
13
12
|
},
|
|
14
13
|
"files": [
|
|
15
14
|
"scripts/install.js",
|
|
16
|
-
"scripts/
|
|
17
|
-
"scripts/uninstall-open-eco-bin.js",
|
|
15
|
+
"scripts/legacy-cli.js",
|
|
18
16
|
"scripts/run.js",
|
|
19
|
-
"scripts/run-open-eco.js",
|
|
20
17
|
"scripts/setup-ziniao-cli.sh",
|
|
21
18
|
"scripts/setup-ziniao-cli.ps1",
|
|
22
19
|
"scripts/remove-ziniao-cli-shims.ps1",
|
package/scripts/install.js
CHANGED
|
@@ -29,7 +29,7 @@ const {
|
|
|
29
29
|
statSync,
|
|
30
30
|
unlinkSync,
|
|
31
31
|
} = require("node:fs");
|
|
32
|
-
const { basename, dirname, join } = require("node:path");
|
|
32
|
+
const { basename, dirname, join, relative } = require("node:path");
|
|
33
33
|
const { tmpdir } = require("node:os");
|
|
34
34
|
const { spawnSync } = require("node:child_process");
|
|
35
35
|
const { createHash } = require("node:crypto");
|
|
@@ -37,7 +37,7 @@ const https = require("node:https");
|
|
|
37
37
|
const http = require("node:http");
|
|
38
38
|
const { URL } = require("node:url");
|
|
39
39
|
|
|
40
|
-
const {
|
|
40
|
+
const { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers, scheduleLegacyVoltaCleanup } = require("./legacy-cli");
|
|
41
41
|
|
|
42
42
|
const PKG = require("../package.json");
|
|
43
43
|
const REPO_ROOT = join(__dirname, "..");
|
|
@@ -302,37 +302,45 @@ async function main() {
|
|
|
302
302
|
await downloadAndInstall();
|
|
303
303
|
}
|
|
304
304
|
|
|
305
|
+
const pendingCleanup = await cleanLegacyComponents();
|
|
305
306
|
runDesktopCliSetup();
|
|
306
|
-
|
|
307
|
+
reportLegacyCLI(pendingCleanup);
|
|
307
308
|
}
|
|
308
309
|
|
|
309
|
-
function
|
|
310
|
-
|
|
310
|
+
async function cleanLegacyComponents() {
|
|
311
|
+
const scope = npmScope();
|
|
312
|
+
if (!scope) return; // Automatic migration is limited to global npm installs.
|
|
313
|
+
const args = ["skills", "cleanup-legacy"];
|
|
314
|
+
if (process.env.INIT_CWD) args.push("--project-dir", process.env.INIT_CWD);
|
|
315
|
+
// Clean skills first, while links into the old package can still be identified.
|
|
316
|
+
const result = spawnSync(TARGET_PATH, args, {
|
|
317
|
+
encoding: "utf8", windowsHide: true, timeout: 60000,
|
|
318
|
+
});
|
|
319
|
+
if (result.error || result.status !== 0) {
|
|
320
|
+
throw new Error(`旧技能自动清理失败: ${result.error?.message || result.stderr?.trim() || result.status}`);
|
|
321
|
+
}
|
|
322
|
+
const report = JSON.parse(result.stdout);
|
|
323
|
+
if (!report.ok || !Array.isArray(report.data?.removed)) throw new Error("旧技能清理结果无效");
|
|
324
|
+
for (const path of report.data.removed) log(`已清理旧技能: ${path}`);
|
|
325
|
+
const oldPackageRemoved = uninstallLegacyPackage({ scope });
|
|
326
|
+
if (oldPackageRemoved) log("已清理旧 npm 包: @ziniao-agent/cli");
|
|
327
|
+
const removed = cleanupLegacyLaunchers({ binDir: scope.binDir });
|
|
328
|
+
for (const path of removed) log(`已清理旧入口: ${path}`);
|
|
329
|
+
log(`当前 npm 范围旧版清理完成:旧技能 ${report.data.removed.length} 个,旧 npm 包 ${oldPackageRemoved ? 1 : 0} 个,残留入口 ${removed.length} 个。`);
|
|
330
|
+
const pendingCleanup = await scheduleLegacyVoltaCleanup({ scope });
|
|
331
|
+
if (pendingCleanup) log(`Volta 旧包清理已安排,结果日志: ${pendingCleanup.logPath}`);
|
|
332
|
+
return pendingCleanup;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function reportLegacyCLI(pendingCleanup) {
|
|
311
336
|
try {
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
});
|
|
317
|
-
if (!binDir) {
|
|
318
|
-
log("npm bin directory was not detected; leaving zn-open-eco unchanged");
|
|
319
|
-
return;
|
|
320
|
-
}
|
|
321
|
-
const replaced = installOpenEcoBins({
|
|
322
|
-
binDir,
|
|
323
|
-
packageRoot: REPO_ROOT,
|
|
324
|
-
platform: process.platform,
|
|
325
|
-
});
|
|
326
|
-
if (replaced.length) {
|
|
327
|
-
log(`replaced existing zn-open-eco shim: ${replaced.join(", ")}`);
|
|
337
|
+
const paths = findLegacyCLI();
|
|
338
|
+
for (const path of paths) {
|
|
339
|
+
if (pendingCleanup && relative(pendingCleanup.binDir, dirname(path)) === "") continue;
|
|
340
|
+
warn(`其他安装范围仍有旧入口,本次未清理: ${path}`);
|
|
328
341
|
}
|
|
329
|
-
log(`installed zn-open-eco -> ${binDir}`);
|
|
330
|
-
// npm 11 does not run preuninstall. Recording the command in the installed
|
|
331
|
-
// package.json makes npm delete the shim itself. The published manifest
|
|
332
|
-
// stays without this bin, so an older zn-open-eco file does not block install.
|
|
333
|
-
recordCompatBin(REPO_ROOT);
|
|
334
342
|
} catch (err) {
|
|
335
|
-
warn(
|
|
343
|
+
warn(`旧 CLI 检查未完成: ${err.message}`);
|
|
336
344
|
}
|
|
337
345
|
}
|
|
338
346
|
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
const { statSync, lstatSync, readFileSync, unlinkSync, existsSync, mkdirSync, mkdtempSync, copyFileSync, openSync, closeSync, writeFileSync, appendFileSync } = require("node:fs");
|
|
2
|
+
const { join, resolve, dirname, isAbsolute, relative, delimiter } = require("node:path");
|
|
3
|
+
const { homedir } = require("node:os");
|
|
4
|
+
const { spawn, spawnSync } = require("node:child_process");
|
|
5
|
+
|
|
6
|
+
const LEGACY_PACKAGE = "@ziniao-agent/cli";
|
|
7
|
+
|
|
8
|
+
function legacyNames(platform) {
|
|
9
|
+
return platform === "win32"
|
|
10
|
+
? ["zn-open-eco", "zn-open-eco.exe", "zn-open-eco.cmd", "zn-open-eco.ps1", "zn-open-eco.bat"]
|
|
11
|
+
: ["zn-open-eco"];
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Inspect names only. Never execute or replace an old binary or npm shim.
|
|
15
|
+
function findLegacyCLI({ path = process.env.PATH || "", platform = process.platform } = {}) {
|
|
16
|
+
const separator = platform === "win32" ? ";" : ":";
|
|
17
|
+
const found = new Set();
|
|
18
|
+
for (const dir of path.split(separator)) {
|
|
19
|
+
if (!dir) continue;
|
|
20
|
+
for (const name of legacyNames(platform)) {
|
|
21
|
+
const candidate = join(dir, name);
|
|
22
|
+
try {
|
|
23
|
+
if (statSync(candidate).isFile()) found.add(candidate);
|
|
24
|
+
} catch (err) {
|
|
25
|
+
if (err.code !== "ENOENT" && err.code !== "ENOTDIR") throw err;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return [...found].sort();
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function npmScope({ env = process.env, platform = process.platform } = {}) {
|
|
33
|
+
// Local postinstall must not re-enter npm's in-progress dependency transaction.
|
|
34
|
+
if (env.npm_config_global !== "true") return null;
|
|
35
|
+
const prefix = env.npm_config_prefix;
|
|
36
|
+
if (!prefix || !isAbsolute(prefix)) throw new Error("npm global prefix must be absolute");
|
|
37
|
+
return {
|
|
38
|
+
global: true, prefix,
|
|
39
|
+
modules: platform === "win32" ? join(prefix, "node_modules") : join(prefix, "lib", "node_modules"),
|
|
40
|
+
binDir: platform === "win32" ? prefix : join(prefix, "bin"),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Remove the old package through the same npm and prefix as this installation.
|
|
45
|
+
// Disabling lifecycle scripts prevents legacy hooks from running during cleanup.
|
|
46
|
+
function uninstallLegacyPackage({ scope, env = process.env }) {
|
|
47
|
+
if (!scope?.global) return false;
|
|
48
|
+
const oldPackage = join(scope.modules, "@ziniao-agent", "cli");
|
|
49
|
+
if (!existsSync(oldPackage)) return false;
|
|
50
|
+
const metadata = JSON.parse(readFileSync(join(oldPackage, "package.json"), "utf8"));
|
|
51
|
+
if (metadata.name !== "@ziniao-agent/cli") throw new Error(`unexpected old package at ${oldPackage}`);
|
|
52
|
+
if (!env.npm_execpath || !isAbsolute(env.npm_execpath)) throw new Error("npm lifecycle executable is unavailable");
|
|
53
|
+
const args = [env.npm_execpath, "uninstall", "@ziniao-agent/cli", "--prefix", scope.prefix,
|
|
54
|
+
"--ignore-scripts", "--offline", "--no-audit", "--no-fund"];
|
|
55
|
+
args.push("--global");
|
|
56
|
+
const result = spawnSync(process.execPath, args, {
|
|
57
|
+
cwd: scope.prefix, env, encoding: "utf8", windowsHide: true, timeout: 60000,
|
|
58
|
+
});
|
|
59
|
+
if (result.error || result.status !== 0 || existsSync(oldPackage)) {
|
|
60
|
+
throw new Error(`旧包自动清理失败: ${result.error?.message || result.stderr?.trim() || result.status}`);
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function voltaPackageRoot(home) {
|
|
66
|
+
const modules = process.platform === "win32" ? "node_modules" : "lib/node_modules";
|
|
67
|
+
return join(home, "tools", "image", "packages", LEGACY_PACKAGE, modules, LEGACY_PACKAGE);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// A Volta npm lifecycle uses a staging prefix, not the installed package image.
|
|
71
|
+
// Match only this installation's Volta home; an explicit unrelated prefix is out of scope.
|
|
72
|
+
function activeVoltaHome(scope, env) {
|
|
73
|
+
if (!scope?.global) return null;
|
|
74
|
+
const home = env.VOLTA_HOME || (process.platform === "win32"
|
|
75
|
+
? join(env.LOCALAPPDATA || join(homedir(), "AppData", "Local"), "Volta")
|
|
76
|
+
: join(homedir(), ".volta"));
|
|
77
|
+
if (!isAbsolute(home)) return null;
|
|
78
|
+
const prefix = relative(home, scope.prefix).split("\\").join("/");
|
|
79
|
+
return /^tmp\/image\/packages\/(?:scope\/)?[^/]+$/.test(prefix)
|
|
80
|
+
|| prefix === "tools/image/packages/@ziniao-open/cli" ? home : null;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function scheduleLegacyVoltaCleanup({ scope, env = process.env }) {
|
|
84
|
+
const home = activeVoltaHome(scope, env);
|
|
85
|
+
if (!home) return null;
|
|
86
|
+
const old = voltaPackageRoot(home);
|
|
87
|
+
if (!existsSync(old)) return null;
|
|
88
|
+
const metadata = JSON.parse(readFileSync(join(old, "package.json"), "utf8"));
|
|
89
|
+
if (metadata.name !== LEGACY_PACKAGE) throw new Error(`unexpected old package at ${old}`);
|
|
90
|
+
const name = process.platform === "win32" ? "volta.exe" : "volta";
|
|
91
|
+
// Windows environment keys are case-insensitive; match Node's spawn ordering.
|
|
92
|
+
const pathKey = Object.keys(env).sort().find((key) => key.toLowerCase() === "path");
|
|
93
|
+
const candidates = [join(home, "bin"), ...(env[pathKey] || "").split(delimiter)];
|
|
94
|
+
const executable = candidates.filter(isAbsolute).map((dir) => join(dir, name)).find((path) => {
|
|
95
|
+
try { return statSync(path).isFile(); } catch (err) {
|
|
96
|
+
if (err.code === "ENOENT" || err.code === "ENOTDIR") return false;
|
|
97
|
+
throw err;
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
if (!executable) throw new Error("Volta 旧包清理未启动:找不到 volta 可执行文件");
|
|
101
|
+
|
|
102
|
+
// Volta moves staging after postinstall. Both the worker and its cwd must live elsewhere.
|
|
103
|
+
mkdirSync(join(home, "tmp"), { recursive: true });
|
|
104
|
+
const work = mkdtempSync(join(home, "tmp", "ziniao-legacy-cleanup-"));
|
|
105
|
+
const worker = join(work, "worker.js");
|
|
106
|
+
const logPath = join(work, "result.log");
|
|
107
|
+
copyFileSync(__filename, worker);
|
|
108
|
+
writeFileSync(logPath, JSON.stringify({ status: "scheduled", package: LEGACY_PACKAGE }) + "\n");
|
|
109
|
+
const output = openSync(logPath, "a");
|
|
110
|
+
try {
|
|
111
|
+
const child = spawn(process.execPath, [worker, "--volta-cleanup", executable], {
|
|
112
|
+
cwd: home, env: { ...env, VOLTA_HOME: home }, detached: true, windowsHide: true,
|
|
113
|
+
stdio: ["ignore", output, output],
|
|
114
|
+
});
|
|
115
|
+
// Wait for process creation only, never for the manager lock or uninstall completion.
|
|
116
|
+
await new Promise((done, reject) => {
|
|
117
|
+
child.once("error", reject);
|
|
118
|
+
child.once("spawn", () => { child.unref(); done(); });
|
|
119
|
+
});
|
|
120
|
+
} catch (err) {
|
|
121
|
+
appendFileSync(logPath, JSON.stringify({ status: "failed", message: err.message }) + "\n");
|
|
122
|
+
unlinkSync(worker);
|
|
123
|
+
throw err;
|
|
124
|
+
} finally {
|
|
125
|
+
closeSync(output);
|
|
126
|
+
}
|
|
127
|
+
return { status: "scheduled", logPath, binDir: join(home, "bin") };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function runLegacyVoltaCleanup(executable) {
|
|
131
|
+
try {
|
|
132
|
+
const old = voltaPackageRoot(process.env.VOLTA_HOME);
|
|
133
|
+
if (existsSync(old)) {
|
|
134
|
+
const metadata = JSON.parse(readFileSync(join(old, "package.json"), "utf8"));
|
|
135
|
+
if (metadata.name !== LEGACY_PACKAGE) throw new Error(`unexpected old package at ${old}`);
|
|
136
|
+
// The parent install holds Volta's lock. This independent worker can wait for it.
|
|
137
|
+
// Call Volta directly: npm in a lifecycle PATH can bypass Volta's shim.
|
|
138
|
+
const result = spawnSync(executable, ["uninstall", LEGACY_PACKAGE], {
|
|
139
|
+
encoding: "utf8", windowsHide: true, timeout: 60000, killSignal: "SIGKILL",
|
|
140
|
+
});
|
|
141
|
+
if (result.error || result.status !== 0 || existsSync(old)) {
|
|
142
|
+
throw new Error(result.error?.message || result.stderr?.trim() || `exit=${result.status}; old package remains`);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
console.log(JSON.stringify({ status: "succeeded", package: LEGACY_PACKAGE }));
|
|
146
|
+
} catch (err) {
|
|
147
|
+
console.error(JSON.stringify({ status: "failed", package: LEGACY_PACKAGE, message: err.message }));
|
|
148
|
+
process.exitCode = 1;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Delete fixed retired command names in this npm scope. Unlinking a symlink
|
|
153
|
+
// removes its entry only; its target and other npm scopes are left intact.
|
|
154
|
+
function cleanupLegacyLaunchers({ binDir, platform = process.platform }) {
|
|
155
|
+
if (!binDir) return [];
|
|
156
|
+
if (!isAbsolute(binDir)) throw new Error("cleanup bin directory must be absolute");
|
|
157
|
+
const removed = [];
|
|
158
|
+
for (const name of legacyNames(platform)) {
|
|
159
|
+
const path = resolve(binDir, name);
|
|
160
|
+
if (dirname(path) !== resolve(binDir)) throw new Error("launcher escapes npm bin directory");
|
|
161
|
+
let info;
|
|
162
|
+
try {
|
|
163
|
+
info = lstatSync(path);
|
|
164
|
+
} catch (err) {
|
|
165
|
+
if (err.code === "ENOENT" || err.code === "ENOTDIR") continue;
|
|
166
|
+
throw err;
|
|
167
|
+
}
|
|
168
|
+
if (!info.isFile() && !info.isSymbolicLink()) throw new Error(`旧入口不是文件或链接: ${path}`);
|
|
169
|
+
unlinkSync(path);
|
|
170
|
+
removed.push(path);
|
|
171
|
+
}
|
|
172
|
+
return removed;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
module.exports = { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers, scheduleLegacyVoltaCleanup };
|
|
176
|
+
|
|
177
|
+
if (require.main === module && process.argv[2] === "--volta-cleanup") {
|
|
178
|
+
runLegacyVoltaCleanup(process.argv[3]);
|
|
179
|
+
}
|
package/scripts/open-eco-bin.js
DELETED
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
const { chmodSync, existsSync, lstatSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } = require("node:fs");
|
|
2
|
-
const { basename, dirname, join, relative } = require("node:path");
|
|
3
|
-
|
|
4
|
-
const COMMAND = "zn-open-eco";
|
|
5
|
-
const LAUNCHER = join("scripts", "run-open-eco.js");
|
|
6
|
-
|
|
7
|
-
function resolveNpmBinDir({ env, platform, packageRoot }) {
|
|
8
|
-
if (env.npm_config_global === "true") {
|
|
9
|
-
if (!env.npm_config_prefix) return null;
|
|
10
|
-
return platform === "win32" ? env.npm_config_prefix : join(env.npm_config_prefix, "bin");
|
|
11
|
-
}
|
|
12
|
-
if (basename(dirname(dirname(packageRoot))) !== "node_modules") return null;
|
|
13
|
-
return join(dirname(dirname(packageRoot)), ".bin");
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
function launcherRelative(binDir, packageRoot) {
|
|
17
|
-
return relative(binDir, join(packageRoot, LAUNCHER));
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function renderWindowsShims(relPosix) {
|
|
21
|
-
const relWin = relPosix.split("/").join("\\");
|
|
22
|
-
return {
|
|
23
|
-
[COMMAND]: `#!/bin/sh
|
|
24
|
-
basedir=$(dirname "$(echo "$0" | sed -e 's,\\\\,/,g')")
|
|
25
|
-
|
|
26
|
-
case \`uname\` in
|
|
27
|
-
*CYGWIN*|*MINGW*|*MSYS*)
|
|
28
|
-
if command -v cygpath > /dev/null 2>&1; then
|
|
29
|
-
basedir=\`cygpath -w "$basedir"\`
|
|
30
|
-
fi
|
|
31
|
-
;;
|
|
32
|
-
esac
|
|
33
|
-
|
|
34
|
-
if [ -x "$basedir/node" ]; then
|
|
35
|
-
exec "$basedir/node" "$basedir/${relPosix}" "$@"
|
|
36
|
-
else
|
|
37
|
-
exec node "$basedir/${relPosix}" "$@"
|
|
38
|
-
fi
|
|
39
|
-
`,
|
|
40
|
-
[`${COMMAND}.cmd`]: `@ECHO off
|
|
41
|
-
GOTO start
|
|
42
|
-
:find_dp0
|
|
43
|
-
SET dp0=%~dp0
|
|
44
|
-
EXIT /b
|
|
45
|
-
:start
|
|
46
|
-
SETLOCAL
|
|
47
|
-
CALL :find_dp0
|
|
48
|
-
|
|
49
|
-
IF EXIST "%dp0%\\node.exe" (
|
|
50
|
-
SET "_prog=%dp0%\\node.exe"
|
|
51
|
-
) ELSE (
|
|
52
|
-
SET "_prog=node"
|
|
53
|
-
SET PATHEXT=%PATHEXT:;.JS;=;%
|
|
54
|
-
)
|
|
55
|
-
|
|
56
|
-
endLocal & goto #_undefined_# 2>NUL || title %COMSPEC% & "%_prog%" "%dp0%\\${relWin}" %*
|
|
57
|
-
`,
|
|
58
|
-
[`${COMMAND}.ps1`]: `#!/usr/bin/env pwsh
|
|
59
|
-
$basedir=Split-Path $MyInvocation.MyCommand.Definition -Parent
|
|
60
|
-
|
|
61
|
-
$exe=""
|
|
62
|
-
if ($PSVersionTable.PSVersion -lt "6.0" -or $IsWindows) {
|
|
63
|
-
# Fix case when both the Windows and Linux builds of Node
|
|
64
|
-
# are installed in the same directory
|
|
65
|
-
$exe=".exe"
|
|
66
|
-
}
|
|
67
|
-
$ret=0
|
|
68
|
-
if (Test-Path "$basedir/node$exe") {
|
|
69
|
-
# Support pipeline input
|
|
70
|
-
if ($MyInvocation.ExpectingInput) {
|
|
71
|
-
$input | & "$basedir/node$exe" "$basedir/${relPosix}" $args
|
|
72
|
-
} else {
|
|
73
|
-
& "$basedir/node$exe" "$basedir/${relPosix}" $args
|
|
74
|
-
}
|
|
75
|
-
$ret=$LASTEXITCODE
|
|
76
|
-
} else {
|
|
77
|
-
# Support pipeline input
|
|
78
|
-
if ($MyInvocation.ExpectingInput) {
|
|
79
|
-
$input | & "node$exe" "$basedir/${relPosix}" $args
|
|
80
|
-
} else {
|
|
81
|
-
& "node$exe" "$basedir/${relPosix}" $args
|
|
82
|
-
}
|
|
83
|
-
$ret=$LASTEXITCODE
|
|
84
|
-
}
|
|
85
|
-
exit $ret
|
|
86
|
-
`,
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function shimNames(platform) {
|
|
91
|
-
return platform === "win32" ? [COMMAND, `${COMMAND}.cmd`, `${COMMAND}.ps1`] : [COMMAND];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function isOurLauncher(filePath, relPosix) {
|
|
95
|
-
let stat;
|
|
96
|
-
try {
|
|
97
|
-
stat = lstatSync(filePath);
|
|
98
|
-
} catch {
|
|
99
|
-
return false;
|
|
100
|
-
}
|
|
101
|
-
if (stat.isSymbolicLink()) {
|
|
102
|
-
const target = readlinkSync(filePath).split("\\").join("/");
|
|
103
|
-
return target === relPosix || target.endsWith("/" + LAUNCHER.split("\\").join("/"));
|
|
104
|
-
}
|
|
105
|
-
if (!stat.isFile() || stat.size > 64 * 1024) return false;
|
|
106
|
-
const text = readFileSync(filePath, "utf8").split("\\").join("/");
|
|
107
|
-
return text.includes(relPosix) || text.includes(LAUNCHER.split("\\").join("/"));
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
function installOpenEcoBins({ binDir, packageRoot, platform }) {
|
|
111
|
-
const relPosix = launcherRelative(binDir, packageRoot).split("\\").join("/");
|
|
112
|
-
if (!relPosix.endsWith("scripts/run-open-eco.js")) {
|
|
113
|
-
throw new Error(`zn-open-eco launcher resolved to ${relPosix}`);
|
|
114
|
-
}
|
|
115
|
-
const replaced = [];
|
|
116
|
-
if (platform === "win32") {
|
|
117
|
-
const shims = renderWindowsShims(relPosix);
|
|
118
|
-
for (const name of shimNames(platform)) {
|
|
119
|
-
const dest = join(binDir, name);
|
|
120
|
-
if (existsSync(dest)) replaced.push(dest);
|
|
121
|
-
writeFileSync(dest, shims[name]);
|
|
122
|
-
}
|
|
123
|
-
return replaced;
|
|
124
|
-
}
|
|
125
|
-
const dest = join(binDir, COMMAND);
|
|
126
|
-
if (existsSync(dest)) replaced.push(dest);
|
|
127
|
-
makeLauncherExecutable(packageRoot);
|
|
128
|
-
rmSync(dest, { force: true });
|
|
129
|
-
symlinkSync(relPosix, dest);
|
|
130
|
-
return replaced;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
function makeLauncherExecutable(packageRoot) {
|
|
134
|
-
chmodSync(join(packageRoot, LAUNCHER), 0o777 & ~process.umask());
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
const COMPAT_BIN = "scripts/run-open-eco.js";
|
|
138
|
-
|
|
139
|
-
function recordCompatBin(packageRoot) {
|
|
140
|
-
const pkgPath = join(packageRoot, "package.json");
|
|
141
|
-
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
|
|
142
|
-
if (!pkg.bin || typeof pkg.bin !== "object" || Array.isArray(pkg.bin)) {
|
|
143
|
-
pkg.bin = {};
|
|
144
|
-
}
|
|
145
|
-
if (pkg.bin[COMMAND] === COMPAT_BIN) {
|
|
146
|
-
return false;
|
|
147
|
-
}
|
|
148
|
-
pkg.bin[COMMAND] = COMPAT_BIN;
|
|
149
|
-
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + "\n");
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
function removeOpenEcoBins({ binDir, packageRoot, platform }) {
|
|
154
|
-
const relPosix = launcherRelative(binDir, packageRoot).split("\\").join("/");
|
|
155
|
-
const removed = [];
|
|
156
|
-
for (const name of shimNames(platform)) {
|
|
157
|
-
const dest = join(binDir, name);
|
|
158
|
-
if (!isOurLauncher(dest, relPosix)) continue;
|
|
159
|
-
rmSync(dest, { force: true });
|
|
160
|
-
removed.push(dest);
|
|
161
|
-
}
|
|
162
|
-
return removed;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
module.exports = {
|
|
166
|
-
installOpenEcoBins,
|
|
167
|
-
isOurLauncher,
|
|
168
|
-
recordCompatBin,
|
|
169
|
-
removeOpenEcoBins,
|
|
170
|
-
renderWindowsShims,
|
|
171
|
-
resolveNpmBinDir,
|
|
172
|
-
};
|
package/scripts/run-open-eco.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const { existsSync } = require("node:fs");
|
|
4
|
-
const { join } = require("node:path");
|
|
5
|
-
const { spawnSync } = require("node:child_process");
|
|
6
|
-
|
|
7
|
-
const binaryName = process.platform === "win32" ? "ziniao-cli.exe" : "ziniao-cli";
|
|
8
|
-
const binaryPath = join(__dirname, "..", "bin", binaryName);
|
|
9
|
-
|
|
10
|
-
if (!existsSync(binaryPath)) {
|
|
11
|
-
console.error(
|
|
12
|
-
[
|
|
13
|
-
`ziniao-cli binary not found at ${binaryPath}.`,
|
|
14
|
-
"For local package testing, set ZINIAO_CLI_BINARY to a built binary before npm install.",
|
|
15
|
-
"Example: $env:ZINIAO_CLI_BINARY = 'C:\\\\path\\\\to\\\\ziniao-cli.exe'",
|
|
16
|
-
].join("\n")
|
|
17
|
-
);
|
|
18
|
-
process.exit(1);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const result = spawnSync(binaryPath, process.argv.slice(2), {
|
|
22
|
-
stdio: "inherit",
|
|
23
|
-
windowsHide: false,
|
|
24
|
-
env: { ...process.env, ZINIAO_CLI_APP: "zn-open-eco" },
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
if (result.error) {
|
|
28
|
-
console.error(result.error.message);
|
|
29
|
-
process.exit(1);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
process.exit(result.status ?? 0);
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const { removeOpenEcoBins, resolveNpmBinDir } = require("./open-eco-bin");
|
|
4
|
-
|
|
5
|
-
const packageRoot = require("node:path").join(__dirname, "..");
|
|
6
|
-
|
|
7
|
-
try {
|
|
8
|
-
const binDir = resolveNpmBinDir({
|
|
9
|
-
env: process.env,
|
|
10
|
-
platform: process.platform,
|
|
11
|
-
packageRoot,
|
|
12
|
-
});
|
|
13
|
-
if (!binDir) process.exit(0);
|
|
14
|
-
removeOpenEcoBins({ binDir, packageRoot, platform: process.platform });
|
|
15
|
-
} catch (err) {
|
|
16
|
-
console.warn(`[ziniao-cli/install] zn-open-eco shim was left in place: ${err.message}`);
|
|
17
|
-
}
|