@kevlns/u-cli-mod 0.2.1 → 0.2.2

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/AGENTS.md CHANGED
@@ -28,7 +28,7 @@
28
28
  6. `exec` 每次调用前都会重新校验 CLI 哈希(防篡改),属正常行为;校验失败按提示跑 `u-cli-mod cli install --force` 修复即可。
29
29
  7. **读取 Editor 当前 Console 首选 `command read_console`**,`get_console_logs` 是兼容别名;`command console` 读取回调捕获流,适合 `since`/cursor 跟随,但不能代替原生 Console 快照。工具必须经 `command <名>` 调用。若实际 schema 中缺少 `read_console`,先检查 `doctor` 的 `patchVersion`/`installedPatchVersion` 和 `state`,不要将其误认成 `console` 的别名。
30
30
 
31
- 8. **长任务默认不阻塞**:`run_tests` 只等待 5 秒,到点后任务仍在 Editor 内继续执行,工具打印输出日志路径并立即返回(退出码 0)。此时**不要重复发起同一命令**,改用 `command test_status` 轮询结果;需要同步拿到完整 `Summary` 时传 `--wait <秒>`,并优先用 `--filter <命名空间或类名>` 缩小范围(通常数秒内即可同步返回)。
31
+ 8. **全量测试必须走 `--async_tests`**:Unity CLI 对同步命令有 30 秒硬性等待上限(`--timeout` 无法延长),同步 `run_tests` 全量必然超时且不入 `test_status` 跟踪。全量:`run_tests --mode EditMode --async_tests`(立即返回)→ 轮询 `test_status` 至 `completed` 拿完整 Summary;小范围用 `--filter` 缩到单命名空间/类,可直接同步等待拿 Summary。
32
32
  ## 典型流程
33
33
 
34
34
  ```bash
@@ -36,7 +36,7 @@ u-cli-mod doctor <project> # 1. 体检(只读)
36
36
  u-cli-mod setup <project> # 2. 就绪(CLI + 适配包)
37
37
  u-cli-mod exec <project> -- command editor_status # 3. 执行
38
38
  u-cli-mod exec <project> -- command run_tests --mode EditMode --filter <命名空间或类名> # 小范围:同步返回 Summary
39
- u-cli-mod exec <project> -- command run_tests --mode EditMode # 全量:5s 后让出,轮询 test_status
39
+ u-cli-mod exec <project> -- command run_tests --mode EditMode --async_tests # 全量:立即返回,轮询 test_status
40
40
  u-cli-mod exec <project> -- command read_console --types error,warning --count 100
41
41
  ```
42
42
 
@@ -8,9 +8,10 @@ export declare function findProjectPathOverride(args: string[]): string | null;
8
8
  export declare function buildExecArgs(cliArgs: string[], projectPath: string): string[];
9
9
  /**
10
10
  * Pipeline commands that block until the Editor-side work finishes.
11
- * The routed Unity CLI caps that wait at 30s, and its own `--timeout` is not wired
12
- * in the shipped build (verified: a full run_tests still waits ~30s with
13
- * `--timeout 1`), so the wait budget belongs to this wrapper.
11
+ * The routed Unity CLI caps that wait at 30s (`--timeout` is not wired in the
12
+ * shipped build), so the wait budget belongs to this wrapper; full-suite run_tests
13
+ * must instead use the pipeline-side `--async_tests` flag (immediate return +
14
+ * `test_status` polling).
14
15
  */
15
16
  export declare const LONG_RUNNING_PIPELINE_COMMANDS: ReadonlySet<string>;
16
17
  /**
@@ -39,6 +40,15 @@ export declare function extractWaitOption(args: string[]): {
39
40
  };
40
41
  /** Exec output log of a handed-back run: project-scoped, beside the install receipts. */
41
42
  export declare function execLogPath(projectPath: string, commandName: string | null, now?: Date): string;
