@hydranium/cli 1.0.0-next.27 → 1.0.0-next.270

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.
Files changed (196) hide show
  1. package/README.md +73 -204
  2. package/bin/hydranium-cli.js +13 -0
  3. package/lib/cli-version.js.map +1 -1
  4. package/lib/cli.js +2 -1
  5. package/lib/cli.js.map +1 -1
  6. package/lib/commands/analyze-heap-args.js.map +1 -1
  7. package/lib/commands/analyze-heap.d.ts +3 -0
  8. package/lib/commands/analyze-heap.d.ts.map +1 -1
  9. package/lib/commands/analyze-heap.js +2 -1
  10. package/lib/commands/analyze-heap.js.map +1 -1
  11. package/lib/commands/ast-ground-truth-args.d.ts +3 -2
  12. package/lib/commands/ast-ground-truth-args.d.ts.map +1 -1
  13. package/lib/commands/ast-ground-truth-args.js +11 -9
  14. package/lib/commands/ast-ground-truth-args.js.map +1 -1
  15. package/lib/commands/ast-ground-truth-driver.js +2 -1
  16. package/lib/commands/ast-ground-truth-driver.js.map +1 -1
  17. package/lib/commands/ast-ground-truth.js +1 -1
  18. package/lib/commands/ast-ground-truth.js.map +1 -1
  19. package/lib/commands/generate-ast-builder.d.ts +49 -0
  20. package/lib/commands/generate-ast-builder.d.ts.map +1 -0
  21. package/lib/commands/generate-ast-builder.js +200 -0
  22. package/lib/commands/generate-ast-builder.js.map +1 -0
  23. package/lib/commands/generate-transfer-model-args.d.ts.map +1 -1
  24. package/lib/commands/generate-transfer-model-args.js +11 -1
  25. package/lib/commands/generate-transfer-model-args.js.map +1 -1
  26. package/lib/commands/generate-transfer-model-config.d.ts.map +1 -1
  27. package/lib/commands/generate-transfer-model-config.js +6 -3
  28. package/lib/commands/generate-transfer-model-config.js.map +1 -1
  29. package/lib/commands/generate-transfer-model.d.ts +9 -0
  30. package/lib/commands/generate-transfer-model.d.ts.map +1 -1
  31. package/lib/commands/generate-transfer-model.js +18 -4
  32. package/lib/commands/generate-transfer-model.js.map +1 -1
  33. package/lib/commands/harness-args.d.ts +37 -42
  34. package/lib/commands/harness-args.d.ts.map +1 -1
  35. package/lib/commands/harness-args.js +91 -27
  36. package/lib/commands/harness-args.js.map +1 -1
  37. package/lib/commands/headless-harness.d.ts +37 -20
  38. package/lib/commands/headless-harness.d.ts.map +1 -1
  39. package/lib/commands/headless-harness.js +68 -17
  40. package/lib/commands/headless-harness.js.map +1 -1
  41. package/lib/commands/init-args.js +6 -6
  42. package/lib/commands/init-args.js.map +1 -1
  43. package/lib/commands/init-prompt.d.ts.map +1 -1
  44. package/lib/commands/init-templates.d.ts +8 -10
  45. package/lib/commands/init-templates.d.ts.map +1 -1
  46. package/lib/commands/init-templates.js +260 -152
  47. package/lib/commands/init-templates.js.map +1 -1
  48. package/lib/commands/init-wizard.js +4 -4
  49. package/lib/commands/init-wizard.js.map +1 -1
  50. package/lib/commands/init-workspace.d.ts +13 -3
  51. package/lib/commands/init-workspace.d.ts.map +1 -1
  52. package/lib/commands/init-workspace.js +21 -6
  53. package/lib/commands/init-workspace.js.map +1 -1
  54. package/lib/commands/init.d.ts +16 -10
  55. package/lib/commands/init.d.ts.map +1 -1
  56. package/lib/commands/init.js +17 -15
  57. package/lib/commands/init.js.map +1 -1
  58. package/lib/commands/lint-grammar-args.d.ts +3 -2
  59. package/lib/commands/lint-grammar-args.d.ts.map +1 -1
  60. package/lib/commands/lint-grammar-args.js +13 -10
  61. package/lib/commands/lint-grammar-args.js.map +1 -1
  62. package/lib/commands/lint-grammar-driver.js +2 -1
  63. package/lib/commands/lint-grammar-driver.js.map +1 -1
  64. package/lib/commands/lint-grammar-report.js.map +1 -1
  65. package/lib/commands/lint-grammar.js +1 -1
  66. package/lib/commands/lint-grammar.js.map +1 -1
  67. package/lib/commands/measure-memory-args.d.ts +3 -2
  68. package/lib/commands/measure-memory-args.d.ts.map +1 -1
  69. package/lib/commands/measure-memory-args.js +22 -11
  70. package/lib/commands/measure-memory-args.js.map +1 -1
  71. package/lib/commands/measure-memory-driver.js +5 -3
  72. package/lib/commands/measure-memory-driver.js.map +1 -1
  73. package/lib/commands/measure-memory.js +1 -1
  74. package/lib/commands/measure-memory.js.map +1 -1
  75. package/lib/commands/model-docs-args.d.ts +4 -3
  76. package/lib/commands/model-docs-args.d.ts.map +1 -1
  77. package/lib/commands/model-docs-args.js +12 -10
  78. package/lib/commands/model-docs-args.js.map +1 -1
  79. package/lib/commands/model-docs-driver.js +2 -1
  80. package/lib/commands/model-docs-driver.js.map +1 -1
  81. package/lib/commands/model-docs.js +1 -1
  82. package/lib/commands/model-docs.js.map +1 -1
  83. package/lib/commands/projects-args.d.ts.map +1 -1
  84. package/lib/commands/projects-args.js +5 -5
  85. package/lib/commands/projects-args.js.map +1 -1
  86. package/lib/commands/projects.d.ts +3 -4
  87. package/lib/commands/projects.d.ts.map +1 -1
  88. package/lib/commands/projects.js +2 -2
  89. package/lib/commands/projects.js.map +1 -1
  90. package/lib/commands/query-args.d.ts.map +1 -1
  91. package/lib/commands/query-args.js +5 -5
  92. package/lib/commands/query-args.js.map +1 -1
  93. package/lib/commands/query.d.ts +3 -4
  94. package/lib/commands/query.d.ts.map +1 -1
  95. package/lib/commands/query.js +2 -2
  96. package/lib/commands/query.js.map +1 -1
  97. package/lib/commands/reflect-args.d.ts +3 -2
  98. package/lib/commands/reflect-args.d.ts.map +1 -1
  99. package/lib/commands/reflect-args.js +13 -10
  100. package/lib/commands/reflect-args.js.map +1 -1
  101. package/lib/commands/reflect-driver.js +2 -1
  102. package/lib/commands/reflect-driver.js.map +1 -1
  103. package/lib/commands/reflect-report.js.map +1 -1
  104. package/lib/commands/reflect.js +1 -1
  105. package/lib/commands/reflect.js.map +1 -1
  106. package/lib/commands/save-args.d.ts.map +1 -1
  107. package/lib/commands/save-args.js +6 -4
  108. package/lib/commands/save-args.js.map +1 -1
  109. package/lib/commands/save.d.ts +8 -4
  110. package/lib/commands/save.d.ts.map +1 -1
  111. package/lib/commands/save.js +31 -6
  112. package/lib/commands/save.js.map +1 -1
  113. package/lib/commands/validate-args.d.ts +3 -2
  114. package/lib/commands/validate-args.d.ts.map +1 -1
  115. package/lib/commands/validate-args.js +13 -10
  116. package/lib/commands/validate-args.js.map +1 -1
  117. package/lib/commands/validate-driver.js +2 -1
  118. package/lib/commands/validate-driver.js.map +1 -1
  119. package/lib/commands/validate-report.js.map +1 -1
  120. package/lib/commands/validate.js +1 -1
  121. package/lib/commands/validate.js.map +1 -1
  122. package/lib/commands/watch-args.d.ts.map +1 -1
  123. package/lib/commands/watch-args.js +6 -4
  124. package/lib/commands/watch-args.js.map +1 -1
  125. package/lib/commands/watch.d.ts +3 -4
  126. package/lib/commands/watch.d.ts.map +1 -1
  127. package/lib/commands/watch.js +20 -2
  128. package/lib/commands/watch.js.map +1 -1
  129. package/lib/driver-heap.d.ts +52 -0
  130. package/lib/driver-heap.d.ts.map +1 -0
  131. package/lib/driver-heap.js +51 -0
  132. package/lib/driver-heap.js.map +1 -0
  133. package/lib/index.d.ts +1 -0
  134. package/lib/index.d.ts.map +1 -1
  135. package/lib/index.js +1 -0
  136. package/lib/index.js.map +1 -1
  137. package/lib/log-level.d.ts +21 -11
  138. package/lib/log-level.d.ts.map +1 -1
  139. package/lib/log-level.js +25 -14
  140. package/lib/log-level.js.map +1 -1
  141. package/lib/spawn-data-server.d.ts +6 -1
  142. package/lib/spawn-data-server.d.ts.map +1 -1
  143. package/lib/spawn-data-server.js +2 -1
  144. package/lib/spawn-data-server.js.map +1 -1
  145. package/lib/stderr-connection-logger.d.ts +12 -0
  146. package/lib/stderr-connection-logger.d.ts.map +1 -0
  147. package/lib/stderr-connection-logger.js +16 -0
  148. package/lib/stderr-connection-logger.js.map +1 -0
  149. package/lib/testing/echo-server.js +9 -1
  150. package/lib/testing/echo-server.js.map +1 -1
  151. package/package.json +50 -39
  152. package/src/cli.ts +2 -1
  153. package/src/commands/analyze-heap.ts +4 -1
  154. package/src/commands/ast-ground-truth-args.ts +15 -12
  155. package/src/commands/ast-ground-truth-driver.ts +2 -1
  156. package/src/commands/ast-ground-truth.ts +1 -1
  157. package/src/commands/generate-ast-builder.ts +246 -0
  158. package/src/commands/generate-transfer-model-args.ts +11 -1
  159. package/src/commands/generate-transfer-model-config.ts +6 -3
  160. package/src/commands/generate-transfer-model.ts +27 -4
  161. package/src/commands/harness-args.ts +76 -31
  162. package/src/commands/headless-harness.ts +86 -26
  163. package/src/commands/init-args.ts +6 -6
  164. package/src/commands/init-templates.ts +270 -154
  165. package/src/commands/init-wizard.ts +4 -4
  166. package/src/commands/init-workspace.ts +31 -7
  167. package/src/commands/init.ts +36 -26
  168. package/src/commands/lint-grammar-args.ts +17 -13
  169. package/src/commands/lint-grammar-driver.ts +2 -1
  170. package/src/commands/lint-grammar.ts +1 -1
  171. package/src/commands/measure-memory-args.ts +27 -14
  172. package/src/commands/measure-memory-driver.ts +5 -3
  173. package/src/commands/measure-memory.ts +1 -1
  174. package/src/commands/model-docs-args.ts +16 -13
  175. package/src/commands/model-docs-driver.ts +2 -1
  176. package/src/commands/model-docs.ts +1 -1
  177. package/src/commands/projects-args.ts +5 -4
  178. package/src/commands/projects.ts +4 -6
  179. package/src/commands/query-args.ts +5 -4
  180. package/src/commands/query.ts +4 -6
  181. package/src/commands/reflect-args.ts +17 -13
  182. package/src/commands/reflect-driver.ts +2 -1
  183. package/src/commands/reflect.ts +1 -1
  184. package/src/commands/save-args.ts +6 -3
  185. package/src/commands/save.ts +47 -10
  186. package/src/commands/validate-args.ts +17 -13
  187. package/src/commands/validate-driver.ts +2 -1
  188. package/src/commands/validate.ts +1 -1
  189. package/src/commands/watch-args.ts +6 -3
  190. package/src/commands/watch.ts +28 -7
  191. package/src/driver-heap.ts +65 -0
  192. package/src/index.ts +1 -0
  193. package/src/log-level.ts +42 -14
  194. package/src/spawn-data-server.ts +14 -2
  195. package/src/stderr-connection-logger.ts +18 -0
  196. package/src/testing/echo-server.ts +11 -2
