@kjanat/dreamcli 0.0.0 → 2.0.0
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/CHANGELOG.md +706 -0
- package/LICENSE +22 -0
- package/README.md +502 -0
- package/dist/core/cli/dispatch.d.mts +85 -0
- package/dist/core/cli/dispatch.mjs +1 -0
- package/dist/core/cli/index.d.mts +418 -0
- package/dist/core/cli/index.mjs +1 -0
- package/dist/core/cli/planner.d.mts +143 -0
- package/dist/core/cli/planner.mjs +1 -0
- package/dist/core/cli/plugin.d.mts +94 -0
- package/dist/core/cli/plugin.mjs +1 -0
- package/dist/core/cli/propagate.d.mts +39 -0
- package/dist/core/cli/propagate.mjs +1 -0
- package/dist/core/cli/root-help.d.mts +28 -0
- package/dist/core/cli/root-help.mjs +7 -0
- package/dist/core/cli/root-surface.d.mts +42 -0
- package/dist/core/cli/root-surface.mjs +1 -0
- package/dist/core/cli/runtime-preflight.d.mts +149 -0
- package/dist/core/cli/runtime-preflight.mjs +1 -0
- package/dist/core/completion/index.d.mts +46 -0
- package/dist/core/completion/index.mjs +1 -0
- package/dist/core/completion/shells/bash.d.mts +24 -0
- package/dist/core/completion/shells/bash.mjs +3 -0
- package/dist/core/completion/shells/fish.d.mts +20 -0
- package/dist/core/completion/shells/fish.mjs +2 -0
- package/dist/core/completion/shells/powershell.d.mts +14 -0
- package/dist/core/completion/shells/powershell.mjs +2 -0
- package/dist/core/completion/shells/shared.d.mts +139 -0
- package/dist/core/completion/shells/shared.mjs +1 -0
- package/dist/core/completion/shells/zsh.d.mts +24 -0
- package/dist/core/completion/shells/zsh.mjs +2 -0
- package/dist/core/config/index.d.mts +161 -0
- package/dist/core/config/index.mjs +1 -0
- package/dist/core/config/package-json.d.mts +68 -0
- package/dist/core/config/package-json.mjs +1 -0
- package/dist/core/errors/index.d.mts +126 -0
- package/dist/core/errors/index.mjs +1 -0
- package/dist/core/execution/index.d.mts +49 -0
- package/dist/core/execution/index.mjs +1 -0
- package/dist/core/help/index.d.mts +49 -0
- package/dist/core/help/index.mjs +8 -0
- package/dist/core/json-schema/index.d.mts +109 -0
- package/dist/core/json-schema/index.mjs +55 -0
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +185 -0
- package/dist/core/json-schema/meta-descriptions.generated.mjs +28 -0
- package/dist/core/output/activity.d.mts +194 -0
- package/dist/core/output/activity.mjs +1 -0
- package/dist/core/output/contracts.d.mts +88 -0
- package/dist/core/output/contracts.mjs +1 -0
- package/dist/core/output/display-value.d.mts +19 -0
- package/dist/core/output/display-value.mjs +1 -0
- package/dist/core/output/index.d.mts +280 -0
- package/dist/core/output/index.mjs +2 -0
- package/dist/core/output/renderers.d.mts +46 -0
- package/dist/core/output/renderers.mjs +1 -0
- package/dist/core/output/writer.d.mts +40 -0
- package/dist/core/output/writer.mjs +1 -0
- package/dist/core/parse/index.d.mts +79 -0
- package/dist/core/parse/index.mjs +1 -0
- package/dist/core/prompt/index.d.mts +205 -0
- package/dist/core/prompt/index.mjs +6 -0
- package/dist/core/resolve/args.d.mts +8 -0
- package/dist/core/resolve/args.mjs +1 -0
- package/dist/core/resolve/coerce.d.mts +22 -0
- package/dist/core/resolve/coerce.mjs +1 -0
- package/dist/core/resolve/config.d.mts +10 -0
- package/dist/core/resolve/config.mjs +1 -0
- package/dist/core/resolve/contracts.d.mts +112 -0
- package/dist/core/resolve/errors.d.mts +11 -0
- package/dist/core/resolve/errors.mjs +2 -0
- package/dist/core/resolve/flags.d.mts +10 -0
- package/dist/core/resolve/flags.mjs +1 -0
- package/dist/core/resolve/index.d.mts +42 -0
- package/dist/core/resolve/index.mjs +1 -0
- package/dist/core/resolve/property.d.mts +47 -0
- package/dist/core/resolve/property.mjs +1 -0
- package/dist/core/schema/activity.d.mts +169 -0
- package/dist/core/schema/arg.d.mts +559 -0
- package/dist/core/schema/arg.mjs +1 -0
- package/dist/core/schema/command.d.mts +960 -0
- package/dist/core/schema/command.mjs +1 -0
- package/dist/core/schema/flag.d.mts +491 -0
- package/dist/core/schema/flag.mjs +1 -0
- package/dist/core/schema/index.d.mts +8 -0
- package/dist/core/schema/middleware.d.mts +122 -0
- package/dist/core/schema/middleware.mjs +1 -0
- package/dist/core/schema/prompt.d.mts +117 -0
- package/dist/core/schema/run.d.mts +171 -0
- package/dist/core/schema-dsl/runtime.d.mts +144 -0
- package/dist/core/schema-dsl/runtime.mjs +2 -0
- package/dist/core/schema-dsl/to-json-schema.d.mts +30 -0
- package/dist/core/schema-dsl/to-json-schema.mjs +1 -0
- package/dist/core/testkit/index.d.mts +54 -0
- package/dist/core/testkit/index.mjs +1 -0
- package/dist/index.d.mts +28 -0
- package/dist/index.mjs +1 -0
- package/dist/runtime/adapter.d.mts +212 -0
- package/dist/runtime/adapter.mjs +1 -0
- package/dist/runtime/auto.d.mts +26 -0
- package/dist/runtime/auto.mjs +1 -0
- package/dist/runtime/bun.d.mts +28 -0
- package/dist/runtime/bun.mjs +1 -0
- package/dist/runtime/deno.d.mts +76 -0
- package/dist/runtime/deno.mjs +2 -0
- package/dist/runtime/detect.d.mts +86 -0
- package/dist/runtime/detect.mjs +1 -0
- package/dist/runtime/index.d.mts +7 -0
- package/dist/runtime/node.d.mts +65 -0
- package/dist/runtime/node.mjs +2 -0
- package/dist/runtime/paths.d.mts +31 -0
- package/dist/runtime/paths.mjs +1 -0
- package/dist/runtime/support.d.mts +42 -0
- package/dist/runtime/support.mjs +1 -0
- package/dist/runtime.d.mts +7 -0
- package/dist/runtime.mjs +1 -0
- package/dist/testkit.d.mts +6 -0
- package/dist/testkit.mjs +1 -0
- package/dist/version.d.mts +16 -0
- package/dist/version.mjs +1 -0
- package/dreamcli.schema.json +338 -0
- package/examples/basic.ts +33 -0
- package/examples/gh/package.json +44 -0
- package/examples/gh/src/commands/auth.ts +66 -0
- package/examples/gh/src/commands/issue.ts +120 -0
- package/examples/gh/src/commands/pr.ts +125 -0
- package/examples/gh/src/data/issues.yaml +5 -0
- package/examples/gh/src/data/issues.yaml.d.ts +17 -0
- package/examples/gh/src/data/pull-requests.yaml +7 -0
- package/examples/gh/src/data/pull-requests.yaml.d.ts +18 -0
- package/examples/gh/src/docs.ts +11 -0
- package/examples/gh/src/lib/auth.ts +37 -0
- package/examples/gh/src/lib/utils.ts +17 -0
- package/examples/gh/src/main.test.ts +307 -0
- package/examples/gh/src/main.ts +40 -0
- package/examples/interactive.ts +66 -0
- package/examples/json-mode.ts +91 -0
- package/examples/middleware.ts +71 -0
- package/examples/multi-command.ts +66 -0
- package/examples/spinner-progress.ts +49 -0
- package/examples/testing.ts +147 -0
- package/package.json +149 -2
- package/src/core/cli/dispatch.ts +286 -0
- package/src/core/cli/index.ts +978 -0
- package/src/core/cli/planner.ts +413 -0
- package/src/core/cli/plugin.ts +117 -0
- package/src/core/cli/propagate.ts +97 -0
- package/src/core/cli/root-help.ts +269 -0
- package/src/core/cli/root-surface.ts +74 -0
- package/src/core/cli/runtime-preflight.ts +409 -0
- package/src/core/completion/index.ts +88 -0
- package/src/core/completion/shells/bash.ts +459 -0
- package/src/core/completion/shells/fish.ts +259 -0
- package/src/core/completion/shells/powershell.ts +405 -0
- package/src/core/completion/shells/shared.ts +283 -0
- package/src/core/completion/shells/zsh.ts +381 -0
- package/src/core/config/index.ts +407 -0
- package/src/core/config/package-json.ts +220 -0
- package/src/core/errors/index.ts +193 -0
- package/src/core/execution/index.ts +270 -0
- package/src/core/help/index.ts +557 -0
- package/src/core/json-schema/index.ts +891 -0
- package/src/core/json-schema/meta-descriptions.generated.ts +202 -0
- package/src/core/output/activity.ts +568 -0
- package/src/core/output/contracts.ts +194 -0
- package/src/core/output/display-value.ts +48 -0
- package/src/core/output/index.ts +669 -0
- package/src/core/output/renderers.ts +125 -0
- package/src/core/output/writer.ts +46 -0
- package/src/core/parse/index.ts +661 -0
- package/src/core/prompt/index.ts +583 -0
- package/src/core/resolve/args.ts +144 -0
- package/src/core/resolve/coerce.ts +432 -0
- package/src/core/resolve/config.ts +26 -0
- package/src/core/resolve/contracts.ts +135 -0
- package/src/core/resolve/errors.ts +227 -0
- package/src/core/resolve/flags.ts +205 -0
- package/src/core/resolve/index.ts +108 -0
- package/src/core/resolve/property.ts +99 -0
- package/src/core/schema/activity.ts +240 -0
- package/src/core/schema/arg.ts +719 -0
- package/src/core/schema/command.ts +1490 -0
- package/src/core/schema/flag.ts +756 -0
- package/src/core/schema/index.ts +91 -0
- package/src/core/schema/middleware.ts +171 -0
- package/src/core/schema/prompt.ts +156 -0
- package/src/core/schema/run.ts +195 -0
- package/src/core/schema-dsl/index.ts +109 -0
- package/src/core/schema-dsl/parse.ts +262 -0
- package/src/core/schema-dsl/runtime.ts +609 -0
- package/src/core/schema-dsl/to-json-schema.ts +102 -0
- package/src/core/testkit/index.ts +123 -0
- package/src/index.ts +170 -0
- package/src/runtime/adapter.ts +303 -0
- package/src/runtime/auto.ts +110 -0
- package/src/runtime/bun.ts +55 -0
- package/src/runtime/deno.ts +369 -0
- package/src/runtime/detect.ts +105 -0
- package/src/runtime/index.ts +21 -0
- package/src/runtime/node-builtins.d.ts +23 -0
- package/src/runtime/node.ts +300 -0
- package/src/runtime/paths.ts +62 -0
- package/src/runtime/support.ts +135 -0
- package/src/runtime.ts +31 -0
- package/src/testkit.ts +25 -0
- package/src/version.ts +20 -0
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Node.js runtime adapter implementation.
|
|
3
|
+
*
|
|
4
|
+
* Bridges the platform-agnostic {@linkcode RuntimeAdapter} interface to Node.js's
|
|
5
|
+
* `globalThis.process` object. This adapter is also compatible with Bun,
|
|
6
|
+
* which provides a Node-compatible `process` global.
|
|
7
|
+
*
|
|
8
|
+
* The adapter reads process state once at creation time and exposes it
|
|
9
|
+
* through the immutable {@linkcode RuntimeAdapter} interface. I/O writers wrap
|
|
10
|
+
* `process.stdout.write` and `process.stderr.write`.
|
|
11
|
+
*
|
|
12
|
+
* @module @kjanat/dreamcli/runtime/node
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import type { WriteFn } from '#internals/core/output/index.ts';
|
|
16
|
+
import type { ReadFn } from '#internals/core/prompt/index.ts';
|
|
17
|
+
import type { RuntimeAdapter } from './adapter.ts';
|
|
18
|
+
import { resolveConfigDirectory, resolveHomeDirectory } from './paths.ts';
|
|
19
|
+
import { assertRuntimeVersionSupported } from './support.ts';
|
|
20
|
+
|
|
21
|
+
// --- Node.js error shape — for ENOENT detection without @types/node
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Minimal shape for Node.js system errors (e.g. `ENOENT`, `EACCES`).
|
|
25
|
+
*
|
|
26
|
+
* Used to detect file-not-found when reading config files.
|
|
27
|
+
* Only the `code` property is needed; other fields are ignored.
|
|
28
|
+
*
|
|
29
|
+
* @internal
|
|
30
|
+
*/
|
|
31
|
+
interface NodeSystemError {
|
|
32
|
+
readonly code: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// --- Minimal process shape — avoids @types/node dependency
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Minimal subset of the Node.js `process` object needed by the adapter.
|
|
39
|
+
*
|
|
40
|
+
* We avoid importing `@types/node` to keep the core runtime-agnostic
|
|
41
|
+
* at the type level. This interface declares only what `createNodeAdapter`
|
|
42
|
+
* actually reads from the global.
|
|
43
|
+
*/
|
|
44
|
+
interface NodeProcess {
|
|
45
|
+
/** Raw process arguments (`[binary, script, ...userArgs]`). */
|
|
46
|
+
readonly argv: readonly string[];
|
|
47
|
+
/** Environment variables (values are `undefined` for unset keys). */
|
|
48
|
+
readonly env: Readonly<Record<string, string | undefined>>;
|
|
49
|
+
/** Runtime version strings — used for version-guard checks. */
|
|
50
|
+
readonly versions?: {
|
|
51
|
+
readonly node?: string;
|
|
52
|
+
readonly bun?: string;
|
|
53
|
+
};
|
|
54
|
+
/** Return the current working directory. */
|
|
55
|
+
cwd(): string;
|
|
56
|
+
/** Platform identifier (e.g. `'linux'`, `'darwin'`, `'win32'`). */
|
|
57
|
+
readonly platform: string;
|
|
58
|
+
/** Standard input stream with TTY detection and async iteration. */
|
|
59
|
+
readonly stdin: {
|
|
60
|
+
readonly isTTY?: boolean;
|
|
61
|
+
/** Async iterable for reading all of stdin (used by readStdin). */
|
|
62
|
+
[Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
|
|
63
|
+
};
|
|
64
|
+
/** Standard output stream with TTY detection and write. */
|
|
65
|
+
readonly stdout: {
|
|
66
|
+
readonly isTTY?: boolean;
|
|
67
|
+
write(data: string): unknown;
|
|
68
|
+
};
|
|
69
|
+
/** Standard error stream with write. */
|
|
70
|
+
readonly stderr: {
|
|
71
|
+
write(data: string): unknown;
|
|
72
|
+
};
|
|
73
|
+
/** Terminate the process with the given exit code. */
|
|
74
|
+
exit(code: number): never;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// --- Process access — isolated for testability
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Access the global `process` object without importing `@types/node`.
|
|
81
|
+
*
|
|
82
|
+
* Cast through `unknown` to avoid TypeScript errors when `@types/node`
|
|
83
|
+
* isn't installed. Safe because this adapter is only used on Node/Bun
|
|
84
|
+
* where `globalThis.process` is always defined.
|
|
85
|
+
*
|
|
86
|
+
* @internal
|
|
87
|
+
*/
|
|
88
|
+
function getNodeProcess(): NodeProcess {
|
|
89
|
+
return (globalThis as unknown as { process: NodeProcess }).process;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// --- Filesystem & path helpers — config discovery primitives
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Detect whether an unknown thrown value is a Node.js system error
|
|
96
|
+
* with a string `code` property (e.g. `'ENOENT'`, `'EACCES'`).
|
|
97
|
+
*
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
function isNodeSystemError(err: unknown): err is NodeSystemError {
|
|
101
|
+
if (typeof err !== 'object' || err === null || !('code' in err)) return false;
|
|
102
|
+
// After the `in` check, TS narrows err to `object & Record<'code', unknown>`.
|
|
103
|
+
const candidate: { code: unknown } = err;
|
|
104
|
+
return typeof candidate.code === 'string';
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Resolve the user's home directory from environment variables.
|
|
109
|
+
*
|
|
110
|
+
* **Windows** (`win32`): `USERPROFILE` → `HOMEDRIVE`+`HOMEPATH` (both required) → `HOME` → `C:\`
|
|
111
|
+
* **Unix**: `HOME` → `/`
|
|
112
|
+
*
|
|
113
|
+
* `HOMEPATH` alone is never used — it is a relative fragment (e.g. `\Users\alice`) that only
|
|
114
|
+
* makes sense when combined with `HOMEDRIVE` (e.g. `C:`). Using `HOMEPATH` without `HOMEDRIVE`
|
|
115
|
+
* could resolve against the working drive, producing an incorrect path.
|
|
116
|
+
*
|
|
117
|
+
* We avoid importing `node:os` to keep the factory synchronous and
|
|
118
|
+
* to maintain the pattern of deriving everything from the process object.
|
|
119
|
+
*
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
function resolveHomedir(
|
|
123
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
124
|
+
platform: string,
|
|
125
|
+
): string {
|
|
126
|
+
return resolveHomeDirectory(env, platform === 'win32');
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Resolve the platform-specific user configuration directory.
|
|
131
|
+
*
|
|
132
|
+
* - Unix: `$XDG_CONFIG_HOME` (if set), otherwise `~/.config`
|
|
133
|
+
* - Windows: `%APPDATA%` (if set), otherwise `~\AppData\Roaming`
|
|
134
|
+
*
|
|
135
|
+
* @internal
|
|
136
|
+
*/
|
|
137
|
+
function resolveConfigDir(
|
|
138
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
139
|
+
platform: string,
|
|
140
|
+
homedir: string,
|
|
141
|
+
): string {
|
|
142
|
+
return resolveConfigDirectory(env, platform === 'win32', homedir);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function assertProcessRuntimeSupported(proc: NodeProcess): void {
|
|
146
|
+
if (proc.versions?.bun !== undefined) {
|
|
147
|
+
assertRuntimeVersionSupported('bun', proc.versions.bun);
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
assertRuntimeVersionSupported('node', proc.versions?.node);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// --- Node adapter factory
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Create a runtime adapter backed by Node.js `process` globals.
|
|
158
|
+
*
|
|
159
|
+
* Reads `process.argv`, `process.env`, `process.cwd()`, and wraps
|
|
160
|
+
* `process.stdout.write`/`process.stderr.write` as {@linkcode WriteFn} functions.
|
|
161
|
+
*
|
|
162
|
+
* Also works on Bun, which provides a Node-compatible `process` global.
|
|
163
|
+
*
|
|
164
|
+
* @param proc - Override the process object (useful for testing the adapter itself).
|
|
165
|
+
* @returns A {@linkcode RuntimeAdapter} backed by Node.js process state.
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* ```ts
|
|
169
|
+
* import { cli } from '@kjanat/dreamcli';
|
|
170
|
+
* import { createNodeAdapter } from '@kjanat/dreamcli/runtime/node';
|
|
171
|
+
*
|
|
172
|
+
* cli('mycli')
|
|
173
|
+
* .command(deploy)
|
|
174
|
+
* .run({ adapter: createNodeAdapter() });
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
177
|
+
function createNodeAdapter(proc?: NodeProcess): RuntimeAdapter {
|
|
178
|
+
const p = proc ?? getNodeProcess();
|
|
179
|
+
assertProcessRuntimeSupported(p);
|
|
180
|
+
|
|
181
|
+
const stdoutWrite: WriteFn = (data) => {
|
|
182
|
+
p.stdout.write(data);
|
|
183
|
+
};
|
|
184
|
+
const stderrWrite: WriteFn = (data) => {
|
|
185
|
+
p.stderr.write(data);
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Stdin line reading via readline — created lazily on first call.
|
|
189
|
+
// This avoids importing readline unless prompting actually occurs.
|
|
190
|
+
const stdinRead = createNodeReadLine(p);
|
|
191
|
+
|
|
192
|
+
// --- Filesystem primitives for config discovery ---
|
|
193
|
+
|
|
194
|
+
const readFile = async (path: string): Promise<string | null> => {
|
|
195
|
+
const fs = await import('node:fs/promises');
|
|
196
|
+
try {
|
|
197
|
+
return await fs.readFile(path, 'utf8');
|
|
198
|
+
} catch (err: unknown) {
|
|
199
|
+
// ENOENT = file not found → null (expected for config discovery probing)
|
|
200
|
+
if (isNodeSystemError(err) && err.code === 'ENOENT') {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
throw err; // Permission denied, is-directory, etc. → caller handles
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const homedir = resolveHomedir(p.env, p.platform);
|
|
208
|
+
const configDir = resolveConfigDir(p.env, p.platform, homedir);
|
|
209
|
+
|
|
210
|
+
const stdinIsTTY = p.stdin.isTTY === true;
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
argv: p.argv,
|
|
214
|
+
env: p.env,
|
|
215
|
+
cwd: p.cwd(),
|
|
216
|
+
stdout: stdoutWrite,
|
|
217
|
+
stderr: stderrWrite,
|
|
218
|
+
stdin: stdinRead,
|
|
219
|
+
readStdin: () => readNodeStdinAll(p, stdinIsTTY),
|
|
220
|
+
isTTY: p.stdout.isTTY === true,
|
|
221
|
+
stdinIsTTY,
|
|
222
|
+
exit: (code) => p.exit(code),
|
|
223
|
+
readFile,
|
|
224
|
+
homedir,
|
|
225
|
+
configDir,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Read all of stdin as a single string (for piped data).
|
|
231
|
+
*
|
|
232
|
+
* Returns `null` immediately if stdin is a TTY — the user is typing
|
|
233
|
+
* interactively, so there's no piped data to consume. When stdin is
|
|
234
|
+
* piped, collects all chunks via the async iterator until EOF and
|
|
235
|
+
* returns the decoded string, which may be empty for an empty pipe.
|
|
236
|
+
*
|
|
237
|
+
* @internal
|
|
238
|
+
*/
|
|
239
|
+
async function readNodeStdinAll(proc: NodeProcess, stdinIsTTY: boolean): Promise<string | null> {
|
|
240
|
+
if (stdinIsTTY) return null;
|
|
241
|
+
|
|
242
|
+
const chunks: string[] = [];
|
|
243
|
+
const decoder = new TextDecoder();
|
|
244
|
+
|
|
245
|
+
for await (const chunk of proc.stdin) {
|
|
246
|
+
chunks.push(decoder.decode(chunk, { stream: true }));
|
|
247
|
+
}
|
|
248
|
+
// Flush any remaining bytes held by the streaming decoder
|
|
249
|
+
chunks.push(decoder.decode());
|
|
250
|
+
|
|
251
|
+
return chunks.join('');
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Read a single line from Node's stdin via its async iterator.
|
|
256
|
+
*
|
|
257
|
+
* Iterates stdin chunks manually (without `for-await` to avoid calling
|
|
258
|
+
* `iterator.return()` which would close the stream). Returns `null`
|
|
259
|
+
* on EOF.
|
|
260
|
+
*
|
|
261
|
+
* @internal
|
|
262
|
+
*/
|
|
263
|
+
function createNodeReadLine(proc: NodeProcess): ReadFn {
|
|
264
|
+
const iter = proc.stdin[Symbol.asyncIterator]();
|
|
265
|
+
const decoder = new TextDecoder();
|
|
266
|
+
let buffer = '';
|
|
267
|
+
let done = false;
|
|
268
|
+
|
|
269
|
+
return async (): Promise<string | null> => {
|
|
270
|
+
for (;;) {
|
|
271
|
+
const nlIndex = buffer.indexOf('\n');
|
|
272
|
+
if (nlIndex !== -1) {
|
|
273
|
+
const line = buffer.slice(0, nlIndex).replace(/\r$/, '');
|
|
274
|
+
buffer = buffer.slice(nlIndex + 1);
|
|
275
|
+
return line;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (done) {
|
|
279
|
+
if (buffer.length === 0) return null;
|
|
280
|
+
const line = buffer;
|
|
281
|
+
buffer = '';
|
|
282
|
+
return line;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const result = await iter.next();
|
|
286
|
+
if (result.done) {
|
|
287
|
+
buffer += decoder.decode();
|
|
288
|
+
done = true;
|
|
289
|
+
continue;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
buffer += decoder.decode(result.value, { stream: true });
|
|
293
|
+
}
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// --- Exports
|
|
298
|
+
|
|
299
|
+
export type { NodeProcess };
|
|
300
|
+
export { createNodeAdapter };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared env-based path resolution for runtime adapters.
|
|
3
|
+
*
|
|
4
|
+
* Node, Bun, and Deno all need the same user home/config directory behavior.
|
|
5
|
+
* These helpers keep the fallback chain identical across runtimes while
|
|
6
|
+
* letting each adapter supply its own platform detection.
|
|
7
|
+
*
|
|
8
|
+
* @module @kjanat/dreamcli/runtime/paths
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Resolve the user's home directory from environment variables.
|
|
13
|
+
*
|
|
14
|
+
* @param env - Environment variable map.
|
|
15
|
+
* @param isWindows - Whether the platform is Windows.
|
|
16
|
+
* @returns Absolute path to the home directory.
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
function resolveHomeDirectory(
|
|
20
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
21
|
+
isWindows: boolean,
|
|
22
|
+
): string {
|
|
23
|
+
if (isWindows) {
|
|
24
|
+
if (env.USERPROFILE) return env.USERPROFILE;
|
|
25
|
+
if (env.HOMEDRIVE && env.HOMEPATH) {
|
|
26
|
+
return env.HOMEDRIVE + env.HOMEPATH;
|
|
27
|
+
}
|
|
28
|
+
return env.HOME || 'C:\\';
|
|
29
|
+
}
|
|
30
|
+
return env.HOME || '/';
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Resolve the user's config directory (`XDG_CONFIG_HOME` / `APPDATA`).
|
|
35
|
+
*
|
|
36
|
+
* @param env - Environment variable map.
|
|
37
|
+
* @param isWindows - Whether the platform is Windows.
|
|
38
|
+
* @param homedir - Pre-resolved home directory (from {@link resolveHomeDirectory}).
|
|
39
|
+
* @returns Absolute path to the config directory.
|
|
40
|
+
* @internal
|
|
41
|
+
*/
|
|
42
|
+
function resolveConfigDirectory(
|
|
43
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
44
|
+
isWindows: boolean,
|
|
45
|
+
homedir: string,
|
|
46
|
+
): string {
|
|
47
|
+
if (isWindows) {
|
|
48
|
+
if (env.APPDATA !== undefined && env.APPDATA !== '') return env.APPDATA;
|
|
49
|
+
const normalizedHome = homedir.replace(/[\\/]+$/, '') || homedir;
|
|
50
|
+
return `${normalizedHome}\\AppData\\Roaming`;
|
|
51
|
+
}
|
|
52
|
+
const normalizedHome = homedir.replace(/\/+$/, '');
|
|
53
|
+
if (env.XDG_CONFIG_HOME !== undefined && env.XDG_CONFIG_HOME !== '') {
|
|
54
|
+
return env.XDG_CONFIG_HOME;
|
|
55
|
+
}
|
|
56
|
+
if (normalizedHome.length === 0) {
|
|
57
|
+
return '/.config';
|
|
58
|
+
}
|
|
59
|
+
return `${normalizedHome}/.config`;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { resolveConfigDirectory, resolveHomeDirectory };
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime compatibility matrix and version guard helpers.
|
|
3
|
+
*
|
|
4
|
+
* Keeps the supported runtime story in one place so adapter guards,
|
|
5
|
+
* package engines, and docs can stay aligned.
|
|
6
|
+
*
|
|
7
|
+
* @internal
|
|
8
|
+
* @module @kjanat/dreamcli/runtime/support
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
type SupportedRuntime = 'node' | 'bun' | 'deno';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Support entry for a single runtime: version bounds, display names, and adapter mapping.
|
|
15
|
+
*
|
|
16
|
+
* @internal
|
|
17
|
+
*/
|
|
18
|
+
interface RuntimeSupportEntry {
|
|
19
|
+
/** Runtime identifier key. */
|
|
20
|
+
readonly runtime: SupportedRuntime;
|
|
21
|
+
/** Human-readable name (e.g. `'Node.js'`, `'Bun'`). */
|
|
22
|
+
readonly displayName: string;
|
|
23
|
+
/** Minimum supported version (semver, e.g. `'22.22.2'`). */
|
|
24
|
+
readonly minimum: string;
|
|
25
|
+
/** `engines` range string for package.json (e.g. `'>=22.22.2'`). */
|
|
26
|
+
readonly engineRange: string;
|
|
27
|
+
/** npm/JSR package name that ships this adapter. */
|
|
28
|
+
readonly packageName: string;
|
|
29
|
+
/** Adapter factory function name (e.g. `'NodeAdapter'`). */
|
|
30
|
+
readonly adapterName: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** All supported runtimes with their version bounds and adapter metadata. @internal */
|
|
34
|
+
const SUPPORTED_RUNTIMES: readonly RuntimeSupportEntry[] = [
|
|
35
|
+
{
|
|
36
|
+
runtime: 'node',
|
|
37
|
+
displayName: 'Node.js',
|
|
38
|
+
minimum: '22.22.2',
|
|
39
|
+
engineRange: '>=22.22.2',
|
|
40
|
+
packageName: '@kjanat/dreamcli',
|
|
41
|
+
adapterName: 'NodeAdapter',
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
runtime: 'bun',
|
|
45
|
+
displayName: 'Bun',
|
|
46
|
+
minimum: '1.3.11',
|
|
47
|
+
engineRange: '>=1.3.11',
|
|
48
|
+
packageName: '@kjanat/dreamcli',
|
|
49
|
+
adapterName: 'BunAdapter',
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
runtime: 'deno',
|
|
53
|
+
displayName: 'Deno',
|
|
54
|
+
minimum: '2.6.0',
|
|
55
|
+
engineRange: '>=2.6.0',
|
|
56
|
+
packageName: '@kjanat/dreamcli',
|
|
57
|
+
adapterName: 'DenoAdapter',
|
|
58
|
+
},
|
|
59
|
+
];
|
|
60
|
+
|
|
61
|
+
interface ParsedVersion {
|
|
62
|
+
readonly major: number;
|
|
63
|
+
readonly minor: number;
|
|
64
|
+
readonly patch: number;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const VERSION_PATTERN = /^(\d+)(?:\.(\d+))?(?:\.(\d+))?/;
|
|
68
|
+
|
|
69
|
+
/** Look up the support entry for a given runtime. Throws on unknown runtime. @internal */
|
|
70
|
+
function getRuntimeSupport(runtime: SupportedRuntime): RuntimeSupportEntry {
|
|
71
|
+
for (const entry of SUPPORTED_RUNTIMES) {
|
|
72
|
+
if (entry.runtime === runtime) return entry;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
throw new Error(`Unknown supported runtime '${runtime}'`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function parseVersion(version: string): ParsedVersion | undefined {
|
|
79
|
+
const match = VERSION_PATTERN.exec(version);
|
|
80
|
+
if (match === null) return undefined;
|
|
81
|
+
|
|
82
|
+
const major = Number.parseInt(match[1] ?? '', 10);
|
|
83
|
+
const minorText = match[2];
|
|
84
|
+
const minor = minorText === undefined ? 0 : Number.parseInt(minorText, 10);
|
|
85
|
+
const patchText = match[3];
|
|
86
|
+
const patch = patchText === undefined ? 0 : Number.parseInt(patchText, 10);
|
|
87
|
+
|
|
88
|
+
if (Number.isNaN(major) || Number.isNaN(minor) || Number.isNaN(patch)) {
|
|
89
|
+
return undefined;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return { major, minor, patch };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function compareVersions(left: ParsedVersion, right: ParsedVersion): number {
|
|
96
|
+
if (left.major !== right.major) return left.major - right.major;
|
|
97
|
+
if (left.minor !== right.minor) return left.minor - right.minor;
|
|
98
|
+
return left.patch - right.patch;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Format a human-readable version requirement string (e.g. `'Node.js >= 22.22.2'`). @internal */
|
|
102
|
+
function formatRuntimeRequirement(runtime: SupportedRuntime): string {
|
|
103
|
+
const support = getRuntimeSupport(runtime);
|
|
104
|
+
return `${support.displayName} >= ${support.minimum}`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** Check whether a version string meets the minimum for the given runtime. @internal */
|
|
108
|
+
function isRuntimeVersionSupported(runtime: SupportedRuntime, version: string): boolean {
|
|
109
|
+
const parsedVersion = parseVersion(version);
|
|
110
|
+
const minimumVersion = parseVersion(getRuntimeSupport(runtime).minimum);
|
|
111
|
+
if (parsedVersion === undefined || minimumVersion === undefined) return false;
|
|
112
|
+
return compareVersions(parsedVersion, minimumVersion) >= 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/** Throw if the detected version is below the minimum. No-op when version is undefined. @internal */
|
|
116
|
+
function assertRuntimeVersionSupported(
|
|
117
|
+
runtime: SupportedRuntime,
|
|
118
|
+
version: string | undefined,
|
|
119
|
+
): void {
|
|
120
|
+
if (version === undefined) return;
|
|
121
|
+
if (isRuntimeVersionSupported(runtime, version)) return;
|
|
122
|
+
|
|
123
|
+
throw new Error(
|
|
124
|
+
`@kjanat/dreamcli requires ${formatRuntimeRequirement(runtime)}. Detected ${getRuntimeSupport(runtime).displayName} ${version}.`,
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export type { RuntimeSupportEntry, SupportedRuntime };
|
|
129
|
+
export {
|
|
130
|
+
assertRuntimeVersionSupported,
|
|
131
|
+
formatRuntimeRequirement,
|
|
132
|
+
getRuntimeSupport,
|
|
133
|
+
isRuntimeVersionSupported,
|
|
134
|
+
SUPPORTED_RUNTIMES,
|
|
135
|
+
};
|
package/src/runtime.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime adapters for platform portability (Node, Bun, Deno).
|
|
3
|
+
*
|
|
4
|
+
* Provides the {@link RuntimeAdapter} interface and concrete adapter
|
|
5
|
+
* factories for each supported platform.
|
|
6
|
+
*
|
|
7
|
+
* Start here when embedding DreamCLI in a real host runtime:
|
|
8
|
+
* - {@link createAdapter} for normal auto-detected production use
|
|
9
|
+
* - {@link createNodeAdapter}, {@link createDenoAdapter}, or
|
|
10
|
+
* {@link createBunAdapter} when the runtime must be selected explicitly
|
|
11
|
+
*
|
|
12
|
+
* Most applications do not need this subpath unless they are wiring DreamCLI
|
|
13
|
+
* into a custom host, testing runtime behavior directly, or building their own
|
|
14
|
+
* process/bootstrap layer.
|
|
15
|
+
*
|
|
16
|
+
* @module @kjanat/dreamcli/runtime
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
export type {
|
|
20
|
+
Runtime,
|
|
21
|
+
RuntimeAdapter,
|
|
22
|
+
} from './runtime/index.ts';
|
|
23
|
+
export {
|
|
24
|
+
createAdapter,
|
|
25
|
+
createBunAdapter,
|
|
26
|
+
createDenoAdapter,
|
|
27
|
+
createNodeAdapter,
|
|
28
|
+
detectRuntime,
|
|
29
|
+
ExitError,
|
|
30
|
+
RUNTIMES,
|
|
31
|
+
} from './runtime/index.ts';
|
package/src/testkit.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test utilities for DreamCLI commands.
|
|
3
|
+
*
|
|
4
|
+
* Provides {@linkcode runCommand | runCommand()} — the in-process test harness — plus test doubles
|
|
5
|
+
* for output capture, prompt simulation, and runtime adaptation.
|
|
6
|
+
*
|
|
7
|
+
* Start here for most tests:
|
|
8
|
+
* - {@link runCommand} to execute a command in-process
|
|
9
|
+
* - {@link createCaptureOutput} to assert on raw output behavior
|
|
10
|
+
* - {@link createTestPrompter} to script interactive answers
|
|
11
|
+
* - {@link createTestAdapter} when you need adapter-level control
|
|
12
|
+
*
|
|
13
|
+
* @module @kjanat/dreamcli/testkit
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
export { type CapturedOutput, createCaptureOutput } from './core/output/index.ts';
|
|
17
|
+
export {
|
|
18
|
+
createTestPrompter,
|
|
19
|
+
PROMPT_CANCEL,
|
|
20
|
+
type TestAnswer,
|
|
21
|
+
type TestPrompterOptions,
|
|
22
|
+
} from './core/prompt/index.ts';
|
|
23
|
+
export type { RunOptions, RunResult } from './core/testkit/index.ts';
|
|
24
|
+
export { runCommand } from './core/testkit/index.ts';
|
|
25
|
+
export { createTestAdapter, type TestAdapterOptions } from './runtime/index.ts';
|
package/src/version.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Build-time version constants.
|
|
3
|
+
*
|
|
4
|
+
* Replaced by tsdown `define` during bundling. In development (unbundled),
|
|
5
|
+
* the declared globals resolve to the `'dev'` fallback.
|
|
6
|
+
*
|
|
7
|
+
* @module dreamcli/version
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
declare const __DREAMCLI_VERSION__: string;
|
|
12
|
+
declare const __DREAMCLI_REVISION__: string;
|
|
13
|
+
|
|
14
|
+
/** Package version (e.g. `"0.9.1"`). `"dev"` when running unbundled. */
|
|
15
|
+
export const DREAMCLI_VERSION: string =
|
|
16
|
+
typeof __DREAMCLI_VERSION__ !== 'undefined' ? __DREAMCLI_VERSION__ : 'dev';
|
|
17
|
+
|
|
18
|
+
/** Git short SHA (e.g. `"f9b5f1a"`). `"dev"` when running unbundled. */
|
|
19
|
+
export const DREAMCLI_REVISION: string =
|
|
20
|
+
typeof __DREAMCLI_REVISION__ !== 'undefined' ? __DREAMCLI_REVISION__ : 'dev';
|