43
+ /**
44
+ * Command line for a handed-back exec, written into a temp .cmd batch: the batch
45
+ * owns the output redirection and is spawned as a single clean path argument, so
46
+ * the CLI outlives the wrapper process (numeric-fd stdio kills the detached child
47
+ * on Windows, and Node's quoting of an inline quoted command string breaks cmd).
48
+ */
49
+ export declare function buildDetachedExecCommand(cliPath: string, args: string[], logPath: string): string;
50
+ /** Temp batch path for a handed-back exec: wrapper plumbing, never project state. */
51
+ export declare function detachedExecBatchPath(logPath: string, tmpDir?: string): string;
42
52
  export interface ExecOptions {
43
53
  downloadIfMissing?: boolean;
44
54
  waitSeconds?: number;
@@ -1,6 +1,7 @@
1
1
  import { spawn, spawnSync } from 'node:child_process';
2
- import { closeSync, mkdirSync, openSync, readFileSync } from 'node:fs';
3
- import { dirname, join } from 'node:path';
2
+ import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';
3
+ import { basename, dirname, join } from 'node:path';
4
+ import { tmpdir } from 'node:os';
4
5
  import { readProjectInfo } from '../projectVersion.js';
5
6
  import { resolveRouteForProject } from '../routes.js';
6
7
  import { verifyCliBinary, ensureCliBinary } from '../ensure.js';
@@ -25,9 +26,10 @@ export function buildExecArgs(cliArgs, projectPath) {
25
26
  }
26
27
  /**
27
28
  * Pipeline commands that block until the Editor-side work finishes.
28
- * The routed Unity CLI caps that wait at 30s, and its own `--timeout` is not wired
29
- * in the shipped build (verified: a full run_tests still waits ~30s with
30
- * `--timeout 1`), so the wait budget belongs to this wrapper.
29
+ * The routed Unity CLI caps that wait at 30s (`--timeout` is not wired in the
30
+ * shipped build), so the wait budget belongs to this wrapper; full-suite run_tests
31
+ * must instead use the pipeline-side `--async_tests` flag (immediate return +
32
+ * `test_status` polling).
31
33
  */
32
34
  export const LONG_RUNNING_PIPELINE_COMMANDS = new Set(['run_tests']);
33
35
  /**
@@ -104,6 +106,25 @@ export function execLogPath(projectPath, commandName, now = new Date()) {
104
106
  const suffix = commandName === null ? '' : `-${commandName}`;
105
107
  return join(projectPath, 'Library', 'editor-pipeline-cli', 'exec-logs', `${stamp}${suffix}.log`);
106
108
  }
109
+ /** Quotes one token for a cmd.exe command line; embedded quotes double up. */
110
+ function quoteForCmd(value) {
111
+ return `"${value.replace(/"/g, '""')}"`;
112
+ }
113
+ /**
114
+ * Command line for a handed-back exec, written into a temp .cmd batch: the batch
115
+ * owns the output redirection and is spawned as a single clean path argument, so
116
+ * the CLI outlives the wrapper process (numeric-fd stdio kills the detached child
117
+ * on Windows, and Node's quoting of an inline quoted command string breaks cmd).
118
+ */
119
+ export function buildDetachedExecCommand(cliPath, args, logPath) {
120
+ const cli = `${quoteForCmd(cliPath)} ${args.map(quoteForCmd).join(' ')}`;
121
+ return `${cli} > ${quoteForCmd(logPath)} 2>&1`;
122
+ }
123
+ /** Temp batch path for a handed-back exec: wrapper plumbing, never project state. */
124
+ export function detachedExecBatchPath(logPath, tmpDir = tmpdir()) {
125
+ const base = basename(logPath).replace(/\.log$/, '.cmd');
126
+ return join(tmpDir, `u-cli-mod-exec-${base}`);
127
+ }
107
128
  export async function runExec(projectPath, cliArgs, options = {}) {
108
129
  const project = readProjectInfo(projectPath);
109
130
  const route = resolveRouteForProject(project);
@@ -112,7 +133,6 @@ export async function runExec(projectPath, cliArgs, options = {}) {
112
133
  throw new CliError(`exec 统一管理 --project-path,禁止在参数中覆盖目标工程:${override}`);
113
134
  }
114
135
  let cliPath = cliBinaryPath(route.cli);
115
- const { existsSync } = await import('node:fs');
116
136
  if (!existsSync(cliPath)) {
117
137
  if (!options.downloadIfMissing) {
118
138
  throw new CliError(`该路由的 CLI 尚未下载:${cliPath}。请先运行 "u-cli-mod cli install",或使用 --download-if-missing。`);
@@ -135,25 +155,25 @@ export async function runExec(projectPath, cliArgs, options = {}) {
135
155
  }
136
156
  /**
137
157
  * Runs the CLI under a wait budget. When the budget expires the child keeps running
138
- * (the Editor-side work is independent of it) and the wrapper hands control back
139
- * with the output log path, so callers can poll the matching status command.
158
+ * (cmd owns the redirection, so the CLI survives the wrapper) and the wrapper hands
159
+ * control back with the output log path, so callers can poll the matching status command.
140
160
  */
141
161
  async function runWithWaitBudget(cliPath, args, projectPath, waitMs, commandName) {
142
162
  const logPath = execLogPath(projectPath, commandName);
143
163
  mkdirSync(dirname(logPath), { recursive: true });
144
- const logFd = openSync(logPath, 'a');
145
- const child = spawn(cliPath, args, { stdio: ['ignore', logFd, logFd], windowsHide: true });
164
+ const batchPath = detachedExecBatchPath(logPath);
165
+ writeFileSync(batchPath, `@${buildDetachedExecCommand(cliPath, args, logPath)}\r\n`);
166
+ const child = spawn('cmd.exe', ['/d', '/c', batchPath], { stdio: 'ignore', windowsHide: true });
146
167
  const spawnState = { error: null };
147
168
  child.once('error', (error) => {
148
169
  spawnState.error = error;
149
170
  });
150
171
  const finished = await waitForExit(child, waitMs);
151
- closeSync(logFd);
152
172
  if (finished) {
153
173
  if (spawnState.error !== null) {
154
174
  throw new CliError(`CLI 执行失败:${spawnState.error.message}`);
155
175
  }
156
- process.stdout.write(readFileSync(logPath, 'utf8'));
176
+ process.stdout.write(existsSync(logPath) ? readFileSync(logPath, 'utf8') : '');
157
177
  return { exitCode: child.exitCode ?? 1 };
158
178
  }
159
179
  child.unref();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kevlns/u-cli-mod",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "u-cli-mod: pin exact Unity Editor version routes and install the adapted com.unity.pipeline package for Unity 2022 (Windows-first). Downloads Unity assets at runtime into the user cache; the package itself contains no Unity binaries or package source. Not an official Unity project.",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -68,4 +68,4 @@
68
68
  "vitest": "^3.2.7"
69
69
  },
70
70
  "license": "MIT"
71
- }
71
+ }