@@ -9,6 +9,7 @@
9
9
 
10
10
  import * as fs from 'fs';
11
11
  import * as path from 'path';
12
+ import { emitAstBuilder } from './generate-ast-builder.js';
12
13
  import {
13
14
  type Node,
14
15
  type ObjectLiteralExpression,
@@ -18,6 +19,7 @@ import {
18
19
  type InterfaceDeclaration,
19
20
  type SourceFile
20
21
  } from 'ts-morph';
22
+ import { describeError } from '@hydranium/protocol';
21
23
 
22
24
  /**
23
25
  * Options for the transfer-model generator.
@@ -35,6 +37,15 @@ export interface GenerateTransferModelOptions {
35
37
  augmentationFile: string;
36
38
  /** Destination path for the generated transfer model. */
37
39
  outFile: string;
40
+ /**
41
+ * Destination for a generated AST-node builder module. Omitted by consumers
42
+ * that construct no AST nodes; the transfer model is emitted either way.
43
+ *
44
+ * Emitted from this command rather than its own because the AST source is
45
+ * already parsed here, and because a consumer that regenerates one artefact
46
+ * and not the other has the two disagreeing about the same grammar.
47
+ */
48
+ astBuilderFile?: string;
38
49
  /** Name used for the base element type in the output. Defaults to `TransferElement`. */
39
50
  elementTypeName?: string;
40
51
  /** Name used for the terminal-patterns const in the output. Defaults to `ModelTerminals`. */
@@ -99,7 +110,7 @@ export function watchTransferModel(options: GenerateTransferModelOptions, contex
99
110
  try {
100
111
  generateTransferModel(options);
101
112
  } catch (error: unknown) {
102
- console.error(`Transfer-model generation failed: ${error instanceof Error ? error.message : String(error)}`);
113
+ console.error(`Transfer-model generation failed: ${describeError(error)}`);
103
114
  }
104
115
  };
105
116
  runSafely();
@@ -396,10 +407,22 @@ export function generateTransferModel(options: GenerateTransferModelOptions): vo
396
407
  fs.mkdirSync(path.dirname(options.outFile), { recursive: true });
397
408
  if (fs.existsSync(options.outFile) && fs.readFileSync(options.outFile, 'utf-8') === content) {
398
409
  console.log('Transfer model is up to date.');
399
- return;
410
+ } else {
411
+ fs.writeFileSync(options.outFile, content, 'utf-8');
412
+ console.log(`Generated: ${options.outFile}`);
413
+ }
414
+
415
+ if (options.astBuilderFile !== undefined) {
416
+ const emitted = emitAstBuilder(astSource, {
417
+ outFile: options.astBuilderFile,
418
+ astFile: options.astFile,
419
+ languageId,
420
+ regenCommand
421
+ });
422
+ if (!emitted) {
423
+ console.warn(`Warning: no '${languageId}AstType' alias in ${options.astFile}; skipping the AST builder.`);
424
+ }
400
425
  }
401
- fs.writeFileSync(options.outFile, content, 'utf-8');
402
- console.log(`Generated: ${options.outFile}`);
403
426
  }
