@nan0web/ui-cli 1.0.2 → 1.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 +10 -10
- package/package.json +18 -3
- package/src/CLI.js +50 -41
- package/src/CLiMessage.js +9 -0
- package/src/CommandHelp.js +2 -10
- package/src/CommandMessage.js +1 -0
- package/src/InputAdapter.js +275 -48
- package/src/OutputAdapter.js +61 -0
- package/src/README.md.js +15 -16
- package/src/components/Alert.js +22 -0
- package/src/index.js +9 -3
- package/src/test/PlaygroundTest.js +157 -0
- package/src/test/index.js +7 -0
- package/src/ui/Adapter.js +3 -0
- package/src/ui/form.js +254 -0
- package/src/ui/input.js +119 -17
- package/src/ui/select.js +64 -27
- package/types/CLI.d.ts +11 -10
- package/types/CLiMessage.d.ts +8 -0
- package/types/CommandMessage.d.ts +1 -0
- package/types/InputAdapter.d.ts +64 -17
- package/types/OutputAdapter.d.ts +29 -0
- package/types/UiMessage.d.ts +40 -0
- package/types/components/Alert.d.ts +15 -0
- package/types/index.d.ts +5 -3
- package/types/test/PlaygroundTest.d.ts +80 -0
- package/types/test/index.d.ts +3 -0
- package/types/ui/Adapter.d.ts +1 -0
- package/types/ui/form.d.ts +90 -0
- package/types/ui/input.d.ts +58 -11
- package/types/ui/select.d.ts +32 -18
package/types/ui/input.d.ts
CHANGED
|
@@ -1,24 +1,60 @@
|
|
|
1
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/** @typedef {import("./select.js").ConsoleLike} ConsoleLike */
|
|
3
|
+
/** @typedef {(input: Input) => Promise<boolean>} LoopFn */
|
|
4
|
+
/** @typedef {(input: Input) => string} NextQuestionFn */
|
|
1
5
|
/**
|
|
2
|
-
*
|
|
6
|
+
* Input function.
|
|
7
|
+
* ---
|
|
8
|
+
* Must be used only as a type — typedef does not work with full arguments description for functions.
|
|
9
|
+
* ---
|
|
10
|
+
* @param {string} question - Prompt displayed to the user.
|
|
11
|
+
* @param {boolean | LoopFn} [loop=false] - Loop‑control flag, validator or boolean that forces a single answer.
|
|
12
|
+
* @param {string | NextQuestionFn} [nextQuestion] - When `false` the prompt ends after one answer.
|
|
13
|
+
* When a `function` is supplied it receives the current {@link Input}
|
|
14
|
+
* and must return a new question string for the next iteration.
|
|
15
|
+
*
|
|
16
|
+
* @returns {Promise<Input>} Resolves with an {@link Input} instance that contains the final answer,
|
|
17
|
+
* the raw value and cancellation state.
|
|
18
|
+
*
|
|
19
|
+
* @throws {Error} May propagate errors from the underlying readline interface.
|
|
20
|
+
*/
|
|
21
|
+
export function InputFn(question: string, loop?: boolean | LoopFn | undefined, nextQuestion?: string | NextQuestionFn | undefined): Promise<Input>;
|
|
22
|
+
/**
|
|
23
|
+
* Low‑level prompt that returns a trimmed string.
|
|
24
|
+
*
|
|
25
|
+
* @param {Object} input
|
|
26
|
+
* @param {string} input.question - Text displayed as a prompt.
|
|
27
|
+
* @param {string} [input.predef] - Optional predefined answer (useful for testing).
|
|
28
|
+
* @param {ConsoleLike} [input.console] - Optional console to show predefined value
|
|
29
|
+
* @param {import("node:readline").Interface} [input.rl] - Readline interface instnace
|
|
30
|
+
* @returns {Promise<string>} The answer without surrounding whitespace.
|
|
3
31
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
32
|
+
* When `predef` is supplied the function mimics the usual readline output
|
|
33
|
+
* (`question + answer + newline`) and returns the trimmed value.
|
|
6
34
|
*/
|
|
7
|
-
export function
|
|
35
|
+
export function _askRaw(input: {
|
|
36
|
+
question: string;
|
|
37
|
+
predef?: string | undefined;
|
|
38
|
+
console?: import("./select.js").ConsoleLike | undefined;
|
|
39
|
+
rl?: import("readline").Interface | undefined;
|
|
40
|
+
}): Promise<string>;
|
|
8
41
|
/**
|
|
9
42
|
* Factory that creates a reusable async input handler.
|
|
10
43
|
*
|
|
11
44
|
* @param {string[]} [stops=[]] Words that trigger cancellation.
|
|
45
|
+
* @param {string|undefined} [predef] Optional predefined answer for testing.
|
|
46
|
+
* @param {ConsoleLike} [console] Optional console instance.
|
|
12
47
|
* @returns {InputFn} Async function that resolves to an {@link Input}.
|
|
13
48
|
*/
|
|
14
|
-
export function createInput(stops?: string[] | undefined): InputFn;
|
|
49
|
+
export function createInput(stops?: string[] | undefined, predef?: string | undefined, console?: import("./select.js").ConsoleLike | undefined): typeof InputFn;
|
|
15
50
|
/**
|
|
16
|
-
* @
|
|
17
|
-
* @param {
|
|
18
|
-
* @param {
|
|
19
|
-
* @
|
|
20
|
-
* @
|
|
51
|
+
* @param {string[]} predefined
|
|
52
|
+
* @param {ConsoleLike} console
|
|
53
|
+
* @param {string[]} [stops=[]]
|
|
54
|
+
* @returns {import("./select.js").InputFn}
|
|
55
|
+
* @throws {CancelError}
|
|
21
56
|
*/
|
|
57
|
+
export function createPredefinedInput(predefined: string[], console: ConsoleLike, stops?: string[] | undefined): import("./select.js").InputFn;
|
|
22
58
|
/**
|
|
23
59
|
* Represents a line of user input.
|
|
24
60
|
*
|
|
@@ -55,5 +91,16 @@ export class Input {
|
|
|
55
91
|
toString(): string;
|
|
56
92
|
#private;
|
|
57
93
|
}
|
|
94
|
+
/**
|
|
95
|
+
* High‑level input helper `ask`.
|
|
96
|
+
*
|
|
97
|
+
* This constant inherits the full {@link InputFn} signature **and** the
|
|
98
|
+
* detailed JSDoc description for each argument, as defined in {@link InputFn}.
|
|
99
|
+
*
|
|
100
|
+
* @type {InputFn}
|
|
101
|
+
*/
|
|
102
|
+
export const ask: typeof InputFn;
|
|
58
103
|
export default createInput;
|
|
59
|
-
export type
|
|
104
|
+
export type ConsoleLike = import("./select.js").ConsoleLike;
|
|
105
|
+
export type LoopFn = (input: Input) => Promise<boolean>;
|
|
106
|
+
export type NextQuestionFn = (input: Input) => string;
|
package/types/ui/select.d.ts
CHANGED
|
@@ -1,33 +1,47 @@
|
|
|
1
|
+
/** @typedef {import("./input.js").Input} Input */
|
|
2
|
+
/** @typedef {import("./input.js").InputFn} InputFn */
|
|
3
|
+
/**
|
|
4
|
+
* @typedef {Object} ConsoleLike
|
|
5
|
+
* @property {(...args: any[]) => void} debug
|
|
6
|
+
* @property {(...args: any[]) => void} log
|
|
7
|
+
* @property {(...args: any[]) => void} info
|
|
8
|
+
* @property {(...args: any[]) => void} warn
|
|
9
|
+
* @property {(...args: any[]) => void} error
|
|
10
|
+
*/
|
|
1
11
|
/**
|
|
2
|
-
* Configuration object for {@link select}.
|
|
3
|
-
*
|
|
4
12
|
* @typedef {Object} SelectConfig
|
|
5
13
|
* @property {string} title – Title displayed above the options list.
|
|
6
14
|
* @property {string} prompt – Prompt displayed for the answer.
|
|
7
15
|
* @property {Array|Map} options – Collection of selectable items.
|
|
8
|
-
* @property {
|
|
16
|
+
* @property {ConsoleLike} console – Console‑like object with an `info` method.
|
|
9
17
|
* @property {string[]} [stops=[]] Words that trigger cancellation.
|
|
10
|
-
* @property {
|
|
18
|
+
* @property {InputFn} [ask] Custom ask function (defaults to {@link createInput}).
|
|
11
19
|
* @property {string} [invalidPrompt="Invalid choice, try again: "] Message shown on invalid input.
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Configuration object for {@link select}.
|
|
12
23
|
*
|
|
24
|
+
* @param {SelectConfig} input
|
|
13
25
|
* @returns {Promise<{index:number,value:any}>} Resolves with the selected index and its value.
|
|
26
|
+
*
|
|
27
|
+
* @throws {CancelError} When the user cancels the operation.
|
|
28
|
+
* @throws {Error} When options are missing or an incorrect value is supplied and no
|
|
29
|
+
* `invalidPrompt` is defined.
|
|
14
30
|
*/
|
|
15
|
-
export function select(
|
|
16
|
-
title: any;
|
|
17
|
-
prompt: any;
|
|
18
|
-
invalidPrompt?: string | undefined;
|
|
19
|
-
options: any;
|
|
20
|
-
console: any;
|
|
21
|
-
stops?: any[] | undefined;
|
|
22
|
-
ask: any;
|
|
23
|
-
}): Promise<{
|
|
31
|
+
export function select(input: SelectConfig): Promise<{
|
|
24
32
|
index: number;
|
|
25
33
|
value: any;
|
|
26
34
|
}>;
|
|
27
35
|
export default select;
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
36
|
+
export type Input = import("./input.js").Input;
|
|
37
|
+
export type InputFn = typeof import("./input.js").InputFn;
|
|
38
|
+
export type ConsoleLike = {
|
|
39
|
+
debug: (...args: any[]) => void;
|
|
40
|
+
log: (...args: any[]) => void;
|
|
41
|
+
info: (...args: any[]) => void;
|
|
42
|
+
warn: (...args: any[]) => void;
|
|
43
|
+
error: (...args: any[]) => void;
|
|
44
|
+
};
|
|
31
45
|
export type SelectConfig = {
|
|
32
46
|
/**
|
|
33
47
|
* – Title displayed above the options list.
|
|
@@ -44,7 +58,7 @@ export type SelectConfig = {
|
|
|
44
58
|
/**
|
|
45
59
|
* – Console‑like object with an `info` method.
|
|
46
60
|
*/
|
|
47
|
-
console:
|
|
61
|
+
console: ConsoleLike;
|
|
48
62
|
/**
|
|
49
63
|
* Words that trigger cancellation.
|
|
50
64
|
*/
|
|
@@ -52,7 +66,7 @@ export type SelectConfig = {
|
|
|
52
66
|
/**
|
|
53
67
|
* Custom ask function (defaults to {@link createInput }).
|
|
54
68
|
*/
|
|
55
|
-
ask?:
|
|
69
|
+
ask?: typeof import("./input.js").InputFn | undefined;
|
|
56
70
|
/**
|
|
57
71
|
* Message shown on invalid input.
|
|
58
72
|
*/
|