@reliverse/rempts 1.7.3 → 1.7.4
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
|
@@ -94,7 +94,7 @@ import {
|
|
|
94
94
|
|
|
95
95
|
### Notices
|
|
96
96
|
|
|
97
|
-
- `setup`/`cleanup` are now `
|
|
97
|
+
- `setup`/`cleanup` are now `onCmdInit`/`onCmdExit` (old names still work for now).
|
|
98
98
|
|
|
99
99
|
### Prompts Usage Example
|
|
100
100
|
|
|
@@ -158,10 +158,10 @@ const main = defineCommand({
|
|
|
158
158
|
version: "1.0.0",
|
|
159
159
|
description: "Rempts Launcher Playground CLI",
|
|
160
160
|
},
|
|
161
|
-
|
|
161
|
+
onCmdInit() {
|
|
162
162
|
relinka("success", "Setup");
|
|
163
163
|
},
|
|
164
|
-
|
|
164
|
+
onCmdExit() {
|
|
165
165
|
relinka("success", "Cleanup");
|
|
166
166
|
},
|
|
167
167
|
commands: {
|
|
@@ -547,18 +547,18 @@ Finally, a full-featured CLI launcher without the ceremony. `@reliverse/rempts`'
|
|
|
547
547
|
- **Lifecycle Hooks:**
|
|
548
548
|
You can define optional lifecycle hooks in your main command:
|
|
549
549
|
- `onLauncherStart` and `onLauncherEnd` (global, called once per CLI process)
|
|
550
|
-
- `
|
|
550
|
+
- `onCmdInit` and `onCmdExit` (per-command, called before/after each command, but NOT for the main `run()` handler)
|
|
551
551
|
|
|
552
552
|
**Global Hooks:**
|
|
553
553
|
- `onLauncherStart`: Called once, before any command/run() is executed.
|
|
554
554
|
- `onLauncherEnd`: Called once, after all command/run() logic is finished (even if an error occurs).
|
|
555
555
|
|
|
556
556
|
**Per-Command Hooks:**
|
|
557
|
-
- `
|
|
558
|
-
- `
|
|
557
|
+
- `onCmdInit`: Called before each command (not for main `run()`).
|
|
558
|
+
- `onCmdExit`: Called after each command (not for main `run()`).
|
|
559
559
|
|
|
560
560
|
This means:
|
|
561
|
-
- If your CLI has multiple commands, `
|
|
561
|
+
- If your CLI has multiple commands, `onCmdInit` and `onCmdExit` will be called for each command invocation, not just once for the whole CLI process.
|
|
562
562
|
- If your main command has a `run()` handler (and no command is invoked), these hooks are **not** called; use the `run()` handler itself or the global hooks for such logic.
|
|
563
563
|
- This allows you to perform setup/teardown logic specific to each command execution.
|
|
564
564
|
- If you want logic to run only once for the entire CLI process, use `onLauncherStart` and `onLauncherEnd`.
|
|
@@ -569,18 +569,18 @@ Finally, a full-featured CLI launcher without the ceremony. `@reliverse/rempts`'
|
|
|
569
569
|
const main = defineCommand({
|
|
570
570
|
onLauncherStart() { relinka('info', 'Global setup (once per process)'); },
|
|
571
571
|
onLauncherEnd() { relinka('info', 'Global cleanup (once per process)'); },
|
|
572
|
-
|
|
573
|
-
|
|
572
|
+
onCmdInit() { relinka('info', 'Setup for each command'); },
|
|
573
|
+
onCmdExit() { relinka('info', 'Cleanup for each command'); },
|
|
574
574
|
commands: { ... },
|
|
575
575
|
run() { relinka('info', 'Main run handler (no command)'); },
|
|
576
576
|
});
|
|
577
577
|
// onLauncherStart/onLauncherEnd are called once per process
|
|
578
|
-
//
|
|
578
|
+
// onCmdInit/onCmdExit are called for every command (not for main run())
|
|
579
579
|
// If you want per-run() logic, use the run() handler or global hooks
|
|
580
580
|
```
|
|
581
581
|
|
|
582
582
|
- **Deprecation Notice**
|
|
583
|
-
- The legacy `setup` and `cleanup` names are still supported as aliases for per-command hooks, but will be removed in a future major version. Prefer `
|
|
583
|
+
- The legacy `setup` and `cleanup` names are still supported as aliases for per-command hooks, but will be removed in a future major version. Prefer `onCmdInit` and `onCmdExit` going forward.
|
|
584
584
|
- The `subCommands` property is deprecated as well. Please use `commands` instead. `subCommands` will be removed in a future major version.
|
|
585
585
|
|
|
586
586
|
- **Dynamic Usage Examples:**
|
|
@@ -69,17 +69,17 @@ type DefineCommandOptions<A extends ArgDefinitions = EmptyArgs> = {
|
|
|
69
69
|
/**
|
|
70
70
|
* Called before the command runs. Receives `{ args, raw }` (parsed args and raw argv).
|
|
71
71
|
*/
|
|
72
|
-
|
|
72
|
+
onCmdInit?: CommandHook<InferArgTypes<A>>;
|
|
73
73
|
/**
|
|
74
74
|
* Called after the command finishes. Receives `{ args, raw }` (parsed args and raw argv).
|
|
75
75
|
*/
|
|
76
|
-
|
|
76
|
+
onCmdExit?: CommandHook<InferArgTypes<A>>;
|
|
77
77
|
/**
|
|
78
|
-
* @deprecated Use
|
|
78
|
+
* @deprecated Use onCmdInit instead
|
|
79
79
|
*/
|
|
80
80
|
setup?: CommandHook<InferArgTypes<A>>;
|
|
81
81
|
/**
|
|
82
|
-
* @deprecated Use
|
|
82
|
+
* @deprecated Use onCmdExit instead
|
|
83
83
|
*/
|
|
84
84
|
cleanup?: CommandHook<InferArgTypes<A>>;
|
|
85
85
|
/**
|
|
@@ -106,17 +106,17 @@ export type Command<A extends ArgDefinitions = EmptyArgs> = {
|
|
|
106
106
|
/**
|
|
107
107
|
* Called before the command runs. Receives `{ args, raw }` (parsed args and raw argv).
|
|
108
108
|
*/
|
|
109
|
-
|
|
109
|
+
onCmdInit?: CommandHook<InferArgTypes<A>>;
|
|
110
110
|
/**
|
|
111
111
|
* Called after the command finishes. Receives `{ args, raw }` (parsed args and raw argv).
|
|
112
112
|
*/
|
|
113
|
-
|
|
113
|
+
onCmdExit?: CommandHook<InferArgTypes<A>>;
|
|
114
114
|
/**
|
|
115
|
-
* @deprecated Use
|
|
115
|
+
* @deprecated Use onCmdInit instead
|
|
116
116
|
*/
|
|
117
117
|
setup?: CommandHook<InferArgTypes<A>>;
|
|
118
118
|
/**
|
|
119
|
-
* @deprecated Use
|
|
119
|
+
* @deprecated Use onCmdExit instead
|
|
120
120
|
*/
|
|
121
121
|
cleanup?: CommandHook<InferArgTypes<A>>;
|
|
122
122
|
/**
|
|
@@ -57,8 +57,8 @@ function isFlag(str) {
|
|
|
57
57
|
return str.startsWith("-");
|
|
58
58
|
}
|
|
59
59
|
export function defineCommand(options) {
|
|
60
|
-
const
|
|
61
|
-
const
|
|
60
|
+
const onCmdInit = options.onCmdInit || options.setup;
|
|
61
|
+
const onCmdExit = options.onCmdExit || options.cleanup;
|
|
62
62
|
const onLauncherStart = options.onLauncherStart;
|
|
63
63
|
const onLauncherEnd = options.onLauncherEnd;
|
|
64
64
|
let commands = options.commands;
|
|
@@ -70,13 +70,13 @@ export function defineCommand(options) {
|
|
|
70
70
|
args: options.args || {},
|
|
71
71
|
run: options.run,
|
|
72
72
|
commands,
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
onCmdInit,
|
|
74
|
+
onCmdExit,
|
|
75
75
|
onLauncherStart,
|
|
76
76
|
onLauncherEnd,
|
|
77
77
|
// Backward-compatible aliases
|
|
78
|
-
setup:
|
|
79
|
-
cleanup:
|
|
78
|
+
setup: onCmdInit,
|
|
79
|
+
cleanup: onCmdExit
|
|
80
80
|
};
|
|
81
81
|
Object.defineProperty(cmdObj, "subCommands", {
|
|
82
82
|
get() {
|
|
@@ -335,15 +335,15 @@ This can cause recursion or unexpected behavior.`
|
|
|
335
335
|
const [subName, ...subCmdArgv] = rawArgv;
|
|
336
336
|
try {
|
|
337
337
|
const ctx = getParsedContext(command, rawArgv, parserOptions);
|
|
338
|
-
if (typeof command.
|
|
339
|
-
await command.
|
|
338
|
+
if (typeof command.onCmdInit === "function")
|
|
339
|
+
await command.onCmdInit(ctx);
|
|
340
340
|
await runFileBasedSubCmd(
|
|
341
341
|
subName,
|
|
342
342
|
subCmdArgv,
|
|
343
343
|
parserOptions.fileBasedCmds,
|
|
344
344
|
parserOptions,
|
|
345
|
-
command.
|
|
346
|
-
await command.
|
|
345
|
+
command.onCmdExit ? async (_subCtx) => {
|
|
346
|
+
await command.onCmdExit?.(ctx);
|
|
347
347
|
} : void 0
|
|
348
348
|
);
|
|
349
349
|
if (autoExit) process.exit(0);
|
|
@@ -375,14 +375,14 @@ This can cause recursion or unexpected behavior.`
|
|
|
375
375
|
if (subSpec) {
|
|
376
376
|
try {
|
|
377
377
|
const ctx = getParsedContext(command, rawArgv, parserOptions);
|
|
378
|
-
if (typeof command.
|
|
379
|
-
await command.
|
|
378
|
+
if (typeof command.onCmdInit === "function")
|
|
379
|
+
await command.onCmdInit(ctx);
|
|
380
380
|
await runSubCommand(
|
|
381
381
|
subSpec,
|
|
382
382
|
subCmdArgv,
|
|
383
383
|
parserOptions,
|
|
384
|
-
command.
|
|
385
|
-
await command.
|
|
384
|
+
command.onCmdExit ? async (_subCtx) => {
|
|
385
|
+
await command.onCmdExit?.(ctx);
|
|
386
386
|
} : void 0
|
|
387
387
|
);
|
|
388
388
|
if (autoExit) process.exit(0);
|
|
@@ -512,7 +512,9 @@ async function runCommandWithArgs(command, argv, parserOptions, returnCtx) {
|
|
|
512
512
|
);
|
|
513
513
|
const defaultMap = {};
|
|
514
514
|
for (const [argKey, def] of Object.entries(command.args || {})) {
|
|
515
|
-
if (def.
|
|
515
|
+
if (def.type === "boolean") {
|
|
516
|
+
defaultMap[argKey] = def.default !== void 0 ? def.default : false;
|
|
517
|
+
} else if (def.default !== void 0) {
|
|
516
518
|
if (def.type === "array" && typeof def.default === "string") {
|
|
517
519
|
defaultMap[argKey] = [def.default];
|
|
518
520
|
} else {
|
|
@@ -561,7 +563,11 @@ async function runCommandWithArgs(command, argv, parserOptions, returnCtx) {
|
|
|
561
563
|
else throw new Error(`Missing required argument: --${key}`);
|
|
562
564
|
}
|
|
563
565
|
try {
|
|
564
|
-
|
|
566
|
+
if (def.type === "boolean") {
|
|
567
|
+
finalArgs[key] = rawVal !== void 0 ? castArgValue(def, rawVal, key) : false;
|
|
568
|
+
} else {
|
|
569
|
+
finalArgs[key] = castArgValue(def, rawVal, key);
|
|
570
|
+
}
|
|
565
571
|
if (def.type === "array" && def.options && finalArgs[key]) {
|
|
566
572
|
const values = finalArgs[key];
|
|
567
573
|
const invalidOptions = values.filter(
|
|
@@ -663,7 +669,9 @@ export async function runCmd(command, argv = [], parserOptions = {}) {
|
|
|
663
669
|
);
|
|
664
670
|
const defaultMap = {};
|
|
665
671
|
for (const [argKey, def] of Object.entries(command.args || {})) {
|
|
666
|
-
if (def.
|
|
672
|
+
if (def.type === "boolean") {
|
|
673
|
+
defaultMap[argKey] = def.default !== void 0 ? def.default : false;
|
|
674
|
+
} else if (def.default !== void 0) {
|
|
667
675
|
if (def.type === "array" && typeof def.default === "string") {
|
|
668
676
|
defaultMap[argKey] = [def.default];
|
|
669
677
|
} else {
|
|
@@ -734,7 +742,9 @@ function getParsedContext(command, argv, parserOptions) {
|
|
|
734
742
|
);
|
|
735
743
|
const defaultMap = {};
|
|
736
744
|
for (const [argKey, def] of Object.entries(command.args || {})) {
|
|
737
|
-
if (def.
|
|
745
|
+
if (def.type === "boolean") {
|
|
746
|
+
defaultMap[argKey] = def.default !== void 0 ? def.default : false;
|
|
747
|
+
} else if (def.default !== void 0) {
|
|
738
748
|
if (def.type === "array" && typeof def.default === "string") {
|
|
739
749
|
defaultMap[argKey] = [def.default];
|
|
740
750
|
} else {
|
|
@@ -768,7 +778,11 @@ function getParsedContext(command, argv, parserOptions) {
|
|
|
768
778
|
if (def.type === "array" && rawVal !== void 0 && !Array.isArray(rawVal)) {
|
|
769
779
|
rawVal = [rawVal];
|
|
770
780
|
}
|
|
771
|
-
|
|
781
|
+
if (def.type === "boolean") {
|
|
782
|
+
finalArgs[key] = rawVal !== void 0 ? castArgValue(def, rawVal, key) : false;
|
|
783
|
+
} else {
|
|
784
|
+
finalArgs[key] = castArgValue(def, rawVal, key);
|
|
785
|
+
}
|
|
772
786
|
}
|
|
773
787
|
return { args: finalArgs, raw: argv };
|
|
774
788
|
}
|