404
427
 
405
428
  // ---------------------------------------------------------------------------
@@ -31,11 +31,11 @@
31
31
  * parser rejects is an invocation that fails for no visible reason.
32
32
  */
33
33
 
34
- import type { LogThreshold } from '@hydranium/protocol';
34
+ import { describeError, type LogThreshold } from '@hydranium/protocol';
35
35
  import { statSync } from 'node:fs';
36
36
  import * as path from 'node:path';
37
37
  import { fileURLToPath } from 'node:url';
38
- import { parseLogLevelOption } from '../log-level.js';
38
+ import { type LogOptions, parseLogLevelOption } from '../log-level.js';
39
39
 
40
40
  /**
41
41
  * How a parser reports a usage problem. It never returns, so a parser can treat
@@ -56,9 +56,18 @@ export function exitWithUsage(message: string): never {
56
56
  process.exit(2);
57
57
  }
58
58
 
59
- /** The value flag {@link parseHarnessArgs} consumes itself, before any the subcommand declares. */
59
+ /** Names the head's services module; every harness subcommand requires it. */
60
60
  export const SERVICES_FLAG = '--services';
61
61
 
62
+ /**
63
+ * Registers a loader in the driver child that imports the head. The child is a
64
+ * fresh `node`, so a loader registered on the CLI's own process never reaches it.
65
+ */
66
+ export const IMPORT_FLAG = '--import';
67
+
68
+ /** The value flags {@link parseHarnessArgs} consumes itself, before any the subcommand declares. */
69
+ export const HARNESS_VALUE_FLAGS = [SERVICES_FLAG, IMPORT_FLAG] as const;
70
+
62
71
  /**
63
72
  * The one spelling of the log threshold, shared by both subcommand families.
64
73
  *
@@ -68,33 +77,64 @@ export const SERVICES_FLAG = '--services';
68
77
  * on which command a caller happened to reach for.
69
78
  */
