@kjanat/dreamcli 3.0.0-rc.1 → 3.0.0-rc.2
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 +26 -1
- package/dist/core/cli/dispatch.mjs +214 -1
- package/dist/core/cli/help-links.mjs +41 -1
- package/dist/core/cli/index.mjs +901 -1
- package/dist/core/cli/planner.mjs +300 -1
- package/dist/core/cli/plugin.mjs +41 -1
- package/dist/core/cli/propagate.mjs +57 -1
- package/dist/core/cli/root-help.mjs +208 -4
- package/dist/core/cli/root-surface.mjs +27 -1
- package/dist/core/cli/runtime-preflight.mjs +168 -1
- package/dist/core/completion/index.mjs +90 -1
- package/dist/core/completion/shells/bash.mjs +304 -3
- package/dist/core/completion/shells/fish.mjs +159 -2
- package/dist/core/completion/shells/powershell.mjs +353 -2
- package/dist/core/completion/shells/shared.mjs +154 -1
- package/dist/core/completion/shells/zsh.mjs +244 -2
- package/dist/core/config/index.mjs +246 -1
- package/dist/core/config/package-json.mjs +385 -3
- package/dist/core/errors/index.mjs +87 -1
- package/dist/core/execution/index.mjs +189 -1
- package/dist/core/help/ansi.mjs +122 -2
- package/dist/core/help/index.mjs +329 -7
- package/dist/core/json-schema/index.mjs +487 -9
- package/dist/core/json-schema/meta-descriptions.generated.mjs +93 -28
- package/dist/core/output/activity.mjs +452 -1
- package/dist/core/output/bind.mjs +56 -1
- 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 -0
- package/dist/core/output/index.mjs +442 -2
- package/dist/core/output/renderers.mjs +46 -1
- package/dist/core/output/writer.mjs +12 -1
- package/dist/core/parse/index.mjs +555 -1
- package/dist/core/prompt/index.d.mts +0 -1
- package/dist/core/prompt/index.mjs +348 -6
- package/dist/core/resolve/args.mjs +105 -1
- package/dist/core/resolve/coerce.mjs +279 -1
- package/dist/core/resolve/config.mjs +19 -1
- package/dist/core/resolve/errors.mjs +140 -2
- package/dist/core/resolve/flags.mjs +196 -1
- package/dist/core/resolve/index.mjs +85 -1
- package/dist/core/resolve/property.d.mts +0 -1
- package/dist/core/resolve/property.mjs +45 -1
- package/dist/core/schema/arg.mjs +463 -1
- package/dist/core/schema/command.d.mts +18 -0
- package/dist/core/schema/command.mjs +712 -1
- package/dist/core/schema/flag.mjs +453 -1
- package/dist/core/schema/middleware.mjs +41 -1
- package/dist/core/schema/number-constraints.mjs +60 -1
- package/dist/core/schema-dsl/runtime.mjs +271 -2
- package/dist/core/schema-dsl/to-json-schema.mjs +74 -1
- package/dist/core/testkit/index.mjs +83 -1
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +22 -1
- package/dist/runtime/adapter.mjs +82 -1
- package/dist/runtime/auto.mjs +46 -1
- package/dist/runtime/bun.mjs +28 -1
- package/dist/runtime/deno.mjs +217 -2
- package/dist/runtime/detect.mjs +45 -1
- package/dist/runtime/node.mjs +169 -2
- package/dist/runtime/paths.mjs +48 -1
- package/dist/runtime.mjs +7 -1
- package/dist/strings.mjs +22 -1
- package/dist/testkit.mjs +5 -1
- package/dist/version.mjs +7 -1
- package/package.json +17 -26
- 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/deno.mjs
CHANGED
|
@@ -1,2 +1,217 @@
|
|
|
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 homedir = resolveDenoHomedir(env, d.build.os === "windows");
|
|
91
|
+
const configDir = resolveDenoConfigDir(env, homedir, d.build.os === "windows");
|
|
92
|
+
const stdinIsTTY = d.stdin.isTerminal();
|
|
93
|
+
return {
|
|
94
|
+
argv,
|
|
95
|
+
env,
|
|
96
|
+
cwd,
|
|
97
|
+
stdout: stdoutWrite,
|
|
98
|
+
stderr: stderrWrite,
|
|
99
|
+
stdin: stdinRead,
|
|
100
|
+
readStdin: () => readDenoStdinAll(d, stdinIsTTY),
|
|
101
|
+
isTTY: d.stdout.isTerminal(),
|
|
102
|
+
stdinIsTTY,
|
|
103
|
+
exit: (code) => d.exit(code),
|
|
104
|
+
readFile,
|
|
105
|
+
homedir,
|
|
106
|
+
configDir
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Access the global `Deno` namespace.
|
|
111
|
+
*
|
|
112
|
+
* Cast through `unknown` to avoid TypeScript errors when Deno types
|
|
113
|
+
* aren't installed. Safe because this adapter is only used on Deno
|
|
114
|
+
* where `globalThis.Deno` is always defined.
|
|
115
|
+
*
|
|
116
|
+
* @internal
|
|
117
|
+
*/
|
|
118
|
+
function getDenoNamespace() {
|
|
119
|
+
return globalThis.Deno;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Read all of stdin as a single string (for piped data).
|
|
123
|
+
*
|
|
124
|
+
* Returns `null` immediately if stdin is a TTY — the user is typing
|
|
125
|
+
* interactively, so there's no piped data to consume. When stdin is
|
|
126
|
+
* piped, collects all chunks via the `ReadableStream` until EOF and
|
|
127
|
+
* returns the decoded string, which may be empty for an empty pipe.
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
async function readDenoStdinAll(deno, stdinIsTTY) {
|
|
132
|
+
if (stdinIsTTY) return null;
|
|
133
|
+
const reader = deno.stdin.readable.getReader();
|
|
134
|
+
const decoder = new TextDecoder();
|
|
135
|
+
const chunks = [];
|
|
136
|
+
try {
|
|
137
|
+
for (;;) {
|
|
138
|
+
const { value, done } = await reader.read();
|
|
139
|
+
if (done) {
|
|
140
|
+
chunks.push(decoder.decode());
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
chunks.push(decoder.decode(value, { stream: true }));
|
|
144
|
+
}
|
|
145
|
+
} finally {
|
|
146
|
+
reader.releaseLock();
|
|
147
|
+
}
|
|
148
|
+
return chunks.join("");
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Read a single line from Deno's stdin.
|
|
152
|
+
*
|
|
153
|
+
* Uses the `ReadableStream` API with a manual buffer to read until
|
|
154
|
+
* a newline character is found. Returns `null` on EOF.
|
|
155
|
+
*
|
|
156
|
+
* This is intentionally simple — the terminal prompter handles retries
|
|
157
|
+
* and validation.
|
|
158
|
+
*
|
|
159
|
+
* @internal
|
|
160
|
+
*/
|
|
161
|
+
function createDenoReadLine(deno) {
|
|
162
|
+
const decoder = new TextDecoder();
|
|
163
|
+
let buffer = "";
|
|
164
|
+
let done = false;
|
|
165
|
+
return async () => {
|
|
166
|
+
const reader = deno.stdin.readable.getReader();
|
|
167
|
+
try {
|
|
168
|
+
for (;;) {
|
|
169
|
+
const newlineIndex = buffer.indexOf("\n");
|
|
170
|
+
if (newlineIndex !== -1) {
|
|
171
|
+
const line = buffer.slice(0, newlineIndex).replace(/\r$/, "");
|
|
172
|
+
buffer = buffer.slice(newlineIndex + 1);
|
|
173
|
+
return line;
|
|
174
|
+
}
|
|
175
|
+
if (done) {
|
|
176
|
+
if (buffer.length === 0) return null;
|
|
177
|
+
const line = buffer;
|
|
178
|
+
buffer = "";
|
|
179
|
+
return line;
|
|
180
|
+
}
|
|
181
|
+
const { value, done: readDone } = await reader.read();
|
|
182
|
+
if (readDone) {
|
|
183
|
+
buffer += decoder.decode();
|
|
184
|
+
done = true;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
buffer += decoder.decode(value, { stream: true });
|
|
188
|
+
}
|
|
189
|
+
} finally {
|
|
190
|
+
reader.releaseLock();
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Resolve the user's home directory from environment variables.
|
|
196
|
+
*
|
|
197
|
+
* Deno doesn't expose `os.homedir()` without `--allow-sys`, so we derive
|
|
198
|
+
* it from env vars — same approach as the Node adapter.
|
|
199
|
+
*
|
|
200
|
+
* @internal
|
|
201
|
+
*/
|
|
202
|
+
function resolveDenoHomedir(env, isWindows) {
|
|
203
|
+
return resolveHomeDirectory(env, isWindows);
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Resolve the platform-specific user configuration directory.
|
|
207
|
+
*
|
|
208
|
+
* Uses `Deno.build.os` for platform detection, then applies the same fallback
|
|
209
|
+
* chain as the Node adapter.
|
|
210
|
+
*
|
|
211
|
+
* @internal
|
|
212
|
+
*/
|
|
213
|
+
function resolveDenoConfigDir(env, homedir, isWindows) {
|
|
214
|
+
return resolveConfigDirectory(env, isWindows, homedir);
|
|
215
|
+
}
|
|
216
|
+
//#endregion
|
|
217
|
+
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/node.mjs
CHANGED
|
@@ -1,2 +1,169 @@
|
|
|
1
|
-
import{resolveConfigDirectory
|
|
2
|
-
|
|
1
|
+
import { resolveConfigDirectory, resolveHomeDirectory } from "./paths.mjs";
|
|
2
|
+
//#region src/runtime/node.ts
|
|
3
|
+
/**
|
|
4
|
+
* Access the global `process` object without importing `@types/node`.
|
|
5
|
+
*
|
|
6
|
+
* Cast through `unknown` to avoid TypeScript errors when `@types/node`
|
|
7
|
+
* isn't installed. Safe because this adapter is only used on Node/Bun
|
|
8
|
+
* where `globalThis.process` is always defined.
|
|
9
|
+
*
|
|
10
|
+
* @internal
|
|
11
|
+
*/
|
|
12
|
+
function getNodeProcess() {
|
|
13
|
+
return globalThis.process;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Detect whether an unknown thrown value is a Node.js system error
|
|
17
|
+
* with a string `code` property (e.g. `'ENOENT'`, `'EACCES'`).
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
function isNodeSystemError(err) {
|
|
22
|
+
if (typeof err !== "object" || err === null || !("code" in err)) return false;
|
|
23
|
+
return typeof err.code === "string";
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Resolve the user's home directory from environment variables.
|
|
27
|
+
*
|
|
28
|
+
* **Windows** (`win32`): `USERPROFILE` → `HOMEDRIVE`+`HOMEPATH` (both required) → `HOME` → `C:\`
|
|
29
|
+
* **Unix**: `HOME` → `/`
|
|
30
|
+
*
|
|
31
|
+
* `HOMEPATH` alone is never used — it is a relative fragment (e.g. `\Users\alice`) that only
|
|
32
|
+
* makes sense when combined with `HOMEDRIVE` (e.g. `C:`). Using `HOMEPATH` without `HOMEDRIVE`
|
|
33
|
+
* could resolve against the working drive, producing an incorrect path.
|
|
34
|
+
*
|
|
35
|
+
* We avoid importing `node:os` to keep the factory synchronous and
|
|
36
|
+
* to maintain the pattern of deriving everything from the process object.
|
|
37
|
+
*
|
|
38
|
+
* @internal
|
|
39
|
+
*/
|
|
40
|
+
function resolveHomedir(env, platform) {
|
|
41
|
+
return resolveHomeDirectory(env, platform === "win32");
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Resolve the platform-specific user configuration directory.
|
|
45
|
+
*
|
|
46
|
+
* - Unix: `$XDG_CONFIG_HOME` (if set), otherwise `~/.config`
|
|
47
|
+
* - Windows: `%APPDATA%` (if set), otherwise `~\AppData\Roaming`
|
|
48
|
+
*
|
|
49
|
+
* @internal
|
|
50
|
+
*/
|
|
51
|
+
function resolveConfigDir(env, platform, homedir) {
|
|
52
|
+
return resolveConfigDirectory(env, platform === "win32", homedir);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Create a runtime adapter backed by Node.js `process` globals.
|
|
56
|
+
*
|
|
57
|
+
* Reads `process.argv`, `process.env`, `process.cwd()`, and wraps
|
|
58
|
+
* `process.stdout.write`/`process.stderr.write` as {@linkcode WriteFn} functions.
|
|
59
|
+
*
|
|
60
|
+
* Also works on Bun, which provides a Node-compatible `process` global.
|
|
61
|
+
*
|
|
62
|
+
* @param proc - Override the process object (useful for testing the adapter itself).
|
|
63
|
+
* @returns A {@linkcode RuntimeAdapter} backed by Node.js process state.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* ```ts
|
|
67
|
+
* import { cli } from '@kjanat/dreamcli';
|
|
68
|
+
* import { createNodeAdapter } from '@kjanat/dreamcli/runtime/node';
|
|
69
|
+
*
|
|
70
|
+
* cli('mycli')
|
|
71
|
+
* .command(deploy)
|
|
72
|
+
* .run({ adapter: createNodeAdapter() });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
function createNodeAdapter(proc) {
|
|
76
|
+
const p = proc ?? getNodeProcess();
|
|
77
|
+
const stdoutWrite = (data) => {
|
|
78
|
+
p.stdout.write(data);
|
|
79
|
+
};
|
|
80
|
+
const stderrWrite = (data) => {
|
|
81
|
+
p.stderr.write(data);
|
|
82
|
+
};
|
|
83
|
+
const stdinRead = createNodeReadLine(p);
|
|
84
|
+
const readFile = async (path) => {
|
|
85
|
+
const fs = await import("node:fs/promises");
|
|
86
|
+
try {
|
|
87
|
+
return await fs.readFile(path, "utf8");
|
|
88
|
+
} catch (err) {
|
|
89
|
+
if (isNodeSystemError(err) && err.code === "ENOENT") return null;
|
|
90
|
+
throw err;
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
const homedir = resolveHomedir(p.env, p.platform);
|
|
94
|
+
const configDir = resolveConfigDir(p.env, p.platform, homedir);
|
|
95
|
+
const stdinIsTTY = p.stdin.isTTY === true;
|
|
96
|
+
return {
|
|
97
|
+
argv: p.argv,
|
|
98
|
+
env: p.env,
|
|
99
|
+
cwd: p.cwd(),
|
|
100
|
+
stdout: stdoutWrite,
|
|
101
|
+
stderr: stderrWrite,
|
|
102
|
+
stdin: stdinRead,
|
|
103
|
+
readStdin: () => readNodeStdinAll(p, stdinIsTTY),
|
|
104
|
+
isTTY: p.stdout.isTTY === true,
|
|
105
|
+
stdinIsTTY,
|
|
106
|
+
exit: (code) => p.exit(code),
|
|
107
|
+
readFile,
|
|
108
|
+
homedir,
|
|
109
|
+
configDir
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Read all of stdin as a single string (for piped data).
|
|
114
|
+
*
|
|
115
|
+
* Returns `null` immediately if stdin is a TTY — the user is typing
|
|
116
|
+
* interactively, so there's no piped data to consume. When stdin is
|
|
117
|
+
* piped, collects all chunks via the async iterator until EOF and
|
|
118
|
+
* returns the decoded string, which may be empty for an empty pipe.
|
|
119
|
+
*
|
|
120
|
+
* @internal
|
|
121
|
+
*/
|
|
122
|
+
async function readNodeStdinAll(proc, stdinIsTTY) {
|
|
123
|
+
if (stdinIsTTY) return null;
|
|
124
|
+
const chunks = [];
|
|
125
|
+
const decoder = new TextDecoder();
|
|
126
|
+
for await (const chunk of proc.stdin) chunks.push(decoder.decode(chunk, { stream: true }));
|
|
127
|
+
chunks.push(decoder.decode());
|
|
128
|
+
return chunks.join("");
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Read a single line from Node's stdin via its async iterator.
|
|
132
|
+
*
|
|
133
|
+
* Iterates stdin chunks manually (without `for-await` to avoid calling
|
|
134
|
+
* `iterator.return()` which would close the stream). Returns `null`
|
|
135
|
+
* on EOF.
|
|
136
|
+
*
|
|
137
|
+
* @internal
|
|
138
|
+
*/
|
|
139
|
+
function createNodeReadLine(proc) {
|
|
140
|
+
const iter = proc.stdin[Symbol.asyncIterator]();
|
|
141
|
+
const decoder = new TextDecoder();
|
|
142
|
+
let buffer = "";
|
|
143
|
+
let done = false;
|
|
144
|
+
return async () => {
|
|
145
|
+
for (;;) {
|
|
146
|
+
const nlIndex = buffer.indexOf("\n");
|
|
147
|
+
if (nlIndex !== -1) {
|
|
148
|
+
const line = buffer.slice(0, nlIndex).replace(/\r$/, "");
|
|
149
|
+
buffer = buffer.slice(nlIndex + 1);
|
|
150
|
+
return line;
|
|
151
|
+
}
|
|
152
|
+
if (done) {
|
|
153
|
+
if (buffer.length === 0) return null;
|
|
154
|
+
const line = buffer;
|
|
155
|
+
buffer = "";
|
|
156
|
+
return line;
|
|
157
|
+
}
|
|
158
|
+
const result = await iter.next();
|
|
159
|
+
if (result.done) {
|
|
160
|
+
buffer += decoder.decode();
|
|
161
|
+
done = true;
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
buffer += decoder.decode(result.value, { stream: true });
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
//#endregion
|
|
169
|
+
export { createNodeAdapter };
|
package/dist/runtime/paths.mjs
CHANGED
|
@@ -1 +1,48 @@
|
|
|
1
|
-
import{stripTrailing
|
|
1
|
+
import { stripTrailing } from "../strings.mjs";
|
|
2
|
+
//#region src/runtime/paths.ts
|
|
3
|
+
/**
|
|
4
|
+
* Shared env-based path resolution for runtime adapters.
|
|
5
|
+
*
|
|
6
|
+
* Node, Bun, and Deno all need the same user home/config directory behavior.
|
|
7
|
+
* These helpers keep the fallback chain identical across runtimes while
|
|
8
|
+
* letting each adapter supply its own platform detection.
|
|
9
|
+
*
|
|
10
|
+
* @module @kjanat/dreamcli/runtime/paths
|
|
11
|
+
*/
|
|
12
|
+
/**
|
|
13
|
+
* Resolve the user's home directory from environment variables.
|
|
14
|
+
*
|
|
15
|
+
* @param env - Environment variable map.
|
|
16
|
+
* @param isWindows - Whether the platform is Windows.
|
|
17
|
+
* @returns Absolute path to the home directory.
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
function resolveHomeDirectory(env, isWindows) {
|
|
21
|
+
if (isWindows) {
|
|
22
|
+
if (env.USERPROFILE) return env.USERPROFILE;
|
|
23
|
+
if (env.HOMEDRIVE && env.HOMEPATH) return env.HOMEDRIVE + env.HOMEPATH;
|
|
24
|
+
return env.HOME || "C:\\";
|
|
25
|
+
}
|
|
26
|
+
return env.HOME || "/";
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Resolve the user's config directory (`XDG_CONFIG_HOME` / `APPDATA`).
|
|
30
|
+
*
|
|
31
|
+
* @param env - Environment variable map.
|
|
32
|
+
* @param isWindows - Whether the platform is Windows.
|
|
33
|
+
* @param homedir - Pre-resolved home directory (from {@link resolveHomeDirectory}).
|
|
34
|
+
* @returns Absolute path to the config directory.
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
function resolveConfigDirectory(env, isWindows, homedir) {
|
|
38
|
+
if (isWindows) {
|
|
39
|
+
if (env.APPDATA !== void 0 && env.APPDATA !== "") return env.APPDATA;
|
|
40
|
+
return `${stripTrailing(homedir, [47, 92]) || homedir}\\AppData\\Roaming`;
|
|
41
|
+
}
|
|
42
|
+
const normalizedHome = stripTrailing(homedir, [47]);
|
|
43
|
+
if (env.XDG_CONFIG_HOME !== void 0 && env.XDG_CONFIG_HOME !== "") return env.XDG_CONFIG_HOME;
|
|
44
|
+
if (normalizedHome.length === 0) return "/.config";
|
|
45
|
+
return `${normalizedHome}/.config`;
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { resolveConfigDirectory, resolveHomeDirectory };
|
package/dist/runtime.mjs
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createNodeAdapter } from "./runtime/node.mjs";
|
|
2
|
+
import { createBunAdapter } from "./runtime/bun.mjs";
|
|
3
|
+
import { createDenoAdapter } from "./runtime/deno.mjs";
|
|
4
|
+
import { RUNTIMES, detectRuntime } from "./runtime/detect.mjs";
|
|
5
|
+
import { createAdapter } from "./runtime/auto.mjs";
|
|
6
|
+
import { ExitError } from "./runtime/adapter.mjs";
|
|
7
|
+
export { ExitError, RUNTIMES, createAdapter, createBunAdapter, createDenoAdapter, createNodeAdapter, detectRuntime };
|
package/dist/strings.mjs
CHANGED
|
@@ -1 +1,22 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/strings.ts
|
|
2
|
+
/**
|
|
3
|
+
* Remove trailing characters whose code points appear in `codes`.
|
|
4
|
+
*
|
|
5
|
+
* Linear-time replacement for trailing-character regexes such as
|
|
6
|
+
* `/[\\/]+$/`. Those patterns are polynomial-ReDoS prone: the engine
|
|
7
|
+
* greedily consumes the run, fails the end anchor, backtracks one
|
|
8
|
+
* character at a time, and repeats that work from every start position —
|
|
9
|
+
* O(n²) on adversarial input. This scan is O(n) with no backtracking.
|
|
10
|
+
*
|
|
11
|
+
* @param input - Source string.
|
|
12
|
+
* @param codes - Character codes to strip from the end of `input`.
|
|
13
|
+
* @returns `input` with all trailing matching characters removed.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
function stripTrailing(input, codes) {
|
|
17
|
+
let end = input.length;
|
|
18
|
+
while (end > 0 && codes.includes(input.charCodeAt(end - 1))) end -= 1;
|
|
19
|
+
return end === input.length ? input : input.slice(0, end);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { stripTrailing };
|
package/dist/testkit.mjs
CHANGED
|
@@ -1 +1,5 @@
|
|
|
1
|
-
import{createCaptureOutput
|
|
1
|
+
import { createCaptureOutput } from "./core/output/index.mjs";
|
|
2
|
+
import { PROMPT_CANCEL, createTestPrompter } from "./core/prompt/index.mjs";
|
|
3
|
+
import { runCommand } from "./core/testkit/index.mjs";
|
|
4
|
+
import { createTestAdapter } from "./runtime/adapter.mjs";
|
|
5
|
+
export { PROMPT_CANCEL, createCaptureOutput, createTestAdapter, createTestPrompter, runCommand };
|
package/dist/version.mjs
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
//#region src/version.ts
|
|
2
|
+
/** Package version (e.g. `"0.9.1"`). `"dev"` when running unbundled. */
|
|
3
|
+
const DREAMCLI_VERSION = "3.0.0-rc.2";
|
|
4
|
+
/** Git short SHA (e.g. `"f9b5f1a"`). `"dev"` when running unbundled. */
|
|
5
|
+
const DREAMCLI_REVISION = "9ba0c14";
|
|
6
|
+
//#endregion
|
|
7
|
+
export { DREAMCLI_REVISION, DREAMCLI_VERSION };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kjanat/dreamcli",
|
|
3
|
-
"version": "3.0.0-rc.
|
|
3
|
+
"version": "3.0.0-rc.2",
|
|
4
4
|
"description": "Schema-first, fully typed TypeScript CLI framework",
|
|
5
5
|
"keywords": ["dreamcli", "cli", "typescript", "framework", "schema-first", "command-line"],
|
|
6
6
|
"homepage": "https://dreamcli.kjanat.dev",
|
|
@@ -29,37 +29,25 @@
|
|
|
29
29
|
"exports": {
|
|
30
30
|
".": {
|
|
31
31
|
"types": "./dist/index.d.mts",
|
|
32
|
-
"bun": "./src/index.ts",
|
|
33
|
-
"deno": "./src/index.ts",
|
|
34
32
|
"default": "./dist/index.mjs"
|
|
35
33
|
},
|
|
36
34
|
"./runtime": {
|
|
37
35
|
"types": "./dist/runtime.d.mts",
|
|
38
|
-
"bun": "./src/runtime.ts",
|
|
39
|
-
"deno": "./src/runtime.ts",
|
|
40
36
|
"default": "./dist/runtime.mjs"
|
|
41
37
|
},
|
|
42
38
|
"./testkit": {
|
|
43
39
|
"types": "./dist/testkit.d.mts",
|
|
44
|
-
"bun": "./src/testkit.ts",
|
|
45
|
-
"deno": "./src/testkit.ts",
|
|
46
40
|
"default": "./dist/testkit.mjs"
|
|
47
41
|
},
|
|
48
|
-
"./
|
|
49
|
-
"./
|
|
42
|
+
"./package.json": "./package.json",
|
|
43
|
+
"./schema": "./dreamcli.schema.json"
|
|
50
44
|
},
|
|
51
45
|
"files": [
|
|
52
46
|
"CHANGELOG.md",
|
|
53
|
-
"LICENSE",
|
|
54
|
-
"README.md",
|
|
55
47
|
"dist",
|
|
56
48
|
"dreamcli.schema.json",
|
|
57
49
|
"examples",
|
|
58
|
-
"
|
|
59
|
-
"package.json",
|
|
60
|
-
"src",
|
|
61
|
-
"!src/**/*.test.ts",
|
|
62
|
-
"!src/**/*test-helpers.ts",
|
|
50
|
+
"!**/.cache",
|
|
63
51
|
"!**/AGENTS.md"
|
|
64
52
|
],
|
|
65
53
|
"workspaces": ["./examples/*"],
|
|
@@ -109,30 +97,33 @@
|
|
|
109
97
|
"overrides": {
|
|
110
98
|
"vitepress": "next"
|
|
111
99
|
},
|
|
100
|
+
"dependencies": {
|
|
101
|
+
"ansispeck": "^0.1.2"
|
|
102
|
+
},
|
|
112
103
|
"devDependencies": {
|
|
113
104
|
"@arethetypeswrong/core": "^0.18.4",
|
|
114
|
-
"@biomejs/biome": "^2.5.
|
|
105
|
+
"@biomejs/biome": "^2.5.2",
|
|
115
106
|
"@iarna/toml": "^2.2.5",
|
|
116
|
-
"@shikijs/transformers": "^4.3.
|
|
117
|
-
"@shikijs/vitepress-twoslash": "^4.3.
|
|
107
|
+
"@shikijs/transformers": "^4.3.1",
|
|
108
|
+
"@shikijs/vitepress-twoslash": "^4.3.1",
|
|
118
109
|
"@types/bun": "^1.3.14",
|
|
119
110
|
"@types/deno": "^2.7.0",
|
|
120
|
-
"@types/node": ">=26.0
|
|
121
|
-
"@typescript/native-preview": "^7.0.0-dev.
|
|
111
|
+
"@types/node": ">=26.1.0",
|
|
112
|
+
"@typescript/native-preview": "^7.0.0-dev.20260705.1",
|
|
122
113
|
"@vitest/coverage-v8": "^4.1.9",
|
|
123
114
|
"dprint": "^0.55.1",
|
|
124
115
|
"floating-vue": "^5.2.2",
|
|
125
|
-
"knip": "^6.
|
|
116
|
+
"knip": "^6.24.0",
|
|
126
117
|
"mermaid": "^11.16.0",
|
|
127
118
|
"publint": "^0.3.21",
|
|
128
|
-
"runner-run": "0.
|
|
119
|
+
"runner-run": "0.19.1",
|
|
129
120
|
"tsdown": "^0.22.3",
|
|
130
121
|
"typedoc": "^0.28.19",
|
|
131
122
|
"typescript": "^6.0.3",
|
|
132
|
-
"vite": "^8.1.
|
|
123
|
+
"vite": "^8.1.3",
|
|
133
124
|
"vitepress": "^2.0.0-alpha.17",
|
|
134
125
|
"vitest": "^4.1.9",
|
|
135
|
-
"wrangler": "^4.
|
|
126
|
+
"wrangler": "^4.107.0",
|
|
136
127
|
"yaml": "^2.9.0"
|
|
137
128
|
},
|
|
138
129
|
"packageManager": "bun@1.3.14",
|
|
@@ -161,7 +152,7 @@
|
|
|
161
152
|
},
|
|
162
153
|
"catalog": {
|
|
163
154
|
"@types/bun": "^1.3.14",
|
|
164
|
-
"@types/node": ">=26.0
|
|
155
|
+
"@types/node": ">=26.1.0",
|
|
165
156
|
"typescript": "^6.0.3"
|
|
166
157
|
}
|
|
167
158
|
}
|