cli-api 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.
Files changed (43) hide show
  1. package/README.md +22 -25
  2. package/dist/index.d.mts +394 -0
  3. package/dist/index.mjs +3 -0
  4. package/dist/interfaces-COq24bNI.mjs +391 -0
  5. package/dist/run-C903J5ca.mjs +1137 -0
  6. package/package.json +37 -37
  7. package/.hgignore +0 -12
  8. package/.idea/$CACHE_FILE$ +0 -6
  9. package/.idea/clap.iml +0 -8
  10. package/.idea/codeStyles/codeStyleConfig.xml +0 -5
  11. package/.idea/deployment.xml +0 -63
  12. package/.idea/inspectionProfiles/Project_Default.xml +0 -10
  13. package/.idea/misc.xml +0 -6
  14. package/.idea/modules.xml +0 -8
  15. package/.idea/vcs.xml +0 -6
  16. package/Makefile +0 -14
  17. package/babel.config.json +0 -33
  18. package/dist/cjs/index.js +0 -588
  19. package/dist/cjs/index.js.map +0 -1
  20. package/dist/es/index.mjs +0 -578
  21. package/dist/es/index.mjs.map +0 -1
  22. package/dist/types/app-help.d.ts +0 -3
  23. package/dist/types/commands/command-help.d.ts +0 -2
  24. package/dist/types/commands/version.d.ts +0 -2
  25. package/dist/types/constants.d.ts +0 -4
  26. package/dist/types/index.d.ts +0 -3
  27. package/dist/types/interfaces.d.ts +0 -79
  28. package/dist/types/options.d.ts +0 -7
  29. package/dist/types/print-command-help.d.ts +0 -2
  30. package/dist/types/run.d.ts +0 -2
  31. package/dist/types/utils.d.ts +0 -17
  32. package/rollup.config.js +0 -44
  33. package/src/app-help.ts +0 -34
  34. package/src/commands/command-help.ts +0 -28
  35. package/src/commands/version.ts +0 -11
  36. package/src/constants.ts +0 -4
  37. package/src/index.ts +0 -3
  38. package/src/interfaces.ts +0 -89
  39. package/src/options.ts +0 -266
  40. package/src/print-command-help.ts +0 -78
  41. package/src/run.ts +0 -45
  42. package/src/utils.ts +0 -86
  43. package/tsconfig.json +0 -32
