@ziniao-open/cli 1.2.0 → 1.3.1
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 -12
- package/package.json +2 -1
- package/scripts/install.js +42 -1
- package/scripts/legacy-cli.js +179 -0
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
|
|
@@ -63,15 +65,3 @@ ziniao-cli skills install
|
|
|
63
65
|
备选:`npx skills add ziniao-open/skills -y -g`
|
|
64
66
|
|
|
65
67
|
详见 [ziniao-skills](https://github.com/ziniao-open/skills)。
|
|
66
|
-
|
|
67
|
-
### 操作店铺中的新标签页
|
|
68
|
-
|
|
69
|
-
点击链接或手动打开新标签后,可列出页面并选择后续命令的默认目标:
|
|
70
|
-
|
|
71
|
-
```bash
|
|
72
|
-
ziniao-cli page list --store-id <店铺ID>
|
|
73
|
-
ziniao-cli page select --store-id <店铺ID> --target-id <页面ID>
|
|
74
|
-
ziniao-cli page content --store-id <店铺ID>
|
|
75
|
-
```
|
|
76
|
-
|
|
77
|
-
列表返回 `targetId`、`title`、`url`、`selected`。`selected` 表示 CLI 默认目标,不代表浏览器前台标签。选择操作不会导航或激活页面,选择在当前店铺运行会话中生效。页面命令仍可使用 `--target-id` 指定目标;沿用现有行为,该操作也会更新后续默认目标。目标关闭后会重新解析;有多个可操作页面且没有有效默认目标时,需要重新选择。需配合支持 `list_pages`、`select_page` 的客户端使用。
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ziniao-open/cli",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.1",
|
|
4
4
|
"description": "紫鸟开放平台命令行工具,为开发者和 AI Agent 提供统一的接口调用入口。",
|
|
5
5
|
"license": "UNLICENSED",
|
|
6
6
|
"bin": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
},
|
|
13
13
|
"files": [
|
|
14
14
|
"scripts/install.js",
|
|
15
|
+
"scripts/legacy-cli.js",
|
|
15
16
|
"scripts/run.js",
|
|
16
17
|
"scripts/setup-ziniao-cli.sh",
|
|
17
18
|
"scripts/setup-ziniao-cli.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,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 { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers, scheduleLegacyVoltaCleanup } = require("./legacy-cli");
|
|
41
|
+
|
|
40
42
|
const PKG = require("../package.json");
|
|
41
43
|
const REPO_ROOT = join(__dirname, "..");
|
|
42
44
|
const BIN_DIR = join(REPO_ROOT, "bin");
|
|
@@ -300,7 +302,46 @@ async function main() {
|
|
|
300
302
|
await downloadAndInstall();
|
|
301
303
|
}
|
|
302
304
|
|
|
305
|
+
const pendingCleanup = await cleanLegacyComponents();
|
|
303
306
|
runDesktopCliSetup();
|
|
307
|
+
reportLegacyCLI(pendingCleanup);
|
|
308
|
+
}
|
|
309
|
+
|
|
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) {
|
|
336
|
+
try {
|
|
337
|
+
const paths = findLegacyCLI();
|
|
338
|
+
for (const path of paths) {
|
|
339
|
+
if (pendingCleanup && relative(pendingCleanup.binDir, dirname(path)) === "") continue;
|
|
340
|
+
warn(`其他安装范围仍有旧入口,本次未清理: ${path}`);
|
|
341
|
+
}
|
|
342
|
+
} catch (err) {
|
|
343
|
+
warn(`旧 CLI 检查未完成: ${err.message}`);
|
|
344
|
+
}
|
|
304
345
|
}
|
|
305
346
|
|
|
306
347
|
main().catch((err) => fail(err.stack || err.message || String(err)));
|
|
@@ -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
|
+
}
|