@ziniao-open/cli 0.1.0-beta.168 → 0.1.0-beta.170

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/package.json CHANGED
@@ -1,18 +1,20 @@
1
1
  {
2
2
  "name": "@ziniao-open/cli",
3
- "version": "0.1.0-beta.168",
3
+ "version": "0.1.0-beta.170",
4
4
  "description": "紫鸟开放平台命令行工具,为开发者和 AI Agent 提供统一的接口调用入口。",
5
5
  "license": "UNLICENSED",
6
6
  "bin": {
7
- "ziniao-cli": "scripts/run.js",
8
- "zn-open-eco": "scripts/run-open-eco.js"
7
+ "ziniao-cli": "scripts/run.js"
9
8
  },
10
9
  "scripts": {
11
10
  "postinstall": "node scripts/install.js",
11
+ "preuninstall": "node scripts/uninstall-open-eco-bin.js",
12
12
  "pack:local": "npm pack"
13
13
  },
14
14
  "files": [
15
15
  "scripts/install.js",
16
+ "scripts/open-eco-bin.js",
17
+ "scripts/uninstall-open-eco-bin.js",
16
18
  "scripts/run.js",
17
19
  "scripts/run-open-eco.js",
18
20
  "scripts/setup-ziniao-cli.sh",
@@ -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, 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,33 @@ 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
+ } catch (err) {
331
+ warn(`zn-open-eco shim was not replaced: ${err.message}`);
332
+ }
304
333
  }
305
334
 
306
335
  main().catch((err) => fail(err.stack || err.message || String(err)));
@@ -0,0 +1,150 @@
1
+ const { 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
+ rmSync(dest, { force: true });
128
+ symlinkSync(relPosix, dest);
129
+ return replaced;
130
+ }
131
+
132
+ function removeOpenEcoBins({ binDir, packageRoot, platform }) {
133
+ const relPosix = launcherRelative(binDir, packageRoot).split("\\").join("/");
134
+ const removed = [];
135
+ for (const name of shimNames(platform)) {
136
+ const dest = join(binDir, name);
137
+ if (!isOurLauncher(dest, relPosix)) continue;
138
+ rmSync(dest, { force: true });
139
+ removed.push(dest);
140
+ }
141
+ return removed;
142
+ }
143
+
144
+ module.exports = {
145
+ installOpenEcoBins,
146
+ isOurLauncher,
147
+ removeOpenEcoBins,
148
+ renderWindowsShims,
149
+ resolveNpmBinDir,
150
+ };
@@ -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
+ }