70
79
  export const LOG_LEVEL_FLAG = '--log-level';
80
+ export const LOG_FILE_FLAG = '--log-file';
81
+ export const LOG_FILE_LEVEL_FLAG = '--log-file-level';
82
+
83
+ /** The log flags every subcommand takes, read by {@link logOptions}. */
84
+ export const LOG_VALUE_FLAGS = [LOG_LEVEL_FLAG, LOG_FILE_FLAG, LOG_FILE_LEVEL_FLAG] as const;
71
85
 
72
86
  /** The value flags {@link parseServerSpawnOptions} consumes itself. */
73
- export const SERVER_SPAWN_VALUE_FLAGS: readonly string[] = ['--server', '--cwd', LOG_LEVEL_FLAG];
87
+ export const SERVER_SPAWN_VALUE_FLAGS: readonly string[] = ['--server', '--cwd', ...LOG_VALUE_FLAGS];
74
88
 
75
89
  /**
76
- * The `--help` line every subcommand describes {@link LOG_LEVEL_FLAG} with.
90
+ * The `--help` lines every subcommand describes {@link LOG_VALUE_FLAGS} with.
77
91
  *
78
92
  * Shared text, per-command padding: `command-args.test.ts` compares the flags a
79
93
  * help block DESCRIBES against the flags the parser accepts, and it recognises a
80
- * description by the two-space indent — so the line has to be a real option entry
94
+ * description by the two-space indent — so each line has to be a real option entry
81
95
  * in each block, and the column each block aligns its descriptions at is the
82
- * block's own.
96
+ * block's own. A flag too wide for that column takes its description on the
97
+ * next line, at the column.
83
98
  */
