@kjanat/dreamcli 2.5.0 → 3.0.0-rc.10

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 (202) hide show
  1. package/CHANGELOG.md +344 -1
  2. package/README.md +9 -2
  3. package/dist/core/cli/dispatch.d.mts +6 -5
  4. package/dist/core/cli/dispatch.mjs +205 -1
  5. package/dist/core/cli/help-links.d.mts +0 -1
  6. package/dist/core/cli/help-links.mjs +41 -1
  7. package/dist/core/cli/index.d.mts +185 -19
  8. package/dist/core/cli/index.mjs +945 -1
  9. package/dist/core/cli/planner.d.mts +32 -6
  10. package/dist/core/cli/planner.mjs +300 -1
  11. package/dist/core/cli/plugin.d.mts +1 -1
  12. package/dist/core/cli/plugin.mjs +41 -1
  13. package/dist/core/cli/propagate.d.mts +1 -1
  14. package/dist/core/cli/propagate.mjs +57 -1
  15. package/dist/core/cli/root-help.d.mts +18 -3
  16. package/dist/core/cli/root-help.mjs +226 -6
  17. package/dist/core/cli/root-surface.d.mts +30 -4
  18. package/dist/core/cli/root-surface.mjs +28 -1
  19. package/dist/core/cli/runtime-preflight.d.mts +30 -3
  20. package/dist/core/cli/runtime-preflight.mjs +169 -1
  21. package/dist/core/completion/index.d.mts +32 -2
  22. package/dist/core/completion/index.mjs +90 -1
  23. package/dist/core/completion/shells/bash.d.mts +0 -1
  24. package/dist/core/completion/shells/bash.mjs +308 -3
  25. package/dist/core/completion/shells/fish.d.mts +0 -1
  26. package/dist/core/completion/shells/fish.mjs +162 -2
  27. package/dist/core/completion/shells/powershell.d.mts +0 -1
  28. package/dist/core/completion/shells/powershell.mjs +358 -2
  29. package/dist/core/completion/shells/shared.d.mts +23 -2
  30. package/dist/core/completion/shells/shared.mjs +176 -1
  31. package/dist/core/completion/shells/zsh.d.mts +0 -1
  32. package/dist/core/completion/shells/zsh.mjs +251 -2
  33. package/dist/core/config/index.d.mts +2 -3
  34. package/dist/core/config/index.mjs +246 -1
  35. package/dist/core/config/package-json.d.mts +27 -4
  36. package/dist/core/config/package-json.mjs +386 -3
  37. package/dist/core/errors/index.d.mts +3 -3
  38. package/dist/core/errors/index.mjs +87 -1
  39. package/dist/core/execution/index.d.mts +0 -1
  40. package/dist/core/execution/index.mjs +201 -1
  41. package/dist/core/help/ansi.d.mts +11 -5
  42. package/dist/core/help/ansi.mjs +121 -2
  43. package/dist/core/help/index.d.mts +65 -2
  44. package/dist/core/help/index.mjs +341 -7
  45. package/dist/core/help/theme.d.mts +68 -0
  46. package/dist/core/help/theme.mjs +46 -0
  47. package/dist/core/json-schema/index.d.mts +15 -2
  48. package/dist/core/json-schema/index.mjs +536 -12
  49. package/dist/core/json-schema/meta-descriptions.generated.d.mts +22 -5
  50. package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
  51. package/dist/core/output/activity.d.mts +0 -1
  52. package/dist/core/output/activity.mjs +452 -1
  53. package/dist/core/output/bind.mjs +56 -1
  54. package/dist/core/output/contracts.d.mts +4 -5
  55. package/dist/core/output/contracts.mjs +57 -1
  56. package/dist/core/output/display-value.mjs +32 -1
  57. package/dist/core/output/index.d.mts +21 -1
  58. package/dist/core/output/index.mjs +442 -2
  59. package/dist/core/output/renderers.d.mts +0 -1
  60. package/dist/core/output/renderers.mjs +46 -1
  61. package/dist/core/output/writer.mjs +12 -1
  62. package/dist/core/parse/index.d.mts +54 -7
  63. package/dist/core/parse/index.mjs +727 -1
  64. package/dist/core/prompt/index.d.mts +12 -4
  65. package/dist/core/prompt/index.mjs +348 -6
  66. package/dist/core/resolve/args.d.mts +1 -1
  67. package/dist/core/resolve/args.mjs +105 -1
  68. package/dist/core/resolve/coerce.d.mts +1 -1
  69. package/dist/core/resolve/coerce.mjs +336 -1
  70. package/dist/core/resolve/config.mjs +19 -1
  71. package/dist/core/resolve/contracts.d.mts +9 -4
  72. package/dist/core/resolve/errors.d.mts +0 -1
  73. package/dist/core/resolve/errors.mjs +140 -2
  74. package/dist/core/resolve/flags.d.mts +7 -2
  75. package/dist/core/resolve/flags.mjs +251 -1
  76. package/dist/core/resolve/index.d.mts +2 -2
  77. package/dist/core/resolve/index.mjs +85 -1
  78. package/dist/core/resolve/property.d.mts +6 -2
  79. package/dist/core/resolve/property.mjs +53 -1
  80. package/dist/core/schema/activity.d.mts +31 -11
  81. package/dist/core/schema/arg.d.mts +92 -13
  82. package/dist/core/schema/arg.mjs +463 -1
  83. package/dist/core/schema/command.d.mts +29 -7
  84. package/dist/core/schema/command.mjs +726 -1
  85. package/dist/core/schema/flag.d.mts +577 -21
  86. package/dist/core/schema/flag.mjs +705 -1
  87. package/dist/core/schema/index.d.mts +5 -2
  88. package/dist/core/schema/middleware.d.mts +4 -3
  89. package/dist/core/schema/middleware.mjs +41 -1
  90. package/dist/core/schema/number-constraints.d.mts +96 -0
  91. package/dist/core/schema/number-constraints.mjs +60 -0
  92. package/dist/core/schema/prompt.d.mts +21 -3
  93. package/dist/core/schema/run.d.mts +24 -5
  94. package/dist/core/schema/string-constraints.d.mts +97 -0
  95. package/dist/core/schema/string-constraints.mjs +69 -0
  96. package/dist/core/schema/value-parsers.d.mts +78 -0
  97. package/dist/core/schema/value-parsers.mjs +142 -0
  98. package/dist/core/schema-dsl/runtime.mjs +271 -2
  99. package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
  100. package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
  101. package/dist/core/testkit/index.d.mts +9 -36
  102. package/dist/core/testkit/index.mjs +83 -1
  103. package/dist/index.d.mts +12 -6
  104. package/dist/index.mjs +22 -1
  105. package/dist/runtime/adapter.d.mts +38 -2
  106. package/dist/runtime/adapter.mjs +87 -1
  107. package/dist/runtime/auto.d.mts +2 -3
  108. package/dist/runtime/auto.mjs +45 -1
  109. package/dist/runtime/deno.d.mts +24 -5
  110. package/dist/runtime/deno.mjs +262 -2
  111. package/dist/runtime/detect.mjs +45 -1
  112. package/dist/runtime/index.d.mts +3 -4
  113. package/dist/runtime/node.d.mts +8 -2
  114. package/dist/runtime/node.mjs +220 -2
  115. package/dist/runtime/paths.mjs +48 -1
  116. package/dist/runtime.d.mts +4 -4
  117. package/dist/runtime.mjs +6 -1
  118. package/dist/strings.mjs +22 -1
  119. package/dist/testkit.d.mts +1 -0
  120. package/dist/testkit.mjs +5 -1
  121. package/dist/version.d.mts +7 -3
  122. package/dist/version.mjs +7 -1
  123. package/dreamcli.schema.json +36 -7
  124. package/examples/basic.ts +5 -1
  125. package/examples/gh/package.json +2 -7
  126. package/examples/pwsh-demo/package.json +1 -7
  127. package/examples/pwsh-demo/src/main.test.ts +1 -4
  128. package/examples/pwsh-demo/src/main.ts +14 -4
  129. package/examples/transport-launcher.ts +85 -0
  130. package/package.json +43 -49
  131. package/dist/runtime/bun.d.mts +0 -28
  132. package/dist/runtime/bun.mjs +0 -1
  133. package/dist/runtime/support.d.mts +0 -42
  134. package/dist/runtime/support.mjs +0 -1
  135. package/src/core/cli/dispatch.ts +0 -349
  136. package/src/core/cli/help-links.ts +0 -82
  137. package/src/core/cli/index.ts +0 -1413
  138. package/src/core/cli/planner.ts +0 -487
  139. package/src/core/cli/plugin.ts +0 -117
  140. package/src/core/cli/propagate.ts +0 -97
  141. package/src/core/cli/root-help.ts +0 -252
  142. package/src/core/cli/root-surface.ts +0 -74
  143. package/src/core/cli/runtime-preflight.ts +0 -438
  144. package/src/core/completion/index.ts +0 -88
  145. package/src/core/completion/shells/bash.ts +0 -459
  146. package/src/core/completion/shells/fish.ts +0 -259
  147. package/src/core/completion/shells/powershell.ts +0 -439
  148. package/src/core/completion/shells/shared.ts +0 -283
  149. package/src/core/completion/shells/zsh.ts +0 -381
  150. package/src/core/config/index.ts +0 -417
  151. package/src/core/config/package-json.ts +0 -560
  152. package/src/core/errors/index.ts +0 -193
  153. package/src/core/execution/index.ts +0 -268
  154. package/src/core/help/ansi.ts +0 -144
  155. package/src/core/help/index.ts +0 -516
  156. package/src/core/json-schema/index.ts +0 -891
  157. package/src/core/json-schema/meta-descriptions.generated.ts +0 -185
  158. package/src/core/output/activity.ts +0 -575
  159. package/src/core/output/bind.ts +0 -59
  160. package/src/core/output/contracts.ts +0 -194
  161. package/src/core/output/display-value.ts +0 -48
  162. package/src/core/output/index.ts +0 -704
  163. package/src/core/output/renderers.ts +0 -125
  164. package/src/core/output/writer.ts +0 -46
  165. package/src/core/parse/index.ts +0 -681
  166. package/src/core/prompt/index.ts +0 -583
  167. package/src/core/resolve/args.ts +0 -144
  168. package/src/core/resolve/coerce.ts +0 -432
  169. package/src/core/resolve/config.ts +0 -26
  170. package/src/core/resolve/contracts.ts +0 -135
  171. package/src/core/resolve/errors.ts +0 -227
  172. package/src/core/resolve/flags.ts +0 -255
  173. package/src/core/resolve/index.ts +0 -108
  174. package/src/core/resolve/property.ts +0 -99
  175. package/src/core/schema/activity.ts +0 -240
  176. package/src/core/schema/arg.ts +0 -719
  177. package/src/core/schema/command.ts +0 -1498
  178. package/src/core/schema/flag.ts +0 -801
  179. package/src/core/schema/index.ts +0 -91
  180. package/src/core/schema/middleware.ts +0 -171
  181. package/src/core/schema/prompt.ts +0 -156
  182. package/src/core/schema/run.ts +0 -198
  183. package/src/core/schema-dsl/index.ts +0 -109
  184. package/src/core/schema-dsl/parse.ts +0 -262
  185. package/src/core/schema-dsl/runtime.ts +0 -609
  186. package/src/core/schema-dsl/to-json-schema.ts +0 -102
  187. package/src/core/testkit/index.ts +0 -123
  188. package/src/index.ts +0 -179
  189. package/src/runtime/adapter.ts +0 -303
  190. package/src/runtime/auto.ts +0 -110
  191. package/src/runtime/bun.ts +0 -55
  192. package/src/runtime/deno.ts +0 -369
  193. package/src/runtime/detect.ts +0 -105
  194. package/src/runtime/index.ts +0 -21
  195. package/src/runtime/node-builtins.d.ts +0 -27
  196. package/src/runtime/node.ts +0 -300
  197. package/src/runtime/paths.ts +0 -64
  198. package/src/runtime/support.ts +0 -135
  199. package/src/runtime.ts +0 -31
  200. package/src/strings.ts +0 -34
  201. package/src/testkit.ts +0 -25
  202. package/src/version.ts +0 -20