@@ -0,0 +1,391 @@
1
+ import { Chalk, supportsColor } from "chalk";
2
+
3
+ //#region src/color.ts
4
+ const DEFAULT_COLOR_LEVEL = supportsColor ? supportsColor.level : 0;
5
+ function getColorLevel(mode) {
6
+ if (mode === "always") return 3;
7
+ if (mode === "never") return 0;
8
+ return DEFAULT_COLOR_LEVEL;
9
+ }
10
+ function createChalk(mode = "auto") {
11
+ return new Chalk({ level: getColorLevel(mode) });
12
+ }
13
+
14
+ //#endregion
15
+ //#region src/interfaces.ts
16
+ let OptType = /* @__PURE__ */ function(OptType$1) {
17
+ OptType$1[OptType$1["STRING"] = 0] = "STRING";
18
+ OptType$1[OptType$1["BOOL"] = 1] = "BOOL";
19
+ OptType$1[OptType$1["INT"] = 2] = "INT";
20
+ OptType$1[OptType$1["FLOAT"] = 3] = "FLOAT";
21
+ /** A string, truncated and converted to lowercase. */
22
+ OptType$1[OptType$1["ENUM"] = 4] = "ENUM";
23
+ /** File must be readable. Single dash will be converted to STDIN. */
24
+ OptType$1[OptType$1["INPUT_FILE"] = 5] = "INPUT_FILE";
25
+ /** Directory must be readable. */
26
+ OptType$1[OptType$1["INPUT_DIRECTORY"] = 6] = "INPUT_DIRECTORY";
27
+ /** File's directory must exist and be writeable. Single dash will be converted to STDOUT. */
28
+ OptType$1[OptType$1["OUTPUT_FILE"] = 7] = "OUTPUT_FILE";
29
+ OptType$1[OptType$1["OUTPUT_DIRECTORY"] = 8] = "OUTPUT_DIRECTORY";
30
+ /** An empty or non-existent directory. */
31
+ OptType$1[OptType$1["EMPTY_DIRECTORY"] = 9] = "EMPTY_DIRECTORY";
32
+ return OptType$1;
33
+ }({});
34
+ /**
35
+ * Per-invocation execution state exposed to command handlers.
36
+ */
37
+ var ExecutionContext = class {
38
+ _app;
39
+ _commandPath;
40
+ _chalk;
41
+ /**
42
+ * Creates a context for a single CLI invocation.
43
+ *
44
+ * @param app The root app being executed.
45
+ * @param colorMode The resolved color mode for the current invocation.
46
+ * @param path The resolved command path for the current invocation.
47
+ */
48
+ constructor(app, colorMode = "auto", path = []) {
49
+ this._app = app;
50
+ this._commandPath = path;
51
+ this._chalk = createChalk(colorMode);
52
+ }
53
+ /**
54
+ * Gets the root app for the current invocation.
55
+ *
56
+ * @returns The app definition being executed.
57
+ */
58
+ get app() {
59
+ return this._app;
60
+ }
61
+ /**
62
+ * Gets the resolved command path for the current invocation.
63
+ *
64
+ * @returns The command names from the app root to the executing command.
65
+ */
66
+ get commandPath() {
67
+ return this._commandPath;
68
+ }
69
+ /**
70
+ * Gets the chalk instance configured for the current invocation.
71
+ *
72
+ * @returns The active chalk instance after built-in color flags such as `--color` and `--no-color` have been applied.
73
+ */
74
+ get chalk() {
75
+ return this._chalk;
76
+ }
77
+ /**
78
+ * Gets the resolved color support level configured for the current invocation.
79
+ *
80
+ * @returns The active color level from `0` to `3`.
81
+ */
82
+ get colorLevel() {
83
+ return this._chalk.level;
84
+ }
85
+ };
86
+ var Command = class {
87
+ _name;
88
+ _alias;
89
+ _description;
90
+ _longDescription;
91
+ _options;
92
+ _positonals;
93
+ _subCommands;
94
+ _handler;
95
+ constructor(name) {
96
+ this._name = name;
97
+ }
98
+ get name() {
99
+ return this._name;
100
+ }
101
+ get alias() {
102
+ return this._alias;
103
+ }
104
+ get description() {
105
+ return this._description;
106
+ }
107
+ get longDescription() {
108
+ return this._longDescription;
109
+ }
110
+ get options() {
111
+ return this._options;
112
+ }
113
+ get positonals() {
114
+ return this._positonals;
115
+ }
116
+ get subCommands() {
117
+ return this._subCommands;
118
+ }
119
+ get handler() {
120
+ return this._handler;
121
+ }
122
+ setLongDescription(longDescription) {
123
+ this._longDescription = longDescription;
124
+ }
125
+ /**
126
+ * Sets one or more aliases for this command.
127
+ *
128
+ * @param aliases Alternative names that should resolve to this command.
129
+ * @returns The same fluent command builder with the aliases applied.
130
+ */
131
+ aliases(...aliases) {
132
+ this._alias = aliases.length <= 1 ? aliases[0] : aliases;
133
+ return this;
134
+ }
135
+ /**
136
+ * Sets the short and long descriptions used in generated help output.
137
+ *
138
+ * @param description The one-line description shown in summaries.
139
+ * @param longDescription Additional help text shown in detailed command help.
140
+ * @returns The same fluent command builder with updated descriptions.
141
+ */
142
+ describe(description, longDescription) {
143
+ this._description = description;
144
+ if (longDescription !== void 0) this._longDescription = longDescription;
145
+ return this;
146
+ }
147
+ /**
148
+ * Adds a boolean flag to this command.
149
+ *
150
+ * @param name The CLI flag name without leading dashes.
151
+ * @param config Optional metadata such as aliases and descriptions.
152
+ * @returns A fluent command builder whose inferred option shape includes the new flag.
153
+ */
154
+ flag(name, config) {
155
+ (this._options ??= []).push({
156
+ name,
157
+ ...config ?? {},
158
+ valueNotRequired: true,
159
+ type: OptType.BOOL
160
+ });
161
+ return this;
162
+ }
163
+ /**
164
+ * Adds an option that accepts a value to this command.
165
+ *
166
+ * @param name The CLI option name without leading dashes.
167
+ * @param config Optional metadata such as aliases, descriptions, and coercion rules.
168
+ * @returns A fluent command builder whose inferred option shape includes the new option.
169
+ */
170
+ opt(name, config) {
171
+ (this._options ??= []).push({
172
+ name,
173
+ ...config ?? {}
174
+ });
175
+ return this;
176
+ }
177
+ /**
178
+ * Adds a positional argument to this command.
179
+ *
180
+ * @param name The positional argument name used in help output and inferred opts.
181
+ * @param config Optional metadata such as coercion and requiredness.
182
+ * @returns A fluent command builder whose inferred positional tuple includes the new argument.
183
+ */
184
+ arg(name, config) {
185
+ (this._positonals ??= []).push({
186
+ name,
187
+ ...config ?? {}
188
+ });
189
+ return this;
190
+ }
191
+ /**
192
+ * Adds a nested sub-command to this command.
193
+ *
194
+ * @param subCommand The child command to register.
195
+ * @returns A fluent command builder that is now treated as a branch command.
196
+ */
197
+ command(subCommand) {
198
+ (this._subCommands ??= []).push(subCommand);
199
+ return this;
200
+ }
201
+ /**
202
+ * Marks this command as executable and registers the handler invoked after parsing.
203
+ *
204
+ * @param handler The function that receives parsed positional arguments, parsed option values, and the current [`ExecutionContext`]{@link ExecutionContext}.
205
+ * @returns A fluent command builder that is now treated as executable.
206
+ */
207
+ run(handler) {
208
+ this._handler = function(opts, args, context) {
209
+ return handler(args, opts, context);
210
+ };
211
+ return this;
212
+ }
213
+ };
214
+ var App = class extends Command {
215
+ /** @internal */
216
+ _bin;
217
+ /** @internal */
218
+ _version;
219
+ /** @internal */
220
+ _author;
221
+ /** @internal */
222
+ _globalOptions;
223
+ /**
224
+ * Applies metadata to the root app in one call.
225
+ *
226
+ * @param config Metadata for the app, including version, author, argv0, and descriptions.
227
+ * @returns The same fluent app builder with the metadata applied.
228
+ */
229
+ meta(config) {
230
+ if (config.bin !== void 0) this.bin(config.bin);
231
+ if (config.version !== void 0) this.version(config.version);
232
+ if (config.author !== void 0) this.author(config.author);
233
+ if (config.description !== void 0) this.describe(config.description, config.longDescription);
234
+ else if (config.longDescription !== void 0) this.setLongDescription(config.longDescription);
235
+ return this;
236
+ }
237
+ /**
238
+ * Sets the program name shown in help and generated messages.
239
+ *
240
+ * @param binaryName The display name for the CLI binary.
241
+ * @returns The same fluent app builder with the updated program name.
242
+ */
243
+ bin(binaryName) {
244
+ this._bin = binaryName;
245
+ return this;
246
+ }
247
+ /**
248
+ * Sets the application version surfaced by the built-in version command.
249
+ *
250
+ * @param version The version string to display.
251
+ * @returns The same fluent app builder with the version applied.
252
+ */
253
+ version(version) {
254
+ this._version = version;
255
+ return this;
256
+ }
257
+ /**
258
+ * Sets the application author surfaced by root help output.
259
+ *
260
+ * @param author The author string to display in app help.
261
+ * @returns The same fluent app builder with the author applied.
262
+ */
263
+ author(author) {
264
+ this._author = author;
265
+ return this;
266
+ }
267
+ /**
268
+ * Adds one or more aliases for the root command while preserving the `App` builder type.
269
+ *
270
+ * @param aliases Alternative names that should resolve to the root app.
271
+ * @returns The same fluent app builder with the aliases applied.
272
+ */
273
+ aliases(...aliases) {
274
+ return super.aliases(...aliases);
275
+ }
276
+ /**
277
+ * Sets the short and long descriptions used in generated help output while preserving the `App` builder type.
278
+ *
279
+ * @param description The one-line description shown in summaries.
280
+ * @param longDescription Additional help text shown in detailed help.
281
+ * @returns The same fluent app builder with updated descriptions.
282
+ */
283
+ describe(description, longDescription) {
284
+ return super.describe(description, longDescription);
285
+ }
286
+ /**
287
+ * Adds a boolean flag to the root app while preserving the `App` builder type.
288
+ *
289
+ * @param name The CLI flag name without leading dashes.
290
+ * @param config Optional metadata such as aliases and descriptions.
291
+ * @returns A fluent app builder whose inferred option shape includes the new flag.
292
+ */
293
+ flag(name, config) {
294
+ return super.flag(name, config);
295
+ }
296
+ /**
297
+ * Adds a valued option to the root app while preserving the `App` builder type.
298
+ *
299
+ * @param name The CLI option name without leading dashes.
300
+ * @param config Optional metadata such as aliases, descriptions, and coercion rules.
301
+ * @returns A fluent app builder whose inferred option shape includes the new option.
302
+ */
303
+ opt(name, config) {
304
+ return super.opt(name, config);
305
+ }
306
+ /**
307
+ * Adds a valued option that is available to the root app and every sub-command.
308
+ *
309
+ * @param name The CLI option name without leading dashes.
310
+ * @param config Optional metadata such as aliases, descriptions, and coercion rules.
311
+ * @returns A fluent app builder whose global option shape includes the new option.
312
+ */
313
+ globalOpt(name, config) {
314
+ (this._globalOptions ??= []).push({
315
+ name,
316
+ ...config ?? {}
317
+ });
318
+ return this;
319
+ }
320
+ /**
321
+ * Adds a positional argument to the root app while preserving the `App` builder type.
322
+ *
323
+ * @param name The positional argument name used in help output and inferred opts.
324
+ * @param config Optional metadata such as coercion and requiredness.
325
+ * @returns A fluent app builder whose inferred positional tuple includes the new argument.
326
+ */
327
+ arg(name, config) {
328
+ return super.arg(name, config);
329
+ }
330
+ /**
331
+ * Adds a nested sub-command to the root app while preserving the `App` builder type.
332
+ *
333
+ * @param subCommand The child command to register.
334
+ * @returns A fluent app builder that is now treated as a branch app.
335
+ */
336
+ command(subCommand) {
337
+ return super.command(subCommand);
338
+ }
339
+ /**
340
+ * Marks the root app as executable by registering the handler invoked after parsing.
341
+ *
342
+ * @param handler The function that receives parsed positional arguments, parsed option values including custom global options, and the current [`ExecutionContext`]{@link ExecutionContext}.
343
+ * @returns A fluent app builder that is now treated as executable.
344
+ */
345
+ run(handler) {
346
+ return super.run(handler);
347
+ }
348
+ /**
349
+ * Parses CLI arguments and executes the matching root command or sub-command.
350
+ *
351
+ * @param args The raw CLI arguments to parse. Defaults to `process.argv.slice(2)`.
352
+ * @returns The numeric exit code returned by the resolved command handler, or `0` when the handler does not return one.
353
+ */
354
+ async execute(args = process.argv.slice(2)) {
355
+ const { executeApp } = await import("./run-C903J5ca.mjs");
356
+ const code = await executeApp(this, args);
357
+ process.exitCode = code;
358
+ return code;
359
+ }
360
+ };
361
+ /**
362
+ * Checks whether a command or app contains nested sub-commands.
363
+ *
364
+ * @param value The command or app to inspect.
365
+ * @returns `true` when the value has sub-commands.
366
+ */
367
+ function hasSubCommands(value) {
368
+ return Array.isArray(value.subCommands);
369
+ }
370
+ /**
371
+ * Checks whether a command or app can be executed directly.
372
+ *
373
+ * @param value The command or app to inspect.
374
+ * @returns `true` when the value has an executable handler.
375
+ */
376
+ function isExecutable(value) {
377
+ return typeof value.handler === "function" || Object.prototype.hasOwnProperty.call(value, "execute");
378
+ }
379
+ /**
380
+ * Resolves the executable handler for an object-style or fluent command/app.
381
+ *
382
+ * @param value The command or app to inspect.
383
+ * @returns The handler function when one exists, otherwise `undefined`.
384
+ */
385
+ function getExecuteHandler(value) {
386
+ if (typeof value.handler === "function") return value.handler;
387
+ if (Object.prototype.hasOwnProperty.call(value, "execute")) return value.execute;
388
+ }
389
+
390
+ //#endregion
391
+ export { getExecuteHandler as a, createChalk as c, OptType as i, Command as n, hasSubCommands as o, ExecutionContext as r, isExecutable as s, App as t };