clawkit-cli-core 0.1.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.
@@ -0,0 +1,70 @@
1
+ //#region src/errors.d.ts
2
+ declare const CLI_ERROR_CODES: readonly ["E_ARG_INVALID", "E_ARG_MISSING", "E_ARG_CONFLICT", "E_ARG_UNSUPPORTED", "E_AUTH_REQUIRED", "E_AUTH_INVALID", "E_NOT_FOUND_RESOURCE", "E_UPSTREAM_NETWORK", "E_UPSTREAM_TIMEOUT", "E_UPSTREAM_BAD_RESPONSE", "E_UNKNOWN"];
3
+ type CliErrorCode = (typeof CLI_ERROR_CODES)[number];
4
+ interface CliAppErrorOptions {
5
+ code: CliErrorCode;
6
+ message: string;
7
+ details?: unknown;
8
+ cause?: unknown;
9
+ }
10
+ declare class CliAppError extends Error {
11
+ readonly code: CliErrorCode;
12
+ readonly details?: unknown;
13
+ constructor(options: CliAppErrorOptions);
14
+ }
15
+ declare function isCliAppError(value: unknown): value is CliAppError;
16
+ declare function toCliAppError(value: unknown): CliAppError;
17
+ //#endregion
18
+ //#region src/envelope.d.ts
19
+ interface Meta {
20
+ requestId: string;
21
+ startedAt: string;
22
+ finishedAt: string;
23
+ durationMs: number;
24
+ verbose: boolean;
25
+ }
26
+ interface CliSuccess<T> {
27
+ ok: true;
28
+ data: T;
29
+ meta: Meta;
30
+ }
31
+ interface CliError {
32
+ ok: false;
33
+ error: {
34
+ code: CliErrorCode;
35
+ message: string;
36
+ details?: unknown;
37
+ };
38
+ }
39
+ type CliEnvelope<T> = CliSuccess<T> | CliError;
40
+ declare function createSuccessEnvelope<T>(data: T, meta: Meta): CliSuccess<T>;
41
+ declare function createErrorEnvelope(code: CliErrorCode, message: string, details?: unknown): CliError;
42
+ //#endregion
43
+ //#region src/command-context.d.ts
44
+ interface CreateCommandContextOptions {
45
+ clock?: () => number;
46
+ now?: () => Date;
47
+ requestIdFactory?: () => string;
48
+ verbose?: boolean;
49
+ }
50
+ interface CommandContext {
51
+ requestId: string;
52
+ startedAt: string;
53
+ verbose: boolean;
54
+ toMeta: () => Meta;
55
+ }
56
+ declare function createCommandContext(options?: CreateCommandContextOptions): CommandContext;
57
+ //#endregion
58
+ //#region src/exit-codes.d.ts
59
+ type CliExitCode = 0 | 2 | 3 | 4 | 5 | 10;
60
+ declare const EXIT_CODES: {
61
+ readonly SUCCESS: 0;
62
+ readonly ARGUMENT_ERROR: 2;
63
+ readonly AUTH_OR_CONFIG_ERROR: 3;
64
+ readonly NOT_FOUND: 4;
65
+ readonly UPSTREAM_ERROR: 5;
66
+ readonly UNKNOWN_ERROR: 10;
67
+ };
68
+ declare function mapErrorCodeToExitCode(code: CliErrorCode): CliExitCode;
69
+ //#endregion
70
+ export { CLI_ERROR_CODES, CliAppError, CliAppErrorOptions, CliEnvelope, CliError, CliErrorCode, CliExitCode, CliSuccess, CommandContext, CreateCommandContextOptions, EXIT_CODES, Meta, createCommandContext, createErrorEnvelope, createSuccessEnvelope, isCliAppError, mapErrorCodeToExitCode, toCliAppError };
package/dist/index.mjs ADDED
@@ -0,0 +1,119 @@
1
+ import { randomUUID } from "node:crypto";
2
+
3
+ //#region src/command-context.ts
4
+ function createCommandContext(options = {}) {
5
+ const clock = options.clock ?? Date.now;
6
+ const now = options.now ?? (() => /* @__PURE__ */ new Date());
7
+ const requestIdFactory = options.requestIdFactory ?? randomUUID;
8
+ const verbose = options.verbose ?? false;
9
+ const startTimeMs = clock();
10
+ const startedAt = now().toISOString();
11
+ const requestId = requestIdFactory();
12
+ return {
13
+ requestId,
14
+ startedAt,
15
+ verbose,
16
+ toMeta: () => {
17
+ const endTimeMs = clock();
18
+ const durationMs = Math.max(0, endTimeMs - startTimeMs);
19
+ return {
20
+ requestId,
21
+ startedAt,
22
+ finishedAt: now().toISOString(),
23
+ durationMs,
24
+ verbose
25
+ };
26
+ }
27
+ };
28
+ }
29
+
30
+ //#endregion
31
+ //#region src/envelope.ts
32
+ function createSuccessEnvelope(data, meta) {
33
+ return {
34
+ ok: true,
35
+ data,
36
+ meta
37
+ };
38
+ }
39
+ function createErrorEnvelope(code, message, details) {
40
+ if (details === void 0) return {
41
+ ok: false,
42
+ error: {
43
+ code,
44
+ message
45
+ }
46
+ };
47
+ return {
48
+ ok: false,
49
+ error: {
50
+ code,
51
+ message,
52
+ details
53
+ }
54
+ };
55
+ }
56
+
57
+ //#endregion
58
+ //#region src/errors.ts
59
+ const CLI_ERROR_CODES = [
60
+ "E_ARG_INVALID",
61
+ "E_ARG_MISSING",
62
+ "E_ARG_CONFLICT",
63
+ "E_ARG_UNSUPPORTED",
64
+ "E_AUTH_REQUIRED",
65
+ "E_AUTH_INVALID",
66
+ "E_NOT_FOUND_RESOURCE",
67
+ "E_UPSTREAM_NETWORK",
68
+ "E_UPSTREAM_TIMEOUT",
69
+ "E_UPSTREAM_BAD_RESPONSE",
70
+ "E_UNKNOWN"
71
+ ];
72
+ var CliAppError = class extends Error {
73
+ code;
74
+ details;
75
+ constructor(options) {
76
+ super(options.message, { cause: options.cause });
77
+ this.name = "CliAppError";
78
+ this.code = options.code;
79
+ this.details = options.details;
80
+ }
81
+ };
82
+ function isCliAppError(value) {
83
+ return value instanceof CliAppError;
84
+ }
85
+ function toCliAppError(value) {
86
+ if (isCliAppError(value)) return value;
87
+ if (value instanceof Error) return new CliAppError({
88
+ code: "E_UNKNOWN",
89
+ message: value.message,
90
+ details: { name: value.name },
91
+ cause: value
92
+ });
93
+ return new CliAppError({
94
+ code: "E_UNKNOWN",
95
+ message: "Unknown error",
96
+ details: value
97
+ });
98
+ }
99
+
100
+ //#endregion
101
+ //#region src/exit-codes.ts
102
+ const EXIT_CODES = {
103
+ SUCCESS: 0,
104
+ ARGUMENT_ERROR: 2,
105
+ AUTH_OR_CONFIG_ERROR: 3,
106
+ NOT_FOUND: 4,
107
+ UPSTREAM_ERROR: 5,
108
+ UNKNOWN_ERROR: 10
109
+ };
110
+ function mapErrorCodeToExitCode(code) {
111
+ if (code.startsWith("E_ARG_")) return EXIT_CODES.ARGUMENT_ERROR;
112
+ if (code.startsWith("E_AUTH_")) return EXIT_CODES.AUTH_OR_CONFIG_ERROR;
113
+ if (code.startsWith("E_NOT_FOUND_")) return EXIT_CODES.NOT_FOUND;
114
+ if (code.startsWith("E_UPSTREAM_")) return EXIT_CODES.UPSTREAM_ERROR;
115
+ return EXIT_CODES.UNKNOWN_ERROR;
116
+ }
117
+
118
+ //#endregion
119
+ export { CLI_ERROR_CODES, CliAppError, EXIT_CODES, createCommandContext, createErrorEnvelope, createSuccessEnvelope, isCliAppError, mapErrorCodeToExitCode, toCliAppError };
package/package.json ADDED
@@ -0,0 +1,25 @@
1
+ {
2
+ "name": "clawkit-cli-core",
3
+ "version": "0.1.0",
4
+ "private": false,
5
+ "description": "Shared CLI contracts and helpers for clawkit packages",
6
+ "license": "MIT",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "type": "module",
11
+ "main": "dist/index.mjs",
12
+ "types": "dist/index.d.mts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.mts",
16
+ "import": "./dist/index.mjs"
17
+ }
18
+ },
19
+ "scripts": {
20
+ "build": "tsdown src/index.ts --format esm --dts",
21
+ "test": "vitest run",
22
+ "typecheck": "tsc --project tsconfig.json --noEmit",
23
+ "check": "pnpm typecheck && pnpm test"
24
+ }
25
+ }