@prisma/cli-engine 0.0.7 → 0.0.9
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/dist/index.d.ts +54 -3
- package/dist/index.js +165 -3
- package/dist/{protocol-C1JQcGl9.js → protocol-D1CRNkhv.js} +25 -4
- package/dist/{protocol-CpZLdxML.d.ts → protocol-YjqPCCOG.d.ts} +23 -3
- package/dist/protocol.d.ts +2 -2
- package/dist/protocol.js +2 -2
- package/dist/{run-summary-DQx6CRvB.d.ts → report-Bh6-ntIa.d.ts} +269 -74
- package/dist/testing.d.ts +31 -2
- package/dist/testing.js +52 -26
- package/dist/{token-claims-DZ3p13Qu.js → token-claims-CGQ-5m9N.js} +1101 -164
- package/package.json +5 -2
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as okVoid, r as notOk, t as CliStructuredError } from "./protocol-D1CRNkhv.js";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { FlagNotFoundError, buildApplication, buildCommand, buildRouteMap, run, text_en } from "@stricli/core";
|
|
4
|
-
import {
|
|
4
|
+
import { randomUUID } from "node:crypto";
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, realpathSync, renameSync, writeFileSync } from "node:fs";
|
|
5
6
|
import { dirname, join, resolve } from "node:path";
|
|
7
|
+
import { detect } from "package-manager-detector/detect";
|
|
8
|
+
import { createColors } from "colorette";
|
|
9
|
+
import stringWidth from "string-width";
|
|
6
10
|
import { Readable, Writable } from "node:stream";
|
|
7
11
|
import { Buffer } from "node:buffer";
|
|
8
12
|
//#region src/args.ts
|
|
@@ -153,6 +157,455 @@ function isChildStatusSettlement(value) {
|
|
|
153
157
|
return typeof value === "object" && value !== null && value[CHILD_STATUS] === true;
|
|
154
158
|
}
|
|
155
159
|
//#endregion
|
|
160
|
+
//#region src/telemetry/endpoint.ts
|
|
161
|
+
/**
|
|
162
|
+
* Production endpoint pinned to the deployed Prisma Compute backend.
|
|
163
|
+
* Compiled as a build-time constant; not user-configurable.
|
|
164
|
+
*/
|
|
165
|
+
const TELEMETRY_BACKEND_URL = "https://cmpbfbsdp09hr3jf7pojjs5qs.ewr.prisma.build";
|
|
166
|
+
/**
|
|
167
|
+
* Path within the backend that accepts telemetry POSTs.
|
|
168
|
+
*/
|
|
169
|
+
const TELEMETRY_ENDPOINT_PATH = "/events";
|
|
170
|
+
/**
|
|
171
|
+
* Resolve the full POST URL the sender targets. The
|
|
172
|
+
* `PRISMA_TELEMETRY_ENDPOINT` env var is an integration-testing
|
|
173
|
+
* affordance only — it lets a test suite spin up a mock HTTP server on an
|
|
174
|
+
* ephemeral port and point the spawned sender at it. The override is
|
|
175
|
+
* intentionally undocumented in user-facing material.
|
|
176
|
+
*
|
|
177
|
+
* Fail-open: a malformed override (typo in a dev shell, bad CI config)
|
|
178
|
+
* silently falls back to the production backend rather than throwing,
|
|
179
|
+
* matching the telemetry layer's broader silent-on-failure contract.
|
|
180
|
+
*/
|
|
181
|
+
function resolveTelemetryEndpoint(env) {
|
|
182
|
+
const override = env.PRISMA_TELEMETRY_ENDPOINT;
|
|
183
|
+
const base = override !== void 0 && override.length > 0 ? override : TELEMETRY_BACKEND_URL;
|
|
184
|
+
try {
|
|
185
|
+
return new URL(TELEMETRY_ENDPOINT_PATH, base).toString();
|
|
186
|
+
} catch {
|
|
187
|
+
return new URL(TELEMETRY_ENDPOINT_PATH, TELEMETRY_BACKEND_URL).toString();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/telemetry/gating.ts
|
|
192
|
+
/**
|
|
193
|
+
* A `PRISMA_DISABLE_TELEMETRY` value counts as an opt-out only if it
|
|
194
|
+
* parses as a truthy string. The set-but-falsy spellings (`''`, `'0'`,
|
|
195
|
+
* `'false'`) are intentionally treated as not-set so a parent shell that
|
|
196
|
+
* exports the variable to a benign value doesn't accidentally disable
|
|
197
|
+
* telemetry for child processes.
|
|
198
|
+
*/
|
|
199
|
+
function isTruthyOptOut(raw) {
|
|
200
|
+
if (raw === void 0) return false;
|
|
201
|
+
const normalised = raw.trim().toLowerCase();
|
|
202
|
+
if (normalised === "") return false;
|
|
203
|
+
if (normalised === "0") return false;
|
|
204
|
+
if (normalised === "false") return false;
|
|
205
|
+
return true;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Pure-function resolution of the gating decision. Same input → same
|
|
209
|
+
* output; no I/O. The caller reads the env, the user config and the CI
|
|
210
|
+
* signal.
|
|
211
|
+
*
|
|
212
|
+
* Decision order:
|
|
213
|
+
* 1. CI (`inCI`) → disabled (`ci`). CI environments never emit,
|
|
214
|
+
* regardless of any stored consent.
|
|
215
|
+
* 2. Env-var override (`PRISMA_DISABLE_TELEMETRY` truthy, or
|
|
216
|
+
* `DO_NOT_TRACK=1`) → disabled (`env-opt-out`), winning over any
|
|
217
|
+
* stored or unset preference.
|
|
218
|
+
* 3. Stored `enableTelemetry === false` → disabled (`stored-opt-out`).
|
|
219
|
+
* 4. Stored `enableTelemetry === true` → enabled (`stored-opt-in`).
|
|
220
|
+
* 5. Stored `enableTelemetry === undefined` (file missing, or field
|
|
221
|
+
* not set) → ENABLED (`default-on`). This is the opt-out default:
|
|
222
|
+
* absence of an explicit choice means telemetry is on. This branch
|
|
223
|
+
* carries the whole opt-out model — do not "fix" it to default-off.
|
|
224
|
+
*/
|
|
225
|
+
function resolveGating(inputs) {
|
|
226
|
+
if (inputs.inCI) return {
|
|
227
|
+
enabled: false,
|
|
228
|
+
reason: "ci"
|
|
229
|
+
};
|
|
230
|
+
if (isTruthyOptOut(inputs.env.PRISMA_DISABLE_TELEMETRY) || inputs.env.DO_NOT_TRACK === "1") return {
|
|
231
|
+
enabled: false,
|
|
232
|
+
reason: "env-opt-out"
|
|
233
|
+
};
|
|
234
|
+
if (inputs.config.enableTelemetry === false) return {
|
|
235
|
+
enabled: false,
|
|
236
|
+
reason: "stored-opt-out"
|
|
237
|
+
};
|
|
238
|
+
if (inputs.config.enableTelemetry === true) return {
|
|
239
|
+
enabled: true,
|
|
240
|
+
reason: "stored-opt-in"
|
|
241
|
+
};
|
|
242
|
+
return {
|
|
243
|
+
enabled: true,
|
|
244
|
+
reason: "default-on"
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/telemetry/payload.ts
|
|
249
|
+
/**
|
|
250
|
+
* Project the parse-time snapshot into the wire-shape command and flag
|
|
251
|
+
* names. Pure; the snapshot's fields are the only inputs.
|
|
252
|
+
*
|
|
253
|
+
* No flag values, no positionals, no raw argv:
|
|
254
|
+
* - The wire ships `telemetry status`, never the binary name (the
|
|
255
|
+
* snapshot's commandPath already excludes it).
|
|
256
|
+
* - Only flags whose source is `cli` survive — defaulted and
|
|
257
|
+
* env-sourced flags say nothing about what the user typed.
|
|
258
|
+
* - Flag names ship in the user-facing kebab-case spelling the engine
|
|
259
|
+
* recorded; nothing is renamed or filtered per command.
|
|
260
|
+
* - `positionalCount` is accepted and never read.
|
|
261
|
+
*/
|
|
262
|
+
function sanitizeCommandSnapshot(snapshot) {
|
|
263
|
+
return {
|
|
264
|
+
command: snapshot.commandPath.join(" "),
|
|
265
|
+
flags: snapshot.flags.flatMap((flag) => flag.source === "cli" ? [flag.name] : [])
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* The whole payload, from the run's identity plus its snapshot. The
|
|
270
|
+
* snapshot reaches the payload only through
|
|
271
|
+
* {@link sanitizeCommandSnapshot}, so no field of it can arrive by
|
|
272
|
+
* another route. `databaseTarget` is left unset — see its doc-block.
|
|
273
|
+
*/
|
|
274
|
+
function composeTelemetryPayload(inputs) {
|
|
275
|
+
return {
|
|
276
|
+
installationId: inputs.installationId,
|
|
277
|
+
version: inputs.version,
|
|
278
|
+
projectRoot: inputs.projectRoot,
|
|
279
|
+
endpoint: inputs.endpoint,
|
|
280
|
+
...sanitizeCommandSnapshot(inputs.snapshot)
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/telemetry/user-config.ts
|
|
285
|
+
const APP_DIR = "prisma";
|
|
286
|
+
const FILE_NAME = "config.json";
|
|
287
|
+
function set(raw) {
|
|
288
|
+
return raw !== void 0 && raw.length > 0 ? raw : void 0;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Resolves the user-level config directory:
|
|
292
|
+
* - Windows: `%APPDATA%\prisma\` (fallback: `%USERPROFILE%\AppData\Roaming\prisma\`).
|
|
293
|
+
* - Unix (incl. macOS): `$XDG_CONFIG_HOME/prisma/` if set, else
|
|
294
|
+
* `$HOME/.config/prisma/` per the XDG Base Directory Specification.
|
|
295
|
+
*
|
|
296
|
+
* XDG is chosen over the macOS-native `~/Library/Preferences/`
|
|
297
|
+
* convention so the path resolution follows an environment variable and
|
|
298
|
+
* matches the documented behaviour on all *nix platforms. The directory
|
|
299
|
+
* is `prisma`, after the binary: the `prisma-next` name it carried is
|
|
300
|
+
* retired with that binary, and nothing reads the old location — no
|
|
301
|
+
* fallback, no migration.
|
|
302
|
+
*
|
|
303
|
+
* EVERY variable comes from the invocation's env — the engine reads no
|
|
304
|
+
* process globals, and `os.homedir()` is one: it answers from the
|
|
305
|
+
* process's own `$HOME`, not the invocation's. An env that names none of
|
|
306
|
+
* them resolves to `undefined`: the preference store is unavailable, and
|
|
307
|
+
* the engine then reads nothing, writes nothing and reports nothing.
|
|
308
|
+
* Failing closed is the only safe direction for a privacy feature —
|
|
309
|
+
* `runtime.env` is `process.env` in production, where `HOME` (or
|
|
310
|
+
* `USERPROFILE`) is always set, so this is a test-shaped state.
|
|
311
|
+
*
|
|
312
|
+
* `process.platform` stays: it does not vary with an invocation and is
|
|
313
|
+
* not modelled on the Runtime.
|
|
314
|
+
*/
|
|
315
|
+
function configDir(env) {
|
|
316
|
+
if (process.platform === "win32") {
|
|
317
|
+
const appData = set(env.APPDATA);
|
|
318
|
+
if (appData !== void 0) return join(appData, APP_DIR);
|
|
319
|
+
const userProfile = set(env.USERPROFILE);
|
|
320
|
+
return userProfile === void 0 ? void 0 : join(userProfile, "AppData", "Roaming", APP_DIR);
|
|
321
|
+
}
|
|
322
|
+
const xdg = set(env.XDG_CONFIG_HOME);
|
|
323
|
+
if (xdg !== void 0) return join(xdg, APP_DIR);
|
|
324
|
+
const home = set(env.HOME);
|
|
325
|
+
return home === void 0 ? void 0 : join(home, ".config", APP_DIR);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Path to the user-level config file for this invocation, or `undefined`
|
|
329
|
+
* when the environment says nothing about where the user's config
|
|
330
|
+
* directory is. Callers treat `undefined` as "the preference store is
|
|
331
|
+
* unavailable".
|
|
332
|
+
*/
|
|
333
|
+
function userConfigPath(env) {
|
|
334
|
+
const dir = configDir(env);
|
|
335
|
+
return dir === void 0 ? void 0 : join(dir, FILE_NAME);
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Reads the user-level config. File-missing, unreadable, or malformed →
|
|
339
|
+
* `{}` (the absence of consent is the same answer in every error mode).
|
|
340
|
+
* Unknown fields from a future client are passed through verbatim.
|
|
341
|
+
*/
|
|
342
|
+
function readUserConfig(env) {
|
|
343
|
+
const path = userConfigPath(env);
|
|
344
|
+
if (path === void 0 || !existsSync(path)) return {};
|
|
345
|
+
try {
|
|
346
|
+
const raw = readFileSync(path, "utf-8");
|
|
347
|
+
const parsed = JSON.parse(raw);
|
|
348
|
+
if (parsed !== null && typeof parsed === "object" && !Array.isArray(parsed)) return parsed;
|
|
349
|
+
return {};
|
|
350
|
+
} catch {
|
|
351
|
+
return {};
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Merges `partial` into the current config and writes the result
|
|
356
|
+
* atomically (temp file + rename) so a crash mid-write never leaves a
|
|
357
|
+
* half-baked file readable on disk. Unknown fields already on disk are
|
|
358
|
+
* preserved.
|
|
359
|
+
*
|
|
360
|
+
* When `partial.enableTelemetry === true` and no `installationId` is
|
|
361
|
+
* stored yet, generates a v4 random UUID and persists both fields in the
|
|
362
|
+
* same write. An existing `installationId` is never rotated. This is the
|
|
363
|
+
* *explicit-consent* mint path: a `false` answer
|
|
364
|
+
* (`writeUserConfig({ enableTelemetry: false })`) writes no id, and a
|
|
365
|
+
* bare `writeUserConfig({ installationId })` mints nothing extra. The
|
|
366
|
+
* default-on first-send path mints its id separately via
|
|
367
|
+
* {@link ensureInstallationId}, which records no consent answer.
|
|
368
|
+
*/
|
|
369
|
+
function writeUserConfig(env, partial) {
|
|
370
|
+
const merged = {
|
|
371
|
+
...readUserConfig(env),
|
|
372
|
+
...partial
|
|
373
|
+
};
|
|
374
|
+
if (partial.enableTelemetry === true && merged.installationId === void 0) merged.installationId = randomUUID();
|
|
375
|
+
const path = userConfigPath(env);
|
|
376
|
+
if (path === void 0) throw new Error("@prisma/cli-engine: cannot resolve the user config directory — the environment sets none of XDG_CONFIG_HOME, HOME, APPDATA or USERPROFILE");
|
|
377
|
+
const dir = dirname(path);
|
|
378
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
379
|
+
const tmpPath = `${path}.${process.pid}.tmp`;
|
|
380
|
+
writeFileSync(tmpPath, `${JSON.stringify(merged, null, 2)}\n`, "utf-8");
|
|
381
|
+
renameSync(tmpPath, path);
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Returns the stored `installationId`, minting and persisting a fresh v4
|
|
385
|
+
* UUID when none exists yet. Crucially, this persists *only* the id —
|
|
386
|
+
* `enableTelemetry` is left untouched (stays `undefined` on a default-on
|
|
387
|
+
* first run), so no explicit consent the user never gave is recorded.
|
|
388
|
+
*
|
|
389
|
+
* Used by the default-on first-run fire path: the gating resolution has
|
|
390
|
+
* already come back enabled, so this only ever runs when telemetry is on.
|
|
391
|
+
*/
|
|
392
|
+
function ensureInstallationId(env) {
|
|
393
|
+
const existing = readUserConfig(env).installationId;
|
|
394
|
+
if (typeof existing === "string" && existing.length > 0) return existing;
|
|
395
|
+
const installationId = randomUUID();
|
|
396
|
+
writeUserConfig(env, { installationId });
|
|
397
|
+
return installationId;
|
|
398
|
+
}
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/telemetry/report.ts
|
|
401
|
+
/**
|
|
402
|
+
* The command path the whole telemetry surface is exempt from. Running
|
|
403
|
+
* `telemetry disable` must not report a usage event on its way to
|
|
404
|
+
* disabling, and `telemetry status` must not mint an id while merely
|
|
405
|
+
* reporting state.
|
|
406
|
+
*/
|
|
407
|
+
const EXEMPT_COMMAND = "telemetry";
|
|
408
|
+
/**
|
|
409
|
+
* The one-time first-run disclosure. `<name> telemetry disable` is named
|
|
410
|
+
* as the primary, friendliest opt-out, alongside the environment
|
|
411
|
+
* variables. The config file is not named and hand-editing it is not
|
|
412
|
+
* suggested: the preference is machine-edited, which is what the
|
|
413
|
+
* telemetry commands are for, and `telemetry status` reports the path
|
|
414
|
+
* for anyone who wants it.
|
|
415
|
+
*/
|
|
416
|
+
function firstRunNotice(cliName, docsUrl) {
|
|
417
|
+
return [
|
|
418
|
+
"Prisma collects anonymous CLI usage data, enabled by default.",
|
|
419
|
+
`What's collected and why: ${docsUrl}.`,
|
|
420
|
+
`Opt out: run "${cliName} telemetry disable", set DO_NOT_TRACK=1 or`,
|
|
421
|
+
"PRISMA_DISABLE_TELEMETRY=1."
|
|
422
|
+
].join(" ");
|
|
423
|
+
}
|
|
424
|
+
/**
|
|
425
|
+
* The first enabled run on this machine: disclose, then mint. Both are
|
|
426
|
+
* best-effort. A failed write returns no id, which skips the event
|
|
427
|
+
* rather than sending a junk one, and leaves the notice to print again
|
|
428
|
+
* next run — the stored id is what makes it print exactly once.
|
|
429
|
+
*/
|
|
430
|
+
function discloseAndMint(inputs) {
|
|
431
|
+
const { host } = inputs;
|
|
432
|
+
try {
|
|
433
|
+
host.stderr.write(`${firstRunNotice(inputs.name, inputs.telemetry.docsUrl)}\n`);
|
|
434
|
+
} catch {}
|
|
435
|
+
try {
|
|
436
|
+
return ensureInstallationId(host.env);
|
|
437
|
+
} catch {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Report one run, at command start — before the handler, from the
|
|
443
|
+
* parse-time snapshot. Exempt the telemetry command, resolve gating,
|
|
444
|
+
* disclose and mint on a first enabled run, compose, hand the payload to
|
|
445
|
+
* the host's seam. The engine composes; the host spawns.
|
|
446
|
+
*
|
|
447
|
+
* A host with no seam is turned away first, before the config is even
|
|
448
|
+
* read: a CLI that cannot deliver an event must not tell the user it
|
|
449
|
+
* collects data, and must not mint an installation id it has no use
|
|
450
|
+
* for. Declaring telemetry and wiring the seam are two halves of one
|
|
451
|
+
* decision; either one missing means the run reports nothing. An
|
|
452
|
+
* environment that says nothing about where the user's config lives is
|
|
453
|
+
* turned away at the same point and for the same reason.
|
|
454
|
+
*
|
|
455
|
+
* Every failure is swallowed: a malformed stored config, an unwritable
|
|
456
|
+
* config directory and a throwing seam all leave the run's exit code,
|
|
457
|
+
* stdout and stderr exactly as they would have been.
|
|
458
|
+
*/
|
|
459
|
+
function reportCommandStart(inputs) {
|
|
460
|
+
try {
|
|
461
|
+
const { host, snapshot } = inputs;
|
|
462
|
+
const deliver = host.spawnTelemetry;
|
|
463
|
+
const configPath = userConfigPath(host.env);
|
|
464
|
+
if (deliver === void 0 || configPath === void 0 || snapshot.commandPath[0] === EXEMPT_COMMAND) return;
|
|
465
|
+
const config = readUserConfig(host.env);
|
|
466
|
+
if (!resolveGating({
|
|
467
|
+
env: host.env,
|
|
468
|
+
config,
|
|
469
|
+
inCI: host.isCI
|
|
470
|
+
}).enabled) return;
|
|
471
|
+
const stored = config.installationId;
|
|
472
|
+
const installationId = typeof stored === "string" && stored.length > 0 ? stored : discloseAndMint(inputs);
|
|
473
|
+
if (installationId === void 0) return;
|
|
474
|
+
deliver(composeTelemetryPayload({
|
|
475
|
+
installationId,
|
|
476
|
+
version: inputs.version,
|
|
477
|
+
projectRoot: host.cwd,
|
|
478
|
+
endpoint: resolveTelemetryEndpoint(host.env),
|
|
479
|
+
snapshot
|
|
480
|
+
}));
|
|
481
|
+
} catch {}
|
|
482
|
+
}
|
|
483
|
+
//#endregion
|
|
484
|
+
//#region src/package-manager.ts
|
|
485
|
+
const MANAGERS = new Set([
|
|
486
|
+
"npm",
|
|
487
|
+
"pnpm",
|
|
488
|
+
"yarn",
|
|
489
|
+
"bun",
|
|
490
|
+
"deno"
|
|
491
|
+
]);
|
|
492
|
+
function isManager(name) {
|
|
493
|
+
return MANAGERS.has(name);
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* The manager that invoked this process, read from the environment the
|
|
497
|
+
* host handed the engine. package-manager-detector's own getUserAgent
|
|
498
|
+
* reads process.env directly, which the engine may not do; this is the
|
|
499
|
+
* same parse over Runtime.env.
|
|
500
|
+
*/
|
|
501
|
+
function invokingManager(env) {
|
|
502
|
+
const name = env.npm_config_user_agent?.split("/")[0];
|
|
503
|
+
return name !== void 0 && isManager(name) ? name : void 0;
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Resolves the manager to drive at `cwd`. In order: the caller's
|
|
507
|
+
* choice, the host's, the project the library detects by walking up
|
|
508
|
+
* from `cwd`, the manager that invoked this process, then npm. Always
|
|
509
|
+
* resolves — "we could not tell" is not an outcome.
|
|
510
|
+
*
|
|
511
|
+
* Two inherited behaviors of package-manager-detector, deliberate and
|
|
512
|
+
* documented so they are not rediscovered as bugs: the walk goes to the
|
|
513
|
+
* filesystem root with no project boundary, so a lockfile above the
|
|
514
|
+
* project counts; and within one directory a package.json
|
|
515
|
+
* `packageManager` field beats a lockfile.
|
|
516
|
+
*/
|
|
517
|
+
async function resolvePackageManager(request) {
|
|
518
|
+
const chosen = request.override ?? request.host;
|
|
519
|
+
if (chosen !== void 0) return chosen;
|
|
520
|
+
const detected = await detect({ cwd: request.cwd });
|
|
521
|
+
if (detected !== null && isManager(detected.name)) return detected.name;
|
|
522
|
+
return invokingManager(request.env) ?? "npm";
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* The characters a POSIX shell hands to a command unchanged. Everything
|
|
526
|
+
* else — whitespace, `|`, `>`, `$`, `*`, quotes — either moves an
|
|
527
|
+
* argument boundary or is syntax the shell acts on, and the line is
|
|
528
|
+
* offered to the user as the command to run by hand: it has to reach
|
|
529
|
+
* their package manager as the argv the engine composed.
|
|
530
|
+
*/
|
|
531
|
+
const SHELL_SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
|
|
532
|
+
/**
|
|
533
|
+
* Single quotes, because a POSIX shell expands nothing inside them; a
|
|
534
|
+
* single quote in the entry itself has to leave and re-enter them.
|
|
535
|
+
* Known limitation: this is not a Windows rule — cmd.exe treats `'` as
|
|
536
|
+
* an ordinary character and reads `%` and `^` — so a line pasted into
|
|
537
|
+
* cmd.exe or PowerShell is not quoted for those shells.
|
|
538
|
+
*/
|
|
539
|
+
function shellQuote(entry) {
|
|
540
|
+
if (SHELL_SAFE.test(entry)) return entry;
|
|
541
|
+
return `'${entry.replaceAll("'", String.raw`'\''`)}'`;
|
|
542
|
+
}
|
|
543
|
+
function spell$1(file, args) {
|
|
544
|
+
return {
|
|
545
|
+
file,
|
|
546
|
+
args,
|
|
547
|
+
line: [file, ...args].map(shellQuote).join(" ")
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
function addSpelling(file) {
|
|
551
|
+
return (packages, dev) => spell$1(file, [
|
|
552
|
+
"add",
|
|
553
|
+
...dev ? ["-D"] : [],
|
|
554
|
+
...packages
|
|
555
|
+
]);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* Every command line the engine will run, for every manager and both
|
|
559
|
+
* forms. Nothing else in the engine spells a manager command.
|
|
560
|
+
*/
|
|
561
|
+
const SPELLINGS = {
|
|
562
|
+
npm: {
|
|
563
|
+
install: addSpelling("npm"),
|
|
564
|
+
run: (packageName, args) => spell$1("npx", [packageName, ...args])
|
|
565
|
+
},
|
|
566
|
+
pnpm: {
|
|
567
|
+
install: addSpelling("pnpm"),
|
|
568
|
+
run: (packageName, args) => spell$1("pnpm", [
|
|
569
|
+
"dlx",
|
|
570
|
+
packageName,
|
|
571
|
+
...args
|
|
572
|
+
])
|
|
573
|
+
},
|
|
574
|
+
yarn: {
|
|
575
|
+
install: addSpelling("yarn"),
|
|
576
|
+
run: (packageName, args) => spell$1("yarn", [
|
|
577
|
+
"dlx",
|
|
578
|
+
packageName,
|
|
579
|
+
...args
|
|
580
|
+
])
|
|
581
|
+
},
|
|
582
|
+
bun: {
|
|
583
|
+
install: addSpelling("bun"),
|
|
584
|
+
run: (packageName, args) => spell$1("bunx", [packageName, ...args])
|
|
585
|
+
},
|
|
586
|
+
deno: {
|
|
587
|
+
install: (packages, dev) => spell$1("deno", [
|
|
588
|
+
"add",
|
|
589
|
+
...dev ? ["--dev"] : [],
|
|
590
|
+
...packages.map((specifier) => `npm:${specifier}`)
|
|
591
|
+
]),
|
|
592
|
+
run: (packageName, args) => spell$1("deno", [
|
|
593
|
+
"run",
|
|
594
|
+
"-A",
|
|
595
|
+
`npm:${packageName}`,
|
|
596
|
+
...args
|
|
597
|
+
])
|
|
598
|
+
}
|
|
599
|
+
};
|
|
600
|
+
/** Adds dependencies to the project. */
|
|
601
|
+
function installCommand(manager, request) {
|
|
602
|
+
return SPELLINGS[manager].install(request.packages, request.dev ?? false);
|
|
603
|
+
}
|
|
604
|
+
/** Runs a package's bin once, without adding a dependency. */
|
|
605
|
+
function runCommand(manager, request) {
|
|
606
|
+
return SPELLINGS[manager].run(request.package, request.args ?? []);
|
|
607
|
+
}
|
|
608
|
+
//#endregion
|
|
156
609
|
//#region src/presentation.ts
|
|
157
610
|
const PRESENTED = Symbol.for("prisma.cli-engine.presented");
|
|
158
611
|
//#endregion
|
|
@@ -281,9 +734,9 @@ const SERVICE_TOKEN_ENV_VAR = "PRISMA_SERVICE_TOKEN";
|
|
|
281
734
|
const WORKSPACE_ID_ENV_VAR = "PRISMA_WORKSPACE_ID";
|
|
282
735
|
//#endregion
|
|
283
736
|
//#region src/execution/debug.ts
|
|
284
|
-
const DEBUG_ENV_VAR = "
|
|
737
|
+
const DEBUG_ENV_VAR = "PRISMA_DEBUG";
|
|
285
738
|
/**
|
|
286
|
-
* The engine's debug valve: silent unless
|
|
739
|
+
* The engine's debug valve: silent unless PRISMA_DEBUG=1 in the
|
|
287
740
|
* injected env (never process.env), writing to the injected stderr.
|
|
288
741
|
* Token material never reaches it.
|
|
289
742
|
*/
|
|
@@ -892,9 +1345,20 @@ function applySharedFlags(state, shared, runtime) {
|
|
|
892
1345
|
state.confirmValues = [...shared.confirm ?? []];
|
|
893
1346
|
state.interactive = shared.interactive ?? defaultInteractive(runtime);
|
|
894
1347
|
state.logLevel = resolveLogLevel(shared);
|
|
895
|
-
state.colorEnabled = shared
|
|
1348
|
+
state.colorEnabled = resolveColorEnabled(shared, runtime);
|
|
896
1349
|
state.configPath = shared.config;
|
|
897
1350
|
}
|
|
1351
|
+
/**
|
|
1352
|
+
* Explicit flag, then the environment, then the stream. The stream is
|
|
1353
|
+
* stderr because that is where blocks render: keying off stdout meant
|
|
1354
|
+
* `cmd > file` lost colour a human was watching and `cmd 2> file` kept
|
|
1355
|
+
* colour nobody could see.
|
|
1356
|
+
*/
|
|
1357
|
+
function resolveColorEnabled(shared, runtime) {
|
|
1358
|
+
if (shared.color !== void 0) return shared.color;
|
|
1359
|
+
if (runtime.env.NO_COLOR !== void 0) return false;
|
|
1360
|
+
return runtime.isTty.stderr;
|
|
1361
|
+
}
|
|
898
1362
|
/** Interactive iff TTY stdin outside CI; --interactive and
|
|
899
1363
|
* --no-interactive override in either direction. Format never decides
|
|
900
1364
|
* interactivity (operator ruling, 2026-08-09): an interactive json run
|
|
@@ -1100,6 +1564,72 @@ function matchFlagRedirect(table, commandPath, typedFlags) {
|
|
|
1100
1564
|
}
|
|
1101
1565
|
}
|
|
1102
1566
|
//#endregion
|
|
1567
|
+
//#region src/execution/palette.ts
|
|
1568
|
+
/**
|
|
1569
|
+
* Tones into bytes, and text into a display width. Basic 16-colour SGR
|
|
1570
|
+
* only — no 256-colour, no truecolor, and therefore no colour-depth
|
|
1571
|
+
* detection to own.
|
|
1572
|
+
*/
|
|
1573
|
+
/**
|
|
1574
|
+
* Every rendering is taken from a shipping implementation rather than
|
|
1575
|
+
* chosen fresh: the platform shell's semantic colours
|
|
1576
|
+
* (`packages/cli/src/shell/ui.ts`) and the ORM CLI's formatters. The
|
|
1577
|
+
* indexed colours are the ORM's lane rotation, in order, which excludes
|
|
1578
|
+
* red so a lane cannot be mistaken for an error.
|
|
1579
|
+
*
|
|
1580
|
+
* `heading` and `identifier` both render cyan because both shipping
|
|
1581
|
+
* CLIs use cyan for both. They stay separately named so a later
|
|
1582
|
+
* re-theme can separate them without touching a single command.
|
|
1583
|
+
*/
|
|
1584
|
+
const TONE_VERB = {
|
|
1585
|
+
ok: "greenBright",
|
|
1586
|
+
warn: "yellow",
|
|
1587
|
+
error: "redBright",
|
|
1588
|
+
info: "blue",
|
|
1589
|
+
heading: "cyan",
|
|
1590
|
+
identifier: "cyan",
|
|
1591
|
+
ref: "green",
|
|
1592
|
+
placeholder: "dim",
|
|
1593
|
+
link: "blue",
|
|
1594
|
+
emphasis: "bold",
|
|
1595
|
+
muted: "dim",
|
|
1596
|
+
structure: "dim",
|
|
1597
|
+
highlight: "greenBright",
|
|
1598
|
+
"color-1": "white",
|
|
1599
|
+
"color-2": "cyan",
|
|
1600
|
+
"color-3": "yellow",
|
|
1601
|
+
"color-4": "blueBright",
|
|
1602
|
+
"color-5": "magenta",
|
|
1603
|
+
"color-6": "green"
|
|
1604
|
+
};
|
|
1605
|
+
const COLORED = createColors({ useColor: true });
|
|
1606
|
+
const PLAIN = createColors({ useColor: false });
|
|
1607
|
+
function makePaint(colorEnabled) {
|
|
1608
|
+
const colors = colorEnabled ? COLORED : PLAIN;
|
|
1609
|
+
return (tone, text) => colors[TONE_VERB[tone]](text);
|
|
1610
|
+
}
|
|
1611
|
+
function renderText(text, paint) {
|
|
1612
|
+
if (typeof text === "string") return text;
|
|
1613
|
+
let rendered = "";
|
|
1614
|
+
for (const span of text) rendered += span.tone === void 0 ? span.text : paint(span.tone, span.text);
|
|
1615
|
+
return rendered;
|
|
1616
|
+
}
|
|
1617
|
+
/**
|
|
1618
|
+
* The display width of what the reader sees, measured on the text the
|
|
1619
|
+
* spans carry rather than on the rendered bytes: colour cannot change a
|
|
1620
|
+
* column's width, and a CJK name is two cells wide, not one.
|
|
1621
|
+
*/
|
|
1622
|
+
function textWidth(text) {
|
|
1623
|
+
return stringWidth(plainText(text));
|
|
1624
|
+
}
|
|
1625
|
+
/** The text a reader sees, with every tone dropped. */
|
|
1626
|
+
function plainText(text) {
|
|
1627
|
+
if (typeof text === "string") return text;
|
|
1628
|
+
let plain = "";
|
|
1629
|
+
for (const span of text) plain += span.text;
|
|
1630
|
+
return plain;
|
|
1631
|
+
}
|
|
1632
|
+
//#endregion
|
|
1103
1633
|
//#region src/execution/rendering.ts
|
|
1104
1634
|
function firstLine(text) {
|
|
1105
1635
|
const newline = text.indexOf("\n");
|
|
@@ -1107,7 +1637,7 @@ function firstLine(text) {
|
|
|
1107
1637
|
}
|
|
1108
1638
|
const STEP_OUTCOME_SYMBOL = {
|
|
1109
1639
|
ok: "✔",
|
|
1110
|
-
failed: "
|
|
1640
|
+
failed: "✘",
|
|
1111
1641
|
skipped: "↷",
|
|
1112
1642
|
warning: "⚠"
|
|
1113
1643
|
};
|
|
@@ -1145,40 +1675,125 @@ function renderEventHuman(invocation, event) {
|
|
|
1145
1675
|
return;
|
|
1146
1676
|
}
|
|
1147
1677
|
}
|
|
1148
|
-
|
|
1678
|
+
/**
|
|
1679
|
+
* The four Status glyphs. The step-outcome map above and the
|
|
1680
|
+
* diagnostic-severity map below answer different questions, but the
|
|
1681
|
+
* three draw the same characters on purpose: one run must not report a
|
|
1682
|
+
* failure with one mark and explain it with another.
|
|
1683
|
+
*/
|
|
1684
|
+
const STATUS_SYMBOL = {
|
|
1149
1685
|
ok: "✔",
|
|
1150
|
-
error: "
|
|
1686
|
+
error: "✘",
|
|
1151
1687
|
warn: "⚠",
|
|
1152
1688
|
info: "ℹ"
|
|
1153
1689
|
};
|
|
1154
|
-
|
|
1690
|
+
const MASK = "********";
|
|
1691
|
+
const COLUMN_GAP = " ";
|
|
1692
|
+
const RAIL = "│";
|
|
1693
|
+
const BRANCH = "├─";
|
|
1694
|
+
const LAST_BRANCH = "└─";
|
|
1695
|
+
const TRAILING_SPACES = / +$/;
|
|
1696
|
+
/**
|
|
1697
|
+
* The engine draws; a command describes. Every escape sequence in the
|
|
1698
|
+
* output originates here, which is what makes width measurable and
|
|
1699
|
+
* alignment safe: a handler hands over spans, never bytes.
|
|
1700
|
+
*/
|
|
1701
|
+
function renderBlock(block, paint, write) {
|
|
1155
1702
|
switch (block.kind) {
|
|
1156
1703
|
case "summary":
|
|
1157
|
-
write(`${
|
|
1704
|
+
write(`${paint(block.tone ?? block.status, STATUS_SYMBOL[block.status])} ${renderText(block.text, paint)}`);
|
|
1158
1705
|
return;
|
|
1159
1706
|
case "fields":
|
|
1160
|
-
|
|
1707
|
+
writeFields(block.rows, block.rail === true, paint, write);
|
|
1161
1708
|
return;
|
|
1162
1709
|
case "table":
|
|
1163
|
-
|
|
1164
|
-
for (const row of block.rows) write(row.join(" "));
|
|
1710
|
+
writeTable(block.columns, block.rows, paint, write);
|
|
1165
1711
|
return;
|
|
1166
1712
|
case "list":
|
|
1167
|
-
for (const item of block.items) write(`- ${item}`);
|
|
1713
|
+
for (const item of block.items) write(`- ${renderText(item, paint)}`);
|
|
1168
1714
|
return;
|
|
1169
1715
|
case "tree":
|
|
1170
|
-
|
|
1716
|
+
for (const root of block.roots) {
|
|
1717
|
+
write(treeLabel(root, paint));
|
|
1718
|
+
writeBranches(root.children ?? [], "", paint, write);
|
|
1719
|
+
}
|
|
1720
|
+
return;
|
|
1721
|
+
case "drawing":
|
|
1722
|
+
for (const line of block.lines) write(renderText(line, paint));
|
|
1171
1723
|
return;
|
|
1172
1724
|
}
|
|
1173
1725
|
}
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1726
|
+
/** Appends to the text's last span, so a pad or a colon inherits the
|
|
1727
|
+
* tone of the run it extends — which is what makes a padded key one
|
|
1728
|
+
* continuous coloured field, as the legacy card drew it. */
|
|
1729
|
+
function extend(text, suffix) {
|
|
1730
|
+
if (suffix === "") return text;
|
|
1731
|
+
if (typeof text === "string") return `${text}${suffix}`;
|
|
1732
|
+
const last = text.at(-1);
|
|
1733
|
+
if (last === void 0) return suffix;
|
|
1734
|
+
return [...text.slice(0, -1), {
|
|
1735
|
+
...last,
|
|
1736
|
+
text: `${last.text}${suffix}`
|
|
1737
|
+
}];
|
|
1738
|
+
}
|
|
1739
|
+
function padTo(text, width) {
|
|
1740
|
+
const gap = width - textWidth(text);
|
|
1741
|
+
return gap > 0 ? extend(text, " ".repeat(gap)) : text;
|
|
1742
|
+
}
|
|
1743
|
+
/** The tone a cell falls back to when it states none of its own. */
|
|
1744
|
+
function toned(text, tone) {
|
|
1745
|
+
if (typeof text === "string") return [{
|
|
1746
|
+
text,
|
|
1747
|
+
tone
|
|
1748
|
+
}];
|
|
1749
|
+
return text.map((span) => span.tone === void 0 ? {
|
|
1750
|
+
...span,
|
|
1751
|
+
tone
|
|
1752
|
+
} : span);
|
|
1753
|
+
}
|
|
1754
|
+
/**
|
|
1755
|
+
* The legacy card, restored: `${label}:` padded to a common width so
|
|
1756
|
+
* every value starts in the same column, the label in the accent
|
|
1757
|
+
* colour, two spaces between the two.
|
|
1758
|
+
*/
|
|
1759
|
+
function writeFields(rows, rail, paint, write) {
|
|
1760
|
+
const cells = rows.map((row) => ({
|
|
1761
|
+
label: toned(extend(row.label, ":"), "heading"),
|
|
1762
|
+
value: row.sensitive === true ? MASK : row.value
|
|
1763
|
+
}));
|
|
1764
|
+
const width = Math.max(0, ...cells.map((cell) => textWidth(cell.label)));
|
|
1765
|
+
const prefix = rail ? `${paint("structure", RAIL)}${COLUMN_GAP}` : "";
|
|
1766
|
+
for (const cell of cells) write(`${prefix}${renderText(padTo(cell.label, width), paint)}${COLUMN_GAP}${renderText(cell.value, paint)}`);
|
|
1767
|
+
}
|
|
1768
|
+
/**
|
|
1769
|
+
* Every column as wide as its widest cell, measured on the text rather
|
|
1770
|
+
* than the bytes so colour cannot shift a column. The last column is
|
|
1771
|
+
* never padded, so no line carries trailing whitespace.
|
|
1772
|
+
*/
|
|
1773
|
+
function writeTable(columns, rows, paint, write) {
|
|
1774
|
+
const all = [columns.map((column) => toned(column, "heading")), ...rows];
|
|
1775
|
+
const widths = [];
|
|
1776
|
+
for (const row of all) for (const [index, cell] of row.entries()) widths[index] = Math.max(widths[index] ?? 0, textWidth(cell));
|
|
1777
|
+
for (const row of all) write(row.map((cell, index) => renderText(index === row.length - 1 ? cell : padTo(cell, widths[index]), paint)).join(COLUMN_GAP).replace(TRAILING_SPACES, ""));
|
|
1778
|
+
}
|
|
1779
|
+
/** Roots carry no connector; every level below is drawn from its
|
|
1780
|
+
* parent's prefix, so a deep branch stays under its own lane. */
|
|
1781
|
+
function writeBranches(nodes, prefix, paint, write) {
|
|
1782
|
+
for (const [index, node] of nodes.entries()) {
|
|
1783
|
+
const last = index === nodes.length - 1;
|
|
1784
|
+
write(`${prefix}${paint("structure", last ? LAST_BRANCH : BRANCH)} ${treeLabel(node, paint)}`);
|
|
1785
|
+
const continuation = last ? " " : `${paint("structure", RAIL)}${COLUMN_GAP}`;
|
|
1786
|
+
writeBranches(node.children ?? [], `${prefix}${continuation}`, paint, write);
|
|
1178
1787
|
}
|
|
1179
1788
|
}
|
|
1789
|
+
function treeLabel(node, paint) {
|
|
1790
|
+
const tone = node.tone ?? node.status;
|
|
1791
|
+
const label = renderText(tone === void 0 ? node.label : toned(node.label, tone), paint);
|
|
1792
|
+
if (node.status === void 0) return label;
|
|
1793
|
+
return `${paint(node.tone ?? node.status, STATUS_SYMBOL[node.status])} ${label}`;
|
|
1794
|
+
}
|
|
1180
1795
|
const DIAGNOSTIC_SYMBOL = {
|
|
1181
|
-
error: "
|
|
1796
|
+
error: "✘",
|
|
1182
1797
|
warn: "⚠",
|
|
1183
1798
|
info: "ℹ"
|
|
1184
1799
|
};
|
|
@@ -1214,7 +1829,8 @@ function withDocsUrl(state, diagnostic) {
|
|
|
1214
1829
|
* payload on stdout, always — human mode stays pipe-clean. */
|
|
1215
1830
|
function renderCompletedHuman(invocation, presented) {
|
|
1216
1831
|
const { runtime, state } = invocation;
|
|
1217
|
-
|
|
1832
|
+
const paint = makePaint(state.colorEnabled);
|
|
1833
|
+
for (const block of presented.presentation.human) renderBlock(block, paint, (line) => runtime.stderr.write(`${line}\n`));
|
|
1218
1834
|
for (const action of presented.presentation.next) runtime.stderr.write(`${renderNextAction(action)}\n`);
|
|
1219
1835
|
for (const diagnostic of presented.diagnostics) writeDiagnostic(runtime.stderr, withDocsUrl(state, diagnostic));
|
|
1220
1836
|
for (const line of presented.presentation.stdout) runtime.stdout.write(`${line}\n`);
|
|
@@ -1237,7 +1853,7 @@ function eventDisplaySeverity(event) {
|
|
|
1237
1853
|
* so the only surface left is a stderr note — throwing here would
|
|
1238
1854
|
* surface as an unhandled rejection in a detached async context. */
|
|
1239
1855
|
function reportAfterResolution(invocation) {
|
|
1240
|
-
invocation.runtime.stderr.write("
|
|
1856
|
+
invocation.runtime.stderr.write("✘ [CLI.INTERNAL_ERROR] @prisma/cli-engine: report() was called after the handler resolved\n");
|
|
1241
1857
|
}
|
|
1242
1858
|
function reportEvent(invocation, event) {
|
|
1243
1859
|
const state = invocation.state;
|
|
@@ -1303,7 +1919,7 @@ function structuredErrorFromDiagnostic(diagnostic) {
|
|
|
1303
1919
|
* success it carries the validated section value for ctx.config. */
|
|
1304
1920
|
async function checkNeeds(def, invocation) {
|
|
1305
1921
|
const needs = def.needs;
|
|
1306
|
-
const failure = checkInteraction(needs, invocation) ?? checkDependencies(needs, invocation);
|
|
1922
|
+
const failure = checkInteraction(needs, invocation) ?? await checkDependencies(needs, invocation);
|
|
1307
1923
|
if (failure !== void 0) return failure;
|
|
1308
1924
|
const credentials = await checkCredentials(needs, invocation);
|
|
1309
1925
|
if (credentials.failure !== void 0) return credentials.failure;
|
|
@@ -1330,8 +1946,15 @@ function checkInteraction(needs, invocation) {
|
|
|
1330
1946
|
}]
|
|
1331
1947
|
}));
|
|
1332
1948
|
}
|
|
1333
|
-
function checkDependencies(needs, invocation) {
|
|
1334
|
-
|
|
1949
|
+
async function checkDependencies(needs, invocation) {
|
|
1950
|
+
const runtime = invocation.runtime;
|
|
1951
|
+
const missing = needs.dependencies.find((specifier) => !dependencyResolvable(specifier, runtime.cwd));
|
|
1952
|
+
if (missing === void 0) return;
|
|
1953
|
+
return needsErrored(missingDependencyError(missing, await resolvePackageManager({
|
|
1954
|
+
cwd: runtime.cwd,
|
|
1955
|
+
env: runtime.env,
|
|
1956
|
+
host: runtime.packageManager
|
|
1957
|
+
})));
|
|
1335
1958
|
}
|
|
1336
1959
|
/**
|
|
1337
1960
|
* The credentials need, single-sourced from the credential manager:
|
|
@@ -1453,30 +2076,18 @@ function dependencyResolvable(specifier, cwd) {
|
|
|
1453
2076
|
return false;
|
|
1454
2077
|
}
|
|
1455
2078
|
}
|
|
1456
|
-
function installCommand(packageManager, specifier) {
|
|
1457
|
-
switch (packageManager) {
|
|
1458
|
-
case "npm": return `npm install ${specifier}`;
|
|
1459
|
-
case "pnpm": return `pnpm add ${specifier}`;
|
|
1460
|
-
case "yarn": return `yarn add ${specifier}`;
|
|
1461
|
-
case "bun": return `bun add ${specifier}`;
|
|
1462
|
-
case "unknown": return;
|
|
1463
|
-
}
|
|
1464
|
-
}
|
|
1465
2079
|
/** Optional peer dependencies: the engine probes and phrases. */
|
|
1466
|
-
function missingDependencyError(specifier,
|
|
1467
|
-
const install = installCommand(
|
|
2080
|
+
function missingDependencyError(specifier, manager) {
|
|
2081
|
+
const install = installCommand(manager, { packages: [specifier] }).line;
|
|
1468
2082
|
return new CliStructuredError("CLI.MISSING_DEPENDENCY", `This command requires the optional dependency '${specifier}', which is not installed in this project.`, {
|
|
1469
|
-
nextActions: [
|
|
1470
|
-
kind: "user-choice",
|
|
1471
|
-
label: `Install '${specifier}' with your package manager, then run the command again.`
|
|
1472
|
-
} : {
|
|
2083
|
+
nextActions: [{
|
|
1473
2084
|
kind: "run-command",
|
|
1474
2085
|
label: `Install '${specifier}'`,
|
|
1475
2086
|
command: install
|
|
1476
2087
|
}],
|
|
1477
2088
|
meta: {
|
|
1478
2089
|
specifier,
|
|
1479
|
-
|
|
2090
|
+
installCommand: install
|
|
1480
2091
|
}
|
|
1481
2092
|
});
|
|
1482
2093
|
}
|
|
@@ -1498,6 +2109,252 @@ async function announceUrl(invocation, request) {
|
|
|
1498
2109
|
}
|
|
1499
2110
|
}
|
|
1500
2111
|
//#endregion
|
|
2112
|
+
//#region src/package-manager-errors.ts
|
|
2113
|
+
function why(failure) {
|
|
2114
|
+
if (failure.reason === "runner-unavailable") return "This host wires no package-manager runner, so nothing was run.";
|
|
2115
|
+
return `${failure.manager} exited with code ${failure.exitCode}.`;
|
|
2116
|
+
}
|
|
2117
|
+
/**
|
|
2118
|
+
* The single constructor of CLI.PACKAGE_MANAGER_FAILED — both forms of
|
|
2119
|
+
* ctx.packages, and the host that can run neither.
|
|
2120
|
+
*/
|
|
2121
|
+
function packageManagerFailedError(failure) {
|
|
2122
|
+
const installing = failure.form === "install";
|
|
2123
|
+
return new CliStructuredError("CLI.PACKAGE_MANAGER_FAILED", installing ? `Installing packages with ${failure.manager} failed.` : `Running ${failure.package} with ${failure.manager} failed.`, {
|
|
2124
|
+
why: why(failure),
|
|
2125
|
+
nextActions: [{
|
|
2126
|
+
kind: "run-command",
|
|
2127
|
+
label: installing ? "Run the install yourself" : "Run the command yourself",
|
|
2128
|
+
command: failure.command
|
|
2129
|
+
}],
|
|
2130
|
+
meta: {
|
|
2131
|
+
form: failure.form,
|
|
2132
|
+
manager: failure.manager,
|
|
2133
|
+
command: failure.command,
|
|
2134
|
+
exitCode: failure.exitCode,
|
|
2135
|
+
stderrTail: failure.stderrTail,
|
|
2136
|
+
...failure.reason === void 0 ? {} : { reason: failure.reason }
|
|
2137
|
+
}
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
//#endregion
|
|
2141
|
+
//#region src/execution/redaction.ts
|
|
2142
|
+
const REDACTED = "…";
|
|
2143
|
+
/** A candidate runs from a scheme to the end of the authority, which is
|
|
2144
|
+
* the only part of a URL userinfo can be in. Delimiting it is all this
|
|
2145
|
+
* does; `URL` below decides whether the candidate is a URL at all. What
|
|
2146
|
+
* follows the authority is never matched, so a path holding an `@`, and
|
|
2147
|
+
* the punctuation a URL ending a sentence carries, are untouched. */
|
|
2148
|
+
const URL_AUTHORITY = /[a-zA-Z][a-zA-Z0-9+.-]*:\/\/[^\s/?#<>]+/g;
|
|
2149
|
+
const AUTHORITY_MARK = "://";
|
|
2150
|
+
/** `new URL` throws for everything that is not a URL, which is how a
|
|
2151
|
+
* candidate is rejected. */
|
|
2152
|
+
function asUrl(candidate) {
|
|
2153
|
+
try {
|
|
2154
|
+
return new URL(candidate);
|
|
2155
|
+
} catch {
|
|
2156
|
+
return;
|
|
2157
|
+
}
|
|
2158
|
+
}
|
|
2159
|
+
/** `URL` reports whether there is userinfo at all, and the `@` that ends
|
|
2160
|
+
* it is the last one in the authority. Only that span of the original
|
|
2161
|
+
* text is replaced: serialising the parsed URL back out would return a
|
|
2162
|
+
* normalised one — default port dropped, path resolved, password
|
|
2163
|
+
* percent-encoded — and the surrounding output has to survive verbatim. */
|
|
2164
|
+
function redactUserinfo(candidate) {
|
|
2165
|
+
const url = asUrl(candidate);
|
|
2166
|
+
if (url === void 0 || url.username === "" && url.password === "") return candidate;
|
|
2167
|
+
const start = candidate.indexOf(AUTHORITY_MARK) + 3;
|
|
2168
|
+
const end = candidate.lastIndexOf("@");
|
|
2169
|
+
return `${candidate.slice(0, start)}${REDACTED}${candidate.slice(end)}`;
|
|
2170
|
+
}
|
|
2171
|
+
const VALUE = String.raw`"[^"]*"|'[^']*'|\S+`;
|
|
2172
|
+
const ASSIGNMENT = new RegExp(String.raw`\b([A-Za-z_][A-Za-z0-9_]*)=(${VALUE})`, "g");
|
|
2173
|
+
/** A header's value runs to the end of its line, because an auth scheme
|
|
2174
|
+
* and its credential are one value: `Authorization: Bearer eyJ…`. */
|
|
2175
|
+
const HEADER = /\b([A-Za-z][A-Za-z0-9_-]*):[ \t]+[^\n]+/g;
|
|
2176
|
+
/** A flag's value is the word after it. A word starting with a dash is
|
|
2177
|
+
* the next flag rather than this one's value. */
|
|
2178
|
+
const FLAG = new RegExp(String.raw`(--?[A-Za-z][A-Za-z0-9_-]*)[ \t]+(?!-)(?:${VALUE})`, "g");
|
|
2179
|
+
const SECRET_WORDS = new Set([
|
|
2180
|
+
"TOKEN",
|
|
2181
|
+
"KEY",
|
|
2182
|
+
"SECRET",
|
|
2183
|
+
"PASSWORD",
|
|
2184
|
+
"AUTHORIZATION"
|
|
2185
|
+
]);
|
|
2186
|
+
/** Underscores, hyphens and camelCase humps: `MY_API_TOKEN`,
|
|
2187
|
+
* `--auth-token` and `_authToken` name a secret; `monkey`,
|
|
2188
|
+
* `--keyring` and `tokenizer` do not. */
|
|
2189
|
+
const NAME_WORDS = /[_-]+|(?<=[a-z0-9])(?=[A-Z])/;
|
|
2190
|
+
function namesSecret(name) {
|
|
2191
|
+
return name.split(NAME_WORDS).some((word) => SECRET_WORDS.has(word.toUpperCase()));
|
|
2192
|
+
}
|
|
2193
|
+
/**
|
|
2194
|
+
* Removes the shapes a package manager's output leaks credentials in:
|
|
2195
|
+
* the userinfo of a URL, and the value carried by a name that says it
|
|
2196
|
+
* holds a secret — after an `=`, after a header's colon, or after a
|
|
2197
|
+
* flag. Everything else survives verbatim, so a caller matching an
|
|
2198
|
+
* error code out of stderr still finds it.
|
|
2199
|
+
*/
|
|
2200
|
+
function redactSecrets(text) {
|
|
2201
|
+
return text.replace(URL_AUTHORITY, redactUserinfo).replace(ASSIGNMENT, (assignment, name) => namesSecret(name) ? `${name}=${REDACTED}` : assignment).replace(HEADER, (header, name) => namesSecret(name) ? `${name}: ${REDACTED}` : header).replace(FLAG, (flag, name) => namesSecret(name) ? `${name} ${REDACTED}` : flag);
|
|
2202
|
+
}
|
|
2203
|
+
//#endregion
|
|
2204
|
+
//#region src/execution/package-operations.ts
|
|
2205
|
+
/** What the seam reports for a child that never ran, and what a host
|
|
2206
|
+
* with no runner at all reports for the same reason. */
|
|
2207
|
+
const NO_CHILD_EXIT_CODE = 1;
|
|
2208
|
+
function spell(operation, manager) {
|
|
2209
|
+
return operation.form === "install" ? installCommand(manager, {
|
|
2210
|
+
packages: operation.packages,
|
|
2211
|
+
dev: operation.dev
|
|
2212
|
+
}) : runCommand(manager, {
|
|
2213
|
+
package: operation.package,
|
|
2214
|
+
args: operation.args
|
|
2215
|
+
});
|
|
2216
|
+
}
|
|
2217
|
+
function failed(operation, command, outcome) {
|
|
2218
|
+
const spelling = redactSecrets(command.line);
|
|
2219
|
+
return packageManagerFailedError(operation.form === "install" ? {
|
|
2220
|
+
...outcome,
|
|
2221
|
+
command: spelling,
|
|
2222
|
+
form: "install"
|
|
2223
|
+
} : {
|
|
2224
|
+
...outcome,
|
|
2225
|
+
command: spelling,
|
|
2226
|
+
form: "run",
|
|
2227
|
+
package: operation.package
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
/** Cancellation throws rather than resolving a failure, so an aborted
|
|
2231
|
+
* operation settles the way Ctrl-C settles everywhere else. Asked again
|
|
2232
|
+
* after detection: that await is long enough for the signal to fire,
|
|
2233
|
+
* and an operation nothing spawned must announce nothing. */
|
|
2234
|
+
function stopIfCancelled(signal) {
|
|
2235
|
+
if (signal.aborted) throw signal.reason;
|
|
2236
|
+
}
|
|
2237
|
+
/** The seam hands over chunks as the child writes them; an `output`
|
|
2238
|
+
* event carries one line, so a line split across two chunks waits for
|
|
2239
|
+
* its second half. */
|
|
2240
|
+
function lineAssembler(emit) {
|
|
2241
|
+
let pending = "";
|
|
2242
|
+
const emitLine = (line) => {
|
|
2243
|
+
emit(redactSecrets(line.endsWith("\r") ? line.slice(0, -1) : line));
|
|
2244
|
+
};
|
|
2245
|
+
return {
|
|
2246
|
+
push: (chunk) => {
|
|
2247
|
+
const lines = (pending + chunk).split("\n");
|
|
2248
|
+
pending = lines.pop() ?? "";
|
|
2249
|
+
for (const line of lines) emitLine(line);
|
|
2250
|
+
},
|
|
2251
|
+
flush: () => {
|
|
2252
|
+
if (pending !== "") {
|
|
2253
|
+
emitLine(pending);
|
|
2254
|
+
pending = "";
|
|
2255
|
+
}
|
|
2256
|
+
}
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
function outputChannels(invocation, manager) {
|
|
2260
|
+
const assemble = (channel) => lineAssembler((line) => {
|
|
2261
|
+
reportEvent(invocation, {
|
|
2262
|
+
kind: "output",
|
|
2263
|
+
source: manager,
|
|
2264
|
+
channel,
|
|
2265
|
+
line
|
|
2266
|
+
});
|
|
2267
|
+
});
|
|
2268
|
+
const channels = {
|
|
2269
|
+
data: assemble("data"),
|
|
2270
|
+
diagnostic: assemble("diagnostic")
|
|
2271
|
+
};
|
|
2272
|
+
return {
|
|
2273
|
+
push: (channel, chunk) => {
|
|
2274
|
+
channels[channel].push(chunk);
|
|
2275
|
+
},
|
|
2276
|
+
flush: () => {
|
|
2277
|
+
channels.data.flush();
|
|
2278
|
+
channels.diagnostic.flush();
|
|
2279
|
+
}
|
|
2280
|
+
};
|
|
2281
|
+
}
|
|
2282
|
+
async function execute(invocation, operation, placement) {
|
|
2283
|
+
const { runtime, signal } = invocation;
|
|
2284
|
+
stopIfCancelled(signal);
|
|
2285
|
+
const cwd = placement.cwd ?? runtime.cwd;
|
|
2286
|
+
const manager = await resolvePackageManager({
|
|
2287
|
+
cwd,
|
|
2288
|
+
env: runtime.env,
|
|
2289
|
+
override: placement.manager,
|
|
2290
|
+
host: runtime.packageManager
|
|
2291
|
+
});
|
|
2292
|
+
stopIfCancelled(signal);
|
|
2293
|
+
const command = spell(operation, manager);
|
|
2294
|
+
const runner = runtime.runPackageManager;
|
|
2295
|
+
if (runner === void 0) return notOk(failed(operation, command, {
|
|
2296
|
+
manager,
|
|
2297
|
+
exitCode: NO_CHILD_EXIT_CODE,
|
|
2298
|
+
stderrTail: "",
|
|
2299
|
+
reason: "runner-unavailable"
|
|
2300
|
+
}));
|
|
2301
|
+
const output = outputChannels(invocation, manager);
|
|
2302
|
+
const step = redactSecrets(command.line);
|
|
2303
|
+
reportEvent(invocation, {
|
|
2304
|
+
kind: "step-started",
|
|
2305
|
+
step
|
|
2306
|
+
});
|
|
2307
|
+
let result;
|
|
2308
|
+
try {
|
|
2309
|
+
result = await runner({
|
|
2310
|
+
file: command.file,
|
|
2311
|
+
args: command.args,
|
|
2312
|
+
cwd,
|
|
2313
|
+
signal,
|
|
2314
|
+
onOutput: output.push
|
|
2315
|
+
});
|
|
2316
|
+
} finally {
|
|
2317
|
+
output.flush();
|
|
2318
|
+
}
|
|
2319
|
+
reportEvent(invocation, {
|
|
2320
|
+
kind: "step-finished",
|
|
2321
|
+
step,
|
|
2322
|
+
outcome: signal.aborted || result.exitCode !== 0 ? "failed" : "ok"
|
|
2323
|
+
});
|
|
2324
|
+
stopIfCancelled(signal);
|
|
2325
|
+
if (result.exitCode === 0) return okVoid();
|
|
2326
|
+
return notOk(failed(operation, command, {
|
|
2327
|
+
manager,
|
|
2328
|
+
exitCode: result.exitCode,
|
|
2329
|
+
stderrTail: redactSecrets(result.stderr)
|
|
2330
|
+
}));
|
|
2331
|
+
}
|
|
2332
|
+
function makePackageOperations(invocation) {
|
|
2333
|
+
const state = invocation.state;
|
|
2334
|
+
const perform = async (operation, placement) => {
|
|
2335
|
+
if (state.delegatedTerminal !== void 0) throw constructionError(`command '${state.commandId}' called ctx.packages while a child owned the terminal`);
|
|
2336
|
+
if (state.packageOperationRunning) throw constructionError("ctx.packages runs one operation at a time, so two package managers can never write one project at once");
|
|
2337
|
+
state.packageOperationRunning = true;
|
|
2338
|
+
try {
|
|
2339
|
+
return await execute(invocation, operation, placement);
|
|
2340
|
+
} finally {
|
|
2341
|
+
state.packageOperationRunning = false;
|
|
2342
|
+
}
|
|
2343
|
+
};
|
|
2344
|
+
return {
|
|
2345
|
+
install: (request) => perform({
|
|
2346
|
+
form: "install",
|
|
2347
|
+
packages: request.packages,
|
|
2348
|
+
dev: request.dev ?? false
|
|
2349
|
+
}, request),
|
|
2350
|
+
run: (request) => perform({
|
|
2351
|
+
form: "run",
|
|
2352
|
+
package: request.package,
|
|
2353
|
+
args: request.args
|
|
2354
|
+
}, request)
|
|
2355
|
+
};
|
|
2356
|
+
}
|
|
2357
|
+
//#endregion
|
|
1501
2358
|
//#region src/execution/clack-renderer.ts
|
|
1502
2359
|
/**
|
|
1503
2360
|
* Interactive prompt rendering backed by @clack/prompts, driven
|
|
@@ -1595,22 +2452,24 @@ function makeLineReader(stdin, invocation) {
|
|
|
1595
2452
|
const decoder = new TextDecoder();
|
|
1596
2453
|
let buffer = "";
|
|
1597
2454
|
let done = false;
|
|
2455
|
+
const takeBufferedLine = () => {
|
|
2456
|
+
const newline = buffer.indexOf("\n");
|
|
2457
|
+
if (newline === -1) return;
|
|
2458
|
+
const line = buffer.slice(0, newline);
|
|
2459
|
+
buffer = buffer.slice(newline + 1);
|
|
2460
|
+
return line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
2461
|
+
};
|
|
2462
|
+
const takeFinalLine = () => {
|
|
2463
|
+
if (buffer.length === 0) return;
|
|
2464
|
+
const line = buffer;
|
|
2465
|
+
buffer = "";
|
|
2466
|
+
return line;
|
|
2467
|
+
};
|
|
1598
2468
|
return async () => {
|
|
1599
2469
|
for (;;) {
|
|
1600
|
-
const
|
|
1601
|
-
if (
|
|
1602
|
-
|
|
1603
|
-
buffer = buffer.slice(newline + 1);
|
|
1604
|
-
return line.endsWith("\r") ? line.slice(0, -1) : line;
|
|
1605
|
-
}
|
|
1606
|
-
if (done) {
|
|
1607
|
-
if (buffer.length > 0) {
|
|
1608
|
-
const line = buffer;
|
|
1609
|
-
buffer = "";
|
|
1610
|
-
return line;
|
|
1611
|
-
}
|
|
1612
|
-
return;
|
|
1613
|
-
}
|
|
2470
|
+
const buffered = takeBufferedLine();
|
|
2471
|
+
if (buffered !== void 0) return buffered;
|
|
2472
|
+
if (done) return takeFinalLine();
|
|
1614
2473
|
const next = await iterator.next();
|
|
1615
2474
|
if (next.done === true) {
|
|
1616
2475
|
done = true;
|
|
@@ -1629,6 +2488,12 @@ function promptUnanswerable(question, state) {
|
|
|
1629
2488
|
label: "Run the command from an interactive terminal, or pass a flag that answers the prompt."
|
|
1630
2489
|
}] });
|
|
1631
2490
|
}
|
|
2491
|
+
/** What a prompt resolves to when it cannot be shown: its declared
|
|
2492
|
+
* default, or a halt when it has none. */
|
|
2493
|
+
function answerWithoutAsking(question, fallback, state) {
|
|
2494
|
+
if (fallback === void 0) throw promptUnanswerable(question, state);
|
|
2495
|
+
return fallback;
|
|
2496
|
+
}
|
|
1632
2497
|
function consentUnavailable(question, state, token) {
|
|
1633
2498
|
const situation = state.yes ? `"${question}" requires explicit consent, which --yes cannot grant.` : `"${question}" requires explicit consent, and the session is not interactive.`;
|
|
1634
2499
|
if (token === void 0) return new CliStructuredError("CLI.CONSENT_REQUIRED", situation, { nextActions: [{
|
|
@@ -1746,6 +2611,28 @@ function makePromptSurface(invocation) {
|
|
|
1746
2611
|
if (typeof typed !== "string" || typed.trim() !== token) throw consentTokenMismatch(question, token, String(typed));
|
|
1747
2612
|
return true;
|
|
1748
2613
|
};
|
|
2614
|
+
const askSelect = async (question, options, fallback) => {
|
|
2615
|
+
const raw = await ask(question, [
|
|
2616
|
+
`? ${question}`,
|
|
2617
|
+
...options.map((option) => ` ${option.value === fallback ? "▸" : " "} ${option.value}: ${option.label}`),
|
|
2618
|
+
"> "
|
|
2619
|
+
].join("\n"));
|
|
2620
|
+
if (typeof raw !== "string") throw promptInvalid(question, String(raw));
|
|
2621
|
+
const answer = raw.trim();
|
|
2622
|
+
if (answer === "") {
|
|
2623
|
+
if (fallback === void 0) throw promptInvalid(question, raw);
|
|
2624
|
+
return fallback;
|
|
2625
|
+
}
|
|
2626
|
+
const match = options.find((option) => option.value === answer);
|
|
2627
|
+
if (match === void 0) throw promptInvalid(question, raw);
|
|
2628
|
+
return match.value;
|
|
2629
|
+
};
|
|
2630
|
+
const askText = async (question, fallback) => {
|
|
2631
|
+
const raw = await ask(question, `? ${question}${fallback === void 0 ? "" : ` (${fallback})`} `);
|
|
2632
|
+
if (typeof raw !== "string") throw promptInvalid(question, String(raw));
|
|
2633
|
+
if (raw === "") return fallback ?? "";
|
|
2634
|
+
return raw;
|
|
2635
|
+
};
|
|
1749
2636
|
/** A prompt writes to stderr and reads the engine's stdin — the same
|
|
1750
2637
|
* terminal a live child inherited. Like ctx.present, prompting while
|
|
1751
2638
|
* a child owns the terminal is a construction error. */
|
|
@@ -1755,10 +2642,7 @@ function makePromptSurface(invocation) {
|
|
|
1755
2642
|
const surface = {
|
|
1756
2643
|
confirm: async (question, opts) => {
|
|
1757
2644
|
const fallback = opts?.default;
|
|
1758
|
-
if (state.yes || !state.interactive)
|
|
1759
|
-
if (fallback === void 0) throw promptUnanswerable(question, state);
|
|
1760
|
-
return fallback;
|
|
1761
|
-
}
|
|
2645
|
+
if (state.yes || !state.interactive) return answerWithoutAsking(question, fallback, state);
|
|
1762
2646
|
if (useClack()) return renderWithClack(question, (r) => r.confirm(question, fallback));
|
|
1763
2647
|
let hint = "(y/n)";
|
|
1764
2648
|
if (fallback === true) hint = "(Y/n)";
|
|
@@ -1777,40 +2661,16 @@ function makePromptSurface(invocation) {
|
|
|
1777
2661
|
},
|
|
1778
2662
|
select: async (question, options, opts) => {
|
|
1779
2663
|
const fallback = opts?.default;
|
|
1780
|
-
if (state.yes || !state.interactive)
|
|
1781
|
-
if (fallback === void 0) throw promptUnanswerable(question, state);
|
|
1782
|
-
return fallback;
|
|
1783
|
-
}
|
|
2664
|
+
if (state.yes || !state.interactive) return answerWithoutAsking(question, fallback, state);
|
|
1784
2665
|
if (useClack()) return renderWithClack(question, (r) => r.select(question, options, fallback));
|
|
1785
|
-
|
|
1786
|
-
`? ${question}`,
|
|
1787
|
-
...options.map((option) => ` ${option.value === fallback ? "▸" : " "} ${option.value}: ${option.label}`),
|
|
1788
|
-
"> "
|
|
1789
|
-
].join("\n"));
|
|
1790
|
-
if (typeof raw !== "string") throw promptInvalid(question, String(raw));
|
|
1791
|
-
const answer = raw.trim();
|
|
1792
|
-
if (answer === "") {
|
|
1793
|
-
if (fallback === void 0) throw promptInvalid(question, raw);
|
|
1794
|
-
return fallback;
|
|
1795
|
-
}
|
|
1796
|
-
const match = options.find((option) => option.value === answer);
|
|
1797
|
-
if (match === void 0) throw promptInvalid(question, raw);
|
|
1798
|
-
return match.value;
|
|
2666
|
+
return askSelect(question, options, fallback);
|
|
1799
2667
|
},
|
|
1800
2668
|
text: async (question, opts) => {
|
|
1801
2669
|
const fallback = opts?.default;
|
|
1802
|
-
if (state.yes || !state.interactive)
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
if (useClack()) {
|
|
1807
|
-
const value = await renderWithClack(question, (r) => r.text(question, opts?.placeholder, fallback));
|
|
1808
|
-
return value === "" ? fallback ?? "" : value;
|
|
1809
|
-
}
|
|
1810
|
-
const raw = await ask(question, `? ${question}${fallback === void 0 ? "" : ` (${fallback})`} `);
|
|
1811
|
-
if (typeof raw !== "string") throw promptInvalid(question, String(raw));
|
|
1812
|
-
if (raw === "") return fallback ?? "";
|
|
1813
|
-
return raw;
|
|
2670
|
+
if (state.yes || !state.interactive) return answerWithoutAsking(question, fallback, state);
|
|
2671
|
+
if (!useClack()) return askText(question, fallback);
|
|
2672
|
+
const value = await renderWithClack(question, (r) => r.text(question, opts?.placeholder, fallback));
|
|
2673
|
+
return value === "" ? fallback ?? "" : value;
|
|
1814
2674
|
},
|
|
1815
2675
|
browserWait: async ({ url, message, poll, timeout, interval }) => {
|
|
1816
2676
|
if (!state.interactive) throw browserWaitUnavailable(message, url);
|
|
@@ -1873,6 +2733,7 @@ function makeSpawn(invocation, def) {
|
|
|
1873
2733
|
if (!def.maySpawn) throw constructionError(`command '${state.commandId}' called ctx.spawn without declaring maySpawn`);
|
|
1874
2734
|
if (state.delegatedTerminal !== void 0) throw constructionError(`command '${state.commandId}' called ctx.spawn while a child was still live (one live child per run)`);
|
|
1875
2735
|
if (state.activePrompts > 0) throw constructionError(`command '${state.commandId}' called ctx.spawn while a prompt owned the terminal`);
|
|
2736
|
+
if (state.packageOperationRunning) throw constructionError(`command '${state.commandId}' called ctx.spawn while a package operation was still running`);
|
|
1876
2737
|
const spawnChild = invocation.runtime.spawn;
|
|
1877
2738
|
if (spawnChild === void 0) throw new Error("@prisma/cli-engine: the command declares maySpawn but the Runtime supplies no spawn adapter");
|
|
1878
2739
|
const terminal = {
|
|
@@ -2030,16 +2891,26 @@ function spawnFailedError(command, cause) {
|
|
|
2030
2891
|
}
|
|
2031
2892
|
//#endregion
|
|
2032
2893
|
//#region src/execution/command-context.ts
|
|
2033
|
-
|
|
2034
|
-
|
|
2035
|
-
|
|
2036
|
-
|
|
2037
|
-
|
|
2038
|
-
|
|
2894
|
+
/** Unbounded off-terminal, so the arithmetic a renderer already does —
|
|
2895
|
+
* Math.min(x, ui.width), ui.width - gutter — stays correct with no
|
|
2896
|
+
* special case. */
|
|
2897
|
+
function availableWidth(stream) {
|
|
2898
|
+
const columns = stream.columns;
|
|
2899
|
+
return columns !== void 0 && columns > 0 ? columns : Number.POSITIVE_INFINITY;
|
|
2900
|
+
}
|
|
2901
|
+
/** Blocks render to stderr, so both the width and the colour decision
|
|
2902
|
+
* are stderr's. `width` is a getter because the contract reads it per
|
|
2903
|
+
* render rather than caching it. */
|
|
2904
|
+
function makeUi(colorEnabled, stderr) {
|
|
2905
|
+
const paint = makePaint(colorEnabled);
|
|
2039
2906
|
return {
|
|
2040
|
-
|
|
2041
|
-
|
|
2042
|
-
|
|
2907
|
+
get width() {
|
|
2908
|
+
return availableWidth(stderr);
|
|
2909
|
+
},
|
|
2910
|
+
emphasize: (text) => paint("emphasis", text),
|
|
2911
|
+
dim: (text) => paint("muted", text),
|
|
2912
|
+
code: (text) => `\`${text}\``,
|
|
2913
|
+
tone: paint
|
|
2043
2914
|
};
|
|
2044
2915
|
}
|
|
2045
2916
|
/** Materializes ONLY the active format's presentation functions, at the
|
|
@@ -2058,9 +2929,9 @@ function materializePresentation(state, ui, presentations) {
|
|
|
2058
2929
|
next: presentations.next?.() ?? []
|
|
2059
2930
|
};
|
|
2060
2931
|
}
|
|
2061
|
-
function makeContext(invocation, def, config,
|
|
2932
|
+
function makeContext(invocation, def, config, capabilities) {
|
|
2062
2933
|
const state = invocation.state;
|
|
2063
|
-
const ui = makeUi(state.colorEnabled);
|
|
2934
|
+
const ui = makeUi(state.colorEnabled, invocation.runtime.stderr);
|
|
2064
2935
|
const present = (outcome, presentations) => {
|
|
2065
2936
|
if (state.delegatedTerminal !== void 0) throw constructionError(`command '${state.commandId}' called ctx.present while a child owned the terminal`);
|
|
2066
2937
|
const exitCode = outcome.exitCode ?? 0;
|
|
@@ -2091,9 +2962,17 @@ function makeContext(invocation, def, config, managesCredentials) {
|
|
|
2091
2962
|
signal: invocation.signal,
|
|
2092
2963
|
cwd: invocation.runtime.cwd,
|
|
2093
2964
|
env: invocation.runtime.env,
|
|
2094
|
-
|
|
2965
|
+
isCI: invocation.runtime.isCI,
|
|
2966
|
+
requireDependency: async (specifier) => {
|
|
2967
|
+
if (dependencyResolvable(specifier, invocation.runtime.cwd)) return okVoid();
|
|
2968
|
+
return notOk(missingDependencyError(specifier, await resolvePackageManager({
|
|
2969
|
+
cwd: invocation.runtime.cwd,
|
|
2970
|
+
env: invocation.runtime.env,
|
|
2971
|
+
host: invocation.runtime.packageManager
|
|
2972
|
+
})));
|
|
2973
|
+
}
|
|
2095
2974
|
};
|
|
2096
|
-
if (managesCredentials) Object.defineProperty(context, "credentialManager", {
|
|
2975
|
+
if (capabilities.managesCredentials) Object.defineProperty(context, "credentialManager", {
|
|
2097
2976
|
enumerable: true,
|
|
2098
2977
|
get() {
|
|
2099
2978
|
const manager = invocation.runtime.credentialManager;
|
|
@@ -2101,6 +2980,10 @@ function makeContext(invocation, def, config, managesCredentials) {
|
|
|
2101
2980
|
return manager;
|
|
2102
2981
|
}
|
|
2103
2982
|
});
|
|
2983
|
+
if (capabilities.installsPackages) Object.defineProperty(context, "packages", {
|
|
2984
|
+
enumerable: true,
|
|
2985
|
+
value: makePackageOperations(invocation)
|
|
2986
|
+
});
|
|
2104
2987
|
return context;
|
|
2105
2988
|
}
|
|
2106
2989
|
//#endregion
|
|
@@ -2128,13 +3011,40 @@ function aliasMap(def) {
|
|
|
2128
3011
|
return aliases;
|
|
2129
3012
|
}
|
|
2130
3013
|
/**
|
|
3014
|
+
* The key a long token marks. Long tokens match in both the kebab and
|
|
3015
|
+
* camel spellings (the scanner allows kebab-for-camel), and a
|
|
3016
|
+
* `--no-<flag>` token marks its base flag. Values are irrelevant here:
|
|
3017
|
+
* `--name=value` is cut at the `=` before matching.
|
|
3018
|
+
*/
|
|
3019
|
+
function longFlagKey(token, declared) {
|
|
3020
|
+
const equals = token.indexOf("=");
|
|
3021
|
+
const raw = token.slice(2, equals === -1 ? void 0 : equals);
|
|
3022
|
+
const exact = camelCase(raw);
|
|
3023
|
+
if (declared.has(exact)) return exact;
|
|
3024
|
+
if (!raw.startsWith("no-")) return;
|
|
3025
|
+
const negated = camelCase(raw.slice(3));
|
|
3026
|
+
return declared.has(negated) ? negated : void 0;
|
|
3027
|
+
}
|
|
3028
|
+
function shortFlagKeys(token, declared, aliases) {
|
|
3029
|
+
const keys = [];
|
|
3030
|
+
for (const char of token.slice(1)) {
|
|
3031
|
+
const key = aliases.get(char);
|
|
3032
|
+
if (key !== void 0 && declared.has(key)) keys.push(key);
|
|
3033
|
+
}
|
|
3034
|
+
return keys;
|
|
3035
|
+
}
|
|
3036
|
+
function tokenFlagKeys(token, declared, aliases) {
|
|
3037
|
+
if (token.startsWith("--")) {
|
|
3038
|
+
const key = longFlagKey(token, declared);
|
|
3039
|
+
return key === void 0 ? [] : [key];
|
|
3040
|
+
}
|
|
3041
|
+
if (token.startsWith("-") && token.length > 1) return shortFlagKeys(token, declared, aliases);
|
|
3042
|
+
return [];
|
|
3043
|
+
}
|
|
3044
|
+
/**
|
|
2131
3045
|
* The flag keys explicitly present on argv, resolved against the
|
|
2132
|
-
* command's declared keys and aliases.
|
|
2133
|
-
*
|
|
2134
|
-
* `--no-<flag>` token marks its base flag, and single-dash tokens are
|
|
2135
|
-
* resolved character-by-character through the alias map. Everything
|
|
2136
|
-
* after a bare `--` is positional and never consulted. Values are
|
|
2137
|
-
* irrelevant here: `--name=value` is cut at the `=` before matching.
|
|
3046
|
+
* command's declared keys and aliases. Everything after a bare `--` is
|
|
3047
|
+
* positional and never consulted.
|
|
2138
3048
|
*/
|
|
2139
3049
|
function explicitFlagKeys(def, declared, argv) {
|
|
2140
3050
|
const declaredSet = new Set(declared);
|
|
@@ -2142,24 +3052,7 @@ function explicitFlagKeys(def, declared, argv) {
|
|
|
2142
3052
|
const explicit = /* @__PURE__ */ new Set();
|
|
2143
3053
|
for (const token of argv) {
|
|
2144
3054
|
if (token === "--") break;
|
|
2145
|
-
|
|
2146
|
-
const equals = token.indexOf("=");
|
|
2147
|
-
const raw = token.slice(2, equals === -1 ? void 0 : equals);
|
|
2148
|
-
const exact = camelCase(raw);
|
|
2149
|
-
if (declaredSet.has(exact)) {
|
|
2150
|
-
explicit.add(exact);
|
|
2151
|
-
continue;
|
|
2152
|
-
}
|
|
2153
|
-
if (raw.startsWith("no-")) {
|
|
2154
|
-
const negated = camelCase(raw.slice(3));
|
|
2155
|
-
if (declaredSet.has(negated)) explicit.add(negated);
|
|
2156
|
-
}
|
|
2157
|
-
continue;
|
|
2158
|
-
}
|
|
2159
|
-
if (token.startsWith("-") && token.length > 1) for (const char of token.slice(1)) {
|
|
2160
|
-
const key = aliases.get(char);
|
|
2161
|
-
if (key !== void 0 && declaredSet.has(key)) explicit.add(key);
|
|
2162
|
-
}
|
|
3055
|
+
for (const key of tokenFlagKeys(token, declaredSet, aliases)) explicit.add(key);
|
|
2163
3056
|
}
|
|
2164
3057
|
return explicit;
|
|
2165
3058
|
}
|
|
@@ -2405,6 +3298,15 @@ function diagnosticOf(error) {
|
|
|
2405
3298
|
const { ok: _ok, ...diagnostic } = error.toEnvelope();
|
|
2406
3299
|
return diagnostic;
|
|
2407
3300
|
}
|
|
3301
|
+
/** The list is typed, but nothing at runtime has checked it: an error
|
|
3302
|
+
* built by another copy of the engine passes `CliStructuredError.is`
|
|
3303
|
+
* on its name, its code and its `toEnvelope` alone, and a handler's
|
|
3304
|
+
* `notOk` failure is checked by nothing at all. A value that is not a
|
|
3305
|
+
* list settles as no accompanying findings rather than reaching the
|
|
3306
|
+
* human renderer and the json envelope as garbage. */
|
|
3307
|
+
function accompanyingFindings(diagnostics) {
|
|
3308
|
+
return Array.isArray(diagnostics) ? diagnostics : [];
|
|
3309
|
+
}
|
|
2408
3310
|
function settleErrored(invocation, error, diagnostics = []) {
|
|
2409
3311
|
const state = invocation.state;
|
|
2410
3312
|
state.settledExitCode = error.code === "CLI.PROMPT_CANCELLED" ? 3 : 2;
|
|
@@ -2412,7 +3314,7 @@ function settleErrored(invocation, error, diagnostics = []) {
|
|
|
2412
3314
|
ok: false,
|
|
2413
3315
|
commandId: state.commandId,
|
|
2414
3316
|
error: diagnosticOf(error),
|
|
2415
|
-
diagnostics,
|
|
3317
|
+
diagnostics: accompanyingFindings(diagnostics),
|
|
2416
3318
|
nextActions: error.nextActions
|
|
2417
3319
|
});
|
|
2418
3320
|
}
|
|
@@ -2444,7 +3346,7 @@ function isAbortCause(cause, signal) {
|
|
|
2444
3346
|
}
|
|
2445
3347
|
function settleThrown(invocation, cause) {
|
|
2446
3348
|
if (isAbortCause(cause, invocation.signal)) settleAborted(invocation);
|
|
2447
|
-
else if (CliStructuredError.is(cause)) settleErrored(invocation, cause);
|
|
3349
|
+
else if (CliStructuredError.is(cause)) settleErrored(invocation, cause, cause.diagnostics);
|
|
2448
3350
|
else settleBug(invocation, cause);
|
|
2449
3351
|
}
|
|
2450
3352
|
function settleAborted(invocation) {
|
|
@@ -2706,6 +3608,7 @@ var EngineImpl = class {
|
|
|
2706
3608
|
delegatedTerminal: void 0,
|
|
2707
3609
|
lastChild: void 0,
|
|
2708
3610
|
activePrompts: 0,
|
|
3611
|
+
packageOperationRunning: false,
|
|
2709
3612
|
deliveredSignal: void 0,
|
|
2710
3613
|
pendingForceExit: void 0,
|
|
2711
3614
|
spawnCredential: void 0
|
|
@@ -2830,25 +3733,71 @@ var EngineImpl = class {
|
|
|
2830
3733
|
hooks.onSettled(summary);
|
|
2831
3734
|
} catch {}
|
|
2832
3735
|
}
|
|
3736
|
+
/** The refusals a maySpawn command can hit before anything runs: a
|
|
3737
|
+
* child's terminal output cannot be framed as a json stream, and a
|
|
3738
|
+
* host that mounts the command without a spawn adapter is
|
|
3739
|
+
* misconfigured. Returns whether the run was settled here. */
|
|
3740
|
+
refuseUnspawnable(invocation, entry) {
|
|
3741
|
+
const state = invocation.state;
|
|
3742
|
+
if (!entry.def.maySpawn) return false;
|
|
3743
|
+
if (formatFlagGiven(state.argv) === "json") {
|
|
3744
|
+
state.format = "human";
|
|
3745
|
+
settleErrored(invocation, jsonUnsupportedError(entry.id));
|
|
3746
|
+
return true;
|
|
3747
|
+
}
|
|
3748
|
+
if (invocation.runtime.spawn === void 0) {
|
|
3749
|
+
state.format = "human";
|
|
3750
|
+
settleBug(invocation, /* @__PURE__ */ new Error(`@prisma/cli-engine: command '${entry.id}' declares maySpawn but the Runtime supplies no spawn adapter`));
|
|
3751
|
+
return true;
|
|
3752
|
+
}
|
|
3753
|
+
return false;
|
|
3754
|
+
}
|
|
3755
|
+
/** Every mounted handler settles the same way: an abandoned child
|
|
3756
|
+
* wins over the handler's own outcome, a failure settles errored, a
|
|
3757
|
+
* child-status value settles as that child's exit, and any other
|
|
3758
|
+
* value is the command's own completion. */
|
|
3759
|
+
async settleHandlerOutcome(invocation, entry, runHandler, settleCompletion) {
|
|
3760
|
+
const state = invocation.state;
|
|
3761
|
+
try {
|
|
3762
|
+
const result = await runHandler();
|
|
3763
|
+
if (await this.settleAbandonedChild(invocation, false)) return;
|
|
3764
|
+
state.resolved = true;
|
|
3765
|
+
if (!result.ok) settleErrored(invocation, result.failure, result.failure.diagnostics);
|
|
3766
|
+
else if (isChildStatusSettlement(result.value)) settleChildStatus(invocation, entry.def, result.value);
|
|
3767
|
+
else settleCompletion(result.value);
|
|
3768
|
+
} catch (cause) {
|
|
3769
|
+
if (await this.settleAbandonedChild(invocation, true, cause)) return;
|
|
3770
|
+
state.resolved = true;
|
|
3771
|
+
settleThrown(invocation, cause);
|
|
3772
|
+
}
|
|
3773
|
+
}
|
|
3774
|
+
/** The telemetry fire, at command start: once per run, from the
|
|
3775
|
+
* parse-time snapshot, before the handler and before any command
|
|
3776
|
+
* output. A CLI that declared no telemetry block reports nothing —
|
|
3777
|
+
* no config read, no disclosure, no mint, no seam call. Runs that
|
|
3778
|
+
* never mount a command never reach here and so never report. */
|
|
3779
|
+
fireTelemetry(invocation) {
|
|
3780
|
+
const { snapshot } = invocation.state;
|
|
3781
|
+
if (this.spec.telemetry === void 0 || snapshot === void 0) return;
|
|
3782
|
+
reportCommandStart({
|
|
3783
|
+
host: invocation.runtime,
|
|
3784
|
+
telemetry: this.spec.telemetry,
|
|
3785
|
+
name: this.spec.name,
|
|
3786
|
+
version: this.spec.version,
|
|
3787
|
+
snapshot
|
|
3788
|
+
});
|
|
3789
|
+
}
|
|
2833
3790
|
async executeMounted(invocation, entry, rawFlags, values) {
|
|
2834
3791
|
const state = invocation.state;
|
|
2835
3792
|
state.commandId = entry.id;
|
|
2836
3793
|
state.docsBaseUrl = entry.docsBaseUrl;
|
|
2837
3794
|
state.snapshot = buildCommandSnapshot(entry.id, entry.def, state.argv, values);
|
|
3795
|
+
this.fireTelemetry(invocation);
|
|
2838
3796
|
if (entry.def.kind === "server-command") {
|
|
2839
3797
|
await this.executeServer(invocation, entry, rawFlags);
|
|
2840
3798
|
return;
|
|
2841
3799
|
}
|
|
2842
|
-
if (
|
|
2843
|
-
state.format = "human";
|
|
2844
|
-
settleErrored(invocation, jsonUnsupportedError(entry.id));
|
|
2845
|
-
return;
|
|
2846
|
-
}
|
|
2847
|
-
if (entry.def.maySpawn && invocation.runtime.spawn === void 0) {
|
|
2848
|
-
state.format = "human";
|
|
2849
|
-
settleBug(invocation, /* @__PURE__ */ new Error(`@prisma/cli-engine: command '${entry.id}' declares maySpawn but the Runtime supplies no spawn adapter`));
|
|
2850
|
-
return;
|
|
2851
|
-
}
|
|
3800
|
+
if (this.refuseUnspawnable(invocation, entry)) return;
|
|
2852
3801
|
let needsOutcome;
|
|
2853
3802
|
try {
|
|
2854
3803
|
applySharedFlags(state, rawFlags, invocation.runtime);
|
|
@@ -2872,34 +3821,12 @@ var EngineImpl = class {
|
|
|
2872
3821
|
flags: declaredFlags(entry.def, rawFlags),
|
|
2873
3822
|
positionals: distributePositionals(entry.def, values)
|
|
2874
3823
|
};
|
|
2875
|
-
const ctx = makeContext(invocation, entry.def, needsOutcome.config, entry.def
|
|
3824
|
+
const ctx = makeContext(invocation, entry.def, needsOutcome.config, declaredCapabilities(entry.def));
|
|
2876
3825
|
if (entry.def.kind === "session-command") {
|
|
2877
|
-
|
|
2878
|
-
const result = await handler(args, ctx);
|
|
2879
|
-
if (await this.settleAbandonedChild(invocation, false)) return;
|
|
2880
|
-
state.resolved = true;
|
|
2881
|
-
if (!result.ok) settleErrored(invocation, result.failure);
|
|
2882
|
-
else if (isChildStatusSettlement(result.value)) settleChildStatus(invocation, entry.def, result.value);
|
|
2883
|
-
else settleSessionCompleted(invocation);
|
|
2884
|
-
} catch (cause) {
|
|
2885
|
-
if (await this.settleAbandonedChild(invocation, true, cause)) return;
|
|
2886
|
-
state.resolved = true;
|
|
2887
|
-
settleThrown(invocation, cause);
|
|
2888
|
-
}
|
|
3826
|
+
await this.settleHandlerOutcome(invocation, entry, () => handler(args, ctx), () => settleSessionCompleted(invocation));
|
|
2889
3827
|
return;
|
|
2890
3828
|
}
|
|
2891
|
-
|
|
2892
|
-
const result = await handler(args, ctx);
|
|
2893
|
-
if (await this.settleAbandonedChild(invocation, false)) return;
|
|
2894
|
-
state.resolved = true;
|
|
2895
|
-
if (!result.ok) settleErrored(invocation, result.failure);
|
|
2896
|
-
else if (isChildStatusSettlement(result.value)) settleChildStatus(invocation, entry.def, result.value);
|
|
2897
|
-
else settleCompleted(invocation, entry.def, result.value);
|
|
2898
|
-
} catch (cause) {
|
|
2899
|
-
if (await this.settleAbandonedChild(invocation, true, cause)) return;
|
|
2900
|
-
state.resolved = true;
|
|
2901
|
-
settleThrown(invocation, cause);
|
|
2902
|
-
}
|
|
3829
|
+
await this.settleHandlerOutcome(invocation, entry, () => handler(args, ctx), (presented) => settleCompleted(invocation, entry.def, presented));
|
|
2903
3830
|
}
|
|
2904
3831
|
/** The handler resolved or threw with its child still live. The
|
|
2905
3832
|
* engine ends that child — SIGTERM, the ruled grace period, then
|
|
@@ -2993,6 +3920,16 @@ function jsonUnsupportedError(commandId) {
|
|
|
2993
3920
|
}]
|
|
2994
3921
|
});
|
|
2995
3922
|
}
|
|
3923
|
+
function declaredCapabilities(def) {
|
|
3924
|
+
if (def.kind !== "result-command") return {
|
|
3925
|
+
managesCredentials: false,
|
|
3926
|
+
installsPackages: false
|
|
3927
|
+
};
|
|
3928
|
+
return {
|
|
3929
|
+
managesCredentials: def.managesCredentials,
|
|
3930
|
+
installsPackages: def.installsPackages
|
|
3931
|
+
};
|
|
3932
|
+
}
|
|
2996
3933
|
function declaredFlags(def, rawFlags) {
|
|
2997
3934
|
const flags = {};
|
|
2998
3935
|
for (const key of Object.keys(def.args.flags)) flags[key] = rawFlags[key];
|
|
@@ -3077,4 +4014,4 @@ function claimedIdentity(token) {
|
|
|
3077
4014
|
};
|
|
3078
4015
|
}
|
|
3079
4016
|
//#endregion
|
|
3080
|
-
export { PRESENTED as _, CONFIG_FILE_NAME as a,
|
|
4017
|
+
export { flag as C, exitWithChildStatus as S, PRESENTED as _, CONFIG_FILE_NAME as a, writeUserConfig as b, PRISMA_CONFIG_VERSION as c, authServiceError as d, credentialRejectedError as f, noSessionForWorkspaceError as g, emptyServiceTokenError as h, buildEngine as i, SERVICE_TOKEN_ENV_VAR as l, credentialsRequiredError as m, claimedIdentity as n, defineConfig as o, credentialWorkspaceMismatchError as p, credentialWorkspaceId as r, loadConfig as s, claimedExpiresAt as t, WORKSPACE_ID_ENV_VAR as u, readUserConfig as v, positional as w, resolveGating as x, userConfigPath as y };
|