cli-api 0.2.0 → 0.2.1
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
CHANGED
|
@@ -18,17 +18,20 @@ const world = new Command('world')
|
|
|
18
18
|
description: 'Person you want to greet',
|
|
19
19
|
required: true,
|
|
20
20
|
})
|
|
21
|
-
.run(
|
|
22
|
-
if (
|
|
21
|
+
.run(opts => {
|
|
22
|
+
if (opts.verbose) {
|
|
23
23
|
console.log('Preparing greeting...')
|
|
24
24
|
}
|
|
25
|
-
console.log(`Hello ${
|
|
25
|
+
console.log(`Hello ${opts.name}`)
|
|
26
26
|
})
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
.meta({version: pkg.version,
|
|
28
|
+
const app = new App('hello')
|
|
29
|
+
.meta({version: pkg.version, bin: pkg.name})
|
|
30
30
|
.command(world)
|
|
31
|
-
|
|
31
|
+
|
|
32
|
+
if(import.meta.main) {
|
|
33
|
+
await app.execute()
|
|
34
|
+
}
|
|
32
35
|
```
|
|
33
36
|
|
|
34
37
|
```shell
|
package/dist/index.d.mts
CHANGED
|
@@ -123,7 +123,7 @@ interface ExecutableInput<Opts extends readonly Option[] | undefined, Flags exte
|
|
|
123
123
|
options?: Opts;
|
|
124
124
|
flags?: Flags;
|
|
125
125
|
positonals?: As;
|
|
126
|
-
execute(opts: OptsOf<Opts, Flags, As>,
|
|
126
|
+
execute(opts: OptsOf<Opts, Flags, As>, context: ExecutionContext): MaybePromise<number | void>;
|
|
127
127
|
}
|
|
128
128
|
interface LeafCommandInput<Opts extends readonly Option[] | undefined, Flags extends readonly Flag[] | undefined, As extends readonly Argument[] | undefined> extends CommandBase, ExecutableInput<Opts, Flags, As> {
|
|
129
129
|
subCommands?: never;
|
|
@@ -179,7 +179,7 @@ interface AnyLeafCommand extends CommandBase {
|
|
|
179
179
|
options?: readonly Option[] | undefined;
|
|
180
180
|
flags?: readonly Flag[] | undefined;
|
|
181
181
|
positonals?: readonly Argument[] | undefined;
|
|
182
|
-
execute(opts: Record<string, any>,
|
|
182
|
+
execute(opts: Record<string, any>, context: ExecutionContext): MaybePromise<number | void>;
|
|
183
183
|
subCommands?: never | undefined;
|
|
184
184
|
}
|
|
185
185
|
interface AnyBranchCommand extends CommandBase {
|
|
@@ -202,7 +202,7 @@ type BuildOption<Name extends string, Config extends OptionConfigInput | undefin
|
|
|
202
202
|
type BuildArgument<Name extends string, Config extends ArgumentConfigInput | undefined> = Flatten<{
|
|
203
203
|
name: Name;
|
|
204
204
|
} & (Config extends undefined ? {} : Config)>;
|
|
205
|
-
type RunHandler<Opts extends readonly Option[] | undefined, Flags extends readonly Flag[] | undefined, As extends readonly Argument[] | undefined> = (
|
|
205
|
+
type RunHandler<Opts extends readonly Option[] | undefined, Flags extends readonly Flag[] | undefined, As extends readonly Argument[] | undefined> = (opts: OptsOf<Opts, Flags, As>, context: ExecutionContext) => MaybePromise<number | void>;
|
|
206
206
|
type ExecuteHandler<Opts extends readonly Option[] | undefined, Flags extends readonly Flag[] | undefined, As extends readonly Argument[] | undefined> = ExecutableInput<Opts, Flags, As>['execute'];
|
|
207
207
|
type AppMetaConfig = {
|
|
208
208
|
bin?: string;
|
|
@@ -211,6 +211,24 @@ type AppMetaConfig = {
|
|
|
211
211
|
description?: string;
|
|
212
212
|
longDescription?: string;
|
|
213
213
|
};
|
|
214
|
+
interface BuiltinEntryConfig {
|
|
215
|
+
/** Override the built-in command and option name. */
|
|
216
|
+
name?: string;
|
|
217
|
+
/** Override the built-in command and option aliases. */
|
|
218
|
+
alias?: string | string[];
|
|
219
|
+
/** Disable the built-in command entry. */
|
|
220
|
+
disableCommand?: boolean;
|
|
221
|
+
/** Disable the built-in global option entry. */
|
|
222
|
+
disableOption?: boolean;
|
|
223
|
+
}
|
|
224
|
+
interface BuiltinOptionConfig {
|
|
225
|
+
/** Override the built-in option name. */
|
|
226
|
+
name?: string;
|
|
227
|
+
/** Override the built-in option aliases. */
|
|
228
|
+
alias?: string | string[];
|
|
229
|
+
/** Disable the built-in global option entry. */
|
|
230
|
+
disableOption?: boolean;
|
|
231
|
+
}
|
|
214
232
|
declare class Command<Opts extends readonly Option[] = [], Flags extends readonly Flag[] = [], As extends readonly Argument[] = [], Cs extends CommandChildren = [], Executable extends boolean = false> {
|
|
215
233
|
private readonly _name;
|
|
216
234
|
private _alias?;
|
|
@@ -279,7 +297,7 @@ declare class Command<Opts extends readonly Option[] = [], Flags extends readonl
|
|
|
279
297
|
/**
|
|
280
298
|
* Marks this command as executable and registers the handler invoked after parsing.
|
|
281
299
|
*
|
|
282
|
-
* @param handler The function that receives parsed positional arguments
|
|
300
|
+
* @param handler The function that receives parsed option values, including positional arguments by name, and the current [`ExecutionContext`]{@link ExecutionContext}.
|
|
283
301
|
* @returns A fluent command builder that is now treated as executable.
|
|
284
302
|
*/
|
|
285
303
|
run(this: Command<Opts, Flags, As, [], false>, handler: RunHandler<Opts, Flags, As>): Command<Opts, Flags, As, [], true>;
|
|
@@ -293,6 +311,12 @@ declare class App<Opts extends readonly Option[] = [], Flags extends readonly Fl
|
|
|
293
311
|
_author?: string;
|
|
294
312
|
/** @internal */
|
|
295
313
|
_globalOptions?: Option[];
|
|
314
|
+
/** @internal */
|
|
315
|
+
_helpConfig?: BuiltinEntryConfig;
|
|
316
|
+
/** @internal */
|
|
317
|
+
_versionConfig?: BuiltinEntryConfig;
|
|
318
|
+
/** @internal */
|
|
319
|
+
_colorConfig?: BuiltinOptionConfig;
|
|
296
320
|
/**
|
|
297
321
|
* Applies metadata to the root app in one call.
|
|
298
322
|
*
|
|
@@ -308,12 +332,33 @@ declare class App<Opts extends readonly Option[] = [], Flags extends readonly Fl
|
|
|
308
332
|
*/
|
|
309
333
|
bin(binaryName: string): this;
|
|
310
334
|
/**
|
|
311
|
-
* Sets the application version surfaced by the built-in version command.
|
|
335
|
+
* Sets the application version surfaced by the built-in version command and option.
|
|
312
336
|
*
|
|
313
337
|
* @param version The version string to display.
|
|
314
|
-
* @returns The same fluent app builder with the version applied.
|
|
338
|
+
* @returns The same fluent app builder with the version text applied.
|
|
315
339
|
*/
|
|
316
340
|
version(version: string): this;
|
|
341
|
+
/**
|
|
342
|
+
* Configures the built-in version command and global option.
|
|
343
|
+
*
|
|
344
|
+
* @param config Built-in version settings such as the displayed name, aliases, and whether the command or option should be disabled.
|
|
345
|
+
* @returns The same fluent app builder with the version command and option configuration applied.
|
|
346
|
+
*/
|
|
347
|
+
version(config: BuiltinEntryConfig): this;
|
|
348
|
+
/**
|
|
349
|
+
* Configures the built-in help command and global option.
|
|
350
|
+
*
|
|
351
|
+
* @param config Built-in help settings such as the displayed name, aliases, and whether the command or option should be disabled.
|
|
352
|
+
* @returns The same fluent app builder with the help command and option configuration applied.
|
|
353
|
+
*/
|
|
354
|
+
help(config: BuiltinEntryConfig): this;
|
|
355
|
+
/**
|
|
356
|
+
* Configures the built-in color global option.
|
|
357
|
+
*
|
|
358
|
+
* @param config Built-in color option settings such as the displayed name, aliases, and whether the option should be disabled.
|
|
359
|
+
* @returns The same fluent app builder with the color option configuration applied.
|
|
360
|
+
*/
|
|
361
|
+
color(config: BuiltinOptionConfig): this;
|
|
317
362
|
/**
|
|
318
363
|
* Sets the application author surfaced by root help output.
|
|
319
364
|
*
|
|
@@ -378,7 +423,7 @@ declare class App<Opts extends readonly Option[] = [], Flags extends readonly Fl
|
|
|
378
423
|
/**
|
|
379
424
|
* Marks the root app as executable by registering the handler invoked after parsing.
|
|
380
425
|
*
|
|
381
|
-
* @param handler The function that receives parsed
|
|
426
|
+
* @param handler The function that receives parsed option values including custom global options and positional arguments by name, and the current [`ExecutionContext`]{@link ExecutionContext}.
|
|
382
427
|
* @returns A fluent app builder that is now treated as executable.
|
|
383
428
|
*/
|
|
384
429
|
run(this: App<Opts, Flags, As, [], false>, handler: RunHandler<Opts, Flags, As>): App<Opts, Flags, As, [], true>;
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,93 @@
|
|
|
1
|
-
import { Chalk
|
|
1
|
+
import { Chalk } from "chalk";
|
|
2
|
+
import os from "os";
|
|
3
|
+
import process$1 from "process";
|
|
2
4
|
|
|
5
|
+
//#region src/supports-color.ts
|
|
6
|
+
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process$1.argv) {
|
|
7
|
+
const prefix = flag.startsWith("-") ? "" : flag.length === 1 ? "-" : "--";
|
|
8
|
+
const position = argv.indexOf(prefix + flag);
|
|
9
|
+
const terminatorPosition = argv.indexOf("--");
|
|
10
|
+
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
|
11
|
+
}
|
|
12
|
+
const { env } = process$1;
|
|
13
|
+
let flagForceColor;
|
|
14
|
+
if (hasFlag("no-color") || hasFlag("no-colors") || hasFlag("color=false") || hasFlag("color=never")) flagForceColor = 0;
|
|
15
|
+
else if (hasFlag("color") || hasFlag("colors") || hasFlag("color=true") || hasFlag("color=always")) flagForceColor = 1;
|
|
16
|
+
function envForceColor() {
|
|
17
|
+
if ("FORCE_COLOR" in env) {
|
|
18
|
+
const forceColor = env.FORCE_COLOR ?? "";
|
|
19
|
+
if (forceColor === "true") return 1;
|
|
20
|
+
if (forceColor === "false") return 0;
|
|
21
|
+
return forceColor.length === 0 ? 1 : Math.min(Number.parseInt(forceColor, 10), 3);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
function translateLevel(level) {
|
|
25
|
+
if (level === 0) return false;
|
|
26
|
+
return {
|
|
27
|
+
level,
|
|
28
|
+
hasBasic: true,
|
|
29
|
+
has256: level >= 2,
|
|
30
|
+
has16m: level >= 3
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
function supportsColorInternal(haveStream, { streamIsTTY, sniffFlags = true } = {}) {
|
|
34
|
+
const noFlagForceColor = envForceColor();
|
|
35
|
+
if (noFlagForceColor !== void 0) flagForceColor = noFlagForceColor === 0 ? 0 : 1;
|
|
36
|
+
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
|
37
|
+
if (forceColor === 0) return 0;
|
|
38
|
+
if (sniffFlags) {
|
|
39
|
+
if (hasFlag("color=16m") || hasFlag("color=full") || hasFlag("color=truecolor")) return 3;
|
|
40
|
+
if (hasFlag("color=256")) return 2;
|
|
41
|
+
}
|
|
42
|
+
if ("TF_BUILD" in env && "AGENT_NAME" in env) return 1;
|
|
43
|
+
if (haveStream && !streamIsTTY && forceColor === void 0) return 0;
|
|
44
|
+
const min = forceColor || 0;
|
|
45
|
+
if ((env.TERM ?? "") === "dumb") return min;
|
|
46
|
+
if (process$1.platform === "win32") {
|
|
47
|
+
const osRelease = os.release().split(".");
|
|
48
|
+
if (Number(osRelease[0]) >= 10 && Number(osRelease[2]) >= 10586) return Number(osRelease[2]) >= 14931 ? 3 : 2;
|
|
49
|
+
return 1;
|
|
50
|
+
}
|
|
51
|
+
if ("CI" in env) {
|
|
52
|
+
if ([
|
|
53
|
+
"GITHUB_ACTIONS",
|
|
54
|
+
"GITEA_ACTIONS",
|
|
55
|
+
"CIRCLECI"
|
|
56
|
+
].some((key) => key in env)) return 3;
|
|
57
|
+
if ([
|
|
58
|
+
"TRAVIS",
|
|
59
|
+
"APPVEYOR",
|
|
60
|
+
"GITLAB_CI",
|
|
61
|
+
"BUILDKITE",
|
|
62
|
+
"DRONE"
|
|
63
|
+
].some((sign) => sign in env) || env.CI_NAME === "codeship") return 1;
|
|
64
|
+
return min;
|
|
65
|
+
}
|
|
66
|
+
if ("TEAMCITY_VERSION" in env) return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION ?? "") ? 1 : 0;
|
|
67
|
+
if ((env.COLORTERM ?? "") === "truecolor") return 3;
|
|
68
|
+
if ((env.TERM ?? "") === "xterm-kitty" || (env.TERM ?? "") === "xterm-ghostty" || (env.TERM ?? "") === "wezterm") return 3;
|
|
69
|
+
if ("TERM_PROGRAM" in env) {
|
|
70
|
+
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || "").split(".")[0], 10);
|
|
71
|
+
switch (env.TERM_PROGRAM) {
|
|
72
|
+
case "iTerm.app": return version >= 3 ? 3 : 2;
|
|
73
|
+
case "Apple_Terminal": return 2;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (/-256(color)?$/i.test(env.TERM ?? "")) return 2;
|
|
77
|
+
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM ?? "")) return 1;
|
|
78
|
+
if ("COLORTERM" in env) return 1;
|
|
79
|
+
return min;
|
|
80
|
+
}
|
|
81
|
+
function createSupportsColor(stream, options = {}) {
|
|
82
|
+
const internalOptions = { ...options };
|
|
83
|
+
if (stream?.isTTY !== void 0) internalOptions.streamIsTTY = stream.isTTY;
|
|
84
|
+
return translateLevel(supportsColorInternal(Boolean(stream), internalOptions));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
//#endregion
|
|
3
88
|
//#region src/color.ts
|
|
4
|
-
const
|
|
89
|
+
const defaultColorSupport = createSupportsColor(process.stdout, { sniffFlags: false });
|
|
90
|
+
const DEFAULT_COLOR_LEVEL = defaultColorSupport ? defaultColorSupport.level : 0;
|
|
5
91
|
function getColorLevel(mode) {
|
|
6
92
|
if (mode === "always") return 3;
|
|
7
93
|
if (mode === "never") return 0;
|
|
@@ -201,12 +287,12 @@ var Command = class {
|
|
|
201
287
|
/**
|
|
202
288
|
* Marks this command as executable and registers the handler invoked after parsing.
|
|
203
289
|
*
|
|
204
|
-
* @param handler The function that receives parsed positional arguments
|
|
290
|
+
* @param handler The function that receives parsed option values, including positional arguments by name, and the current [`ExecutionContext`]{@link ExecutionContext}.
|
|
205
291
|
* @returns A fluent command builder that is now treated as executable.
|
|
206
292
|
*/
|
|
207
293
|
run(handler) {
|
|
208
|
-
this._handler = function(opts,
|
|
209
|
-
return handler(
|
|
294
|
+
this._handler = function(opts, context) {
|
|
295
|
+
return handler(opts, context);
|
|
210
296
|
};
|
|
211
297
|
return this;
|
|
212
298
|
}
|
|
@@ -220,6 +306,12 @@ var App = class extends Command {
|
|
|
220
306
|
_author;
|
|
221
307
|
/** @internal */
|
|
222
308
|
_globalOptions;
|
|
309
|
+
/** @internal */
|
|
310
|
+
_helpConfig;
|
|
311
|
+
/** @internal */
|
|
312
|
+
_versionConfig;
|
|
313
|
+
/** @internal */
|
|
314
|
+
_colorConfig;
|
|
223
315
|
/**
|
|
224
316
|
* Applies metadata to the root app in one call.
|
|
225
317
|
*
|
|
@@ -244,14 +336,41 @@ var App = class extends Command {
|
|
|
244
336
|
this._bin = binaryName;
|
|
245
337
|
return this;
|
|
246
338
|
}
|
|
339
|
+
version(versionOrConfig) {
|
|
340
|
+
if (typeof versionOrConfig === "string") {
|
|
341
|
+
this._version = versionOrConfig;
|
|
342
|
+
return this;
|
|
343
|
+
}
|
|
344
|
+
this._versionConfig = {
|
|
345
|
+
...this._versionConfig ?? {},
|
|
346
|
+
...versionOrConfig
|
|
347
|
+
};
|
|
348
|
+
return this;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Configures the built-in help command and global option.
|
|
352
|
+
*
|
|
353
|
+
* @param config Built-in help settings such as the displayed name, aliases, and whether the command or option should be disabled.
|
|
354
|
+
* @returns The same fluent app builder with the help command and option configuration applied.
|
|
355
|
+
*/
|
|
356
|
+
help(config) {
|
|
357
|
+
this._helpConfig = {
|
|
358
|
+
...this._helpConfig ?? {},
|
|
359
|
+
...config
|
|
360
|
+
};
|
|
361
|
+
return this;
|
|
362
|
+
}
|
|
247
363
|
/**
|
|
248
|
-
*
|
|
364
|
+
* Configures the built-in color global option.
|
|
249
365
|
*
|
|
250
|
-
* @param
|
|
251
|
-
* @returns The same fluent app builder with the
|
|
366
|
+
* @param config Built-in color option settings such as the displayed name, aliases, and whether the option should be disabled.
|
|
367
|
+
* @returns The same fluent app builder with the color option configuration applied.
|
|
252
368
|
*/
|
|
253
|
-
|
|
254
|
-
this.
|
|
369
|
+
color(config) {
|
|
370
|
+
this._colorConfig = {
|
|
371
|
+
...this._colorConfig ?? {},
|
|
372
|
+
...config
|
|
373
|
+
};
|
|
255
374
|
return this;
|
|
256
375
|
}
|
|
257
376
|
/**
|
|
@@ -339,7 +458,7 @@ var App = class extends Command {
|
|
|
339
458
|
/**
|
|
340
459
|
* Marks the root app as executable by registering the handler invoked after parsing.
|
|
341
460
|
*
|
|
342
|
-
* @param handler The function that receives parsed
|
|
461
|
+
* @param handler The function that receives parsed option values including custom global options and positional arguments by name, and the current [`ExecutionContext`]{@link ExecutionContext}.
|
|
343
462
|
* @returns A fluent app builder that is now treated as executable.
|
|
344
463
|
*/
|
|
345
464
|
run(handler) {
|
|
@@ -352,7 +471,7 @@ var App = class extends Command {
|
|
|
352
471
|
* @returns The numeric exit code returned by the resolved command handler, or `0` when the handler does not return one.
|
|
353
472
|
*/
|
|
354
473
|
async execute(args = process.argv.slice(2)) {
|
|
355
|
-
const { executeApp } = await import("./run-
|
|
474
|
+
const { executeApp } = await import("./run-DEkBUX4b.mjs");
|
|
356
475
|
const code = await executeApp(this, args);
|
|
357
476
|
process.exitCode = code;
|
|
358
477
|
return code;
|