@boyingliu01/xp-gate 0.41.67 → 0.41.69

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/bin/xp-gate.js CHANGED
@@ -144,36 +144,60 @@ function runAlignmentViaNpx(runnerPath, specPath, testDirs, outputPath, jsonMode
144
144
  return result.status === 0 ? 0 : 1;
145
145
  }
146
146
 
147
- async function runAlignmentCheck(subargs) {
148
- const specIdx = subargs.indexOf('--spec');
149
- const specPath = specIdx >= 0 ? subargs[specIdx + 1] : path.join(process.cwd(), 'specification.yaml');
150
-
151
- const testsIdx = subargs.indexOf('--tests');
152
- const testDirs = testsIdx >= 0 ? [subargs[testsIdx + 1]] : undefined;
153
-
154
- const outputIdx = subargs.indexOf('--output');
155
- const outputPath = outputIdx >= 0 ? subargs[outputIdx + 1] : undefined;
156
-
157
- const jsonMode = subargs.includes('--json');
147
+ /**
148
+ * Parse check-alignment's flags.
149
+ *
150
+ * Extracted from runAlignmentCheck, which measured 63 lines against a 50-line
151
+ * rule. The flags are one independent question; the injection guard and the
152
+ * loader dance are two more.
153
+ *
154
+ * @param {string[]} subargs
155
+ * @returns {{specPath: string, testDirs: string[]|undefined, outputPath: string|undefined, jsonMode: boolean}}
156
+ */
157
+ function parseAlignmentArgs(subargs) {
158
+ const pick = (flag) => {
159
+ const i = subargs.indexOf(flag);
160
+ return i >= 0 ? subargs[i + 1] : undefined;
161
+ };
162
+ const testsValue = pick('--tests');
163
+ return {
164
+ specPath: pick('--spec') ?? path.join(process.cwd(), 'specification.yaml'),
165
+ testDirs: testsValue === undefined ? undefined : [testsValue],
166
+ outputPath: pick('--output'),
167
+ jsonMode: subargs.includes('--json'),
168
+ };
169
+ }
158
170
 
159
- // Injection guard (#392-#6) runs BEFORE any launcher, including the
160
- // in-process fast path: a metachar path must be refused identically on both
161
- // platforms (Delphi walkthrough R1, MJ-02 — behavioral, not shape-only).
162
- for (const value of [specPath, ...(testDirs || []), outputPath].filter(v => v !== undefined)) {
171
+ /**
172
+ * Refuse a path containing a shell metacharacter (#392-#6 / Delphi R1 MJ-02).
173
+ *
174
+ * Runs BEFORE any launcher, including the in-process fast path, so both
175
+ * platforms refuse identically. Behavioural, not shape-only.
176
+ *
177
+ * @param {{specPath: string, testDirs?: string[], outputPath?: string}} args
178
+ * @returns {1|null} exit code when refused
179
+ */
180
+ function refuseUnsafeAlignmentPath({ specPath, testDirs, outputPath }) {
181
+ for (const value of [specPath, ...(testDirs || []), outputPath].filter((v) => v !== undefined)) {
163
182
  const unsafe = firstUnsafeArg([value]);
164
183
  if (unsafe) {
165
184
  console.error(`check-alignment: refusing path containing a shell metacharacter: ${unsafe}`);
166
185
  return 1;
167
186
  }
168
187
  }
188
+ return null;
189
+ }
190
+
191
+ async function runAlignmentCheck(subargs) {
192
+ const args = parseAlignmentArgs(subargs);
193
+ const refused = refuseUnsafeAlignmentPath(args);
194
+ if (refused !== null) return refused;
169
195
 
170
196
  const runnerPath = path.join(__dirname, '..', 'lib', 'check-alignment-runner.cjs');
171
197
 
172
- // #894: the runner is referenced by PATH, so a packaging mistake turns this
173
- // command into a bare ERR_MODULE_NOT_FOUND stack trace -- which shipped in every
174
- // published version before 0.41.10. A gate that cannot start must say so in the
175
- // same voice as the others: what is missing, and what the operator can do about
176
- // it. "Cannot find module" from inside a tsx loader names neither.
198
+ // #894: the runner is referenced by PATH, so a packaging mistake turned this
199
+ // command into a bare ERR_MODULE_NOT_FOUND stack trace, shipped in every
200
+ // version before 0.41.10. A gate that cannot start must say what is missing.
177
201
  if (!require('node:fs').existsSync(runnerPath)) {
178
202
  console.error('❌ BLOCKED - check-alignment: the alignment runner is missing from this install.');
179
203
  console.error(` Expected: ${runnerPath}`);
@@ -182,19 +206,13 @@ async function runAlignmentCheck(subargs) {
182
206
  return 1;
183
207
  }
184
208
 
185
- // Primary: run the engine in-process via tsx/cjs (fast path, works in-repo
186
- // where tsx is a devDependency). Fall back to `npx -y tsx` when tsx is not
187
- // resolvable, which happens in the published global install (the package
188
- // ships zero runtime deps per the zero-install contract). npx self-provisions
189
- // tsx on demand, so the published command stays functional without adding a dep.
209
+ // Primary: run the engine in-process via tsx/cjs (fast path, works in-repo).
210
+ // Fall back to `npx -y tsx` when tsx is not resolvable, which is the published
211
+ // global install (the package ships zero runtime deps).
190
212
  //
191
- // The try guards ONLY the tsx loader's module-load step. The engine call sits
192
- // outside it: an engine-level failure is a real bug that must surface, not be
193
- // silently swallowed and misattributed to "tsx unavailable" by the fallback.
194
- // Delphi walkthrough R2 CI-02: require('../lib/test-alignment.ts') also needs
195
- // the loader, so it belongs INSIDE the guard — if the .ts file itself cannot
196
- // load, the npx fallback re-attempts it under a provisioned tsx instead of
197
- // crashing with an unhandled exception. Engine EXECUTION stays outside.
213
+ // The try guards ONLY the module-LOAD step. The engine call sits outside it: an
214
+ // engine-level failure is a real bug that must surface, not be misattributed to
215
+ // "tsx unavailable" (Delphi R2 CI-02).
198
216
  let runFullAlignment;
199
217
  try {
200
218
  require('tsx/cjs');
@@ -203,9 +221,9 @@ async function runAlignmentCheck(subargs) {
203
221
  if (!err || (err.code !== 'MODULE_NOT_FOUND' && !/Cannot find module/.test(String(err && err.message)))) {
204
222
  throw err;
205
223
  }
206
- return runAlignmentViaNpx(runnerPath, specPath, testDirs, outputPath, jsonMode);
224
+ return runAlignmentViaNpx(runnerPath, args.specPath, args.testDirs, args.outputPath, args.jsonMode);
207
225
  }
208
- return runAlignmentInProcess(specPath, testDirs, outputPath, jsonMode, runFullAlignment);
226
+ return runAlignmentInProcess(args.specPath, args.testDirs, args.outputPath, args.jsonMode, runFullAlignment);
209
227
  }
210
228
 
211
229
  const COMMANDS = {