@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.11
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 +227 -1
- package/dist/core/cli/dispatch.d.mts +6 -5
- package/dist/core/cli/dispatch.mjs +205 -1
- package/dist/core/cli/help-links.d.mts +0 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.d.mts +81 -8
- package/dist/core/cli/index.mjs +945 -1
- package/dist/core/cli/planner.d.mts +5 -1
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.d.mts +1 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.d.mts +1 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.d.mts +2 -1
- package/dist/core/cli/root-help.mjs +226 -4
- package/dist/core/cli/root-surface.d.mts +12 -1
- package/dist/core/cli/root-surface.mjs +28 -1
- package/dist/core/cli/runtime-preflight.d.mts +14 -2
- package/dist/core/cli/runtime-preflight.mjs +169 -1
- package/dist/core/completion/index.d.mts +0 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.d.mts +0 -1
- package/dist/core/completion/shells/bash.mjs +308 -3
- package/dist/core/completion/shells/fish.d.mts +0 -1
- package/dist/core/completion/shells/fish.mjs +162 -2
- package/dist/core/completion/shells/powershell.d.mts +0 -1
- package/dist/core/completion/shells/powershell.mjs +358 -2
- package/dist/core/completion/shells/shared.d.mts +13 -2
- package/dist/core/completion/shells/shared.mjs +176 -1
- package/dist/core/completion/shells/zsh.d.mts +0 -1
- package/dist/core/completion/shells/zsh.mjs +251 -2
- package/dist/core/config/index.d.mts +2 -3
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.d.mts +27 -4
- package/dist/core/config/package-json.mjs +386 -3
- package/dist/core/errors/index.d.mts +3 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.d.mts +0 -1
- package/dist/core/execution/index.mjs +201 -1
- package/dist/core/help/ansi.d.mts +11 -5
- package/dist/core/help/ansi.mjs +121 -2
- package/dist/core/help/index.d.mts +22 -3
- package/dist/core/help/index.mjs +341 -7
- package/dist/core/help/theme.d.mts +68 -0
- package/dist/core/help/theme.mjs +46 -0
- package/dist/core/json-schema/index.d.mts +15 -2
- package/dist/core/json-schema/index.mjs +536 -12
- package/dist/core/json-schema/meta-descriptions.generated.d.mts +55 -38
- package/dist/core/json-schema/meta-descriptions.generated.mjs +102 -28
- package/dist/core/output/activity.d.mts +0 -1
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- package/dist/core/output/contracts.d.mts +4 -5
- package/dist/core/output/contracts.mjs +57 -1
- package/dist/core/output/display-value.mjs +32 -1
- package/dist/core/output/index.d.mts +21 -1
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.d.mts +0 -1
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.d.mts +40 -7
- package/dist/core/parse/index.mjs +727 -1
- package/dist/core/prompt/index.d.mts +1 -1
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.d.mts +1 -1
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.d.mts +1 -1
- package/dist/core/resolve/coerce.mjs +336 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/contracts.d.mts +9 -4
- package/dist/core/resolve/errors.d.mts +0 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.d.mts +7 -2
- package/dist/core/resolve/flags.mjs +251 -1
- package/dist/core/resolve/index.d.mts +2 -2
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +4 -2
- package/dist/core/resolve/property.mjs +53 -1
- package/dist/core/schema/activity.d.mts +31 -11
- package/dist/core/schema/arg.d.mts +3 -4
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +26 -6
- package/dist/core/schema/command.mjs +726 -1
- package/dist/core/schema/flag.d.mts +485 -19
- package/dist/core/schema/flag.mjs +705 -1
- package/dist/core/schema/index.d.mts +4 -2
- package/dist/core/schema/middleware.d.mts +4 -3
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema/prompt.d.mts +6 -3
- package/dist/core/schema/run.d.mts +20 -1
- package/dist/core/schema/string-constraints.d.mts +97 -0
- package/dist/core/schema/string-constraints.mjs +69 -0
- package/dist/core/schema/value-parsers.d.mts +78 -0
- package/dist/core/schema/value-parsers.mjs +142 -0
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.d.mts +0 -1
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.d.mts +0 -31
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +11 -6
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.d.mts +38 -2
- package/dist/runtime/adapter.mjs +87 -1
- package/dist/runtime/auto.d.mts +2 -3
- package/dist/runtime/auto.mjs +45 -1
- package/dist/runtime/deno.d.mts +24 -5
- package/dist/runtime/deno.mjs +262 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/index.d.mts +3 -4
- package/dist/runtime/node.d.mts +8 -2
- package/dist/runtime/node.mjs +220 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.d.mts +4 -4
- package/dist/runtime.mjs +6 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.d.mts +1 -0
- package/dist/testkit.mjs +5 -1
- package/dist/version.d.mts +7 -3
- package/dist/version.mjs +7 -1
- package/dreamcli.schema.json +36 -7
- package/package.json +45 -42
- package/dist/runtime/bun.d.mts +0 -28
- package/dist/runtime/bun.mjs +0 -1
- package/src/core/cli/dispatch.ts +0 -355
- package/src/core/cli/help-links.ts +0 -82
- package/src/core/cli/index.ts +0 -1614
- package/src/core/cli/planner.ts +0 -656
- package/src/core/cli/plugin.ts +0 -117
- package/src/core/cli/propagate.ts +0 -97
- package/src/core/cli/root-help.ts +0 -342
- package/src/core/cli/root-surface.ts +0 -87
- package/src/core/cli/runtime-preflight.ts +0 -456
- package/src/core/completion/index.ts +0 -138
- package/src/core/completion/shells/bash.ts +0 -459
- package/src/core/completion/shells/fish.ts +0 -259
- package/src/core/completion/shells/powershell.ts +0 -439
- package/src/core/completion/shells/shared.ts +0 -310
- package/src/core/completion/shells/zsh.ts +0 -381
- package/src/core/config/index.ts +0 -417
- package/src/core/config/package-json.ts +0 -560
- package/src/core/errors/index.ts +0 -193
- package/src/core/execution/index.ts +0 -265
- package/src/core/help/ansi.ts +0 -144
- package/src/core/help/index.ts +0 -563
- package/src/core/json-schema/index.ts +0 -910
- package/src/core/json-schema/meta-descriptions.generated.ts +0 -202
- package/src/core/output/activity.ts +0 -575
- package/src/core/output/bind.ts +0 -59
- package/src/core/output/contracts.ts +0 -194
- package/src/core/output/display-value.ts +0 -48
- package/src/core/output/index.ts +0 -704
- package/src/core/output/renderers.ts +0 -125
- package/src/core/output/writer.ts +0 -46
- package/src/core/parse/index.ts +0 -742
- package/src/core/prompt/index.ts +0 -623
- package/src/core/resolve/args.ts +0 -144
- package/src/core/resolve/coerce.ts +0 -500
- package/src/core/resolve/config.ts +0 -26
- package/src/core/resolve/contracts.ts +0 -135
- package/src/core/resolve/errors.ts +0 -227
- package/src/core/resolve/flags.ts +0 -269
- package/src/core/resolve/index.ts +0 -108
- package/src/core/resolve/property.ts +0 -99
- package/src/core/schema/activity.ts +0 -240
- package/src/core/schema/arg.ts +0 -841
- package/src/core/schema/command.ts +0 -1500
- package/src/core/schema/flag.ts +0 -917
- package/src/core/schema/index.ts +0 -96
- package/src/core/schema/middleware.ts +0 -171
- package/src/core/schema/number-constraints.ts +0 -153
- package/src/core/schema/prompt.ts +0 -171
- package/src/core/schema/run.ts +0 -198
- package/src/core/schema-dsl/index.ts +0 -109
- package/src/core/schema-dsl/parse.ts +0 -262
- package/src/core/schema-dsl/runtime.ts +0 -609
- package/src/core/schema-dsl/to-json-schema.ts +0 -102
- package/src/core/testkit/index.ts +0 -147
- package/src/index.ts +0 -181
- package/src/runtime/adapter.ts +0 -303
- package/src/runtime/auto.ts +0 -86
- package/src/runtime/bun.ts +0 -48
- package/src/runtime/deno.ts +0 -367
- package/src/runtime/detect.ts +0 -105
- package/src/runtime/index.ts +0 -21
- package/src/runtime/node-builtins.d.ts +0 -27
- package/src/runtime/node.ts +0 -289
- package/src/runtime/paths.ts +0 -64
- package/src/runtime/support.ts +0 -77
- package/src/runtime.ts +0 -31
- package/src/schema.ts +0 -16
- package/src/strings.ts +0 -34
- package/src/testkit.ts +0 -25
- package/src/version.ts +0 -20
package/dist/runtime/adapter.mjs
CHANGED
|
@@ -1 +1,87 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/runtime/adapter.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown by adapter implementations that model process exit as an
|
|
4
|
+
* exception instead of terminating immediately.
|
|
5
|
+
*
|
|
6
|
+
* Runtime and CLI dispatch layers can catch this to perform real process
|
|
7
|
+
* termination, while tests can assert on exit codes without killing the
|
|
8
|
+
* runner:
|
|
9
|
+
*
|
|
10
|
+
* ```ts
|
|
11
|
+
* try {
|
|
12
|
+
* await cli.run({ adapter: createTestAdapter() });
|
|
13
|
+
* } catch (e) {
|
|
14
|
+
* if (e instanceof ExitError) expect(e.code).toBe(0);
|
|
15
|
+
* }
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
var ExitError = class extends Error {
|
|
19
|
+
/** Error name — always `'ExitError'` for `instanceof` checks. */
|
|
20
|
+
name = "ExitError";
|
|
21
|
+
/** The exit code passed to `exit()`. */
|
|
22
|
+
code;
|
|
23
|
+
/** Create an ExitError for the given process exit code. */
|
|
24
|
+
constructor(code) {
|
|
25
|
+
super(`Process exited with code ${code}`);
|
|
26
|
+
this.code = code;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
/** Noop writer — silently discards output. */
|
|
30
|
+
const noopWrite = () => {};
|
|
31
|
+
/** Noop reader — returns `null` (EOF) immediately. */
|
|
32
|
+
const eofRead = () => Promise.resolve(null);
|
|
33
|
+
/** Noop file reader — returns `null` (not found) for all paths. */
|
|
34
|
+
const noopReadFile = () => Promise.resolve(null);
|
|
35
|
+
/** Noop stat — reports `null` (nothing exists) for all paths. */
|
|
36
|
+
const noopStat = () => Promise.resolve(null);
|
|
37
|
+
/**
|
|
38
|
+
* Create a test runtime adapter with injectable process state.
|
|
39
|
+
*
|
|
40
|
+
* Use this in `@kjanat/dreamcli/testkit` tests when you need to simulate argv,
|
|
41
|
+
* environment variables, TTY state, stdin, or config-file reads without
|
|
42
|
+
* touching the host process.
|
|
43
|
+
*
|
|
44
|
+
* @param options - Optional overrides for any adapter field.
|
|
45
|
+
* @returns A {@linkcode RuntimeAdapter} suitable for test scenarios.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* const adapter = createTestAdapter({
|
|
50
|
+
* argv: ['node', 'cli.js', 'deploy', '--force'],
|
|
51
|
+
* env: { DEPLOY_REGION: 'us' },
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* const result = await cli('mycli').run({ adapter });
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
function createTestAdapter(options) {
|
|
58
|
+
const stdinIsTTY = options?.stdinIsTTY ?? false;
|
|
59
|
+
let stdinData = !stdinIsTTY ? options?.stdinData : void 0;
|
|
60
|
+
return {
|
|
61
|
+
argv: options?.argv ?? ["node", "test"],
|
|
62
|
+
env: options?.env ?? {},
|
|
63
|
+
cwd: options?.cwd ?? "/test",
|
|
64
|
+
stdout: options?.stdout ?? noopWrite,
|
|
65
|
+
stderr: options?.stderr ?? noopWrite,
|
|
66
|
+
stdin: options?.stdin ?? eofRead,
|
|
67
|
+
readStdin: () => {
|
|
68
|
+
if (stdinData === void 0) return Promise.resolve(null);
|
|
69
|
+
const result = stdinData;
|
|
70
|
+
stdinData = void 0;
|
|
71
|
+
return Promise.resolve(result);
|
|
72
|
+
},
|
|
73
|
+
isTTY: options?.isTTY ?? false,
|
|
74
|
+
stdinIsTTY,
|
|
75
|
+
getTerminalSize: () => options?.terminalSize,
|
|
76
|
+
onTerminalResize: (listener) => options?.onTerminalResize?.(listener),
|
|
77
|
+
exit: options?.exit ?? ((code) => {
|
|
78
|
+
throw new ExitError(code);
|
|
79
|
+
}),
|
|
80
|
+
readFile: options?.readFile ?? noopReadFile,
|
|
81
|
+
stat: options?.stat ?? noopStat,
|
|
82
|
+
homedir: options?.homedir ?? "/home/test",
|
|
83
|
+
configDir: options?.configDir ?? "/home/test/.config"
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
//#endregion
|
|
87
|
+
export { ExitError, createTestAdapter };
|
package/dist/runtime/auto.d.mts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
import { RuntimeAdapter } from "./adapter.mjs";
|
|
2
2
|
import { GlobalForDetect } from "./detect.mjs";
|
|
3
|
-
|
|
4
3
|
//#region src/runtime/auto.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Create a runtime adapter for the current environment.
|
|
7
6
|
*
|
|
8
7
|
* Detection order follows {@link detectRuntime}: Bun → Deno → Node → unknown.
|
|
9
|
-
*
|
|
10
|
-
* expose a Node-compatible `process` global.
|
|
8
|
+
* Only Deno needs a distinct adapter; Bun, Node, and unknown runtimes all use
|
|
9
|
+
* the Node adapter because they expose a Node-compatible `process` global.
|
|
11
10
|
*
|
|
12
11
|
* @param globals - Override `globalThis` for testing. Production callers
|
|
13
12
|
* should omit this parameter.
|
package/dist/runtime/auto.mjs
CHANGED
|
@@ -1 +1,45 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import { createDenoAdapter } from "./deno.mjs";
|
|
2
|
+
import { detectRuntime } from "./detect.mjs";
|
|
3
|
+
import { createNodeAdapter } from "./node.mjs";
|
|
4
|
+
//#region src/runtime/auto.ts
|
|
5
|
+
function isDenoNamespace(value) {
|
|
6
|
+
if (typeof value !== "object" || value === null) return false;
|
|
7
|
+
const candidate = value;
|
|
8
|
+
return Array.isArray(candidate.args) && typeof candidate.cwd === "function" && typeof candidate.exit === "function" && typeof candidate.readTextFile === "function" && typeof candidate.env?.get === "function" && typeof candidate.env.toObject === "function" && typeof candidate.stdout?.writeSync === "function" && typeof candidate.stdout.isTerminal === "function" && typeof candidate.stderr?.writeSync === "function" && typeof candidate.stdin?.isTerminal === "function" && typeof candidate.stdin?.readable?.getReader === "function";
|
|
9
|
+
}
|
|
10
|
+
function getInjectedDenoNamespace(globals) {
|
|
11
|
+
const candidate = globals.Deno;
|
|
12
|
+
return isDenoNamespace(candidate) ? candidate : void 0;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Create a runtime adapter for the current environment.
|
|
16
|
+
*
|
|
17
|
+
* Detection order follows {@link detectRuntime}: Bun → Deno → Node → unknown.
|
|
18
|
+
* Only Deno needs a distinct adapter; Bun, Node, and unknown runtimes all use
|
|
19
|
+
* the Node adapter because they expose a Node-compatible `process` global.
|
|
20
|
+
*
|
|
21
|
+
* @param globals - Override `globalThis` for testing. Production callers
|
|
22
|
+
* should omit this parameter.
|
|
23
|
+
* @returns A {@linkcode RuntimeAdapter} for the detected runtime.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```ts
|
|
27
|
+
* import { cli } from '@kjanat/dreamcli';
|
|
28
|
+
*
|
|
29
|
+
* // Auto-detects Node/Bun/Deno and creates the right adapter
|
|
30
|
+
* cli('mycli').run(); // uses createAdapter() internally
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
function createAdapter(globals) {
|
|
34
|
+
const runtimeGlobals = globals ?? globalThis;
|
|
35
|
+
const runtime = detectRuntime(runtimeGlobals);
|
|
36
|
+
switch (runtime) {
|
|
37
|
+
case "deno": return createDenoAdapter(getInjectedDenoNamespace(runtimeGlobals));
|
|
38
|
+
case "bun":
|
|
39
|
+
case "node":
|
|
40
|
+
case "unknown": return createNodeAdapter();
|
|
41
|
+
default: return runtime;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { createAdapter };
|
package/dist/runtime/deno.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { RuntimeAdapter } from "./adapter.mjs";
|
|
2
|
-
|
|
3
2
|
//#region src/runtime/deno.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Minimal subset of the Deno namespace needed by the adapter.
|
|
@@ -24,29 +23,49 @@ interface DenoNamespace {
|
|
|
24
23
|
readonly args: readonly string[];
|
|
25
24
|
/** Environment variable access (requires `--allow-env`). */
|
|
26
25
|
readonly env: {
|
|
27
|
-
/** Get a single env var. Returns `undefined` if unset or permission denied. */
|
|
26
|
+
/** Get a single env var. Returns `undefined` if unset or permission denied. */
|
|
27
|
+
get(key: string): string | undefined;
|
|
28
|
+
/** Get all env vars as a plain object. Throws on permission denied. */
|
|
28
29
|
toObject(): Record<string, string>;
|
|
29
30
|
};
|
|
30
31
|
/** Current working directory (may throw if `--allow-read` is denied for cwd). */
|
|
31
32
|
cwd(): string;
|
|
32
33
|
/** Standard output — synchronous byte writer with TTY detection. */
|
|
33
34
|
readonly stdout: {
|
|
34
|
-
/** Write raw bytes to stdout synchronously. */
|
|
35
|
+
/** Write raw bytes to stdout synchronously. */
|
|
36
|
+
writeSync(p: Uint8Array): number;
|
|
37
|
+
/** Whether stdout is connected to a TTY. */
|
|
35
38
|
isTerminal(): boolean;
|
|
36
39
|
};
|
|
40
|
+
/** Current console dimensions. */
|
|
41
|
+
consoleSize?(): {
|
|
42
|
+
readonly columns: number;
|
|
43
|
+
readonly rows: number;
|
|
44
|
+
};
|
|
45
|
+
/** Register a native signal listener. */
|
|
46
|
+
addSignalListener?(signal: 'SIGWINCH', listener: () => void): void;
|
|
47
|
+
/** Remove a native signal listener. */
|
|
48
|
+
removeSignalListener?(signal: 'SIGWINCH', listener: () => void): void;
|
|
37
49
|
/** Standard error — synchronous byte writer. */
|
|
38
50
|
readonly stderr: {
|
|
39
|
-
/** Write raw bytes to stderr synchronously. */
|
|
51
|
+
/** Write raw bytes to stderr synchronously. */
|
|
52
|
+
writeSync(p: Uint8Array): number;
|
|
40
53
|
};
|
|
41
54
|
/** Standard input — TTY detection and readable byte stream. */
|
|
42
55
|
readonly stdin: {
|
|
43
|
-
/** Whether stdin is connected to a TTY. */
|
|
56
|
+
/** Whether stdin is connected to a TTY. */
|
|
57
|
+
isTerminal(): boolean;
|
|
58
|
+
/** Readable stream for stdin bytes. */
|
|
44
59
|
readonly readable: ReadableStream<Uint8Array>;
|
|
45
60
|
};
|
|
46
61
|
/** Exit the process with the given code. */
|
|
47
62
|
exit(code: number): never;
|
|
48
63
|
/** Read a file as UTF-8 text (requires `--allow-read`). */
|
|
49
64
|
readTextFile(path: string): Promise<string>;
|
|
65
|
+
/** Probe a filesystem path (requires `--allow-read`). */
|
|
66
|
+
stat(path: string): Promise<{
|
|
67
|
+
readonly isDirectory: boolean;
|
|
68
|
+
}>;
|
|
50
69
|
}
|
|
51
70
|
/**
|
|
52
71
|
* Create a runtime adapter backed by the Deno namespace.
|
package/dist/runtime/deno.mjs
CHANGED
|
@@ -1,2 +1,262 @@
|
|
|
1
|
-
import{resolveConfigDirectory
|
|
2
|
-
|
|
1
|
+
import { resolveConfigDirectory, resolveHomeDirectory } from "./paths.mjs";
|
|
2
|
+
//#region src/runtime/deno.ts
|
|
3
|
+
/**
|
|
4
|
+
* Detect whether an unknown thrown value is a Deno error by name.
|
|
5
|
+
*
|
|
6
|
+
* @internal
|
|
7
|
+
*/
|
|
8
|
+
function isDenoErrorNamed(err, name) {
|
|
9
|
+
if (typeof err !== "object" || err === null || !("name" in err)) return false;
|
|
10
|
+
const candidate = err;
|
|
11
|
+
return typeof candidate.name === "string" && candidate.name === name;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Safely read all environment variables. Returns empty object if
|
|
15
|
+
* `--allow-env` permission is not granted.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
function safeEnvToObject(deno) {
|
|
20
|
+
try {
|
|
21
|
+
return deno.env.toObject();
|
|
22
|
+
} catch (err) {
|
|
23
|
+
if (isDenoErrorNamed(err, "PermissionDenied")) return {};
|
|
24
|
+
throw err;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Safely get the current working directory. Returns `/` if the cwd is
|
|
29
|
+
* inaccessible due to missing permissions.
|
|
30
|
+
*
|
|
31
|
+
* @internal
|
|
32
|
+
*/
|
|
33
|
+
function safeCwd(deno) {
|
|
34
|
+
try {
|
|
35
|
+
return deno.cwd();
|
|
36
|
+
} catch (err) {
|
|
37
|
+
if (isDenoErrorNamed(err, "PermissionDenied")) return "/";
|
|
38
|
+
throw err;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a runtime adapter backed by the Deno namespace.
|
|
43
|
+
*
|
|
44
|
+
* Reads `Deno.args`, `Deno.env`, `Deno.cwd()`, and wraps Deno's stream-based
|
|
45
|
+
* I/O into the {@linkcode WriteFn}/{@linkcode ReadFn} functions expected by the framework.
|
|
46
|
+
*
|
|
47
|
+
* Unlike Node/Bun, Deno strips the binary and script path from `Deno.args`.
|
|
48
|
+
* The adapter prepends synthetic entries (`['deno', 'run']`) so the argv
|
|
49
|
+
* shape matches the {@linkcode RuntimeAdapter} contract (binary + script + user args).
|
|
50
|
+
*
|
|
51
|
+
* @param ns - Override the Deno namespace (useful for testing the adapter itself).
|
|
52
|
+
* @returns A {@linkcode RuntimeAdapter} backed by Deno's namespace APIs.
|
|
53
|
+
*
|
|
54
|
+
* @example
|
|
55
|
+
* ```ts
|
|
56
|
+
* import { cli } from '@kjanat/dreamcli';
|
|
57
|
+
* import { createDenoAdapter } from '@kjanat/dreamcli/runtime';
|
|
58
|
+
*
|
|
59
|
+
* cli('mycli')
|
|
60
|
+
* .command(deploy)
|
|
61
|
+
* .run({ adapter: createDenoAdapter() });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function createDenoAdapter(ns) {
|
|
65
|
+
const d = ns ?? getDenoNamespace();
|
|
66
|
+
const encoder = new TextEncoder();
|
|
67
|
+
const argv = [
|
|
68
|
+
"deno",
|
|
69
|
+
"run",
|
|
70
|
+
...d.args
|
|
71
|
+
];
|
|
72
|
+
const env = safeEnvToObject(d);
|
|
73
|
+
const cwd = safeCwd(d);
|
|
74
|
+
const stdoutWrite = (data) => {
|
|
75
|
+
d.stdout.writeSync(encoder.encode(data));
|
|
76
|
+
};
|
|
77
|
+
const stderrWrite = (data) => {
|
|
78
|
+
d.stderr.writeSync(encoder.encode(data));
|
|
79
|
+
};
|
|
80
|
+
const stdinRead = createDenoReadLine(d);
|
|
81
|
+
const readFile = async (path) => {
|
|
82
|
+
try {
|
|
83
|
+
return await d.readTextFile(path);
|
|
84
|
+
} catch (err) {
|
|
85
|
+
if (isDenoErrorNamed(err, "NotFound")) return null;
|
|
86
|
+
if (isDenoErrorNamed(err, "PermissionDenied")) return null;
|
|
87
|
+
throw err;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
const stat = async (path) => {
|
|
91
|
+
try {
|
|
92
|
+
return (await d.stat(path)).isDirectory ? "directory" : "file";
|
|
93
|
+
} catch (err) {
|
|
94
|
+
if (isDenoErrorNamed(err, "NotFound")) return null;
|
|
95
|
+
throw err;
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
const homedir = resolveDenoHomedir(env, d.build.os === "windows");
|
|
99
|
+
const configDir = resolveDenoConfigDir(env, homedir, d.build.os === "windows");
|
|
100
|
+
const stdinIsTTY = d.stdin.isTerminal();
|
|
101
|
+
return {
|
|
102
|
+
argv,
|
|
103
|
+
env,
|
|
104
|
+
cwd,
|
|
105
|
+
stdout: stdoutWrite,
|
|
106
|
+
stderr: stderrWrite,
|
|
107
|
+
stdin: stdinRead,
|
|
108
|
+
readStdin: () => readDenoStdinAll(d, stdinIsTTY),
|
|
109
|
+
isTTY: d.stdout.isTerminal(),
|
|
110
|
+
stdinIsTTY,
|
|
111
|
+
getTerminalSize: () => readDenoTerminalSize(d),
|
|
112
|
+
onTerminalResize: (listener) => onDenoTerminalResize(d, listener),
|
|
113
|
+
exit: (code) => d.exit(code),
|
|
114
|
+
readFile,
|
|
115
|
+
stat,
|
|
116
|
+
homedir,
|
|
117
|
+
configDir
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
/** Normalize terminal dimensions and discard unusable values. @internal */
|
|
121
|
+
function normalizeTerminalSize(columns, rows) {
|
|
122
|
+
if (columns === void 0 || rows === void 0) return void 0;
|
|
123
|
+
if (!Number.isFinite(columns) || !Number.isFinite(rows)) return void 0;
|
|
124
|
+
if (columns <= 0 || rows <= 0) return void 0;
|
|
125
|
+
return {
|
|
126
|
+
columns,
|
|
127
|
+
rows
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
/** Read native Deno terminal dimensions. @internal */
|
|
131
|
+
function readDenoTerminalSize(deno) {
|
|
132
|
+
if (!deno.stdout.isTerminal() || deno.consoleSize === void 0) return void 0;
|
|
133
|
+
try {
|
|
134
|
+
const size = deno.consoleSize();
|
|
135
|
+
return normalizeTerminalSize(size.columns, size.rows);
|
|
136
|
+
} catch {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
/** Subscribe to native Deno resize signals where SIGWINCH exists. @internal */
|
|
141
|
+
function onDenoTerminalResize(deno, listener) {
|
|
142
|
+
if (!deno.stdout.isTerminal()) return void 0;
|
|
143
|
+
if (deno.build.os === "windows") return void 0;
|
|
144
|
+
if (deno.addSignalListener === void 0 || deno.removeSignalListener === void 0) return;
|
|
145
|
+
try {
|
|
146
|
+
deno.addSignalListener("SIGWINCH", listener);
|
|
147
|
+
} catch {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
return () => {
|
|
151
|
+
deno.removeSignalListener?.("SIGWINCH", listener);
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Access the global `Deno` namespace.
|
|
156
|
+
*
|
|
157
|
+
* Cast through `unknown` to avoid TypeScript errors when Deno types
|
|
158
|
+
* aren't installed. Safe because this adapter is only used on Deno
|
|
159
|
+
* where `globalThis.Deno` is always defined.
|
|
160
|
+
*
|
|
161
|
+
* @internal
|
|
162
|
+
*/
|
|
163
|
+
function getDenoNamespace() {
|
|
164
|
+
return globalThis.Deno;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Read all of stdin as a single string (for piped data).
|
|
168
|
+
*
|
|
169
|
+
* Returns `null` immediately if stdin is a TTY — the user is typing
|
|
170
|
+
* interactively, so there's no piped data to consume. When stdin is
|
|
171
|
+
* piped, collects all chunks via the `ReadableStream` until EOF and
|
|
172
|
+
* returns the decoded string, which may be empty for an empty pipe.
|
|
173
|
+
*
|
|
174
|
+
* @internal
|
|
175
|
+
*/
|
|
176
|
+
async function readDenoStdinAll(deno, stdinIsTTY) {
|
|
177
|
+
if (stdinIsTTY) return null;
|
|
178
|
+
const reader = deno.stdin.readable.getReader();
|
|
179
|
+
const decoder = new TextDecoder();
|
|
180
|
+
const chunks = [];
|
|
181
|
+
try {
|
|
182
|
+
for (;;) {
|
|
183
|
+
const { value, done } = await reader.read();
|
|
184
|
+
if (done) {
|
|
185
|
+
chunks.push(decoder.decode());
|
|
186
|
+
break;
|
|
187
|
+
}
|
|
188
|
+
chunks.push(decoder.decode(value, { stream: true }));
|
|
189
|
+
}
|
|
190
|
+
} finally {
|
|
191
|
+
reader.releaseLock();
|
|
192
|
+
}
|
|
193
|
+
return chunks.join("");
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Read a single line from Deno's stdin.
|
|
197
|
+
*
|
|
198
|
+
* Uses the `ReadableStream` API with a manual buffer to read until
|
|
199
|
+
* a newline character is found. Returns `null` on EOF.
|
|
200
|
+
*
|
|
201
|
+
* This is intentionally simple — the terminal prompter handles retries
|
|
202
|
+
* and validation.
|
|
203
|
+
*
|
|
204
|
+
* @internal
|
|
205
|
+
*/
|
|
206
|
+
function createDenoReadLine(deno) {
|
|
207
|
+
const decoder = new TextDecoder();
|
|
208
|
+
let buffer = "";
|
|
209
|
+
let done = false;
|
|
210
|
+
return async () => {
|
|
211
|
+
const reader = deno.stdin.readable.getReader();
|
|
212
|
+
try {
|
|
213
|
+
for (;;) {
|
|
214
|
+
const newlineIndex = buffer.indexOf("\n");
|
|
215
|
+
if (newlineIndex !== -1) {
|
|
216
|
+
const line = buffer.slice(0, newlineIndex).replace(/\r$/, "");
|
|
217
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
218
|
+
return line;
|
|
219
|
+
}
|
|
220
|
+
if (done) {
|
|
221
|
+
if (buffer.length === 0) return null;
|
|
222
|
+
const line = buffer;
|
|
223
|
+
buffer = "";
|
|
224
|
+
return line;
|
|
225
|
+
}
|
|
226
|
+
const { value, done: readDone } = await reader.read();
|
|
227
|
+
if (readDone) {
|
|
228
|
+
buffer += decoder.decode();
|
|
229
|
+
done = true;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
buffer += decoder.decode(value, { stream: true });
|
|
233
|
+
}
|
|
234
|
+
} finally {
|
|
235
|
+
reader.releaseLock();
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Resolve the user's home directory from environment variables.
|
|
241
|
+
*
|
|
242
|
+
* Deno doesn't expose `os.homedir()` without `--allow-sys`, so we derive
|
|
243
|
+
* it from env vars — same approach as the Node adapter.
|
|
244
|
+
*
|
|
245
|
+
* @internal
|
|
246
|
+
*/
|
|
247
|
+
function resolveDenoHomedir(env, isWindows) {
|
|
248
|
+
return resolveHomeDirectory(env, isWindows);
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Resolve the platform-specific user configuration directory.
|
|
252
|
+
*
|
|
253
|
+
* Uses `Deno.build.os` for platform detection, then applies the same fallback
|
|
254
|
+
* chain as the Node adapter.
|
|
255
|
+
*
|
|
256
|
+
* @internal
|
|
257
|
+
*/
|
|
258
|
+
function resolveDenoConfigDir(env, homedir, isWindows) {
|
|
259
|
+
return resolveConfigDirectory(env, isWindows, homedir);
|
|
260
|
+
}
|
|
261
|
+
//#endregion
|
|
262
|
+
export { createDenoAdapter };
|
package/dist/runtime/detect.mjs
CHANGED
|
@@ -1 +1,45 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/runtime/detect.ts
|
|
2
|
+
/**
|
|
3
|
+
* All known runtime values as a readonly tuple.
|
|
4
|
+
*
|
|
5
|
+
* Useful for validation, iteration, and exhaustiveness checks.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* if (RUNTIMES.includes(value)) {
|
|
10
|
+
* // value is a valid Runtime
|
|
11
|
+
* }
|
|
12
|
+
* ```
|
|
13
|
+
*/
|
|
14
|
+
const RUNTIMES = [
|
|
15
|
+
"node",
|
|
16
|
+
"bun",
|
|
17
|
+
"deno",
|
|
18
|
+
"unknown"
|
|
19
|
+
];
|
|
20
|
+
/**
|
|
21
|
+
* Detect the current JavaScript runtime.
|
|
22
|
+
*
|
|
23
|
+
* Uses `globalThis` feature detection to identify the host runtime.
|
|
24
|
+
* Detection order is significant: Bun is checked before Node because
|
|
25
|
+
* Bun exposes a Node-compatible `process` global.
|
|
26
|
+
*
|
|
27
|
+
* @param globals - Override `globalThis` for testing. Production callers
|
|
28
|
+
* should omit this parameter.
|
|
29
|
+
* @returns The detected {@link Runtime} identifier.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const rt = detectRuntime();
|
|
34
|
+
* // rt === 'node' | 'bun' | 'deno' | 'unknown'
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
function detectRuntime(globals) {
|
|
38
|
+
const g = globals ?? globalThis;
|
|
39
|
+
if (typeof g.Bun?.version === "string") return "bun";
|
|
40
|
+
if (typeof g.Deno?.version?.deno === "string") return "deno";
|
|
41
|
+
if (typeof g.process?.versions?.node === "string") return "node";
|
|
42
|
+
return "unknown";
|
|
43
|
+
}
|
|
44
|
+
//#endregion
|
|
45
|
+
export { RUNTIMES, detectRuntime };
|
package/dist/runtime/index.d.mts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { ExitError, RuntimeAdapter, TestAdapterOptions, createTestAdapter } from "./adapter.mjs";
|
|
1
|
+
import { ExitError, RuntimeAdapter, TerminalSize, TestAdapterOptions, createTestAdapter } from "./adapter.mjs";
|
|
2
2
|
import { GlobalForDetect, RUNTIMES, Runtime, detectRuntime } from "./detect.mjs";
|
|
3
3
|
import { createAdapter } from "./auto.mjs";
|
|
4
|
-
import { NodeProcess, createNodeAdapter } from "./node.mjs";
|
|
5
|
-
import { createBunAdapter } from "./bun.mjs";
|
|
6
4
|
import { DenoNamespace, createDenoAdapter } from "./deno.mjs";
|
|
7
|
-
|
|
5
|
+
import { NodeProcess, createNodeAdapter } from "./node.mjs";
|
|
6
|
+
export { type DenoNamespace, ExitError, type GlobalForDetect, type NodeProcess, RUNTIMES, type Runtime, type RuntimeAdapter, type TerminalSize, type TestAdapterOptions, createAdapter, createDenoAdapter, createNodeAdapter, createTestAdapter, detectRuntime };
|
package/dist/runtime/node.d.mts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { RuntimeAdapter } from "./adapter.mjs";
|
|
2
|
-
|
|
3
2
|
//#region src/runtime/node.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Minimal subset of the Node.js `process` object needed by the adapter.
|
|
@@ -24,12 +23,19 @@ interface NodeProcess {
|
|
|
24
23
|
readonly platform: string;
|
|
25
24
|
/** Standard input stream with TTY detection and async iteration. */
|
|
26
25
|
readonly stdin: {
|
|
27
|
-
readonly isTTY?: boolean;
|
|
26
|
+
readonly isTTY?: boolean;
|
|
27
|
+
/** Async iterable for reading all of stdin (used by readStdin). */
|
|
28
28
|
[Symbol.asyncIterator](): AsyncIterator<Uint8Array>;
|
|
29
29
|
};
|
|
30
30
|
/** Standard output stream with TTY detection and write. */
|
|
31
31
|
readonly stdout: {
|
|
32
32
|
readonly isTTY?: boolean;
|
|
33
|
+
readonly columns?: number;
|
|
34
|
+
readonly rows?: number;
|
|
35
|
+
getWindowSize?(): readonly [number, number];
|
|
36
|
+
on?(event: 'resize', listener: () => void): unknown;
|
|
37
|
+
off?(event: 'resize', listener: () => void): unknown;
|
|
38
|
+
removeListener?(event: 'resize', listener: () => void): unknown;
|
|
33
39
|
write(data: string): unknown;
|
|
34
40
|
};
|
|
35
41
|
/** Standard error stream with write. */
|