@ziniao-open/cli 0.1.1-beta.10 → 0.1.1-beta.12
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 +1 -0
- package/package.json +1 -1
- package/scripts/run.js +153 -22
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ ziniao-cli --help
|
|
|
48
48
|
| 基础 | `config` | apiKey 等配置初始化与查看 |
|
|
49
49
|
| 基础 | `doctor` | 环境自检(认证、紫鸟客户端、Skills、本地浏览器桥) |
|
|
50
50
|
| 基础 | `completion` | 生成 shell 自动补全 |
|
|
51
|
+
| 基础 | `update` / `update --check` | 更新 CLI 或查看当前版本和更新状态 |
|
|
51
52
|
| 通用 | `api <path>` | 调用任意紫鸟 OpenAPI 路径 |
|
|
52
53
|
| 桥接 | `zclaw` | 本地浏览器 Bridge(页面操作、店铺管理等) |
|
|
53
54
|
| 快捷 | `staff` / `store` / `account` / `department` / `role` / `device` / `page` / `automation` | 高频业务操作的封装 |
|
package/package.json
CHANGED
package/scripts/run.js
CHANGED
|
@@ -1,31 +1,162 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
const { existsSync } = require("node:fs");
|
|
4
|
-
const {
|
|
5
|
-
const {
|
|
3
|
+
const { existsSync, lstatSync, readFileSync, realpathSync, unlinkSync, writeFileSync } = require("node:fs");
|
|
4
|
+
const { homedir } = require("node:os");
|
|
5
|
+
const { join, toNamespacedPath } = require("node:path");
|
|
6
|
+
const { createHash } = require("node:crypto");
|
|
7
|
+
const { spawn, spawnSync } = require("node:child_process");
|
|
6
8
|
|
|
9
|
+
const pkg = require("../package.json");
|
|
7
10
|
const binaryName = process.platform === "win32" ? "ziniao-cli.exe" : "ziniao-cli";
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
const packageRoot = realpathSync(join(__dirname, ".."));
|
|
12
|
+
const bundledBinaryPath = join(packageRoot, "bin", binaryName);
|
|
13
|
+
const args = process.argv.slice(2);
|
|
14
|
+
|
|
15
|
+
function executablePath(binaryPath) {
|
|
16
|
+
// Windows 的进程启动 API 对超过 MAX_PATH 的绝对路径需要加命名空间前缀。
|
|
17
|
+
return process.platform === "win32" && binaryPath.length >= 260 ? toNamespacedPath(binaryPath) : binaryPath;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function readState(dir) {
|
|
21
|
+
try {
|
|
22
|
+
const state = JSON.parse(readFileSync(join(dir, "state.json"), "utf8")) || {};
|
|
23
|
+
if (typeof state !== "object" || Array.isArray(state)
|
|
24
|
+
|| (state.auto != null && typeof state.auto !== "boolean")) return { auto: false };
|
|
25
|
+
return state;
|
|
26
|
+
} catch (err) {
|
|
27
|
+
// 新安装默认启用;已有状态损坏或不可读时,避免误覆盖用户的关闭设置。
|
|
28
|
+
return err.code === "ENOENT" ? {} : { auto: false };
|
|
29
|
+
}
|
|
19
30
|
}
|
|
20
31
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
32
|
+
function unsupportedBuild(dir) {
|
|
33
|
+
try {
|
|
34
|
+
const cached = JSON.parse(readFileSync(join(dir, "unsupported"), "utf8"));
|
|
35
|
+
const binary = lstatSync(bundledBinaryPath);
|
|
36
|
+
// 同版本重装也可能更换本地构建;文件变化后必须重新检测更新能力。
|
|
37
|
+
return binary.isFile() && cached.size === binary.size && cached.modified_ms === Math.trunc(binary.mtimeMs);
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
25
42
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
43
|
+
function releaseBinaryPath(dir, release) {
|
|
44
|
+
if (typeof release?.version !== "string" || typeof release.directory !== "string"
|
|
45
|
+
|| !/^[0-9A-Za-z.+-]+-[a-f0-9]{64}(-[a-f0-9]{16})?$/.test(release.directory)
|
|
46
|
+
|| !release.directory.startsWith(`${release.version}-`)) return null;
|
|
47
|
+
const binaryPath = join(dir, "versions", release.directory, binaryName);
|
|
48
|
+
if (existsSync(join(dir, `failed-${release.directory}`)) || existsSync(join(dir, `gc-${release.directory}`))) return null;
|
|
49
|
+
try {
|
|
50
|
+
return lstatSync(binaryPath, { throwIfNoEntry: false })?.isFile() ? binaryPath : null;
|
|
51
|
+
} catch {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
29
54
|
}
|
|
30
55
|
|
|
31
|
-
|
|
56
|
+
function permanentLaunchFailure(error, binaryPath, release) {
|
|
57
|
+
if (["ENOENT", "ENOEXEC", "EFTYPE", "EACCES", "EPERM"].includes(error.code)) return true;
|
|
58
|
+
if (process.platform !== "win32" || error.code !== "UNKNOWN") return false;
|
|
59
|
+
// Windows 部分格式错误只返回 UNKNOWN。仅有文件损坏证据时才隔离,保留临时错误的重试能力。
|
|
60
|
+
try {
|
|
61
|
+
const data = readFileSync(binaryPath);
|
|
62
|
+
if (/^[a-f0-9]{64}$/.test(release.sha256 || "")) {
|
|
63
|
+
return createHash("sha256").update(data).digest("hex") !== release.sha256;
|
|
64
|
+
}
|
|
65
|
+
// 兼容旧状态尚未保存文件摘要的安装。
|
|
66
|
+
if (data.length < 64 || data.readUInt16LE(0) !== 0x5a4d) return true;
|
|
67
|
+
const offset = data.readUInt32LE(0x3c);
|
|
68
|
+
return offset > data.length - 24 || data.readUInt32LE(offset) !== 0x00004550;
|
|
69
|
+
} catch {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
// npm 正式包使用三段版本号,测试包沿用流水线的 beta 编号。
|
|
75
|
+
const channel = /^\d+\.\d+\.\d+$/.test(pkg.version) ? "latest"
|
|
76
|
+
: /^\d+\.\d+\.\d+-beta\.\d+$/.test(pkg.version) ? "beta" : null;
|
|
77
|
+
const ci = !["", "false", "0"].includes((process.env.CI || "").toLowerCase());
|
|
78
|
+
const forceBundled = ci || process.env.ZINIAO_CLI_USE_BUNDLED === "1";
|
|
79
|
+
const env = { ...process.env };
|
|
80
|
+
// 始终由实际 npm 包位置覆盖,缓存 exe 不能用自身目录推断配套脚本的位置。
|
|
81
|
+
env.ZINIAO_CLI_PACKAGE_ROOT = packageRoot;
|
|
82
|
+
delete env.ZINIAO_CLI_LAUNCHER_DIR;
|
|
83
|
+
delete env.ZINIAO_CLI_LAUNCHER_VERSION;
|
|
84
|
+
let candidates = [];
|
|
85
|
+
let updateDir;
|
|
86
|
+
if (channel === "latest" || channel === "beta") {
|
|
87
|
+
// 版本纳入安装标识,重新装指定版本不会沿用另一个 npm 版本的生效记录。
|
|
88
|
+
const root = process.platform === "win32" ? packageRoot.toLowerCase() : packageRoot;
|
|
89
|
+
const id = createHash("sha256").update(JSON.stringify([root, pkg.version, channel])).digest("hex").slice(0, 32);
|
|
90
|
+
updateDir = join(homedir(), ".ziniao-cli", "updates", id);
|
|
91
|
+
env.ZINIAO_CLI_LAUNCHER_DIR = updateDir;
|
|
92
|
+
env.ZINIAO_CLI_LAUNCHER_VERSION = pkg.version;
|
|
93
|
+
const state = readState(updateDir);
|
|
94
|
+
if (!forceBundled) {
|
|
95
|
+
if (args[0] !== "update") {
|
|
96
|
+
candidates = [state.active, state.previous]
|
|
97
|
+
.map(release => ({ binaryPath: releaseBinaryPath(updateDir, release), release }))
|
|
98
|
+
.filter(candidate => candidate.binaryPath);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!forceBundled && state.auto !== false && process.env.ZINIAO_CLI_AUTO_UPDATE !== "0"
|
|
102
|
+
&& !unsupportedBuild(updateDir)
|
|
103
|
+
&& process.env.ZINIAO_CLI_NO_UPDATE_CHECK !== "1" && args[0] !== "update"
|
|
104
|
+
&& !args.some(arg => ["--help", "-h", "--version", "-v", "version"].includes(arg))
|
|
105
|
+
&& !(Date.parse(state.next_check_at) > Date.now())) {
|
|
106
|
+
// 业务进程不继承下载进程的句柄。下载进程只写用户目录,进程锁在 Go 中处理。
|
|
107
|
+
const worker = spawn(executablePath(bundledBinaryPath), ["update", "--background"], {
|
|
108
|
+
env,
|
|
109
|
+
stdio: "ignore",
|
|
110
|
+
detached: true,
|
|
111
|
+
windowsHide: true,
|
|
112
|
+
});
|
|
113
|
+
worker.on("error", () => {});
|
|
114
|
+
worker.unref();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
candidates.push({ binaryPath: bundledBinaryPath });
|
|
118
|
+
|
|
119
|
+
for (const { binaryPath, release } of candidates) {
|
|
120
|
+
// 父进程在同步业务调用期间存活,清理器据此保护候选文件;异常退出的记录可自动回收。
|
|
121
|
+
const lease = release && join(updateDir, `in-use-${process.pid}-${release.directory}`);
|
|
122
|
+
let result;
|
|
123
|
+
let attempted = false;
|
|
124
|
+
try {
|
|
125
|
+
if (lease) {
|
|
126
|
+
try { writeFileSync(lease, "", { mode: 0o600 }); } catch {
|
|
127
|
+
// 记录失败不能改变业务选版;新版进程持有只读共享锁,旧版缺少锁文件时清理器保守保留。
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
if (release && existsSync(join(updateDir, `gc-${release.directory}`))) continue;
|
|
131
|
+
attempted = true;
|
|
132
|
+
result = spawnSync(executablePath(binaryPath), args, { env, stdio: "inherit", windowsHide: false });
|
|
133
|
+
} catch (error) {
|
|
134
|
+
// 仅处理真正的启动异常,已启动的业务请求仍不重放。
|
|
135
|
+
result = { error, pid: 0 };
|
|
136
|
+
} finally {
|
|
137
|
+
if (lease) {
|
|
138
|
+
try { unlinkSync(lease); } catch { /* 清理失败的记录由更新器检查进程是否存活。 */ }
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!result.error) {
|
|
142
|
+
// 被信号终止时返回失败,尤其不能在此时重试已启动的业务请求。
|
|
143
|
+
process.exit(result.status ?? (result.signal ? 1 : 0));
|
|
144
|
+
}
|
|
145
|
+
if (release && result.pid === 0) {
|
|
146
|
+
// 仅隔离文件缺失、格式或执行权限问题;资源不足等临时错误留待下次调用重试。
|
|
147
|
+
if (attempted && permanentLaunchFailure(result.error, binaryPath, release)) {
|
|
148
|
+
try {
|
|
149
|
+
writeFileSync(join(updateDir, `failed-${release.directory}`), "", { flag: "wx", mode: 0o600 });
|
|
150
|
+
} catch {
|
|
151
|
+
// 并发调用可能已记录失败;仍可尝试上一个已知可用版本。
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
if (binaryPath === bundledBinaryPath && !existsSync(bundledBinaryPath)) {
|
|
157
|
+
console.error(`ziniao-cli binary not found at ${bundledBinaryPath}. Set ZINIAO_CLI_BINARY before npm install for local testing.`);
|
|
158
|
+
} else {
|
|
159
|
+
console.error(result.error.message);
|
|
160
|
+
}
|
|
161
|
+
process.exit(1);
|
|
162
|
+
}
|