@monster-orange/den 0.4.5 → 0.4.6

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.
@@ -15,7 +15,10 @@ import * as fs from "node:fs";
15
15
  import * as path from "node:path";
16
16
  import { ENGINE_PACKAGE, getEngineRoot } from "../../shared/paths.js";
17
17
  import { profileFrom } from "../../shared/profile.js";
18
+ import { parse as parseYaml } from "yaml";
18
19
  import { plural } from "../../shared/plural.js";
20
+ import { DEFAULT_PORT } from "../../shared/constants.js";
21
+ import { pickFreePort } from "../../shared/port-pick.js";
19
22
 
20
23
  export interface CreateOptions {
21
24
  /** Product name: lower-case `[a-z][a-z0-9-]*`. */
@@ -296,18 +299,34 @@ export function writeDistribution(dir: string, opts: CreateOptions): string[] {
296
299
 
297
300
  export interface ParsedCreateArgs extends Omit<CreateOptions, "denVersion"> {
298
301
  dir: string;
302
+ /** `--yes`: never prompt (create never does; accepted so agents can say so). */
303
+ yes: boolean;
304
+ /** `--json`: print one JSON object on stdout, nothing else. */
305
+ json: boolean;
306
+ /** `--port <n>`: the web UI's port, taken as given (no search). */
307
+ port?: number;
299
308
  }
300
309
 
301
310
  const CREATE_USAGE =
302
- "usage: den create <name> [dir] [--title <title>] [--cli <cli>] [--env-prefix <PREFIX>]";
311
+ "usage: den create <name> [dir] [--title <title>] [--cli <cli>] [--env-prefix <PREFIX>] [--port <n>] [--yes] [--json]";
303
312
 
304
313
  /** Parse `create` argv (after the word `create`). `cwd` resolves the dir. */
305
314
  export function parseCreateArgs(argv: string[], cwd: string): ParsedCreateArgs {
306
315
  const positional: string[] = [];
307
316
  const flags: Record<string, string> = {};
317
+ let yes = false;
318
+ let json = false;
308
319
  for (let i = 0; i < argv.length; i++) {
309
320
  const a = argv[i];
310
- const m = /^--(title|cli|env-prefix)(?:=(.*))?$/.exec(a);
321
+ if (a === "--yes" || a === "-y") {
322
+ yes = true;
323
+ continue;
324
+ }
325
+ if (a === "--json") {
326
+ json = true;
327
+ continue;
328
+ }
329
+ const m = /^--(title|cli|env-prefix|port)(?:=(.*))?$/.exec(a);
311
330
  if (m) {
312
331
  const value = m[2] ?? argv[++i];
313
332
  if (value === undefined)
@@ -326,9 +345,19 @@ export function parseCreateArgs(argv: string[], cwd: string): ParsedCreateArgs {
326
345
  title: flags.title,
327
346
  cli: flags.cli,
328
347
  envPrefix: flags["env-prefix"],
348
+ yes,
349
+ json,
350
+ ...(flags.port !== undefined ? { port: parsePortFlag(flags.port) } : {}),
329
351
  };
330
352
  }
331
353
 
354
+ function parsePortFlag(v: string): number {
355
+ const n = Number(v);
356
+ if (!/^\d+$/.test(v) || n < 1 || n > 65535)
357
+ throw new Error(`--port must be a port number\n${CREATE_USAGE}`);
358
+ return n;
359
+ }
360
+
332
361
  export const CREATE_HELP = `${CREATE_USAGE}
333
362
 
334
363
  Scaffold a new Den distribution in [dir] (default ./<name>):
@@ -338,41 +367,245 @@ config/environments/local.json, manifests/ and tests/pillars/.
338
367
 
339
368
  --title <title> brand name shown to people (default: <Name>)
340
369
  --cli <cli> the command people type (default: <name>)
341
- --env-prefix <PREFIX> env-var prefix (default: <NAME>)`;
370
+ --env-prefix <PREFIX> env-var prefix (default: <NAME>)
371
+ --port <n> the web UI's port (default: ${DEFAULT_PORT}, or the next
372
+ free one above it when that is in use)
373
+ --yes, -y never prompt (create never does; for agents)
374
+ --json print one JSON object: { ok, dir, cli, existed, port, next }
375
+
376
+ Safe to re-run: a [dir] that already holds this distribution answers ok
377
+ (existed: true) and writes nothing.`;
378
+
379
+ /**
380
+ * Whether `dir` already holds the distribution `create` would make: its
381
+ * den.yml names this product (and this cli, when one was given).
382
+ */
383
+ export function existingDistribution(
384
+ dir: string,
385
+ opts: { name: string; cli?: string },
386
+ ): boolean {
387
+ let text: string;
388
+ try {
389
+ text = fs.readFileSync(path.join(dir, "den.yml"), "utf-8");
390
+ } catch {
391
+ return false;
392
+ }
393
+ const name = /^\s+name:\s*(\S+)\s*$/m.exec(text)?.[1];
394
+ if (name !== opts.name) return false;
395
+ const cli = /^\s+cli:\s*(\S+)\s*$/m.exec(text)?.[1] ?? opts.name;
396
+ return cli === (opts.cli ?? opts.name);
397
+ }
398
+
399
+ /** What `create --json` prints. */
400
+ export interface CreateJson {
401
+ ok: boolean;
402
+ dir?: string;
403
+ cli?: string;
404
+ existed?: boolean;
405
+ files?: number;
406
+ /** The web UI's port for this distribution. */
407
+ port?: number;
408
+ /** Set when DEFAULT_PORT was in use and `port` was picked instead. */
409
+ portPicked?: { wanted: number; file: string };
410
+ next?: string[];
411
+ error?: string;
412
+ fix?: string;
413
+ ask?: string;
414
+ }
415
+
416
+ function cdPathFor(cwd: string, dir: string): string {
417
+ // Relative only when the new directory is under the caller's cwd; a
418
+ // path that climbs out (../../..) is harder to read than the absolute one.
419
+ const rel = path.relative(cwd, dir);
420
+ return rel === ""
421
+ ? "."
422
+ : rel.startsWith("..") || path.isAbsolute(rel)
423
+ ? dir
424
+ : rel;
425
+ }
426
+
427
+ /** The web port a distribution at `dir` is set to (user, then team, config). */
428
+ export function configuredWebPort(dir: string, name: string): number {
429
+ for (const rel of [
430
+ path.join("config", "local", "user.yml"),
431
+ path.join("config", `${name}.yml`),
432
+ ]) {
433
+ try {
434
+ const doc = parseYaml(fs.readFileSync(path.join(dir, rel), "utf-8")) as {
435
+ web?: { port?: unknown };
436
+ } | null;
437
+ const p = doc?.web?.port;
438
+ if (typeof p === "number" && Number.isInteger(p)) return p;
439
+ } catch {
440
+ /* absent or unreadable: next layer */
441
+ }
442
+ }
443
+ return DEFAULT_PORT;
444
+ }
445
+
446
+ export interface CreatePortOptions {
447
+ /**
448
+ * The port to start from: DEFAULT_PORT unless a test passes a port of its
449
+ * own (so no test probes the real default). The CLI reads it from
450
+ * DEN_WEB_PORT_START.
451
+ */
452
+ startPort?: number;
453
+ inUse?: (port: number) => Promise<boolean>;
454
+ }
342
455
 
343
- /** Run `create` end to end; returns the process exit code. */
344
- export function runCreate(argv: string[], cwd: string): number {
456
+ /** DEN_WEB_PORT_START, for tests and CI: never probe the real default. */
457
+ export function startPortFromEnv(
458
+ env: NodeJS.ProcessEnv = process.env,
459
+ ): number | undefined {
460
+ const v = env.DEN_WEB_PORT_START?.trim();
461
+ if (!v) return undefined;
462
+ const n = Number(v);
463
+ return Number.isInteger(n) && n > 0 && n < 65536 ? n : undefined;
464
+ }
465
+
466
+ /**
467
+ * `create` as data: never prompts, never throws. A dir that already holds
468
+ * this distribution is ok (existed); any other non-empty dir is an error
469
+ * that names a fix.
470
+ *
471
+ * The web port: `--port` as given; else the default when it is free, else
472
+ * the next free port above it (never a reserved Den port), written to the
473
+ * new distribution's config/local/user.yml as `web.port`. Checking a port
474
+ * sends nothing to whoever holds it (port-pick.ts). A re-run reports the
475
+ * port the distribution is set to and picks nothing.
476
+ */
477
+ export async function createResult(
478
+ args: ParsedCreateArgs,
479
+ cwd: string,
480
+ denVersion: string = runningDenVersion(),
481
+ portOpts: CreatePortOptions = {},
482
+ ): Promise<CreateJson> {
483
+ const cli = args.cli ?? args.name;
484
+ const next = (dir: string): string[] => [
485
+ `cd ${cdPathFor(cwd, dir)}`,
486
+ "npm install",
487
+ `./bin/${cli} repo add <path|git-url> --json`,
488
+ ];
489
+ try {
490
+ validateCreateOptions({ ...args, denVersion });
491
+ if (existingDistribution(args.dir, args))
492
+ return {
493
+ ok: true,
494
+ dir: args.dir,
495
+ cli,
496
+ existed: true,
497
+ port: configuredWebPort(args.dir, args.name),
498
+ next: next(args.dir),
499
+ };
500
+ // Refuse a busy dir before looking for a port.
501
+ if (
502
+ fs.existsSync(args.dir) &&
503
+ fs.statSync(args.dir).isDirectory() &&
504
+ fs.readdirSync(args.dir).length > 0
505
+ )
506
+ writeDistribution(args.dir, { ...args, denVersion }); // throws "not empty"
507
+ const wanted = args.port ?? portOpts.startPort ?? DEFAULT_PORT;
508
+ const port =
509
+ args.port ??
510
+ (await pickFreePort({ start: wanted, inUse: portOpts.inUse })) ??
511
+ wanted;
512
+ const written = writeDistribution(args.dir, { ...args, denVersion });
513
+ let portPicked: CreateJson["portPicked"];
514
+ // Written when the port differs from what the distribution would use
515
+ // anyway (an explicit --port that isn't the default, or a picked one).
516
+ if (port !== (args.port !== undefined ? DEFAULT_PORT : wanted)) {
517
+ const rel = path.join("config", "local", "user.yml");
518
+ const file = path.join(args.dir, rel);
519
+ fs.mkdirSync(path.dirname(file), { recursive: true });
520
+ fs.writeFileSync(
521
+ file,
522
+ `# Yours, not the team's (gitignored). Set by create: ${
523
+ args.port !== undefined
524
+ ? "--port"
525
+ : `port ${wanted} was in use on this machine`
526
+ }.\nweb:\n port: ${port}\n`,
527
+ { encoding: "utf-8", flag: "wx" },
528
+ );
529
+ written.push(rel);
530
+ if (args.port === undefined) portPicked = { wanted, file: rel };
531
+ }
532
+ return {
533
+ ok: true,
534
+ dir: args.dir,
535
+ cli,
536
+ existed: false,
537
+ files: written.length,
538
+ port,
539
+ ...(portPicked ? { portPicked } : {}),
540
+ next: next(args.dir),
541
+ };
542
+ } catch (e) {
543
+ const error = (e as Error).message;
544
+ if (/is not empty/.test(error))
545
+ return {
546
+ ok: false,
547
+ error,
548
+ fix: `npx ${ENGINE_PACKAGE} create ${args.name} ${path.join(path.dirname(args.dir), `${args.name}-den`)} --yes --json`,
549
+ ask: `${args.dir} already has files that are not a ${args.name} distribution. Which new or empty folder should the distribution go in?`,
550
+ };
551
+ return {
552
+ ok: false,
553
+ error,
554
+ ask: "Which name should the distribution have? Lower-case letters, digits and dashes, starting with a letter.",
555
+ };
556
+ }
557
+ }
558
+
559
+ /** Run `create` end to end; resolves to the process exit code. */
560
+ export async function runCreate(
561
+ argv: string[],
562
+ cwd: string,
563
+ portOpts: CreatePortOptions = { startPort: startPortFromEnv() },
564
+ ): Promise<number> {
345
565
  if (argv.includes("-h") || argv.includes("--help")) {
346
566
  console.log(CREATE_HELP);
347
567
  return 0;
348
568
  }
569
+ const json = argv.includes("--json");
570
+ let args: ParsedCreateArgs;
349
571
  try {
350
- const args = parseCreateArgs(argv, cwd);
351
- const written = writeDistribution(args.dir, {
352
- ...args,
353
- denVersion: runningDenVersion(),
354
- });
355
- const cli = args.cli ?? args.name;
356
- // Relative only when the new directory is under the caller's cwd; a
357
- // path that climbs out (../../..) is harder to read than the absolute one.
358
- const rel = path.relative(cwd, args.dir);
359
- const cdPath =
360
- rel === ""
361
- ? "."
362
- : rel.startsWith("..") || path.isAbsolute(rel)
363
- ? args.dir
364
- : rel;
365
- console.log(
366
- `Created ${args.name} in ${args.dir} (${plural(written.length, "file")}).`,
367
- );
368
- console.log("");
369
- console.log("Next:");
370
- console.log(` cd ${cdPath}`);
371
- console.log(" npm install");
372
- console.log(` ./bin/${cli} --help`);
373
- return 0;
572
+ args = parseCreateArgs(argv, cwd);
374
573
  } catch (e) {
375
- console.error(`create: ${(e as Error).message}`);
574
+ if (json) {
575
+ console.log(
576
+ JSON.stringify({
577
+ ok: false,
578
+ error: (e as Error).message.split("\n")[0],
579
+ fix: `npx ${ENGINE_PACKAGE} create <name> [dir] --yes --json`,
580
+ } satisfies CreateJson),
581
+ );
582
+ } else console.error(`create: ${(e as Error).message}`);
583
+ return 1;
584
+ }
585
+ const r = await createResult(args, cwd, runningDenVersion(), portOpts);
586
+ if (json) {
587
+ console.log(JSON.stringify(r));
588
+ return r.ok ? 0 : 1;
589
+ }
590
+ if (!r.ok) {
591
+ console.error(`create: ${r.error}`);
376
592
  return 1;
377
593
  }
594
+ const dir = r.dir ?? args.dir;
595
+ console.log(
596
+ r.existed
597
+ ? `${args.name} is already in ${dir}: nothing to do.`
598
+ : `Created ${args.name} in ${dir} (${plural(r.files ?? 0, "file")}).`,
599
+ );
600
+ console.log(
601
+ r.portPicked
602
+ ? `Web UI port: ${r.port} (${r.portPicked.wanted} is in use here; set in ${r.portPicked.file}).`
603
+ : `Web UI port: ${r.port}.`,
604
+ );
605
+ console.log("");
606
+ console.log("Next:");
607
+ console.log(` cd ${cdPathFor(cwd, dir)}`);
608
+ console.log(" npm install");
609
+ console.log(` ./bin/${r.cli} --help`);
610
+ return 0;
378
611
  }
@@ -40,6 +40,14 @@ import {
40
40
  DEFAULT_RUNTIME_ID,
41
41
  RUNTIME_KIND,
42
42
  } from "../../shared/agents/runtimes.js";
43
+ import {
44
+ defaultOnboardDeps,
45
+ onboardingChecks,
46
+ runnableCli,
47
+ type OnboardCheck,
48
+ type OnboardDeps,
49
+ } from "./doctor-onboard.js";
50
+ import { printJson } from "./json-out.js";
43
51
 
44
52
  export interface DoctorRow {
45
53
  kind: string;
@@ -219,15 +227,70 @@ function readConfigOrNull(): DenConfig | null {
219
227
  }
220
228
  }
221
229
 
230
+ /** A strategy row as a `doctor --json` check. */
231
+ export function strategyCheck(r: DoctorRow): OnboardCheck {
232
+ const sel = r.selected
233
+ ? `${r.selected}${r.implicit ? " (implicit)" : ""}`
234
+ : "(invalid config)";
235
+ const why = r.reason ? `: ${r.reason}` : "";
236
+ return {
237
+ id: `strategy:${r.kind}`,
238
+ status: r.problem
239
+ ? "fail"
240
+ : !r.available || r.warnings.length
241
+ ? "warn"
242
+ : "ok",
243
+ detail: `${sel} ${r.available ? "available" : "unavailable"}${why}${r.warnings.length ? ` (${r.warnings.join("; ")})` : ""}`,
244
+ };
245
+ }
246
+
247
+ /**
248
+ * `doctor --json` (ON-2): one JSON object, `{ ok, checks }`. The
249
+ * onboarding checks first (doctor-onboard.ts), then every strategy row;
250
+ * ok is false when any check fails. Exit 1 then, as for the human doctor.
251
+ */
252
+ export async function doctorJson(
253
+ deps: OnboardDeps,
254
+ rows: DoctorRow[] = doctorRows(),
255
+ ): Promise<{ ok: boolean; checks: OnboardCheck[] }> {
256
+ const checks = [
257
+ ...(await onboardingChecks(deps)),
258
+ ...rows.map(strategyCheck),
259
+ ];
260
+ return { ok: !checks.some((c) => c.status === "fail"), checks };
261
+ }
262
+
222
263
  export async function cmdDoctor(args: string[]): Promise<number> {
223
264
  const probe = args.includes("--probe");
224
- const unknown = args.filter((a) => a !== "--probe");
265
+ const json = args.includes("--json");
266
+ const unknown = args.filter((a) => a !== "--probe" && a !== "--json");
225
267
  if (unknown.length) {
226
- console.error(
227
- `✗ doctor: unknown argument ${unknown[0]} (doctor [--probe])`,
228
- );
268
+ const msg = `unknown argument ${unknown[0]} (doctor [--probe] [--json])`;
269
+ if (json)
270
+ printJson({
271
+ ok: false,
272
+ error: msg,
273
+ fix: `${runnableCli()} doctor --json`,
274
+ });
275
+ else console.error(`✗ doctor: ${msg}`);
229
276
  return 2;
230
277
  }
278
+ if (json) {
279
+ if (probe) {
280
+ printJson({
281
+ ok: false,
282
+ error: "--probe and --json do not combine: the probe starts sessions",
283
+ fix: `${runnableCli()} doctor --json`,
284
+ });
285
+ return 2;
286
+ }
287
+ const { probeWebServer } = await import("./mcp-connect.js");
288
+ const r = await doctorJson(
289
+ defaultOnboardDeps((url) => probeWebServer(url)),
290
+ );
291
+ printJson(r);
292
+ return r.ok ? 0 : 1;
293
+ }
231
294
  const rows = doctorRows();
232
295
  for (const l of formatDoctor(rows, readBrand().name)) console.log(l);
233
296
  const cli = product().cli;