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

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,6 +1,6 @@
1
1
  {
2
2
  "name": "@ziniao-open/cli",
3
- "version": "0.1.0-beta.182",
3
+ "version": "0.1.0-beta.186",
4
4
  "description": "紫鸟开放平台命令行工具,为开发者和 AI Agent 提供统一的接口调用入口。",
5
5
  "license": "UNLICENSED",
6
6
  "bin": {
@@ -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,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 { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers } = require("./legacy-cli");
40
+ const { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers, scheduleLegacyVoltaCleanup } = require("./legacy-cli");
41
41
 
42
42
  const PKG = require("../package.json");
43
43
  const REPO_ROOT = join(__dirname, "..");
@@ -302,12 +302,12 @@ async function main() {
302
302
  await downloadAndInstall();
303
303
  }
304
304
 
305
- cleanLegacyComponents();
305
+ const pendingCleanup = await cleanLegacyComponents();
306
306
  runDesktopCliSetup();
307
- reportLegacyCLI();
307
+ reportLegacyCLI(pendingCleanup);
308
308
  }
309
309
 
310
- function cleanLegacyComponents() {
310
+ async function cleanLegacyComponents() {
311
311
  const scope = npmScope();
312
312
  if (!scope) return; // Automatic migration is limited to global npm installs.
313
313
  const args = ["skills", "cleanup-legacy"];
@@ -326,13 +326,17 @@ function cleanLegacyComponents() {
326
326
  if (oldPackageRemoved) log("已清理旧 npm 包: @ziniao-agent/cli");
327
327
  const removed = cleanupLegacyLaunchers({ binDir: scope.binDir });
328
328
  for (const path of removed) log(`已清理旧入口: ${path}`);
329
- log(`旧版清理完成:旧技能 ${report.data.removed.length} 个,旧 npm 包 ${oldPackageRemoved ? 1 : 0} 个,残留入口 ${removed.length} 个。`);
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;
330
333
  }
331
334
 
332
- function reportLegacyCLI() {
335
+ function reportLegacyCLI(pendingCleanup) {
333
336
  try {
334
337
  const paths = findLegacyCLI();
335
338
  for (const path of paths) {
339
+ if (pendingCleanup && relative(pendingCleanup.binDir, dirname(path)) === "") continue;
336
340
  warn(`其他安装范围仍有旧入口,本次未清理: ${path}`);
337
341
  }
338
342
  } catch (err) {
@@ -1,6 +1,9 @@
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");
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";
4
7
 
5
8
  function legacyNames(platform) {
6
9
  return platform === "win32"
@@ -59,6 +62,93 @@ function uninstallLegacyPackage({ scope, env = process.env }) {
59
62
  return true;
60
63
  }
61
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
+
62
152
  // Delete fixed retired command names in this npm scope. Unlinking a symlink
63
153
  // removes its entry only; its target and other npm scopes are left intact.
64
154
  function cleanupLegacyLaunchers({ binDir, platform = process.platform }) {
@@ -82,4 +172,8 @@ function cleanupLegacyLaunchers({ binDir, platform = process.platform }) {
82
172
  return removed;
83
173
  }
84
174
 
85
- module.exports = { findLegacyCLI, npmScope, uninstallLegacyPackage, cleanupLegacyLaunchers };
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
+ }