hexbus 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.
- package/README.md +17 -0
- package/dist/index.d.mts +312 -0
- package/dist/index.mjs +1050 -0
- package/package.json +48 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# hexbus
|
|
2
|
+
|
|
3
|
+
Reusable CLI chassis for Hexbus tools.
|
|
4
|
+
|
|
5
|
+
Includes:
|
|
6
|
+
|
|
7
|
+
- argument parsing and global flags
|
|
8
|
+
- context creation
|
|
9
|
+
- logger and spinner helpers
|
|
10
|
+
- configurable telemetry
|
|
11
|
+
- configurable error catalog
|
|
12
|
+
- project root, framework, and package manager detection
|
|
13
|
+
- intro and help rendering
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { createCliContext, displayIntro, showHelpMenu } from 'hexbus';
|
|
17
|
+
```
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
import color from "picocolors";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type LogLevel = 'error' | 'warn' | 'info' | 'debug';
|
|
5
|
+
type FlagType = 'boolean' | 'string' | 'special';
|
|
6
|
+
interface CliFlag {
|
|
7
|
+
names: string[];
|
|
8
|
+
description: string;
|
|
9
|
+
type: FlagType;
|
|
10
|
+
expectsValue: boolean;
|
|
11
|
+
defaultValue?: string | boolean;
|
|
12
|
+
}
|
|
13
|
+
interface ParsedArgs {
|
|
14
|
+
commandName: string | undefined;
|
|
15
|
+
commandArgs: string[];
|
|
16
|
+
parsedFlags: Record<string, string | boolean | undefined>;
|
|
17
|
+
}
|
|
18
|
+
interface CliCommand<TContext extends CliContext = CliContext> {
|
|
19
|
+
name: string;
|
|
20
|
+
label: string;
|
|
21
|
+
hint: string;
|
|
22
|
+
description: string;
|
|
23
|
+
action: (context: TContext) => Promise<void>;
|
|
24
|
+
subcommands?: CliCommand<TContext>[];
|
|
25
|
+
hidden?: boolean;
|
|
26
|
+
}
|
|
27
|
+
interface PackageInfo {
|
|
28
|
+
name: string;
|
|
29
|
+
version: string;
|
|
30
|
+
[key: string]: unknown;
|
|
31
|
+
}
|
|
32
|
+
interface FrameworkDetectionResult<TPackage extends string = string> {
|
|
33
|
+
framework: string | null;
|
|
34
|
+
frameworkVersion: string | null;
|
|
35
|
+
pkg: TPackage | null;
|
|
36
|
+
hasReact: boolean;
|
|
37
|
+
reactVersion: string | null;
|
|
38
|
+
tailwindVersion: string | null;
|
|
39
|
+
}
|
|
40
|
+
type PackageManager = 'npm' | 'yarn' | 'pnpm' | 'bun';
|
|
41
|
+
interface PackageManagerResult {
|
|
42
|
+
name: PackageManager;
|
|
43
|
+
installCommand: string;
|
|
44
|
+
addCommand: string;
|
|
45
|
+
runCommand: string;
|
|
46
|
+
execCommand: string;
|
|
47
|
+
version?: string | null;
|
|
48
|
+
}
|
|
49
|
+
interface CliLogger {
|
|
50
|
+
debug(message: string, ...args: unknown[]): void;
|
|
51
|
+
info(message: string, ...args: unknown[]): void;
|
|
52
|
+
warn(message: string, ...args: unknown[]): void;
|
|
53
|
+
error(message: string, ...args: unknown[]): void;
|
|
54
|
+
message(message: string): void;
|
|
55
|
+
note(content: string, title?: string): void;
|
|
56
|
+
outro(message: string): void;
|
|
57
|
+
success(message: string): void;
|
|
58
|
+
failed(message: string, exitCode?: number): never;
|
|
59
|
+
step(current: number, total: number, label: string): void;
|
|
60
|
+
}
|
|
61
|
+
interface ErrorHandlers {
|
|
62
|
+
handleError(error: unknown, command: string): never;
|
|
63
|
+
handleCancel(message?: string, context?: {
|
|
64
|
+
command?: string;
|
|
65
|
+
stage?: string;
|
|
66
|
+
}): never;
|
|
67
|
+
}
|
|
68
|
+
interface ConfigManagement {
|
|
69
|
+
loadConfig<TConfig = unknown>(): Promise<TConfig | null>;
|
|
70
|
+
requireConfig<TConfig = unknown>(): Promise<TConfig>;
|
|
71
|
+
getPathAliases(configPath?: string): Record<string, string> | null;
|
|
72
|
+
}
|
|
73
|
+
interface FileSystemUtils {
|
|
74
|
+
getPackageInfo(): PackageInfo;
|
|
75
|
+
exists(path: string): Promise<boolean>;
|
|
76
|
+
read(path: string): Promise<string>;
|
|
77
|
+
write(path: string, content: string): Promise<void>;
|
|
78
|
+
mkdir(path: string): Promise<void>;
|
|
79
|
+
}
|
|
80
|
+
interface Telemetry {
|
|
81
|
+
trackEvent(eventName: string, properties?: Record<string, unknown>): void;
|
|
82
|
+
trackCommand(command: string, args?: string[], flags?: Record<string, string | number | boolean | undefined>): void;
|
|
83
|
+
trackError(error: Error, command?: string): void;
|
|
84
|
+
flush(): Promise<void>;
|
|
85
|
+
shutdown(): Promise<void>;
|
|
86
|
+
isDisabled(): boolean;
|
|
87
|
+
}
|
|
88
|
+
interface CliContext<TPackage extends string = string> {
|
|
89
|
+
logger: CliLogger;
|
|
90
|
+
flags: ParsedArgs['parsedFlags'];
|
|
91
|
+
commandName: string | undefined;
|
|
92
|
+
commandArgs: string[];
|
|
93
|
+
cwd: string;
|
|
94
|
+
error: ErrorHandlers;
|
|
95
|
+
config: ConfigManagement;
|
|
96
|
+
fs: FileSystemUtils;
|
|
97
|
+
telemetry: Telemetry;
|
|
98
|
+
confirm(message: string, initialValue?: boolean): Promise<boolean>;
|
|
99
|
+
projectRoot: string;
|
|
100
|
+
framework: FrameworkDetectionResult<TPackage>;
|
|
101
|
+
packageManager: PackageManagerResult;
|
|
102
|
+
}
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/context.d.ts
|
|
105
|
+
interface CreateContextOptions<TPackage extends string = string> {
|
|
106
|
+
rawArgs: string[];
|
|
107
|
+
cwd?: string;
|
|
108
|
+
commands: CliCommand[];
|
|
109
|
+
appName?: string;
|
|
110
|
+
configName?: string;
|
|
111
|
+
telemetry?: {
|
|
112
|
+
disabled?: boolean;
|
|
113
|
+
debug?: boolean;
|
|
114
|
+
endpoint?: string;
|
|
115
|
+
envVarPrefix?: string;
|
|
116
|
+
defaultProperties?: Record<string, unknown>;
|
|
117
|
+
};
|
|
118
|
+
packageMap?: {
|
|
119
|
+
core?: TPackage;
|
|
120
|
+
react?: TPackage;
|
|
121
|
+
next?: TPackage;
|
|
122
|
+
};
|
|
123
|
+
interactivePackageManagerDetection?: boolean;
|
|
124
|
+
}
|
|
125
|
+
declare function createCliContext<TPackage extends string = string>(options: CreateContextOptions<TPackage>): Promise<CliContext<TPackage>>;
|
|
126
|
+
declare function createTestContext(overrides?: Partial<CliContext>): CliContext;
|
|
127
|
+
//#endregion
|
|
128
|
+
//#region src/detection.d.ts
|
|
129
|
+
interface FrameworkPackageMap<TPackage extends string = string> {
|
|
130
|
+
core?: TPackage;
|
|
131
|
+
react?: TPackage;
|
|
132
|
+
next?: TPackage;
|
|
133
|
+
}
|
|
134
|
+
declare function detectFramework<TPackage extends string = string>(projectRoot: string, logger?: CliLogger, packageMap?: FrameworkPackageMap<TPackage>): Promise<FrameworkDetectionResult<TPackage>>;
|
|
135
|
+
declare function detectProjectRoot(cwd: string, logger?: CliLogger): Promise<string>;
|
|
136
|
+
declare function detectPackageManager(projectRoot: string, logger?: CliLogger, options?: {
|
|
137
|
+
interactive?: boolean;
|
|
138
|
+
}): Promise<PackageManagerResult>;
|
|
139
|
+
declare function getInstallCommand(pm: PackageManagerResult, packages: string[], options?: {
|
|
140
|
+
dev?: boolean;
|
|
141
|
+
}): string;
|
|
142
|
+
declare function getRunCommand(pm: PackageManagerResult, script: string): string;
|
|
143
|
+
declare function getExecCommand(pm: PackageManagerResult, binary: string, args?: string[]): string;
|
|
144
|
+
//#endregion
|
|
145
|
+
//#region src/errors.d.ts
|
|
146
|
+
interface ErrorCatalogEntry {
|
|
147
|
+
code: string;
|
|
148
|
+
message: string;
|
|
149
|
+
hint?: string;
|
|
150
|
+
docs?: string;
|
|
151
|
+
}
|
|
152
|
+
type ErrorCatalog = Record<string, ErrorCatalogEntry>;
|
|
153
|
+
declare const DEFAULT_ERROR_CATALOG: {
|
|
154
|
+
readonly CONFIG_NOT_FOUND: {
|
|
155
|
+
readonly code: "CONFIG_NOT_FOUND";
|
|
156
|
+
readonly message: "Configuration not found";
|
|
157
|
+
readonly hint: "Run the setup command to create a configuration";
|
|
158
|
+
};
|
|
159
|
+
readonly FLAG_VALUE_REQUIRED: {
|
|
160
|
+
readonly code: "FLAG_VALUE_REQUIRED";
|
|
161
|
+
readonly message: "Flag requires a value";
|
|
162
|
+
};
|
|
163
|
+
readonly COMMAND_NOT_FOUND: {
|
|
164
|
+
readonly code: "COMMAND_NOT_FOUND";
|
|
165
|
+
readonly message: "Unknown command";
|
|
166
|
+
readonly hint: "Run --help to see available commands";
|
|
167
|
+
};
|
|
168
|
+
readonly CANCELLED: {
|
|
169
|
+
readonly code: "CANCELLED";
|
|
170
|
+
readonly message: "Operation cancelled";
|
|
171
|
+
};
|
|
172
|
+
readonly UNKNOWN_ERROR: {
|
|
173
|
+
readonly code: "UNKNOWN_ERROR";
|
|
174
|
+
readonly message: "An unexpected error occurred";
|
|
175
|
+
};
|
|
176
|
+
};
|
|
177
|
+
declare function extendErrorCatalog(entries: ErrorCatalog): void;
|
|
178
|
+
type ErrorCode = keyof typeof DEFAULT_ERROR_CATALOG | string;
|
|
179
|
+
declare class CliError extends Error {
|
|
180
|
+
readonly code: ErrorCode;
|
|
181
|
+
readonly context?: Record<string, unknown>;
|
|
182
|
+
readonly entry: ErrorCatalogEntry;
|
|
183
|
+
constructor(code: ErrorCode, context?: Record<string, unknown>);
|
|
184
|
+
display(logger: CliLogger): void;
|
|
185
|
+
static from(error: unknown, fallbackCode?: ErrorCode): CliError;
|
|
186
|
+
}
|
|
187
|
+
declare function isCliError(error: unknown, code?: ErrorCode): error is CliError;
|
|
188
|
+
declare function createErrorHandlers(logger: CliLogger, telemetry?: {
|
|
189
|
+
trackError(error: Error, command?: string): void;
|
|
190
|
+
}): {
|
|
191
|
+
handleError(error: unknown, command: string): never;
|
|
192
|
+
handleCancel(message?: string, context?: {
|
|
193
|
+
command?: string;
|
|
194
|
+
stage?: string;
|
|
195
|
+
}): never;
|
|
196
|
+
};
|
|
197
|
+
declare function withErrorHandling<T extends (...args: unknown[]) => Promise<unknown>>(fn: T, logger: CliLogger, context?: {
|
|
198
|
+
command?: string;
|
|
199
|
+
}): T;
|
|
200
|
+
//#endregion
|
|
201
|
+
//#region src/help.d.ts
|
|
202
|
+
interface ShowHelpMenuOptions {
|
|
203
|
+
appName: string;
|
|
204
|
+
version: string;
|
|
205
|
+
docsUrl?: string;
|
|
206
|
+
}
|
|
207
|
+
declare function showHelpMenu(context: Pick<CliContext, 'logger'>, options: ShowHelpMenuOptions, commands: CliCommand[], flags: CliFlag[]): void;
|
|
208
|
+
//#endregion
|
|
209
|
+
//#region src/intro.d.ts
|
|
210
|
+
interface DisplayIntroOptions {
|
|
211
|
+
appName: string;
|
|
212
|
+
version?: string;
|
|
213
|
+
tagline?: string;
|
|
214
|
+
figletText?: string;
|
|
215
|
+
}
|
|
216
|
+
declare function displayIntro(context: Pick<CliContext, 'logger'>, options: DisplayIntroOptions): Promise<void>;
|
|
217
|
+
//#endregion
|
|
218
|
+
//#region src/logger.d.ts
|
|
219
|
+
declare const LOG_LEVELS: LogLevel[];
|
|
220
|
+
declare const validLogLevels: LogLevel[];
|
|
221
|
+
declare function formatLogMessage(logLevel: LogLevel | 'success' | 'failed' | string, message: unknown, args?: unknown[]): string;
|
|
222
|
+
declare function logMessage(logLevel: LogLevel | 'success' | 'failed' | string, message: unknown, ...args: unknown[]): void;
|
|
223
|
+
declare function formatStep(current: number, total: number, label: string): string;
|
|
224
|
+
declare function createCliLogger(level?: LogLevel): CliLogger;
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/parser.d.ts
|
|
227
|
+
declare const globalFlags: CliFlag[];
|
|
228
|
+
declare function parseCliArgs(rawArgs: string[], commands: CliCommand[]): ParsedArgs;
|
|
229
|
+
declare function formatFlagHelp(flag: CliFlag): string;
|
|
230
|
+
declare function generateFlagsHelp(): string;
|
|
231
|
+
declare function hasFlag(flags: ParsedArgs['parsedFlags'], name: string): boolean;
|
|
232
|
+
declare function getFlagValue(flags: ParsedArgs['parsedFlags'], name: string): string | undefined;
|
|
233
|
+
declare function parseSubcommand(args: string[], subcommands: CliCommand[]): {
|
|
234
|
+
subcommand: CliCommand | undefined;
|
|
235
|
+
remainingArgs: string[];
|
|
236
|
+
};
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/spinner.d.ts
|
|
239
|
+
interface Spinner {
|
|
240
|
+
start(message?: string): void;
|
|
241
|
+
stop(message?: string): void;
|
|
242
|
+
message(message: string): void;
|
|
243
|
+
}
|
|
244
|
+
declare function createSpinner(initialMessage?: string): Spinner;
|
|
245
|
+
declare function withSpinner<T>(message: string, task: () => Promise<T>, options?: {
|
|
246
|
+
successMessage?: string;
|
|
247
|
+
errorMessage?: string;
|
|
248
|
+
}): Promise<T>;
|
|
249
|
+
//#endregion
|
|
250
|
+
//#region src/telemetry.d.ts
|
|
251
|
+
interface TelemetryOptions {
|
|
252
|
+
disabled?: boolean;
|
|
253
|
+
debug?: boolean;
|
|
254
|
+
endpoint?: string;
|
|
255
|
+
appName?: string;
|
|
256
|
+
envVarPrefix?: string;
|
|
257
|
+
defaultProperties?: Record<string, unknown>;
|
|
258
|
+
logger?: Pick<CliLogger, 'debug' | 'warn'>;
|
|
259
|
+
}
|
|
260
|
+
declare const TelemetryEventName: {
|
|
261
|
+
readonly CLI_INVOKED: "cli_invoked";
|
|
262
|
+
readonly CLI_ENVIRONMENT_DETECTED: "cli_environment_detected";
|
|
263
|
+
readonly CLI_COMPLETED: "cli_completed";
|
|
264
|
+
readonly COMMAND_INVOKED: "command_invoked";
|
|
265
|
+
readonly COMMAND_SUCCEEDED: "command_succeeded";
|
|
266
|
+
readonly COMMAND_FAILED: "command_failed";
|
|
267
|
+
readonly COMMAND_UNKNOWN: "command_unknown";
|
|
268
|
+
readonly ERROR_OCCURRED: "error_occurred";
|
|
269
|
+
readonly HELP_DISPLAYED: "help_displayed";
|
|
270
|
+
readonly VERSION_DISPLAYED: "version_displayed";
|
|
271
|
+
readonly INTERACTIVE_MENU_OPENED: "interactive_menu_opened";
|
|
272
|
+
readonly INTERACTIVE_MENU_EXITED: "interactive_menu_exited";
|
|
273
|
+
};
|
|
274
|
+
type TelemetryEventNameType = (typeof TelemetryEventName)[keyof typeof TelemetryEventName];
|
|
275
|
+
declare function createDisabledTelemetry(): Telemetry;
|
|
276
|
+
declare function createTelemetry(options?: TelemetryOptions): Telemetry;
|
|
277
|
+
//#endregion
|
|
278
|
+
//#region src/version-check.d.ts
|
|
279
|
+
type InstallSource = 'npm-global' | 'brew' | 'npx' | 'bunx' | 'pnpm-dlx' | 'yarn-dlx' | 'local' | 'unknown';
|
|
280
|
+
interface UpdateCheckResult {
|
|
281
|
+
currentVersion: string;
|
|
282
|
+
latestVersion: string | null;
|
|
283
|
+
isOutdated: boolean;
|
|
284
|
+
source: InstallSource;
|
|
285
|
+
updateCommand: string | null;
|
|
286
|
+
hint: string | null;
|
|
287
|
+
}
|
|
288
|
+
interface UpdateCheckOptions {
|
|
289
|
+
packageName: string;
|
|
290
|
+
currentVersion: string;
|
|
291
|
+
brewFormula?: string;
|
|
292
|
+
registryUrl?: string;
|
|
293
|
+
timeoutMs?: number;
|
|
294
|
+
cacheDir?: string;
|
|
295
|
+
cacheTtlMs?: number;
|
|
296
|
+
binPath?: string;
|
|
297
|
+
now?: () => number;
|
|
298
|
+
}
|
|
299
|
+
type VersionInfoLogger = Pick<CliLogger, 'message' | 'note'> & Partial<Pick<CliLogger, 'debug'>>;
|
|
300
|
+
interface VersionInfoOptions extends UpdateCheckOptions {
|
|
301
|
+
appName: string;
|
|
302
|
+
logger?: VersionInfoLogger;
|
|
303
|
+
}
|
|
304
|
+
declare function isVersionRequest(rawArgs: string[]): boolean;
|
|
305
|
+
declare function detectInstallSource(binPath?: string): InstallSource;
|
|
306
|
+
declare function getUpdateCommand(source: InstallSource, packageName: string, brewFormula?: string): string | null;
|
|
307
|
+
declare function checkForUpdate(options: UpdateCheckOptions): Promise<UpdateCheckResult>;
|
|
308
|
+
declare function formatUpdateHint(result: UpdateCheckResult): string | null;
|
|
309
|
+
declare function printVersionInfo(options: VersionInfoOptions): Promise<void>;
|
|
310
|
+
declare function startBackgroundUpdateCheck(options: VersionInfoOptions): void;
|
|
311
|
+
//#endregion
|
|
312
|
+
export { type CliCommand, type CliContext, CliError, type CliFlag, type CliLogger, type ConfigManagement, type CreateContextOptions, DEFAULT_ERROR_CATALOG, type DisplayIntroOptions, type ErrorCatalog, type ErrorCatalogEntry, type ErrorCode, type ErrorHandlers, type FileSystemUtils, type FlagType, type FrameworkDetectionResult, type FrameworkPackageMap, type InstallSource, LOG_LEVELS, type LogLevel, type PackageInfo, type PackageManager, type PackageManagerResult, type ParsedArgs, type ShowHelpMenuOptions, type Spinner, type Telemetry, TelemetryEventName, type TelemetryEventNameType, type TelemetryOptions, type UpdateCheckOptions, type UpdateCheckResult, type VersionInfoOptions, checkForUpdate, color, createCliContext, createCliLogger, createDisabledTelemetry, createErrorHandlers, createSpinner, createTelemetry, createTestContext, detectFramework, detectInstallSource, detectPackageManager, detectProjectRoot, displayIntro, extendErrorCatalog, formatFlagHelp, formatLogMessage, formatStep, formatUpdateHint, generateFlagsHelp, getExecCommand, getFlagValue, getInstallCommand, getRunCommand, getUpdateCommand, globalFlags, hasFlag, isCliError, isVersionRequest, logMessage, parseCliArgs, parseSubcommand, printVersionInfo, showHelpMenu, startBackgroundUpdateCheck, validLogLevels, withErrorHandling, withSpinner };
|