hexbus 0.1.1 → 0.2.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/dist/index.d.mts +33 -9
- package/dist/index.mjs +39 -14
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -358,6 +358,14 @@ interface Telemetry {
|
|
|
358
358
|
* Sends queued events when possible and clears the local queue.
|
|
359
359
|
*/
|
|
360
360
|
flush(): Promise<void>;
|
|
361
|
+
/**
|
|
362
|
+
* Starts an immediate best-effort flush without requiring callers to await it.
|
|
363
|
+
*
|
|
364
|
+
* @remarks
|
|
365
|
+
* Entrypoints can call this before short-lived process exits. Implementations
|
|
366
|
+
* should not throw from this method.
|
|
367
|
+
*/
|
|
368
|
+
flushBackground(): void;
|
|
361
369
|
/**
|
|
362
370
|
* Performs final telemetry cleanup before process exit.
|
|
363
371
|
*/
|
|
@@ -463,6 +471,14 @@ interface CreateContextOptions<TPackage extends string = string> {
|
|
|
463
471
|
* Top-level commands used to identify `commandName` during argument parsing.
|
|
464
472
|
*/
|
|
465
473
|
commands: CliCommand[];
|
|
474
|
+
/**
|
|
475
|
+
* Global flags parsed before command execution.
|
|
476
|
+
*
|
|
477
|
+
* @remarks
|
|
478
|
+
* Pass this when a product CLI needs extra global flags while keeping Hexbus'
|
|
479
|
+
* parser and context bootstrap.
|
|
480
|
+
*/
|
|
481
|
+
globalFlags?: CliFlag[];
|
|
466
482
|
/**
|
|
467
483
|
* Application name used for config lookup, telemetry defaults, and logger
|
|
468
484
|
* metadata.
|
|
@@ -1101,16 +1117,21 @@ declare function createCliLogger(level?: LogLevel): CliLogger;
|
|
|
1101
1117
|
declare const globalFlags: CliFlag[];
|
|
1102
1118
|
/**
|
|
1103
1119
|
* Parses raw command-line arguments into command name, command args, and
|
|
1104
|
-
* global flags.
|
|
1120
|
+
* global and caller-provided flags.
|
|
1105
1121
|
*
|
|
1106
1122
|
* @remarks
|
|
1107
|
-
*
|
|
1108
|
-
* positional arguments are preserved as command arguments
|
|
1109
|
-
* positional argument matches a registered top-level command
|
|
1123
|
+
* Flags declared in `globalFlags` and `flags` are parsed. Unknown flags and
|
|
1124
|
+
* other positional arguments are preserved as command arguments. If the first
|
|
1125
|
+
* positional argument matches a registered top-level command in `commands`, it
|
|
1126
|
+
* is returned as `commandName` and removed from `commandArgs`.
|
|
1110
1127
|
*
|
|
1111
1128
|
* @param rawArgs - Arguments after the executable and script path.
|
|
1112
1129
|
* @param commands - Top-level commands used to identify the command name.
|
|
1113
|
-
* @
|
|
1130
|
+
* @param flags - Additional global flag definitions to parse alongside
|
|
1131
|
+
* `globalFlags`. Caller-provided flags override built-in definitions that use
|
|
1132
|
+
* the same primary flag name.
|
|
1133
|
+
* @returns Normalized parsed output containing the matched command name,
|
|
1134
|
+
* remaining command arguments, and parsed flag values keyed by primary name.
|
|
1114
1135
|
*
|
|
1115
1136
|
* @example
|
|
1116
1137
|
* ```ts
|
|
@@ -1119,7 +1140,7 @@ declare const globalFlags: CliFlag[];
|
|
|
1119
1140
|
* // parsed.parsedFlags.logger === 'debug'
|
|
1120
1141
|
* ```
|
|
1121
1142
|
*/
|
|
1122
|
-
declare function parseCliArgs(rawArgs: string[], commands: CliCommand[]): ParsedArgs;
|
|
1143
|
+
declare function parseCliArgs(rawArgs: string[], commands: CliCommand[], flags?: CliFlag[]): ParsedArgs;
|
|
1123
1144
|
/**
|
|
1124
1145
|
* Formats a single flag for display in help output.
|
|
1125
1146
|
*
|
|
@@ -1128,11 +1149,14 @@ declare function parseCliArgs(rawArgs: string[], commands: CliCommand[]): Parsed
|
|
|
1128
1149
|
*/
|
|
1129
1150
|
declare function formatFlagHelp(flag: CliFlag): string;
|
|
1130
1151
|
/**
|
|
1131
|
-
* Formats
|
|
1152
|
+
* Formats a provided set of flags for help output.
|
|
1153
|
+
*
|
|
1154
|
+
* @param flags - Flag definitions to render with `formatFlagHelp`, defaulting
|
|
1155
|
+
* to `globalFlags`.
|
|
1132
1156
|
*
|
|
1133
|
-
* @returns Newline-delimited help rows for
|
|
1157
|
+
* @returns Newline-delimited help rows for the provided flag definitions.
|
|
1134
1158
|
*/
|
|
1135
|
-
declare function generateFlagsHelp(): string;
|
|
1159
|
+
declare function generateFlagsHelp(flags?: CliFlag[]): string;
|
|
1136
1160
|
/**
|
|
1137
1161
|
* Checks whether a parsed boolean flag is enabled.
|
|
1138
1162
|
*
|
package/dist/index.mjs
CHANGED
|
@@ -794,18 +794,35 @@ function getPrimaryFlagName(flag) {
|
|
|
794
794
|
const fallback = flag.names.reduce((longest, name) => name.length > longest.length ? name : longest, "");
|
|
795
795
|
return (longName ?? fallback).replace(/^--?/, "");
|
|
796
796
|
}
|
|
797
|
+
function mergeFlags(flags) {
|
|
798
|
+
const merged = /* @__PURE__ */ new Map();
|
|
799
|
+
for (const flag of globalFlags) {
|
|
800
|
+
const primaryName = getPrimaryFlagName(flag);
|
|
801
|
+
if (primaryName) merged.set(primaryName, flag);
|
|
802
|
+
}
|
|
803
|
+
for (const flag of flags) {
|
|
804
|
+
const primaryName = getPrimaryFlagName(flag);
|
|
805
|
+
if (primaryName) merged.set(primaryName, flag);
|
|
806
|
+
}
|
|
807
|
+
return [...merged.values()];
|
|
808
|
+
}
|
|
797
809
|
/**
|
|
798
810
|
* Parses raw command-line arguments into command name, command args, and
|
|
799
|
-
* global flags.
|
|
811
|
+
* global and caller-provided flags.
|
|
800
812
|
*
|
|
801
813
|
* @remarks
|
|
802
|
-
*
|
|
803
|
-
* positional arguments are preserved as command arguments
|
|
804
|
-
* positional argument matches a registered top-level command
|
|
814
|
+
* Flags declared in `globalFlags` and `flags` are parsed. Unknown flags and
|
|
815
|
+
* other positional arguments are preserved as command arguments. If the first
|
|
816
|
+
* positional argument matches a registered top-level command in `commands`, it
|
|
817
|
+
* is returned as `commandName` and removed from `commandArgs`.
|
|
805
818
|
*
|
|
806
819
|
* @param rawArgs - Arguments after the executable and script path.
|
|
807
820
|
* @param commands - Top-level commands used to identify the command name.
|
|
808
|
-
* @
|
|
821
|
+
* @param flags - Additional global flag definitions to parse alongside
|
|
822
|
+
* `globalFlags`. Caller-provided flags override built-in definitions that use
|
|
823
|
+
* the same primary flag name.
|
|
824
|
+
* @returns Normalized parsed output containing the matched command name,
|
|
825
|
+
* remaining command arguments, and parsed flag values keyed by primary name.
|
|
809
826
|
*
|
|
810
827
|
* @example
|
|
811
828
|
* ```ts
|
|
@@ -814,13 +831,14 @@ function getPrimaryFlagName(flag) {
|
|
|
814
831
|
* // parsed.parsedFlags.logger === 'debug'
|
|
815
832
|
* ```
|
|
816
833
|
*/
|
|
817
|
-
function parseCliArgs(rawArgs, commands) {
|
|
834
|
+
function parseCliArgs(rawArgs, commands, flags = []) {
|
|
835
|
+
const mergedFlags = mergeFlags(flags);
|
|
818
836
|
const parsedFlags = {};
|
|
819
837
|
const potentialCommandArgs = [];
|
|
820
838
|
let commandName;
|
|
821
839
|
const commandArgs = [];
|
|
822
|
-
const knownFlagSet = new Set(
|
|
823
|
-
for (const flag of
|
|
840
|
+
const knownFlagSet = new Set(mergedFlags.flatMap((flag) => flag.names));
|
|
841
|
+
for (const flag of mergedFlags) {
|
|
824
842
|
const primaryName = getPrimaryFlagName(flag);
|
|
825
843
|
if (!primaryName) continue;
|
|
826
844
|
if (flag.type === "boolean") parsedFlags[primaryName] = flag.defaultValue ?? false;
|
|
@@ -830,7 +848,7 @@ function parseCliArgs(rawArgs, commands) {
|
|
|
830
848
|
const arg = rawArgs[i];
|
|
831
849
|
if (typeof arg !== "string") continue;
|
|
832
850
|
let isFlag = false;
|
|
833
|
-
for (const flag of
|
|
851
|
+
for (const flag of mergedFlags) {
|
|
834
852
|
if (!flag.names.includes(arg)) continue;
|
|
835
853
|
const primaryName = getPrimaryFlagName(flag);
|
|
836
854
|
if (!primaryName) continue;
|
|
@@ -868,12 +886,15 @@ function formatFlagHelp(flag) {
|
|
|
868
886
|
return ` ${flag.names.join(", ")}${flag.expectsValue ? " <value>" : ""}\t${flag.description}`;
|
|
869
887
|
}
|
|
870
888
|
/**
|
|
871
|
-
* Formats
|
|
889
|
+
* Formats a provided set of flags for help output.
|
|
872
890
|
*
|
|
873
|
-
* @
|
|
891
|
+
* @param flags - Flag definitions to render with `formatFlagHelp`, defaulting
|
|
892
|
+
* to `globalFlags`.
|
|
893
|
+
*
|
|
894
|
+
* @returns Newline-delimited help rows for the provided flag definitions.
|
|
874
895
|
*/
|
|
875
|
-
function generateFlagsHelp() {
|
|
876
|
-
return
|
|
896
|
+
function generateFlagsHelp(flags = globalFlags) {
|
|
897
|
+
return flags.map(formatFlagHelp).join("\n");
|
|
877
898
|
}
|
|
878
899
|
/**
|
|
879
900
|
* Checks whether a parsed boolean flag is enabled.
|
|
@@ -964,6 +985,7 @@ function isEnvDisabled(prefix) {
|
|
|
964
985
|
function createDisabledTelemetry() {
|
|
965
986
|
return {
|
|
966
987
|
flush: async () => {},
|
|
988
|
+
flushBackground: () => {},
|
|
967
989
|
isDisabled: () => true,
|
|
968
990
|
shutdown: async () => {},
|
|
969
991
|
trackCommand: () => {},
|
|
@@ -1020,6 +1042,9 @@ function createTelemetry(options = {}) {
|
|
|
1020
1042
|
};
|
|
1021
1043
|
return {
|
|
1022
1044
|
flush,
|
|
1045
|
+
flushBackground() {
|
|
1046
|
+
flush();
|
|
1047
|
+
},
|
|
1023
1048
|
isDisabled() {
|
|
1024
1049
|
return false;
|
|
1025
1050
|
},
|
|
@@ -1139,7 +1164,7 @@ function createFileSystem(cwd) {
|
|
|
1139
1164
|
async function createCliContext(options) {
|
|
1140
1165
|
const cwd = options.cwd ?? process.cwd();
|
|
1141
1166
|
const appName = options.appName ?? "cli";
|
|
1142
|
-
const { commandName, commandArgs, parsedFlags } = parseCliArgs(options.rawArgs, options.commands);
|
|
1167
|
+
const { commandName, commandArgs, parsedFlags } = parseCliArgs(options.rawArgs, options.commands, options.globalFlags);
|
|
1143
1168
|
const logger = createCliLogger(getLogLevel(parsedFlags));
|
|
1144
1169
|
const projectRoot = await detectProjectRoot(cwd, logger);
|
|
1145
1170
|
const fsUtils = createFileSystem(projectRoot);
|