@@ -1,55 +0,0 @@
1
- /**
2
- * Bun runtime adapter implementation.
3
- *
4
- * Bun provides a Node-compatible `process` global, so this adapter
5
- * delegates to {@link createNodeAdapter} for all operations. The
6
- * separate factory exists to:
7
- *
8
- * 1. Make runtime selection explicit when auto-detection picks Bun
9
- * 2. Provide an extension point for Bun-specific overrides in the future
10
- * (e.g. `Bun.write` for optimized I/O, `Bun.stdin` for streaming)
11
- *
12
- * @module @kjanat/dreamcli/runtime/bun
13
- */
14
-
15
- import type { RuntimeAdapter } from './adapter.ts';
16
- import type { NodeProcess } from './node.ts';
17
- import { createNodeAdapter } from './node.ts';
18
- import { assertRuntimeVersionSupported } from './support.ts';
19
-
20
- function resolveBunVersion(proc?: NodeProcess): string | undefined {
21
- if (proc?.versions?.bun !== undefined) return proc.versions.bun;
22
- return (globalThis as unknown as { Bun?: { version?: string } }).Bun?.version;
23
- }
24
-
25
- // --- Bun adapter factory
26
-
27
- /**
28
- * Create a runtime adapter backed by Bun's Node-compatible `process` global.
29
- *
30
- * Bun exposes `globalThis.process` with the same shape as Node.js, so this
31
- * delegates entirely to {@link createNodeAdapter}. If Bun-specific optimizations
32
- * are needed in the future (e.g. `Bun.write` for I/O), they can be layered
33
- * here without changing the public API.
34
- *
35
- * @param proc - Override the process object (useful for testing the adapter itself).
36
- * @returns A {@linkcode RuntimeAdapter} backed by Bun's process state.
37
- *
38
- * @example
39
- * ```ts
40
- * import { cli } from '@kjanat/dreamcli';
41
- * import { createBunAdapter } from '@kjanat/dreamcli/runtime';
42
- *
43
- * cli('mycli')
44
- * .command(deploy)
45
- * .run({ adapter: createBunAdapter() });
46
- * ```
47
- */
48
- function createBunAdapter(proc?: NodeProcess): RuntimeAdapter {
49
- assertRuntimeVersionSupported('bun', resolveBunVersion(proc));
50
- return createNodeAdapter(proc);
51
- }
52
-
53
- // --- Exports
54
-
55
- export { createBunAdapter };
@@ -1,369 +0,0 @@
1
- /**
2
- * Deno runtime adapter implementation.
3
- *
4
- * Bridges the platform-agnostic {@linkcode RuntimeAdapter} interface to Deno's
5
- * namespace APIs (`Deno.args`, `Deno.env`, `Deno.cwd()`, etc.).
6
- *
7
- * Deno requires explicit permissions for some operations. The adapter
8
- * handles missing permissions gracefully:
9
- *
10
- * - **`--allow-env`**: needed for `Deno.env.toObject()`. Without it,
11
- * the adapter falls back to an empty env (flags using env sources
12
- * simply won't resolve from env).
13
- * - **`--allow-read`**: needed for `Deno.readTextFile()`. Without it,
14
- * `readFile` returns `null` (same as file-not-found).
15
- *
16
- * No permissions are needed for `Deno.args`, `Deno.stdout`, `Deno.stderr`,
17
- * `Deno.stdin`, `Deno.exit()`, or TTY detection.
18
- *
19
- * @module @kjanat/dreamcli/runtime/deno
20
- */
21
-
22
- import type { WriteFn } from '#internals/core/output/index.ts';
23
- import type { ReadFn } from '#internals/core/prompt/index.ts';
24
- import type { RuntimeAdapter } from './adapter.ts';
25
- import { resolveConfigDirectory, resolveHomeDirectory } from './paths.ts';
26
- import { assertRuntimeVersionSupported } from './support.ts';
27
-
28
- // --- Minimal Deno namespace shape — avoids @types/deno dependency
29
-
30
- /**
31
- * Minimal subset of the Deno namespace needed by the adapter.
32
- *
33
- * We avoid `@types/deno` to keep the core runtime-agnostic at the type level.
34
- * This interface declares only what `createDenoAdapter` actually reads.
35
- *
36
- * The `env` property is optional because it requires `--allow-env` permission.
37
- * When permission is denied, the adapter catches the error and falls back to
38
- * an empty env object.
39
- */
40
- interface DenoNamespace {
41
- /** Build target metadata (OS detection). */
42
- readonly build: {
43
- readonly os:
44
- | 'darwin'
45
- | 'linux'
46
- | 'android'
47
- | 'windows'
48
- | 'freebsd'
49
- | 'netbsd'
50
- | 'aix'
51
- | 'solaris'
52
- | 'illumos';
53
- };
54
- /** Deno version info — used for minimum-version guard. */
55
- readonly version?: {
56
- readonly deno?: string;
57
- };
58
- /** Raw command-line args (excludes the binary/script — Deno pre-strips them). */
59
- readonly args: readonly string[];
60
-
61
- /** Environment variable access (requires `--allow-env`). */
62
- readonly env: {
63
- /** Get a single env var. Returns `undefined` if unset or permission denied. */
64
- get(key: string): string | undefined;
65
- /** Get all env vars as a plain object. Throws on permission denied. */
66
- toObject(): Record<string, string>;
67
- };
68
-
69
- /** Current working directory (may throw if `--allow-read` is denied for cwd). */
70
- cwd(): string;
71
-
72
- /** Standard output — synchronous byte writer with TTY detection. */
73
- readonly stdout: {
74
- /** Write raw bytes to stdout synchronously. */
75
- writeSync(p: Uint8Array): number;
76
- /** Whether stdout is connected to a TTY. */
77
- isTerminal(): boolean;
78
- };
79
-
80
- /** Standard error — synchronous byte writer. */
81
- readonly stderr: {
82
- /** Write raw bytes to stderr synchronously. */
83
- writeSync(p: Uint8Array): number;
84
- };
85
-
86
- /** Standard input — TTY detection and readable byte stream. */
87
- readonly stdin: {
88
- /** Whether stdin is connected to a TTY. */
89
- isTerminal(): boolean;
90
- /** Readable stream for stdin bytes. */
91
- readonly readable: ReadableStream<Uint8Array>;
92
- };
93
-
94
- /** Exit the process with the given code. */
95
- exit(code: number): never;
96
-
97
- /** Read a file as UTF-8 text (requires `--allow-read`). */
98
- readTextFile(path: string): Promise<string>;
99
- }
100
-
101
- // --- Permission-safe helpers
102
-
103
- /**
104
- * Detect whether an unknown thrown value is a Deno error by name.
105
- *
106
- * @internal
107
- */
108
- function isDenoErrorNamed(err: unknown, name: string): boolean {
109
- if (typeof err !== 'object' || err === null || !('name' in err)) return false;
110
- // After the `in` check, TS narrows err to `object & Record<'name', unknown>`.
111
- const candidate: { name: unknown } = err;
112
- return typeof candidate.name === 'string' && candidate.name === name;
113
- }
114
-
115
- /**
116
- * Safely read all environment variables. Returns empty object if
117
- * `--allow-env` permission is not granted.
118
- *
119
- * @internal
120
- */
121
- function safeEnvToObject(deno: DenoNamespace): Readonly<Record<string, string | undefined>> {
122
- try {
123
- return deno.env.toObject();
124
- } catch (err: unknown) {
125
- if (isDenoErrorNamed(err, 'PermissionDenied')) return {};
126
- throw err;
127
- }
128
- }
129
-
130
- /**
131
- * Safely get the current working directory. Returns `/` if the cwd is
132
- * inaccessible due to missing permissions.
133
- *
134
- * @internal
135
- */
136
- function safeCwd(deno: DenoNamespace): string {
137
- try {
138
- return deno.cwd();
139
- } catch (err: unknown) {
140
- if (isDenoErrorNamed(err, 'PermissionDenied')) return '/';
141
- throw err;
142
- }
143
- }
144
-
145
- // --- Deno adapter factory
146
-
147
- /**
148
- * Create a runtime adapter backed by the Deno namespace.
149
- *
150
- * Reads `Deno.args`, `Deno.env`, `Deno.cwd()`, and wraps Deno's stream-based
151
- * I/O into the {@linkcode WriteFn}/{@linkcode ReadFn} functions expected by the framework.
152
- *
153
- * Unlike Node/Bun, Deno strips the binary and script path from `Deno.args`.
154
- * The adapter prepends synthetic entries (`['deno', 'run']`) so the argv
155
- * shape matches the {@linkcode RuntimeAdapter} contract (binary + script + user args).
156
- *
157
- * @param ns - Override the Deno namespace (useful for testing the adapter itself).
158
- * @returns A {@linkcode RuntimeAdapter} backed by Deno's namespace APIs.
159
- *
160
- * @example
161
- * ```ts
162
- * import { cli } from '@kjanat/dreamcli';
163
- * import { createDenoAdapter } from '@kjanat/dreamcli/runtime';
164
- *
165
- * cli('mycli')
166
- * .command(deploy)
167
- * .run({ adapter: createDenoAdapter() });
168
- * ```
169
- */
170
- function createDenoAdapter(ns?: DenoNamespace): RuntimeAdapter {
171
- const d = ns ?? getDenoNamespace();
172
- assertRuntimeVersionSupported('deno', d.version?.deno);
173
- const encoder = new TextEncoder();
174
-
175
- // --- Synthetic argv: Deno.args has user args only (no binary/script) ---
176
- const argv: readonly string[] = ['deno', 'run', ...d.args];
177
-
178
- // --- Environment (permission-safe) ---
179
- const env = safeEnvToObject(d);
180
-
181
- // --- CWD (permission-safe) ---
182
- const cwd = safeCwd(d);
183
-
184
- // --- I/O writers ---
185
- const stdoutWrite: WriteFn = (data) => {
186
- d.stdout.writeSync(encoder.encode(data));
187
- };
188
- const stderrWrite: WriteFn = (data) => {
189
- d.stderr.writeSync(encoder.encode(data));
190
- };
191
-
192
- // --- Stdin line reading ---
193
- const stdinRead = createDenoReadLine(d);
194
-
195
- // --- Filesystem ---
196
- const readFile = async (path: string): Promise<string | null> => {
197
- try {
198
- return await d.readTextFile(path);
199
- } catch (err: unknown) {
200
- // NotFound = file doesn't exist → null (expected for config probing)
201
- if (isDenoErrorNamed(err, 'NotFound')) return null;
202
- // PermissionDenied = --allow-read not granted → treat as not found
203
- // (config discovery is opt-in; failing silently is the graceful path)
204
- if (isDenoErrorNamed(err, 'PermissionDenied')) return null;
205
- throw err; // Other I/O errors propagate
206
- }
207
- };
208
-
209
- // --- Home directory ---
210
- const homedir = resolveDenoHomedir(env, d.build.os === 'windows');
211
-
212
- // --- Config directory ---
213
- const configDir = resolveDenoConfigDir(env, homedir, d.build.os === 'windows');
214
-
215
- const stdinIsTTY = d.stdin.isTerminal();
216
-
217
- return {
218
- argv,
219
- env,
220
- cwd,
221
- stdout: stdoutWrite,
222
- stderr: stderrWrite,
223
- stdin: stdinRead,
224
- readStdin: () => readDenoStdinAll(d, stdinIsTTY),
225
- isTTY: d.stdout.isTerminal(),
226
- stdinIsTTY,
227
- exit: (code) => d.exit(code),
228
- readFile,
229
- homedir,
230
- configDir,
231
- };
232
- }
233
-
234
- // --- Deno namespace access — isolated for testability
235
-
236
- /**
237
- * Access the global `Deno` namespace.
238
- *
239
- * Cast through `unknown` to avoid TypeScript errors when Deno types
240
- * aren't installed. Safe because this adapter is only used on Deno
241
- * where `globalThis.Deno` is always defined.
242
- *
243
- * @internal
244
- */
245
- function getDenoNamespace(): DenoNamespace {
246
- return (globalThis as unknown as { Deno: DenoNamespace }).Deno;
247
- }
248
-
249
- // --- Stdin reading — line (prompts) and full (piped data)
250
-
251
- /**
252
- * Read all of stdin as a single string (for piped data).
253
- *
254
- * Returns `null` immediately if stdin is a TTY — the user is typing
255
- * interactively, so there's no piped data to consume. When stdin is
256
- * piped, collects all chunks via the `ReadableStream` until EOF and
257
- * returns the decoded string, which may be empty for an empty pipe.
258
- *
259
- * @internal
260
- */
261
- async function readDenoStdinAll(deno: DenoNamespace, stdinIsTTY: boolean): Promise<string | null> {
262
- if (stdinIsTTY) return null;
263
-
264
- const reader = deno.stdin.readable.getReader();
265
- const decoder = new TextDecoder();
266
- const chunks: string[] = [];
267
-
268
- try {
269
- for (;;) {
270
- const { value, done } = await reader.read();
271
- if (done) {
272
- // Flush any trailing bytes held by the streaming decoder
273
- chunks.push(decoder.decode());
274
- break;
275
- }
276
- chunks.push(decoder.decode(value, { stream: true }));
277
- }
278
- } finally {
279
- reader.releaseLock();
280
- }
281
-
282
- return chunks.join('');
283
- }
284
-
285
- /**
286
- * Read a single line from Deno's stdin.
287
- *
288
- * Uses the `ReadableStream` API with a manual buffer to read until
289
- * a newline character is found. Returns `null` on EOF.
290
- *
291
- * This is intentionally simple — the terminal prompter handles retries
292
- * and validation.
293
- *
294
- * @internal
295
- */
296
- function createDenoReadLine(deno: DenoNamespace): ReadFn {
297
- const decoder = new TextDecoder();
298
- let buffer = '';
299
- let done = false;
300
-
301
- return async (): Promise<string | null> => {
302
- const reader = deno.stdin.readable.getReader();
303
- try {
304
- for (;;) {
305
- const newlineIndex = buffer.indexOf('\n');
306
- if (newlineIndex !== -1) {
307
- const line = buffer.slice(0, newlineIndex).replace(/\r$/, '');
308
- buffer = buffer.slice(newlineIndex + 1);
309
- return line;
310
- }
311
-
312
- if (done) {
313
- if (buffer.length === 0) return null;
314
- const line = buffer;
315
- buffer = '';
316
- return line;
317
- }
318
-
319
- const { value, done: readDone } = await reader.read();
320
- if (readDone) {
321
- buffer += decoder.decode();
322
- done = true;
323
- continue;
324
- }
325
- buffer += decoder.decode(value, { stream: true });
326
- }
327
- } finally {
328
- reader.releaseLock();
329
- }
330
- };
331
- }
332
-
333
- // --- Home / config directory resolution
334
-
335
- /**
336
- * Resolve the user's home directory from environment variables.
337
- *
338
- * Deno doesn't expose `os.homedir()` without `--allow-sys`, so we derive
339
- * it from env vars — same approach as the Node adapter.
340
- *
341
- * @internal
342
- */
343
- function resolveDenoHomedir(
344
- env: Readonly<Record<string, string | undefined>>,
345
- isWindows: boolean,
346
- ): string {
347
- return resolveHomeDirectory(env, isWindows);
348
- }
349
-
350
- /**
351
- * Resolve the platform-specific user configuration directory.
352
- *
353
- * Uses `Deno.build.os` for platform detection, then applies the same fallback
354
- * chain as the Node adapter.
355
- *
356
- * @internal
357
- */
358
- function resolveDenoConfigDir(
359
- env: Readonly<Record<string, string | undefined>>,
360
- homedir: string,
361
- isWindows: boolean,
362
- ): string {
363
- return resolveConfigDirectory(env, isWindows, homedir);
364
- }
365
-
366
- // --- Exports
367
-
368
- export type { DenoNamespace };
369
- export { createDenoAdapter };
@@ -1,105 +0,0 @@
1
- /**
2
- * Auto-detects the current JavaScript runtime environment.
3
- *
4
- * Uses `globalThis` feature detection (version globals) to identify
5
- * the runtime without importing platform-specific modules. This is
6
- * the recommended approach because each runtime defines a unique
7
- * global marker:
8
- *
9
- * - **Bun:** `globalThis.Bun.version` (string)
10
- * - **Deno:** `globalThis.Deno.version` (object with `deno` key)
11
- * - **Node:** `globalThis.process.versions.node` (string)
12
- *
13
- * Detection order matters: Bun provides a Node-compatible `process`
14
- * global, so it must be checked *before* Node to avoid misidentification.
15
- *
16
- * @module @kjanat/dreamcli/runtime/detect
17
- */
18
-
19
- // --- Runtime type
20
-
21
- /**
22
- * Known JavaScript runtime environments.
23
- *
24
- * - `'node'` — Node.js
25
- * - `'bun'` — Bun
26
- * - `'deno'` — Deno
27
- * - `'unknown'` — Unrecognized environment
28
- */
29
- type Runtime = 'node' | 'bun' | 'deno' | 'unknown';
30
-
31
- /**
32
- * All known runtime values as a readonly tuple.
33
- *
34
- * Useful for validation, iteration, and exhaustiveness checks.
35
- *
36
- * @example
37
- * ```ts
38
- * if (RUNTIMES.includes(value)) {
39
- * // value is a valid Runtime
40
- * }
41
- * ```
42
- */
43
- const RUNTIMES: readonly ['node', 'bun', 'deno', 'unknown'] = [
44
- 'node',
45
- 'bun',
46
- 'deno',
47
- 'unknown',
48
- ] as const satisfies readonly Runtime[];
49
-
50
- // --- Minimal global shapes — avoid importing @types/* for each runtime
51
-
52
- /**
53
- * Minimal `globalThis` shape used for runtime detection.
54
- *
55
- * Each field is optional — only the present one identifies the runtime.
56
- * Typed as `unknown` where we only need truthiness; version fields are
57
- * typed just enough to distinguish runtimes safely.
58
- */
59
- interface GlobalForDetect {
60
- /** Present when running on Bun. */
61
- readonly Bun?: { readonly version?: string };
62
- /** Present when running on Deno. */
63
- readonly Deno?: { readonly version?: { readonly deno?: string } };
64
- /** Present on Node.js (and Bun, which mimics it). */
65
- readonly process?: { readonly versions?: { readonly node?: string; readonly bun?: string } };
66
- }
67
-
68
- // --- Detection
69
-
70
- /**
71
- * Detect the current JavaScript runtime.
72
- *
73
- * Uses `globalThis` feature detection to identify the host runtime.
74
- * Detection order is significant: Bun is checked before Node because
75
- * Bun exposes a Node-compatible `process` global.
76
- *
77
- * @param globals - Override `globalThis` for testing. Production callers
78
- * should omit this parameter.
79
- * @returns The detected {@link Runtime} identifier.
80
- *
81
- * @example
82
- * ```ts
83
- * const rt = detectRuntime();
84
- * // rt === 'node' | 'bun' | 'deno' | 'unknown'
85
- * ```
86
- */
87
- function detectRuntime(globals?: GlobalForDetect): Runtime {
88
- const g = globals ?? (globalThis as unknown as GlobalForDetect);
89
-
90
- // Bun first — it also has `process.versions.node`
91
- if (typeof g.Bun?.version === 'string') return 'bun';
92
-
93
- // Deno — check the nested version object
94
- if (typeof g.Deno?.version?.deno === 'string') return 'deno';
95
-
96
- // Node.js — last because Bun mimics it
97
- if (typeof g.process?.versions?.node === 'string') return 'node';
98
-
99
- return 'unknown';
100
- }
101
-
102
- // --- Exports
103
-
104
- export type { GlobalForDetect, Runtime };
105
- export { detectRuntime, RUNTIMES };
@@ -1,21 +0,0 @@
1
- /**
2
- * Runtime adapters for platform portability (Node, Bun, Deno).
3
- *
4
- * The adapter interface ({@link RuntimeAdapter}) defines the contract
5
- * between the platform-agnostic core and the host runtime. Concrete
6
- * adapters wrap platform-specific APIs (Node `process`, Deno namespace, etc.)
7
- * behind this uniform interface.
8
- *
9
- * @module @kjanat/dreamcli/runtime
10
- */
11
-
12
- export type { RuntimeAdapter, TestAdapterOptions } from './adapter.ts';
13
- export { createTestAdapter, ExitError } from './adapter.ts';
14
- export { createAdapter } from './auto.ts';
15
- export { createBunAdapter } from './bun.ts';
16
- export type { DenoNamespace } from './deno.ts';
17
- export { createDenoAdapter } from './deno.ts';
18
- export type { GlobalForDetect, Runtime } from './detect.ts';
19
- export { detectRuntime, RUNTIMES } from './detect.ts';
20
- export type { NodeProcess } from './node.ts';
21
- export { createNodeAdapter } from './node.ts';
@@ -1,27 +0,0 @@
1
- /**
2
- * Minimal type declarations for Node.js built-in modules.
3
- *
4
- * This file provides just enough type information for the runtime
5
- * adapter to dynamically import `readline` without depending on
6
- * `@types/node`. Only the subset actually used is declared.
7
- *
8
- * @internal
9
- */
10
-
11
- declare module 'node:readline' {
12
- interface ReadlineInterface {
13
- once(event: 'line', listener: (line: string) => void): void;
14
- once(event: 'close', listener: () => void): void;
15
- close(): void;
16
- }
17
-
18
- function createInterface(options: { input: unknown; terminal: boolean }): ReadlineInterface;
19
- }
20
-
21
- declare module 'node:fs/promises' {
22
- function readFile(path: string | URL, encoding: 'utf8'): Promise<string>;
23
- }
24
-
25
- declare module 'node:url' {
26
- function fileURLToPath(url: string | URL): string;
27
- }