@ziniao-open/cli 0.1.0-beta.173 → 0.1.0-beta.182

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
@@ -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.173",
3
+ "version": "0.1.0-beta.182",
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/open-eco-bin.js",
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",
@@ -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 { installOpenEcoBins, resolveNpmBinDir } = require("./open-eco-bin");
40
+ const { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers } = require("./legacy-cli");
41
41
 
42
42
  const PKG = require("../package.json");
43
43
  const REPO_ROOT = join(__dirname, "..");
@@ -302,33 +302,41 @@ async function main() {
302
302
  await downloadAndInstall();
303
303
  }
304
304
 
305
+ cleanLegacyComponents();
305
306
  runDesktopCliSetup();
306
- installCompatCommand();
307
+ reportLegacyCLI();
307
308
  }
308
309
 
309
- function installCompatCommand() {
310
- let binDir;
310
+ 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(`旧版清理完成:旧技能 ${report.data.removed.length} 个,旧 npm 包 ${oldPackageRemoved ? 1 : 0} 个,残留入口 ${removed.length} 个。`);
330
+ }
331
+
332
+ function reportLegacyCLI() {
311
333
  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(", ")}`);
334
+ const paths = findLegacyCLI();
335
+ for (const path of paths) {
336
+ warn(`其他安装范围仍有旧入口,本次未清理: ${path}`);
328
337
  }
329
- log(`installed zn-open-eco -> ${binDir}`);
330
338
  } catch (err) {
331
- warn(`zn-open-eco shim was not replaced: ${err.message}`);
339
+ warn(`旧 CLI 检查未完成: ${err.message}`);
332
340
  }
333
341
  }
334
342
 
@@ -0,0 +1,85 @@
1
+ const { statSync, lstatSync, readFileSync, unlinkSync, existsSync } = require("node:fs");
2
+ const { join, resolve, dirname, isAbsolute } = require("node:path");
3
+ const { spawnSync } = require("node:child_process");
4
+
5
+ function legacyNames(platform) {
6
+ return platform === "win32"
7
+ ? ["zn-open-eco", "zn-open-eco.exe", "zn-open-eco.cmd", "zn-open-eco.ps1", "zn-open-eco.bat"]
8
+ : ["zn-open-eco"];
9
+ }
10
+
11
+ // Inspect names only. Never execute or replace an old binary or npm shim.
12
+ function findLegacyCLI({ path = process.env.PATH || "", platform = process.platform } = {}) {
13
+ const separator = platform === "win32" ? ";" : ":";
14
+ const found = new Set();
15
+ for (const dir of path.split(separator)) {
16
+ if (!dir) continue;
17
+ for (const name of legacyNames(platform)) {
18
+ const candidate = join(dir, name);
19
+ try {
20
+ if (statSync(candidate).isFile()) found.add(candidate);
21
+ } catch (err) {
22
+ if (err.code !== "ENOENT" && err.code !== "ENOTDIR") throw err;
23
+ }
24
+ }
25
+ }
26
+ return [...found].sort();
27
+ }
28
+
29
+ function npmScope({ env = process.env, platform = process.platform } = {}) {
30
+ // Local postinstall must not re-enter npm's in-progress dependency transaction.
31
+ if (env.npm_config_global !== "true") return null;
32
+ const prefix = env.npm_config_prefix;
33
+ if (!prefix || !isAbsolute(prefix)) throw new Error("npm global prefix must be absolute");
34
+ return {
35
+ global: true, prefix,
36
+ modules: platform === "win32" ? join(prefix, "node_modules") : join(prefix, "lib", "node_modules"),
37
+ binDir: platform === "win32" ? prefix : join(prefix, "bin"),
38
+ };
39
+ }
40
+
41
+ // Remove the old package through the same npm and prefix as this installation.
42
+ // Disabling lifecycle scripts prevents legacy hooks from running during cleanup.
43
+ function uninstallLegacyPackage({ scope, env = process.env }) {
44
+ if (!scope?.global) return false;
45
+ const oldPackage = join(scope.modules, "@ziniao-agent", "cli");
46
+ if (!existsSync(oldPackage)) return false;
47
+ const metadata = JSON.parse(readFileSync(join(oldPackage, "package.json"), "utf8"));
48
+ if (metadata.name !== "@ziniao-agent/cli") throw new Error(`unexpected old package at ${oldPackage}`);
49
+ if (!env.npm_execpath || !isAbsolute(env.npm_execpath)) throw new Error("npm lifecycle executable is unavailable");
50
+ const args = [env.npm_execpath, "uninstall", "@ziniao-agent/cli", "--prefix", scope.prefix,
51
+ "--ignore-scripts", "--offline", "--no-audit", "--no-fund"];
52
+ args.push("--global");
53
+ const result = spawnSync(process.execPath, args, {
54
+ cwd: scope.prefix, env, encoding: "utf8", windowsHide: true, timeout: 60000,
55
+ });
56
+ if (result.error || result.status !== 0 || existsSync(oldPackage)) {
57
+ throw new Error(`旧包自动清理失败: ${result.error?.message || result.stderr?.trim() || result.status}`);
58
+ }
59
+ return true;
60
+ }
61
+
62
+ // Delete fixed retired command names in this npm scope. Unlinking a symlink
63
+ // removes its entry only; its target and other npm scopes are left intact.
64
+ function cleanupLegacyLaunchers({ binDir, platform = process.platform }) {
65
+ if (!binDir) return [];
66
+ if (!isAbsolute(binDir)) throw new Error("cleanup bin directory must be absolute");
67
+ const removed = [];
68
+ for (const name of legacyNames(platform)) {
69
+ const path = resolve(binDir, name);
70
+ if (dirname(path) !== resolve(binDir)) throw new Error("launcher escapes npm bin directory");
71
+ let info;
72
+ try {
73
+ info = lstatSync(path);
74
+ } catch (err) {
75
+ if (err.code === "ENOENT" || err.code === "ENOTDIR") continue;
76
+ throw err;
77
+ }
78
+ if (!info.isFile() && !info.isSymbolicLink()) throw new Error(`旧入口不是文件或链接: ${path}`);
79
+ unlinkSync(path);
80
+ removed.push(path);
81
+ }
82
+ return removed;
83
+ }
84
+
85
+ module.exports = { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers };
@@ -1,150 +0,0 @@
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
- };
@@ -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
- }