84
- export function logLevelHelpLine(padTo: number): string {
85
- return ` ${`${LOG_LEVEL_FLAG} <lvl>`.padEnd(padTo)}Log threshold for the head this boots (off|error|warn|info|debug|trace).`;
99
+ export function logHelpLines(padTo: number, head = 'the head this boots'): string[] {
100
+ return [
101
+ helpLine(`${LOG_LEVEL_FLAG} <lvl>`, `Log threshold for ${head} (off|error|warn|info|debug|trace).`, padTo),
102
+ helpLine(`${LOG_FILE_FLAG} <file>`, `Also write ${head}'s log to <file>; {workspace} expands to the workspace.`, padTo),
103
+ helpLine(`${LOG_FILE_LEVEL_FLAG} <lvl>`, `Threshold for that file, which the log-level setting does not change.`, padTo)
104
+ ].flat();
105
+ }
106
+
107
+ /** The `--help` lines every `--services` subcommand describes {@link IMPORT_FLAG} with, aligned as {@link logHelpLines}. */
108
+ export function importHelpLines(padTo: number): string[] {
109
+ return helpLine(`${IMPORT_FLAG} <specifier>`, 'Loader to register before importing <module>, e.g. tsx (repeatable).', padTo);
110
+ }
111
+
112
+ function helpLine(flag: string, text: string, padTo: number): string[] {
113
+ return flag.length < padTo - 1 ? [` ${flag.padEnd(padTo)}${text}`] : [` ${flag}`, `${' '.repeat(padTo + 2)}${text}`];
86
114
  }
87
115
 
88
116
  /**
89
- * Narrow a raw {@link LOG_LEVEL_FLAG} value that {@link parseHarnessArgs}
90
- * collected, leaving an absent flag absent.
117
+ * Narrow the raw {@link LOG_VALUE_FLAGS} values a parser collected, leaving
118
+ * absent flags absent.
119
+ *
120
+ * `--log-file` resolves against the caller's directory: the child may run
121
+ * elsewhere (`--cwd`), and a relative path would land the log there.
91
122
  *
92
123
  * Throws rather than reaching `onError`, matching {@link parseServerSpawnOptions}:
93
124
  * the level vocabulary belongs to the protocol package, which reports it, and the
94
125
  * entry point's catch turns that into the same message-and-exit.
95
126
  */
96
- export function logLevelOption(value: string | undefined): LogThreshold | undefined {
97
- return value === undefined ? undefined : parseLogLevelOption(value);
127
+ export function logOptions(options: Readonly<Record<string, string | undefined>>): LogOptions {
128
+ const level = (flag: string): LogThreshold | undefined => {
129
+ const value = options[flag];
130
+ return value === undefined ? undefined : parseLogLevelOption(value, flag);
131
+ };
132
+ const logFile = options[LOG_FILE_FLAG];
133
+ return {
134
+ logLevel: level(LOG_LEVEL_FLAG),
135
+ logFile: logFile === undefined ? undefined : path.resolve(logFile),
136
+ logFileLevel: level(LOG_FILE_LEVEL_FLAG)
137
+ };
98
138
  }
99
139
 
100
140
  /**
@@ -134,6 +174,8 @@ export function printHelp(lines: readonly string[]): void {
134
174
  /** What {@link parseHarnessArgs} recovered from an argv. */
135
175
  export interface ParsedHarnessArgs {
136
176
  servicesModule: string;
177
+ /** The {@link IMPORT_FLAG} values in the order given, absent when there were none. */
178
+ imports?: string[];
137
179
  /**
138
180
  * An absolute directory path, resolved by {@link resolveWorkspaceArgument} and
139
181
  * checked to exist. `''` when the subcommand declared `requireWorkspace: false`.
@@ -163,12 +205,12 @@ export interface HarnessArgsConfig {
163
205
 
164
206
  /**
165
207
  * Shared parser for the headless-harness subcommands. Consumes the required
166
- * `--services <module>` value flag and a single positional `<workspace>`, plus
167
- * the `valueFlags` (each takes a value) and `boolFlags` (presence-only, recorded
168
- * as `'true'`) the subcommand recognises. Unknown flags or a missing
169
- * `--services`/`<workspace>` are usage errors, and so is a `<workspace>` that
170
- * reaches no directory — see {@link resolveWorkspaceArgument} for why that cannot
171
- * be left to the run.
208
+ * `--services <module>` value flag, the repeatable `--import <specifier>` and a
209
+ * single positional `<workspace>`, plus the `valueFlags` (each takes a value)
210
+ * and `boolFlags` (presence-only, recorded as `'true'`) the subcommand
211
+ * recognises. Unknown flags or a missing `--services`/`<workspace>` are usage
212
+ * errors, and so is a `<workspace>` that reaches no directory — see
213
+ * {@link resolveWorkspaceArgument} for why that cannot be left to the run.
172
214
  */
173
215
  export function parseHarnessArgs(
174
216
  args: string[],
@@ -185,6 +227,7 @@ export function parseHarnessArgs(
185
227
  const options: Record<string, string | undefined> = {};
186
228
  const values: Record<string, string[]> = {};
187
229
  let servicesModule: string | undefined;
230
+ let imports: string[] | undefined;
188
231
  let workspace: string | undefined;
189
232
  for (let index = 0; index < args.length; index += 1) {
190
233
  const flag = args[index];
@@ -196,8 +239,10 @@ export function parseHarnessArgs(
196
239
  index += 1;
197
240
  return value;
198
241
  };
199
- if (flag === '--services') {
242
+ if (flag === SERVICES_FLAG) {
200
243
  servicesModule = next();
244
+ } else if (flag === IMPORT_FLAG) {
245
+ (imports ??= []).push(next());
201
246
  } else if (boolSet.has(flag)) {
202
247
  options[flag] = 'true';
203
248
  } else if (repeatableSet.has(flag)) {
@@ -213,7 +258,8 @@ export function parseHarnessArgs(
213
258
  }
214
259
  }
215
260
  return {
216
- servicesModule: assertRequired(servicesModule, '--services', commandName, onError),
261
+ servicesModule: assertRequired(servicesModule, SERVICES_FLAG, commandName, onError),
262
+ imports,
217
263
  workspace: requireWorkspace
218
264
  ? resolveWorkspaceArgument(assertRequired(workspace, '<workspace>', commandName, onError), commandName, onError)
219
265
  : (workspace ?? ''),
@@ -257,7 +303,7 @@ export function resolveWorkspaceArgument(workspace: string, commandName: string,
257
303
  try {
258
304
  stats = statSync(resolved, { throwIfNoEntry: false });
259
305
  } catch (err: unknown) {
260
- const detail = err instanceof Error ? err.message : String(err);
306
+ const detail = describeError(err);
261
307
  onError(`<workspace> is not reachable: '${workspace}' resolved to ${resolved} (${detail}) ${help}`);
262
308
  }
263
309
  if (stats === undefined) {
@@ -276,7 +322,7 @@ function toWorkspacePath(workspace: string, commandName: string, onError: UsageE
276
322
  try {
277
323
  return fileURLToPath(workspace);
278
324
  } catch (err: unknown) {
279
- const detail = err instanceof Error ? err.message : String(err);
325
+ const detail = describeError(err);
280
326
  onError(`<workspace> is not a usable file URI: '${workspace}' (${detail}) (hydranium-cli ${commandName} --help)`);
281
327
  }
282
328
  }
@@ -336,11 +382,10 @@ function kebabToCamel(name: string): string {
336
382
  }
337
383
 
338
384
  /** What {@link parseServerSpawnOptions} recovered from an argv. */
339
- export interface SpawnOptions {
385
+ export interface SpawnOptions extends LogOptions {
340
386
  serverCommand: string;
341
387
  serverArgs: string[];
342
388
  cwd?: string;
343
- logLevel?: LogThreshold;
344
389
  /** The tokens this parser did not claim, for the subcommand's own options. */
345
390
  extra: string[];
346
391
  }
@@ -363,10 +408,10 @@ function isRelativePathToken(token: string, isCommand: boolean): boolean {
363
408
 
364
409
  /**
365
410
  * Shared option parser for subcommands that spawn a data-server child. Consumes
366
- * `--server` / `--cwd` / `--log-level` and returns the surviving tokens as
411
+ * `--server` / `--cwd` / the {@link LOG_VALUE_FLAGS} and returns the surviving tokens as
367
412
  * `extra` for {@link parseFlagOptions} to finish.
368
413
  *
369
- * An unrecognised `--log-level` value throws rather than reaching `onError`: the
414
+ * An unrecognised log level value throws rather than reaching `onError`: the
370
415
  * level vocabulary belongs to the protocol package, which reports it, and the
371
416
  * entry point's catch turns that into the same message-and-exit.
372
417
  *
@@ -383,7 +428,7 @@ function isRelativePathToken(token: string, isCommand: boolean): boolean {
383
428
  export function parseServerSpawnOptions(args: string[], commandName: string, onError: UsageError = exitWithUsage): SpawnOptions {
384
429
  let serverSpec: string | undefined;
385
430
  let cwd: string | undefined;
386
- let logLevel: LogThreshold | undefined;
431
+ const logFlags: Record<string, string> = {};
387
432
  const extra: string[] = [];
388
433
  for (let index = 0; index < args.length; index += 1) {
389
434
  const flag = args[index];
@@ -399,8 +444,8 @@ export function parseServerSpawnOptions(args: string[], commandName: string, onE
399
444
  serverSpec = claim();
400
445
  } else if (flag === '--cwd') {
401
446
  cwd = claim();
402
- } else if (flag === LOG_LEVEL_FLAG) {
403
- logLevel = parseLogLevelOption(claim());
447
+ } else if ((LOG_VALUE_FLAGS as readonly string[]).includes(flag)) {
448
+ logFlags[flag] = claim();
404
449
  } else {
405
450
  extra.push(flag);
406
451
  }
@@ -425,5 +470,5 @@ export function parseServerSpawnOptions(args: string[], commandName: string, onE
425
470
  );
426
471
  }
427
472
  }
428
- return { serverCommand, serverArgs, cwd, logLevel, extra };
473
+ return { serverCommand, serverArgs, cwd, ...logOptions(logFlags), extra };
429
474
  }
@@ -8,14 +8,14 @@
8
8
  ********************************************************************************/
9
9
 
10
10
  import type * as HydraniumCoreNode from '@hydranium/core/node';
11
- import type { LogThreshold } from '@hydranium/protocol';
12
11
  import { spawn } from 'node:child_process';
13
12
  import { statSync, writeFileSync } from 'node:fs';
14
13
  import { createRequire } from 'node:module';
15
14
  import * as path from 'node:path';
16
15
  import { pathToFileURL } from 'node:url';
17
- import { logLevelEnv } from '../log-level.js';
18
- import { SERVICES_FLAG } from './harness-args.js';
16
+ import { driverHeapArgs, type HeapReading } from '../driver-heap.js';
17
+ import { logEnv, type LogOptions } from '../log-level.js';
18
+ import { IMPORT_FLAG, SERVICES_FLAG } from './harness-args.js';
19
19
 
20
20
  /** The flag every report-producing subcommand names its destination file with. */
21
21
  export const OUT_FILE_FLAG = '--out-file';
@@ -43,13 +43,15 @@ export interface HeadlessContext {
43
43
  * its `createServices` thunk AND the `@hydranium/core/node` harness, both
44
44
  * resolved from the HEAD's location rather than the CLI's.
45
45
  *
46
- * The module is an ESM file (typically a compiled `lib/*.js`) exporting a
47
- * ZERO-ARG `createServices(): { shared }`. `hydranium-cli` is language-agnostic
46
+ * The module is an ESM file exporting a ZERO-ARG `createServices(): { shared }`:
47
+ * a compiled `lib/*.js`, or the TypeScript source when the child was started
48
+ * with a loader through {@link IMPORT_FLAG}. `hydranium-cli` is language-agnostic
48
49
  * and cannot statically import a head's `create<Lang>Services`, so the dynamic
49
50
  * import is the seam that keeps the binary head-neutral. Resolving the harness
50
- * from the head's graph (via `createRequire` rooted at the head module) means the
51
- * CLI itself needs no `@hydranium/core` dependency — the head always has it — and
52
- * guarantees the harness runs against the SAME core copy as `createServices`.
51
+ * from the head's graph (via `createRequire` rooted at the head module) rather
52
+ * than from the CLI's guarantees it runs against the SAME core copy as
53
+ * `createServices`, which a second copy of the module would break even where
54
+ * both resolve.
53
55
  *
54
56
  * Throws a clear error when the path names no file, and when the export is
55
57
  * missing or not a function.
@@ -64,12 +66,17 @@ export async function loadHeadlessContext(servicesModule: string): Promise<Headl
64
66
  if (stats === undefined || !stats.isFile()) {
65
67
  throw new Error(
66
68
  `${SERVICES_FLAG} must name an existing ESM file; '${servicesModule}' resolved to ${modulePath}, ` +
67
- `which is not one. Pass the head's COMPILED entry, e.g. \`${SERVICES_FLAG} ./lib/services.js\` — ` +
68
- 'not the TypeScript source and not a package name.'
69
+ `which is not one. Pass the path of the head's entry, e.g. \`${SERVICES_FLAG} ./lib/services.js\` — ` +
70
+ 'not a package name.'
69
71
  );
70
72
  }
71
73
  const moduleUrl = pathToFileURL(modulePath).href;
72
- const imported = (await import(moduleUrl)) as Record<string, unknown>;
74
+ let imported: Record<string, unknown>;
75
+ try {
76
+ imported = (await import(moduleUrl)) as Record<string, unknown>;
77
+ } catch (err: unknown) {
78
+ throw withLoaderHint(err, modulePath);
79
+ }
73
80
  const createServices = imported.createServices;
74
81
  if (typeof createServices !== 'function') {
75
82
  throw new Error(
@@ -84,6 +91,32 @@ export async function loadHeadlessContext(servicesModule: string): Promise<Headl
84
91
  return { createServices: createServices as ServicesFactory, coreNode };
85
92
  }
86
93
 
94
+ /** The codes Node fails a TypeScript entry with when no loader handled it, depending on its version. */
95
+ const UNLOADED_TYPESCRIPT_CODES: ReadonlySet<string> = new Set([
96
+ 'ERR_UNKNOWN_FILE_EXTENSION',
97
+ 'ERR_MODULE_NOT_FOUND',
98
+ 'ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX'
99
+ ]);
100
+
101
+ /**
102
+ * Append a loader hint to a TypeScript entry's import failure, unless the child
103
+ * was started with {@link IMPORT_FLAG}, where the hint would only repeat the
104
+ * caller's own command. Appended rather than substituted: a loader set through
105
+ * `NODE_OPTIONS` does not show in `execArgv`, and under it the failure can be a
106
+ * missing module that only Node's own message names.
107
+ */
108
+ function withLoaderHint(err: unknown, modulePath: string): unknown {
109
+ if (!(err instanceof Error) || !/\.[cm]?ts$/.test(modulePath) || !('code' in err) || typeof err.code !== 'string') {
110
+ return err;
111
+ }
112
+ if (!UNLOADED_TYPESCRIPT_CODES.has(err.code) || process.execArgv.some(arg => arg.startsWith(IMPORT_FLAG))) {
113
+ return err;
114
+ }
115
+ return new Error(`${err.message}\nA TypeScript head needs a loader registered with \`${IMPORT_FLAG}\`, e.g. \`${IMPORT_FLAG} tsx\`.`, {
116
+ cause: err
117
+ });
118
+ }
119
+
87
120
  /**
88
121
  * Child-side: deliver a finished report — to `outFile` when one was named, else
89
122
  * to stdout.
@@ -129,37 +162,64 @@ export function spawnNodeChild(execArgs: string[], env?: Record<string, string>)
129
162
  /** How a subcommand parent reaches its driver child; the seam `__spawnForTest` replaces. */
130
163
  export type SpawnDriverChild = (execArgs: string[], env?: Record<string, string>) => Promise<number>;
131
164
 
132
- /** What every `--services` subcommand carries for the child it spawns, on top of its own options. */
133
- export interface DriverSpawnOptions {
134
- /**
135
- * Log threshold for the head the driver boots. Absent leaves the child's
136
- * inherited environment alone, so an ambient `HYDRANIUM_LOG_LEVEL` still wins
137
- * where a caller set one.
138
- */
139
- readonly logLevel?: LogThreshold;
165
+ /**
166
+ * What every `--services` subcommand carries for the child it spawns, on top of
167
+ * its own options. The log options apply to the head the driver boots; an
168
+ * absent one leaves the child's inherited environment alone, so an ambient
169
+ * variable still wins where a caller set one.
170
+ */
171
+ export interface DriverSpawnOptions extends LogOptions {
172
+ /** Specifiers the driver child registers with Node's `--import`, in order, before it imports the head. */
173
+ readonly imports?: readonly string[];
140
174
  /** Test-only: capture the node argv and env instead of spawning the real child. */
141
175
  readonly __spawnForTest?: SpawnDriverChild;
176
+ /**
177
+ * Test-only: decide the heap ceiling from a stated cgroup reading rather
178
+ * than the machine's. Without it an argv assertion means one thing on a
179
+ * workstation and nothing at all under a memory limit, so a suite that
180
+ * asserts the ceiling is present passes vacuously in a container.
181
+ */
182
+ readonly __heapReadingForTest?: HeapReading;
142
183
  }
143
184
 
144
185
  /**
145
186
  * Parent-side: run a subcommand's driver child and propagate its exit code, so a
146
187
  * shell or CI step sees the gate.
147
188
  *
148
- * **The log threshold travels in the child's ENVIRONMENT, not its argv.** The
149
- * head's logger reads `HYDRANIUM_LOG_LEVEL` while `createServices` constructs it,
150
- * which is inside the driver's dynamic import — a flag the driver parsed would
151
- * arrive after the only moment it can be read. Setting it here rather than
152
- * exporting it also leaves the CLI's own output alone, unlike an ambient
153
- * `HYDRANIUM_LOG_LEVEL=…` the parent would carry too.
189
+ * **The log options travel in the child's ENVIRONMENT, not its argv.** The
190
+ * head's logger reads its `HYDRANIUM_LOG_*` variables while `createServices`
191
+ * constructs it, which is inside the driver's dynamic import — a flag the driver
192
+ * parsed would arrive after the only moment it can be read. Setting them here
193
+ * rather than exporting them also leaves the CLI's own output alone, unlike
194
+ * ambient variables the parent would carry too.
154
195
  *
155
196
  * A head that binds a logger of its own decides for itself whether the flag means
156
197
  * anything, which is the intended seam: the CLI is language-agnostic and cannot
157
198
  * reach past `createServices`.
199
+ *
200
+ * **The heap ceiling is prepended HERE rather than passed by each subcommand.**
201
+ * Every caller wants the same answer to the same question, and the answer is not
202
+ * a constant — see {@link driverHeapArgs}. A literal at the call site is a place
203
+ * the container case can be missed, and the caller that misses it takes the whole
204
+ * cgroup down with it rather than failing on its own.
158
205
  */
159
206
  export async function runDriverChild(execArgs: string[], options: DriverSpawnOptions): Promise<void> {
160
207
  const spawnChild = options.__spawnForTest ?? spawnNodeChild;
161
- const code = await spawnChild(execArgs, options.logLevel === undefined ? undefined : logLevelEnv(options.logLevel));
208
+ const importArgs = (options.imports ?? []).map(specifier => `${IMPORT_FLAG}=${loaderSpecifier(specifier)}`);
209
+ const code = await spawnChild([...driverHeapArgs(options.__heapReadingForTest), ...importArgs, ...execArgs], logEnv(options));
162
210
  if (code) {
163
211
  process.exitCode = code;
164
212
  }
165
213
  }
214
+
215
+ /**
216
+ * A path, absolute or `./`/`../` relative with either separator, becomes a file
217
+ * URL resolved against the caller's directory. Node reads `--import` as a URL or
218
+ * package specifier, so it would take a Windows `C:\…` for the URL scheme `c:`
219
+ * and a `.\…` for a package name. Any other specifier passes through as Node
220
+ * reads it.
221
+ */
222
+ function loaderSpecifier(specifier: string): string {
223
+ const isPath = path.isAbsolute(specifier) || /^\.\.?[\\/]/.test(specifier);
224
+ return isPath ? pathToFileURL(path.resolve(specifier)).href : specifier;
225
+ }
@@ -208,10 +208,10 @@ export const INIT_HELP: readonly string[] = [
208
208
  ' --force Scaffold into a non-empty directory anyway.',
209
209
  ' --monorepo Scaffold a member of the surrounding npm workspace:',
210
210
  ' tsconfig extends the root config that actually carries',
211
- ' compilerOptions, .gitignore is left to the root, and the',
212
- ' scripts address the package by --prefix. Errors when no',
213
- ' ancestor declares `workspaces`. Never writes outside the',
214
- ' target — the root `workspaces` entry is printed, not added.',
211
+ ' compilerOptions, and the scripts address the package by',
212
+ ' --prefix. Errors when no ancestor declares `workspaces`.',
213
+ ' Never writes outside the target — the root `workspaces`',
214
+ ' entry and dependency pins are printed, not added.',
215
215
  ' --scope <@scope> npm scope for the package name, e.g. @acme. Not inferred:',
216
216
  ' a root manifest is usually named for the repo, not the',
217
217
  ' scope. The wizard offers the siblings’ scope as a default.',
@@ -233,8 +233,8 @@ export const INIT_HELP: readonly string[] = [
233
233
  ' (default: <project-id>-<grammar-id>).',
234
234
  ' --diagram Scaffold a GLSP diagram for that grammar. Requires',
235
235
  ' `glsp` in --heads. Derived when there is exactly one',
236
- ' grammar; required when there are several, because a',
237
- ' diagram type binds exactly one grammar.',
236
+ ' grammar; required when there are several, because the',
237
+ ' scaffolded diagram is typed over one grammar’s model.',
238
238
  '',
239
239
  'Example — three grammars, one with a differing extension, three heads:',
240
240
  ' hydranium-cli init ./order-flow --name OrderFlow --heads lsp,data,glsp \\',