@kjanat/dreamcli 0.0.0 → 1.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 +667 -0
- package/LICENSE +22 -0
- package/README.md +370 -0
- package/dist/adapter-B37d9oia.mjs +1 -0
- package/dist/adapter-CCSh5juX.cjs +1 -0
- package/dist/adapter-DIwO-q7K.d.mts +3447 -0
- package/dist/adapter-DQnxxLiE.d.cts +3447 -0
- package/dist/auto-5cjZaF9V.mjs +3 -0
- package/dist/auto-B0eSgoDw.cjs +3 -0
- package/dist/index.cjs +10 -0
- package/dist/index.d.cts +920 -0
- package/dist/index.d.mts +920 -0
- package/dist/index.mjs +10 -0
- package/dist/runtime.cjs +1 -0
- package/dist/runtime.d.cts +253 -0
- package/dist/runtime.d.mts +253 -0
- package/dist/runtime.mjs +1 -0
- package/dist/testkit-CEDYPvhF.mjs +14 -0
- package/dist/testkit-ClIKijxC.cjs +14 -0
- package/dist/testkit.cjs +1 -0
- package/dist/testkit.d.cts +25 -0
- package/dist/testkit.d.mts +25 -0
- package/dist/testkit.mjs +1 -0
- package/examples/basic.ts +33 -0
- package/examples/gh/package.json +48 -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/lib/auth.ts +37 -0
- package/examples/gh/src/lib/utils.ts +17 -0
- package/examples/gh/src/main.test.ts +306 -0
- package/examples/gh/src/main.ts +38 -0
- package/examples/gh/tsconfig.json +37 -0
- package/examples/interactive.ts +66 -0
- package/examples/json-mode.ts +93 -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 +145 -0
- package/examples/tsconfig.json +17 -0
- package/package.json +123 -2
- package/src/core/cli/AGENTS.md +72 -0
- package/src/core/cli/dispatch.ts +282 -0
- package/src/core/cli/index.ts +1425 -0
- package/src/core/cli/plugin.ts +111 -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/completion/AGENTS.md +72 -0
- package/src/core/completion/completion-test-helpers.ts +48 -0
- package/src/core/completion/index.ts +82 -0
- package/src/core/completion/shells/bash.ts +456 -0
- package/src/core/completion/shells/fish.ts +24 -0
- package/src/core/completion/shells/powershell.ts +24 -0
- package/src/core/completion/shells/shared.ts +283 -0
- package/src/core/completion/shells/zsh.ts +376 -0
- package/src/core/config/index.ts +404 -0
- package/src/core/config/package-json.ts +216 -0
- package/src/core/errors/index.ts +178 -0
- package/src/core/help/index.ts +559 -0
- package/src/core/json-schema/index.ts +705 -0
- package/src/core/output/AGENTS.md +69 -0
- package/src/core/output/activity.ts +568 -0
- package/src/core/output/display-value.ts +48 -0
- package/src/core/output/index.ts +704 -0
- package/src/core/output/writer.ts +46 -0
- package/src/core/parse/index.ts +643 -0
- package/src/core/prompt/index.ts +566 -0
- package/src/core/resolve/AGENTS.md +69 -0
- package/src/core/resolve/index.ts +1331 -0
- package/src/core/schema/AGENTS.md +61 -0
- package/src/core/schema/activity.ts +237 -0
- package/src/core/schema/arg.ts +707 -0
- package/src/core/schema/command.ts +1263 -0
- package/src/core/schema/flag.ts +651 -0
- package/src/core/schema/index.ts +82 -0
- package/src/core/schema/middleware.ts +171 -0
- package/src/core/schema/prompt.ts +148 -0
- package/src/core/schema/run.ts +187 -0
- package/src/core/testkit/AGENTS.md +55 -0
- package/src/core/testkit/index.ts +365 -0
- package/src/index.ts +164 -0
- package/src/runtime/AGENTS.md +74 -0
- package/src/runtime/adapter.ts +300 -0
- package/src/runtime/auto.ts +110 -0
- package/src/runtime/bun.ts +55 -0
- package/src/runtime/deno.ts +355 -0
- package/src/runtime/detect.ts +102 -0
- package/src/runtime/index.ts +21 -0
- package/src/runtime/node-builtins.d.ts +23 -0
- package/src/runtime/node.ts +278 -0
- package/src/runtime/paths.ts +56 -0
- package/src/runtime/support.ts +119 -0
- package/src/runtime/test-helpers.ts +40 -0
- package/src/runtime.ts +31 -0
- package/src/testkit.ts +23 -0
- package/src/version.ts +20 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deno runtime adapter implementation.
|
|
3
|
+
*
|
|
4
|
+
* Bridges the platform-agnostic `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 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
|
+
readonly build: {
|
|
42
|
+
readonly os:
|
|
43
|
+
| 'darwin'
|
|
44
|
+
| 'linux'
|
|
45
|
+
| 'android'
|
|
46
|
+
| 'windows'
|
|
47
|
+
| 'freebsd'
|
|
48
|
+
| 'netbsd'
|
|
49
|
+
| 'aix'
|
|
50
|
+
| 'solaris'
|
|
51
|
+
| 'illumos';
|
|
52
|
+
};
|
|
53
|
+
readonly version?: {
|
|
54
|
+
readonly deno?: string;
|
|
55
|
+
};
|
|
56
|
+
/** Raw command-line args (excludes the binary/script — Deno pre-strips them). */
|
|
57
|
+
readonly args: readonly string[];
|
|
58
|
+
|
|
59
|
+
/** Environment variable access (requires `--allow-env`). */
|
|
60
|
+
readonly env: {
|
|
61
|
+
/** Get a single env var. Returns `undefined` if unset or permission denied. */
|
|
62
|
+
get(key: string): string | undefined;
|
|
63
|
+
/** Get all env vars as a plain object. Throws on permission denied. */
|
|
64
|
+
toObject(): Record<string, string>;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/** Current working directory (may throw if `--allow-read` is denied for cwd). */
|
|
68
|
+
cwd(): string;
|
|
69
|
+
|
|
70
|
+
readonly stdout: {
|
|
71
|
+
/** Write raw bytes to stdout synchronously. */
|
|
72
|
+
writeSync(p: Uint8Array): number;
|
|
73
|
+
/** Whether stdout is connected to a TTY. */
|
|
74
|
+
isTerminal(): boolean;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
readonly stderr: {
|
|
78
|
+
/** Write raw bytes to stderr synchronously. */
|
|
79
|
+
writeSync(p: Uint8Array): number;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
readonly stdin: {
|
|
83
|
+
/** Whether stdin is connected to a TTY. */
|
|
84
|
+
isTerminal(): boolean;
|
|
85
|
+
/** Readable stream for stdin bytes. */
|
|
86
|
+
readonly readable: ReadableStream<Uint8Array>;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/** Exit the process with the given code. */
|
|
90
|
+
exit(code: number): never;
|
|
91
|
+
|
|
92
|
+
/** Read a file as UTF-8 text (requires `--allow-read`). */
|
|
93
|
+
readTextFile(path: string): Promise<string>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// --- Permission-safe helpers
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Detect whether an unknown thrown value is a Deno error by name.
|
|
100
|
+
*
|
|
101
|
+
* @internal
|
|
102
|
+
*/
|
|
103
|
+
function isDenoErrorNamed(err: unknown, name: string): boolean {
|
|
104
|
+
if (typeof err !== 'object' || err === null || !('name' in err)) return false;
|
|
105
|
+
// After the `in` check, TS narrows err to `object & Record<'name', unknown>`.
|
|
106
|
+
const candidate: { name: unknown } = err;
|
|
107
|
+
return typeof candidate.name === 'string' && candidate.name === name;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Safely read all environment variables. Returns empty object if
|
|
112
|
+
* `--allow-env` permission is not granted.
|
|
113
|
+
*
|
|
114
|
+
* @internal
|
|
115
|
+
*/
|
|
116
|
+
function safeEnvToObject(deno: DenoNamespace): Readonly<Record<string, string | undefined>> {
|
|
117
|
+
try {
|
|
118
|
+
return deno.env.toObject();
|
|
119
|
+
} catch (err: unknown) {
|
|
120
|
+
if (isDenoErrorNamed(err, 'PermissionDenied')) return {};
|
|
121
|
+
throw err;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Safely get the current working directory. Returns `/` if the cwd is
|
|
127
|
+
* inaccessible due to missing permissions.
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
function safeCwd(deno: DenoNamespace): string {
|
|
132
|
+
try {
|
|
133
|
+
return deno.cwd();
|
|
134
|
+
} catch (err: unknown) {
|
|
135
|
+
if (isDenoErrorNamed(err, 'PermissionDenied')) return '/';
|
|
136
|
+
throw err;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// --- Deno adapter factory
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Create a runtime adapter backed by the Deno namespace.
|
|
144
|
+
*
|
|
145
|
+
* Reads `Deno.args`, `Deno.env`, `Deno.cwd()`, and wraps Deno's stream-based
|
|
146
|
+
* I/O into the `WriteFn`/`ReadFn` functions expected by the framework.
|
|
147
|
+
*
|
|
148
|
+
* Unlike Node/Bun, Deno strips the binary and script path from `Deno.args`.
|
|
149
|
+
* The adapter prepends synthetic entries (`['deno', 'run']`) so the argv
|
|
150
|
+
* shape matches the `RuntimeAdapter` contract (binary + script + user args).
|
|
151
|
+
*
|
|
152
|
+
* @param ns - Override the Deno namespace (useful for testing the adapter itself).
|
|
153
|
+
* @returns A `RuntimeAdapter` backed by Deno's namespace APIs.
|
|
154
|
+
*
|
|
155
|
+
* @example
|
|
156
|
+
* ```ts
|
|
157
|
+
* import { cli } from 'dreamcli';
|
|
158
|
+
* import { createDenoAdapter } from 'dreamcli/runtime';
|
|
159
|
+
*
|
|
160
|
+
* cli('mycli')
|
|
161
|
+
* .command(deploy)
|
|
162
|
+
* .run({ adapter: createDenoAdapter() });
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
function createDenoAdapter(ns?: DenoNamespace): RuntimeAdapter {
|
|
166
|
+
const d = ns ?? getDenoNamespace();
|
|
167
|
+
assertRuntimeVersionSupported('deno', d.version?.deno);
|
|
168
|
+
const encoder = new TextEncoder();
|
|
169
|
+
|
|
170
|
+
// --- Synthetic argv: Deno.args has user args only (no binary/script) ---
|
|
171
|
+
const argv: readonly string[] = ['deno', 'run', ...d.args];
|
|
172
|
+
|
|
173
|
+
// --- Environment (permission-safe) ---
|
|
174
|
+
const env = safeEnvToObject(d);
|
|
175
|
+
|
|
176
|
+
// --- CWD (permission-safe) ---
|
|
177
|
+
const cwd = safeCwd(d);
|
|
178
|
+
|
|
179
|
+
// --- I/O writers ---
|
|
180
|
+
const stdoutWrite: WriteFn = (data) => {
|
|
181
|
+
d.stdout.writeSync(encoder.encode(data));
|
|
182
|
+
};
|
|
183
|
+
const stderrWrite: WriteFn = (data) => {
|
|
184
|
+
d.stderr.writeSync(encoder.encode(data));
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// --- Stdin line reading ---
|
|
188
|
+
const stdinRead: ReadFn = () => readDenoStdinLine(d);
|
|
189
|
+
|
|
190
|
+
// --- Filesystem ---
|
|
191
|
+
const readFile = async (path: string): Promise<string | null> => {
|
|
192
|
+
try {
|
|
193
|
+
return await d.readTextFile(path);
|
|
194
|
+
} catch (err: unknown) {
|
|
195
|
+
// NotFound = file doesn't exist → null (expected for config probing)
|
|
196
|
+
if (isDenoErrorNamed(err, 'NotFound')) return null;
|
|
197
|
+
// PermissionDenied = --allow-read not granted → treat as not found
|
|
198
|
+
// (config discovery is opt-in; failing silently is the graceful path)
|
|
199
|
+
if (isDenoErrorNamed(err, 'PermissionDenied')) return null;
|
|
200
|
+
throw err; // Other I/O errors propagate
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
// --- Home directory ---
|
|
205
|
+
const homedir = resolveDenoHomedir(env, d.build.os === 'windows');
|
|
206
|
+
|
|
207
|
+
// --- Config directory ---
|
|
208
|
+
const configDir = resolveDenoConfigDir(env, homedir, d.build.os === 'windows');
|
|
209
|
+
|
|
210
|
+
const stdinIsTTY = d.stdin.isTerminal();
|
|
211
|
+
|
|
212
|
+
return {
|
|
213
|
+
argv,
|
|
214
|
+
env,
|
|
215
|
+
cwd,
|
|
216
|
+
stdout: stdoutWrite,
|
|
217
|
+
stderr: stderrWrite,
|
|
218
|
+
stdin: stdinRead,
|
|
219
|
+
readStdin: () => readDenoStdinAll(d, stdinIsTTY),
|
|
220
|
+
isTTY: d.stdout.isTerminal(),
|
|
221
|
+
stdinIsTTY,
|
|
222
|
+
exit: (code) => d.exit(code),
|
|
223
|
+
readFile,
|
|
224
|
+
homedir,
|
|
225
|
+
configDir,
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// --- Deno namespace access — isolated for testability
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Access the global `Deno` namespace.
|
|
233
|
+
*
|
|
234
|
+
* Cast through `unknown` to avoid TypeScript errors when Deno types
|
|
235
|
+
* aren't installed. Safe because this adapter is only used on Deno
|
|
236
|
+
* where `globalThis.Deno` is always defined.
|
|
237
|
+
*
|
|
238
|
+
* @internal
|
|
239
|
+
*/
|
|
240
|
+
function getDenoNamespace(): DenoNamespace {
|
|
241
|
+
return (globalThis as unknown as { Deno: DenoNamespace }).Deno;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// --- Stdin reading — line (prompts) and full (piped data)
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Read all of stdin as a single string (for piped data).
|
|
248
|
+
*
|
|
249
|
+
* Returns `null` immediately if stdin is a TTY — the user is typing
|
|
250
|
+
* interactively, so there's no piped data to consume. When stdin is
|
|
251
|
+
* piped, collects all chunks via the `ReadableStream` until EOF and
|
|
252
|
+
* returns the decoded string, which may be empty for an empty pipe.
|
|
253
|
+
*
|
|
254
|
+
* @internal
|
|
255
|
+
*/
|
|
256
|
+
async function readDenoStdinAll(deno: DenoNamespace, stdinIsTTY: boolean): Promise<string | null> {
|
|
257
|
+
if (stdinIsTTY) return null;
|
|
258
|
+
|
|
259
|
+
const reader = deno.stdin.readable.getReader();
|
|
260
|
+
const decoder = new TextDecoder();
|
|
261
|
+
const chunks: string[] = [];
|
|
262
|
+
|
|
263
|
+
try {
|
|
264
|
+
for (;;) {
|
|
265
|
+
const { value, done } = await reader.read();
|
|
266
|
+
if (done) {
|
|
267
|
+
// Flush any trailing bytes held by the streaming decoder
|
|
268
|
+
chunks.push(decoder.decode());
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
chunks.push(decoder.decode(value, { stream: true }));
|
|
272
|
+
}
|
|
273
|
+
} finally {
|
|
274
|
+
reader.releaseLock();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return chunks.join('');
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Read a single line from Deno's stdin.
|
|
282
|
+
*
|
|
283
|
+
* Uses the `ReadableStream` API with a manual buffer to read until
|
|
284
|
+
* a newline character is found. Returns `null` on EOF.
|
|
285
|
+
*
|
|
286
|
+
* This is intentionally simple — the terminal prompter handles retries
|
|
287
|
+
* and validation.
|
|
288
|
+
*
|
|
289
|
+
* @internal
|
|
290
|
+
*/
|
|
291
|
+
async function readDenoStdinLine(deno: DenoNamespace): Promise<string | null> {
|
|
292
|
+
const reader = deno.stdin.readable.getReader();
|
|
293
|
+
const decoder = new TextDecoder();
|
|
294
|
+
let buffer = '';
|
|
295
|
+
|
|
296
|
+
try {
|
|
297
|
+
for (;;) {
|
|
298
|
+
const { value, done } = await reader.read();
|
|
299
|
+
if (done) {
|
|
300
|
+
// Flush any trailing bytes held by the streaming decoder
|
|
301
|
+
// (partial multibyte UTF-8 sequences buffered internally).
|
|
302
|
+
buffer += decoder.decode();
|
|
303
|
+
return buffer.length > 0 ? buffer : null;
|
|
304
|
+
}
|
|
305
|
+
buffer += decoder.decode(value, { stream: true });
|
|
306
|
+
const newlineIndex = buffer.indexOf('\n');
|
|
307
|
+
if (newlineIndex !== -1) {
|
|
308
|
+
// Return the line without the newline character.
|
|
309
|
+
// Remaining data after the newline is lost — this is acceptable
|
|
310
|
+
// because each prompt reads one line at a time.
|
|
311
|
+
return buffer.slice(0, newlineIndex).replace(/\r$/, '');
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
} finally {
|
|
315
|
+
reader.releaseLock();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
// --- Home / config directory resolution
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Resolve the user's home directory from environment variables.
|
|
323
|
+
*
|
|
324
|
+
* Deno doesn't expose `os.homedir()` without `--allow-sys`, so we derive
|
|
325
|
+
* it from env vars — same approach as the Node adapter.
|
|
326
|
+
*
|
|
327
|
+
* @internal
|
|
328
|
+
*/
|
|
329
|
+
function resolveDenoHomedir(
|
|
330
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
331
|
+
isWindows: boolean,
|
|
332
|
+
): string {
|
|
333
|
+
return resolveHomeDirectory(env, isWindows);
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* Resolve the platform-specific user configuration directory.
|
|
338
|
+
*
|
|
339
|
+
* Uses `Deno.build.os` for platform detection, then applies the same fallback
|
|
340
|
+
* chain as the Node adapter.
|
|
341
|
+
*
|
|
342
|
+
* @internal
|
|
343
|
+
*/
|
|
344
|
+
function resolveDenoConfigDir(
|
|
345
|
+
env: Readonly<Record<string, string | undefined>>,
|
|
346
|
+
homedir: string,
|
|
347
|
+
isWindows: boolean,
|
|
348
|
+
): string {
|
|
349
|
+
return resolveConfigDirectory(env, isWindows, homedir);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// --- Exports
|
|
353
|
+
|
|
354
|
+
export type { DenoNamespace };
|
|
355
|
+
export { createDenoAdapter };
|
|
@@ -0,0 +1,102 @@
|
|
|
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 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
|
+
readonly Bun?: { readonly version?: string };
|
|
61
|
+
readonly Deno?: { readonly version?: { readonly deno?: string } };
|
|
62
|
+
readonly process?: { readonly versions?: { readonly node?: string; readonly bun?: string } };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// --- Detection
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Detect the current JavaScript runtime.
|
|
69
|
+
*
|
|
70
|
+
* Uses `globalThis` feature detection to identify the host runtime.
|
|
71
|
+
* Detection order is significant: Bun is checked before Node because
|
|
72
|
+
* Bun exposes a Node-compatible `process` global.
|
|
73
|
+
*
|
|
74
|
+
* @param globals - Override `globalThis` for testing. Production callers
|
|
75
|
+
* should omit this parameter.
|
|
76
|
+
* @returns The detected {@link Runtime} identifier.
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* ```ts
|
|
80
|
+
* const rt = detectRuntime();
|
|
81
|
+
* // rt === 'node' | 'bun' | 'deno' | 'unknown'
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
function detectRuntime(globals?: GlobalForDetect): Runtime {
|
|
85
|
+
const g = globals ?? (globalThis as unknown as GlobalForDetect);
|
|
86
|
+
|
|
87
|
+
// Bun first — it also has `process.versions.node`
|
|
88
|
+
if (typeof g.Bun?.version === 'string') return 'bun';
|
|
89
|
+
|
|
90
|
+
// Deno — check the nested version object
|
|
91
|
+
if (typeof g.Deno?.version?.deno === 'string') return 'deno';
|
|
92
|
+
|
|
93
|
+
// Node.js — last because Bun mimics it
|
|
94
|
+
if (typeof g.process?.versions?.node === 'string') return 'node';
|
|
95
|
+
|
|
96
|
+
return 'unknown';
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// --- Exports
|
|
100
|
+
|
|
101
|
+
export type { GlobalForDetect, Runtime };
|
|
102
|
+
export { detectRuntime, RUNTIMES };
|
|
@@ -0,0 +1,21 @@
|
|
|
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 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';
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
}
|