@gobing-ai/ts-utils 0.3.19 → 0.3.21

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/dist/output.d.ts CHANGED
@@ -15,6 +15,21 @@ export declare function setDefaultOutputTargets(opts: {
15
15
  stdout?: WriteTarget;
16
16
  stderr?: WriteTarget;
17
17
  }): () => void;
18
+ /** Terminates the current process with `code`. Anything matching `(code?: number) => never`. */
19
+ export type ExitTarget = (code?: number) => never;
20
+ /**
21
+ * Terminate the process with the given exit code — the single sanctioned seam
22
+ * for `process.exit` in the workspace (enforced by the `no-direct-process-exit`
23
+ * spur rule). Defaults to `0`. Pass or set an {@link ExitTarget} to intercept
24
+ * in tests or non-`process` runtimes.
25
+ */
26
+ export declare function exitProcess(code?: number, target?: ExitTarget): never;
27
+ /**
28
+ * Override the default exit target.
29
+ *
30
+ * Returns a rollback function that restores the previous target.
31
+ */
32
+ export declare function setDefaultExitTarget(target: ExitTarget | undefined): () => void;
18
33
  /** In-memory `WriteTarget` that records all chunks for later retrieval. */
19
34
  export interface BufferTarget extends WriteTarget {
20
35
  readonly chunks: string[];
@@ -1 +1 @@
1
- {"version":3,"file":"output.d.ts","sourceRoot":"","sources":["../src/output.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,MAAM,WAAW,WAAW;IACxB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC;CACjC;AAoBD,sDAAsD;AACtD,wBAAgB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,WAA4D,GAAG,IAAI,CAEhH;AAED,sDAAsD;AACtD,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,GAAE,WAA4D,GAAG,IAAI,CAErH;AAED;;;;GAIG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE;IAAE,MAAM,CAAC,EAAE,WAAW,CAAC;IAAC,MAAM,CAAC,EAAE,WAAW,CAAA;CAAE,GAAG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6FAA6F;AAC7F,wBAAgB,kBAAkB,IAAI,YAAY,CAejD"}
package/dist/output.js CHANGED
@@ -38,6 +38,38 @@ export function setDefaultOutputTargets(opts) {
38
38
  defaultStderrTarget = prevStderr;
39
39
  };
40
40
  }
41
+ // Resolved lazily, not at module load: reading `process.exit` eagerly would throw on import in
42
+ // runtimes without `process` (e.g. Cloudflare Workers). `undefined` means "fall back to process".
43
+ let defaultExitTarget;
44
+ function processExit() {
45
+ const proc = globalThis.process;
46
+ const exit = proc?.exit;
47
+ if (exit === undefined) {
48
+ throw new Error('No exit target available: set one via setDefaultExitTarget or pass an explicit target');
49
+ }
50
+ return exit;
51
+ }
52
+ /**
53
+ * Terminate the process with the given exit code — the single sanctioned seam
54
+ * for `process.exit` in the workspace (enforced by the `no-direct-process-exit`
55
+ * spur rule). Defaults to `0`. Pass or set an {@link ExitTarget} to intercept
56
+ * in tests or non-`process` runtimes.
57
+ */
58
+ export function exitProcess(code = 0, target = defaultExitTarget ?? processExit()) {
59
+ return target(code);
60
+ }
61
+ /**
62
+ * Override the default exit target.
63
+ *
64
+ * Returns a rollback function that restores the previous target.
65
+ */
66
+ export function setDefaultExitTarget(target) {
67
+ const prev = defaultExitTarget;
68
+ defaultExitTarget = target;
69
+ return () => {
70
+ defaultExitTarget = prev;
71
+ };
72
+ }
41
73
  /** Create an in-memory {@link BufferTarget} for capturing output during tests or tooling. */
42
74
  export function createBufferTarget() {
43
75
  const chunks = [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gobing-ai/ts-utils",
3
- "version": "0.3.19",
3
+ "version": "0.3.21",
4
4
  "description": "Zero-dependency TypeScript utilities for dates, cursors, errors, output, origins, roles, and API responses.",
5
5
  "keywords": [
6
6
  "typescript",
package/src/output.ts CHANGED
@@ -47,6 +47,45 @@ export function setDefaultOutputTargets(opts: { stdout?: WriteTarget; stderr?: W
47
47
  };
48
48
  }
49
49
 
50
+ /** Terminates the current process with `code`. Anything matching `(code?: number) => never`. */
51
+ export type ExitTarget = (code?: number) => never;
52
+
53
+ // Resolved lazily, not at module load: reading `process.exit` eagerly would throw on import in
54
+ // runtimes without `process` (e.g. Cloudflare Workers). `undefined` means "fall back to process".
55
+ let defaultExitTarget: ExitTarget | undefined;
56
+
57
+ function processExit(): ExitTarget {
58
+ const proc = (globalThis as { process?: { exit?: ExitTarget } }).process;
59
+ const exit = proc?.exit;
60
+ if (exit === undefined) {
61
+ throw new Error('No exit target available: set one via setDefaultExitTarget or pass an explicit target');
62
+ }
63
+ return exit;
64
+ }
65
+
66
+ /**
67
+ * Terminate the process with the given exit code — the single sanctioned seam
68
+ * for `process.exit` in the workspace (enforced by the `no-direct-process-exit`
69
+ * spur rule). Defaults to `0`. Pass or set an {@link ExitTarget} to intercept
70
+ * in tests or non-`process` runtimes.
71
+ */
72
+ export function exitProcess(code = 0, target: ExitTarget = defaultExitTarget ?? processExit()): never {
73
+ return target(code);
74
+ }
75
+
76
+ /**
77
+ * Override the default exit target.
78
+ *
79
+ * Returns a rollback function that restores the previous target.
80
+ */
81
+ export function setDefaultExitTarget(target: ExitTarget | undefined): () => void {
82
+ const prev = defaultExitTarget;
83
+ defaultExitTarget = target;
84
+ return () => {
85
+ defaultExitTarget = prev;
86
+ };
87
+ }
88
+
50
89
  /** In-memory `WriteTarget` that records all chunks for later retrieval. */
51
90
  export interface BufferTarget extends WriteTarget {
52
91
  readonly chunks: string[];