@ziniao-open/cli 1.1.3 → 1.3.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/README.md CHANGED
@@ -44,7 +44,7 @@ ziniao-cli --help
44
44
  | 类别 | 命令 | 说明 |
45
45
  |------|------|------|
46
46
  | 基础 | `config` | apiKey 等配置初始化与查看 |
47
- | 基础 | `doctor` | 环境自检(认证、网络、Skills、本地浏览器桥) |
47
+ | 基础 | `doctor` | 环境自检(认证、紫鸟客户端、Skills、本地浏览器桥) |
48
48
  | 基础 | `completion` | 生成 shell 自动补全 |
49
49
  | 通用 | `api <path>` | 调用任意紫鸟 OpenAPI 路径 |
50
50
  | 桥接 | `zclaw` | 本地浏览器 Bridge(页面操作、店铺管理等) |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ziniao-open/cli",
3
- "version": "1.1.3",
3
+ "version": "1.3.0",
4
4
  "description": "紫鸟开放平台命令行工具,为开发者和 AI Agent 提供统一的接口调用入口。",
5
5
  "license": "UNLICENSED",
6
6
  "bin": {
@@ -8,11 +8,15 @@
8
8
  },
9
9
  "scripts": {
10
10
  "postinstall": "node scripts/install.js",
11
+ "preuninstall": "node scripts/uninstall-open-eco-bin.js",
11
12
  "pack:local": "npm pack"
12
13
  },
13
14
  "files": [
14
15
  "scripts/install.js",
16
+ "scripts/open-eco-bin.js",
17
+ "scripts/uninstall-open-eco-bin.js",
15
18
  "scripts/run.js",
19
+ "scripts/run-open-eco.js",
16
20
  "scripts/setup-ziniao-cli.sh",
17
21
  "scripts/setup-ziniao-cli.ps1",
18
22
  "scripts/remove-ziniao-cli-shims.ps1",
@@ -37,6 +37,8 @@ const https = require("node:https");
37
37
  const http = require("node:http");
38
38
  const { URL } = require("node:url");
39
39
 
40
+ const { installOpenEcoBins, recordCompatBin, resolveNpmBinDir } = require("./open-eco-bin");
41
+
40
42
  const PKG = require("../package.json");
41
43
  const REPO_ROOT = join(__dirname, "..");
42
44
  const BIN_DIR = join(REPO_ROOT, "bin");
@@ -301,6 +303,37 @@ async function main() {
301
303
  }
302
304
 
303
305
  runDesktopCliSetup();
306
+ installCompatCommand();
307
+ }
308
+
309
+ function installCompatCommand() {
310
+ let binDir;
311
+ try {
312
+ binDir = resolveNpmBinDir({
313
+ env: process.env,
314
+ platform: process.platform,
315
+ packageRoot: REPO_ROOT,
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(", ")}`);
328
+ }
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
+ } catch (err) {
335
+ warn(`zn-open-eco shim was not replaced: ${err.message}`);
336
+ }
304
337
  }
305
338
 
306
339
  main().catch((err) => fail(err.stack || err.message || String(err)));
@@ -0,0 +1,172 @@
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
+ };
@@ -0,0 +1,32 @@
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);
@@ -0,0 +1,17 @@
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
+ }