@kjanat/dreamcli 2.3.0 → 2.4.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.
@@ -445,7 +445,7 @@ function parseLongFlag(
445
445
 
446
446
  // --flag value (next token is the value)
447
447
  const nextToken = tokens[startIdx + 1];
448
- if (!nextToken || nextToken.kind !== 'positional') {
448
+ if (nextToken?.kind !== 'positional') {
449
449
  throw new ParseError(`Flag --${token.name} requires a value`, {
450
450
  code: 'MISSING_VALUE',
451
451
  details: { flag: canonicalName, input: token.name, kind: flagSchema.kind },
@@ -505,7 +505,7 @@ function parseShortFlags(
505
505
 
506
506
  // Last char in the group — consume next token as value
507
507
  const nextToken = tokens[nextIdx];
508
- if (!nextToken || nextToken.kind !== 'positional') {
508
+ if (nextToken?.kind !== 'positional') {
509
509
  throw new ParseError(`Flag -${ch} requires a value`, {
510
510
  code: 'MISSING_VALUE',
511
511
  details: { flag: canonicalName, input: ch, kind: flagSchema.kind },
@@ -152,6 +152,14 @@ interface Out {
152
152
  warn(message: string): void;
153
153
  /** Error to stderr. */
154
154
  error(message: string): void;
155
+ /**
156
+ * Request a process exit code without emitting error-shaped output.
157
+ *
158
+ * Use this for check/status commands that should print normal output but
159
+ * still signal a non-zero status to scripts. This does not stop execution;
160
+ * later calls overwrite earlier calls, and thrown errors still win.
161
+ */
162
+ setExitCode(code: number): void;
155
163
  /**
156
164
  * Emit a structured JSON value to stdout.
157
165
  *
@@ -155,9 +155,10 @@ export interface RunOptions {
155
155
  /**
156
156
  * Structured result from {@linkcode runCommand}.
157
157
  *
158
- * Contains the exit code, captured stdout/stderr output, recorded
159
- * {@linkcode ActivityEvent | activity events}, and an `error` field
160
- * that is `undefined` on success and a {@linkcode CLIError} on failure.
158
+ * Contains the exit code, captured stdout/stderr output, recorded
159
+ * {@linkcode ActivityEvent | activity events}, and an `error` field.
160
+ * `error` is `undefined` when execution completed without throwing, even if
161
+ * the handler requested a non-zero status via {@linkcode Out.setExitCode}.
161
162
  *
162
163
  * @example
163
164
  * ```ts
@@ -188,8 +189,10 @@ export interface RunResult {
188
189
  readonly activity: readonly ActivityEvent[];
189
190
 
190
191
  /**
191
- * The error that caused a non-zero exit, or `undefined` on success.
192
- * {@linkcode CLIError} instances are preserved; unknown errors are wrapped.
192
+ * The error that caused a failure, or `undefined` when execution completed.
193
+ * A non-zero `exitCode` can still have no error when a handler calls
194
+ * {@linkcode Out.setExitCode}. {@linkcode CLIError} instances are preserved;
195
+ * unknown errors are wrapped.
193
196
  */
194
197
  readonly error: CLIError | undefined;
195
198
  }
@@ -8,6 +8,8 @@
8
8
  * @module @kjanat/dreamcli/runtime/paths
9
9
  */
10
10
 
11
+ import { BACKSLASH, SLASH, stripTrailing } from '#internals/strings.ts';
12
+
11
13
  /**
12
14
  * Resolve the user's home directory from environment variables.
13
15
  *
@@ -46,10 +48,10 @@ function resolveConfigDirectory(
46
48
  ): string {
47
49
  if (isWindows) {
48
50
  if (env.APPDATA !== undefined && env.APPDATA !== '') return env.APPDATA;
49
- const normalizedHome = homedir.replace(/[\\/]+$/, '') || homedir;
51
+ const normalizedHome = stripTrailing(homedir, [SLASH, BACKSLASH]) || homedir;
50
52
  return `${normalizedHome}\\AppData\\Roaming`;
51
53
  }
52
- const normalizedHome = homedir.replace(/\/+$/, '');
54
+ const normalizedHome = stripTrailing(homedir, [SLASH]);
53
55
  if (env.XDG_CONFIG_HOME !== undefined && env.XDG_CONFIG_HOME !== '') {
54
56
  return env.XDG_CONFIG_HOME;
55
57
  }
package/src/strings.ts ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Internal string utilities shared across runtime and core modules.
3
+ *
4
+ * @module dreamcli/strings
5
+ */
6
+
7
+ /** Character code for `/` (forward slash). */
8
+ const SLASH = 0x2f;
9
+ /** Character code for `\` (backslash). */
10
+ const BACKSLASH = 0x5c;
11
+
12
+ /**
13
+ * Remove trailing characters whose code points appear in `codes`.
14
+ *
15
+ * Linear-time replacement for trailing-character regexes such as
16
+ * `/[\\/]+$/`. Those patterns are polynomial-ReDoS prone: the engine
17
+ * greedily consumes the run, fails the end anchor, backtracks one
18
+ * character at a time, and repeats that work from every start position —
19
+ * O(n²) on adversarial input. This scan is O(n) with no backtracking.
20
+ *
21
+ * @param input - Source string.
22
+ * @param codes - Character codes to strip from the end of `input`.
23
+ * @returns `input` with all trailing matching characters removed.
24
+ * @internal
25
+ */
26
+ function stripTrailing(input: string, codes: readonly number[]): string {
27
+ let end = input.length;
28
+ while (end > 0 && codes.includes(input.charCodeAt(end - 1))) {
29
+ end -= 1;
30
+ }
31
+ return end === input.length ? input : input.slice(0, end);
32
+ }
33
+
34
+ export { BACKSLASH, SLASH, stripTrailing };