@nan0web/ui-cli 1.0.2 → 1.1.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 +10 -10
- package/package.json +18 -3
- package/src/CLI.js +51 -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/src/ui/select.js
CHANGED
|
@@ -5,35 +5,59 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import { CancelError } from "@nan0web/ui/core"
|
|
8
|
-
import createInput
|
|
8
|
+
import createInput from "./input.js"
|
|
9
|
+
|
|
10
|
+
/** @typedef {import("./input.js").Input} Input */
|
|
11
|
+
/** @typedef {import("./input.js").InputFn} InputFn */
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @typedef {Object} ConsoleLike
|
|
15
|
+
* @property {(...args: any[]) => void} debug
|
|
16
|
+
* @property {(...args: any[]) => void} log
|
|
17
|
+
* @property {(...args: any[]) => void} info
|
|
18
|
+
* @property {(...args: any[]) => void} warn
|
|
19
|
+
* @property {(...args: any[]) => void} error
|
|
20
|
+
*/
|
|
9
21
|
|
|
10
22
|
/**
|
|
11
|
-
* Configuration object for {@link select}.
|
|
12
|
-
*
|
|
13
23
|
* @typedef {Object} SelectConfig
|
|
14
24
|
* @property {string} title – Title displayed above the options list.
|
|
15
25
|
* @property {string} prompt – Prompt displayed for the answer.
|
|
16
26
|
* @property {Array|Map} options – Collection of selectable items.
|
|
17
|
-
* @property {
|
|
27
|
+
* @property {ConsoleLike} console – Console‑like object with an `info` method.
|
|
18
28
|
* @property {string[]} [stops=[]] Words that trigger cancellation.
|
|
19
|
-
* @property {
|
|
29
|
+
* @property {InputFn} [ask] Custom ask function (defaults to {@link createInput}).
|
|
20
30
|
* @property {string} [invalidPrompt="Invalid choice, try again: "] Message shown on invalid input.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Configuration object for {@link select}.
|
|
21
35
|
*
|
|
36
|
+
* @param {SelectConfig} input
|
|
22
37
|
* @returns {Promise<{index:number,value:any}>} Resolves with the selected index and its value.
|
|
38
|
+
*
|
|
39
|
+
* @throws {CancelError} When the user cancels the operation.
|
|
40
|
+
* @throws {Error} When options are missing or an incorrect value is supplied and no
|
|
41
|
+
* `invalidPrompt` is defined.
|
|
23
42
|
*/
|
|
24
|
-
export async function select({
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
43
|
+
export async function select(input) {
|
|
44
|
+
const {
|
|
45
|
+
title,
|
|
46
|
+
prompt,
|
|
47
|
+
invalidPrompt = "Invalid choice, try again: ",
|
|
48
|
+
options: initOptins,
|
|
49
|
+
console,
|
|
50
|
+
stops = [],
|
|
51
|
+
ask: initAsk,
|
|
52
|
+
} = input
|
|
53
|
+
let options = initOptins
|
|
54
|
+
/** @type {InputFn} */
|
|
33
55
|
const ask = initAsk ?? createInput(stops)
|
|
56
|
+
|
|
34
57
|
// Normalise Map → Array of {label,value}
|
|
35
58
|
if (options instanceof Map) {
|
|
36
|
-
options = Array.from(options.entries()).map(
|
|
59
|
+
options = Array.from(options.entries()).map(
|
|
60
|
+
([value, label]) => ({ label, value }))
|
|
37
61
|
}
|
|
38
62
|
if (!Array.isArray(options) || options.length === 0) {
|
|
39
63
|
throw new Error("Options array is required and must not be empty")
|
|
@@ -45,24 +69,37 @@ export async function select({
|
|
|
45
69
|
console.info(title)
|
|
46
70
|
list.forEach(({ label }, i) => console.info(` ${i + 1}) ${label}`))
|
|
47
71
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
if (
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
72
|
+
/**
|
|
73
|
+
* Validation function passed to `ask` as the *loop* argument.
|
|
74
|
+
* @type {import("./input.js").LoopFn}
|
|
75
|
+
*/
|
|
76
|
+
const validator = async (input) => {
|
|
77
|
+
if (input.cancelled) {
|
|
78
|
+
throw new CancelError()
|
|
79
|
+
}
|
|
80
|
+
const idx = Number(input.value) - 1
|
|
57
81
|
if (isNaN(idx) || idx < 0 || idx >= list.length) {
|
|
58
82
|
if (invalidPrompt) {
|
|
59
|
-
|
|
60
|
-
continue
|
|
83
|
+
return true // repeat asking
|
|
61
84
|
}
|
|
62
85
|
throw new Error("Incorrect value provided")
|
|
63
86
|
}
|
|
64
|
-
|
|
87
|
+
// valid selection – store index for later return
|
|
88
|
+
// we reuse `idx` after ask resolves
|
|
89
|
+
return false // stop looping
|
|
65
90
|
}
|
|
91
|
+
|
|
92
|
+
// Ask with validator loop; when validator returns false we have a valid answer.
|
|
93
|
+
const answer = await ask(prompt, validator, invalidPrompt)
|
|
94
|
+
|
|
95
|
+
// After validator passes, compute the final index once more (safe)
|
|
96
|
+
const finalIdx = Number(answer.value) - 1
|
|
97
|
+
return { index: finalIdx, value: list[finalIdx].value }
|
|
66
98
|
}
|
|
67
99
|
|
|
100
|
+
/**
|
|
101
|
+
* Default export for convenience.
|
|
102
|
+
*
|
|
103
|
+
* @type {typeof select}
|
|
104
|
+
*/
|
|
68
105
|
export default select
|
package/types/CLI.d.ts
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Main
|
|
2
|
+
* Main CLi class.
|
|
3
3
|
*/
|
|
4
|
-
export default class
|
|
4
|
+
export default class CLi {
|
|
5
5
|
/**
|
|
6
|
-
* Factory to create a
|
|
6
|
+
* Factory to create a CLi instance from various inputs.
|
|
7
7
|
*
|
|
8
|
-
* @param {
|
|
9
|
-
* @returns {
|
|
10
|
-
* @throws {TypeError} If input is neither a
|
|
8
|
+
* @param {CLi|Object} input - Existing CLi instance or configuration object.
|
|
9
|
+
* @returns {CLi}
|
|
10
|
+
* @throws {TypeError} If input is neither a CLi nor an object.
|
|
11
11
|
*/
|
|
12
|
-
static from(input:
|
|
12
|
+
static from(input: CLi | any): CLi;
|
|
13
13
|
/**
|
|
14
14
|
* @param {Object} [input={}]
|
|
15
15
|
* @param {string[]} [input.argv] - Command‑line arguments (defaults to `process.argv.slice(2)`).
|
|
@@ -32,13 +32,14 @@ export default class CLI {
|
|
|
32
32
|
/** @returns {Map<string,Function>} The command map. */
|
|
33
33
|
get commands(): Map<string, Function>;
|
|
34
34
|
/**
|
|
35
|
-
* Execute the
|
|
35
|
+
* Execute the CLi workflow.
|
|
36
36
|
*
|
|
37
37
|
* @param {Message} [msg] - Optional pre‑built message.
|
|
38
|
-
* @
|
|
38
|
+
* @returns {AsyncGenerator<OutputMessage>}
|
|
39
39
|
*/
|
|
40
|
-
run(msg?: Message | undefined): AsyncGenerator<
|
|
40
|
+
run(msg?: Message | undefined): AsyncGenerator<OutputMessage>;
|
|
41
41
|
#private;
|
|
42
42
|
}
|
|
43
43
|
import Logger from "@nan0web/log";
|
|
44
44
|
import { Message } from "@nan0web/co";
|
|
45
|
+
import { OutputMessage } from "@nan0web/co";
|
package/types/InputAdapter.d.ts
CHANGED
|
@@ -1,49 +1,96 @@
|
|
|
1
|
+
/** @typedef {import("./ui/select.js").InputFn} InputFn */
|
|
2
|
+
/** @typedef {import("./ui/select.js").ConsoleLike} ConsoleLike */
|
|
1
3
|
/**
|
|
2
4
|
* Extends the generic {@link BaseInputAdapter} with CLI‑specific behaviour.
|
|
3
5
|
*
|
|
4
6
|
* @class
|
|
5
7
|
* @extends BaseInputAdapter
|
|
6
8
|
*/
|
|
7
|
-
export default class
|
|
9
|
+
export default class CLiInputAdapter extends BaseInputAdapter {
|
|
10
|
+
constructor(options?: {});
|
|
11
|
+
/** @returns {ConsoleLike} */
|
|
12
|
+
get console(): import("./ui/select.js").ConsoleLike;
|
|
13
|
+
/** @returns {NodeJS.WriteStream} */
|
|
14
|
+
get stdout(): NodeJS.WriteStream;
|
|
15
|
+
/**
|
|
16
|
+
* Create a handler with stop words that supports predefined answers.
|
|
17
|
+
*
|
|
18
|
+
* @param {string[]} stops - Stop words for cancellation.
|
|
19
|
+
* @returns {InputFn}
|
|
20
|
+
*/
|
|
21
|
+
createHandler(stops?: string[]): InputFn;
|
|
8
22
|
/**
|
|
9
23
|
* Prompt the user for a full form, handling navigation and validation.
|
|
10
24
|
*
|
|
11
|
-
* @param {
|
|
25
|
+
* @param {UiForm} form - Form definition to present.
|
|
12
26
|
* @param {Object} [options={}]
|
|
13
27
|
* @param {boolean} [options.silent=true] - Suppress console output if `true`.
|
|
14
28
|
* @returns {Promise<Object>} Result object containing form data and meta‑information.
|
|
15
29
|
*/
|
|
16
|
-
requestForm(form:
|
|
30
|
+
requestForm(form: UiForm, options?: {
|
|
17
31
|
silent?: boolean | undefined;
|
|
18
32
|
} | undefined): Promise<any>;
|
|
33
|
+
/**
|
|
34
|
+
* Render a UI component in the CLI environment.
|
|
35
|
+
*
|
|
36
|
+
* The current CLI adapter only supports simple textual rendering.
|
|
37
|
+
*
|
|
38
|
+
* @param {string} component - Component name (e.g. `"Alert"`).
|
|
39
|
+
* @param {object} props - Props object passed to the component.
|
|
40
|
+
* @returns {Promise<void>}
|
|
41
|
+
*/
|
|
42
|
+
render(component: string, props: object): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Process a full form – thin wrapper around {@link requestForm}.
|
|
45
|
+
*
|
|
46
|
+
* @param {UiForm} form - Form definition.
|
|
47
|
+
* @param {object} [_state] - Unused, kept for compatibility with `CLiMessage`.
|
|
48
|
+
* @returns {Promise<Object>} Same shape as {@link requestForm} result.
|
|
49
|
+
*/
|
|
50
|
+
processForm(form: UiForm, _state?: object): Promise<any>;
|
|
19
51
|
/**
|
|
20
52
|
* Prompt the user to select an option from a list.
|
|
21
53
|
*
|
|
22
|
-
* @param {Object} config - Configuration
|
|
23
|
-
* @returns {Promise<
|
|
54
|
+
* @param {Object} config - Configuration object.
|
|
55
|
+
* @returns {Promise<string>} Selected value (or empty string on cancel).
|
|
24
56
|
*/
|
|
25
|
-
requestSelect(config: any): Promise<
|
|
57
|
+
requestSelect(config: any): Promise<string>;
|
|
26
58
|
/**
|
|
27
59
|
* Prompt for a single string input.
|
|
28
60
|
*
|
|
29
61
|
* @param {Object} config - Prompt configuration.
|
|
30
|
-
* @param {string} [config.prompt] - Prompt text.
|
|
31
|
-
* @param {string} [config.label] - Optional label.
|
|
32
|
-
* @param {string} [config.name] - Optional identifier.
|
|
33
62
|
* @returns {Promise<string>} User response string.
|
|
34
63
|
*/
|
|
35
|
-
requestInput(config:
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
64
|
+
requestInput(config: any): Promise<string>;
|
|
65
|
+
/**
|
|
66
|
+
* Asks user a question or form and returns the completed form
|
|
67
|
+
* @param {string | UiForm} question
|
|
68
|
+
* @param {object} [options={}]
|
|
69
|
+
*
|
|
70
|
+
*/
|
|
71
|
+
ask(question: string | UiForm, options?: object): Promise<any>;
|
|
42
72
|
/** @inheritDoc */
|
|
43
73
|
select(cfg: any): Promise<{
|
|
44
74
|
index: number;
|
|
45
75
|
value: any;
|
|
46
76
|
}>;
|
|
77
|
+
/**
|
|
78
|
+
* **New API** – Require input for a {@link UiMessage} instance.
|
|
79
|
+
*
|
|
80
|
+
* This method mirrors the previous `UiMessage.requireInput` logic, but is now
|
|
81
|
+
* owned by the UI adapter. It validates the message according to its static
|
|
82
|
+
* {@link UiMessage.Body} schema, presents a generated {@link UiForm} and
|
|
83
|
+
* returns the updated body. Cancellation results in a {@link CancelError}.
|
|
84
|
+
*
|
|
85
|
+
* @param {UiMessage} msg - Message instance needing input.
|
|
86
|
+
* @returns {Promise<any>} Updated message body.
|
|
87
|
+
* @throws {CancelError} When user cancels the input process.
|
|
88
|
+
*/
|
|
89
|
+
requireInput(msg: UiMessage): Promise<any>;
|
|
90
|
+
#private;
|
|
47
91
|
}
|
|
92
|
+
export type InputFn = import("./ui/select.js").InputFn;
|
|
93
|
+
export type ConsoleLike = import("./ui/select.js").ConsoleLike;
|
|
48
94
|
import { InputAdapter as BaseInputAdapter } from "@nan0web/ui";
|
|
49
|
-
import {
|
|
95
|
+
import { UiForm } from "@nan0web/ui";
|
|
96
|
+
import { UiMessage } from "@nan0web/ui";
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OutputAdapter handles UI output operations in command-line environment.
|
|
3
|
+
* @class
|
|
4
|
+
*/
|
|
5
|
+
export default class OutputAdapter {
|
|
6
|
+
/**
|
|
7
|
+
* Creates new output adapter.
|
|
8
|
+
* @param {Object} [options] - Configuration options.
|
|
9
|
+
* @param {any} [options.console] - Console implementation.
|
|
10
|
+
* @param {Map<string, () => Promise<Function>>} [options.components] - Component loaders.
|
|
11
|
+
*/
|
|
12
|
+
constructor(options?: {
|
|
13
|
+
console?: any;
|
|
14
|
+
components?: Map<string, () => Promise<Function>> | undefined;
|
|
15
|
+
} | undefined);
|
|
16
|
+
/** @returns {any} */
|
|
17
|
+
get console(): any;
|
|
18
|
+
/**
|
|
19
|
+
* Render a UI component in the CLI environment.
|
|
20
|
+
*
|
|
21
|
+
* The current implementation supports simple textual rendering.
|
|
22
|
+
*
|
|
23
|
+
* @param {string} component - Component name (e.g. `"Alert"`).
|
|
24
|
+
* @param {object} props - Props object passed to the component.
|
|
25
|
+
* @returns {Promise<void>}
|
|
26
|
+
*/
|
|
27
|
+
render(component: string, props: object): Promise<void>;
|
|
28
|
+
#private;
|
|
29
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** @typedef {import("@nan0web/co").MessageBodySchema} MessageBodySchema */
|
|
2
|
+
/** @typedef {import("./InputAdapter.js").default} InputAdapter */
|
|
3
|
+
/** @typedef {import("./OutputAdapter.js").default} OutputAdapter */
|
|
4
|
+
/**
|
|
5
|
+
* Represents a message with UI input requirements.
|
|
6
|
+
* @template {Record<string, any>} T
|
|
7
|
+
* @class
|
|
8
|
+
* @extends {Message}
|
|
9
|
+
*/
|
|
10
|
+
export default class UiMessage<T extends Record<string, any>> extends Message {
|
|
11
|
+
constructor(input?: import("../node_modules/@nan0web/co/types/Message.js").MessageInput | undefined);
|
|
12
|
+
/**
|
|
13
|
+
* Validates the message body against its schema.
|
|
14
|
+
*
|
|
15
|
+
* NOTE: The signature must exactly match `Message.validate` – it returns a
|
|
16
|
+
* `Map<string,string>` regardless of the generic type, otherwise TypeScript
|
|
17
|
+
* reports incompatibility with the base class.
|
|
18
|
+
*
|
|
19
|
+
* @param {any} [body=this.body] - Optional body to validate.
|
|
20
|
+
* @returns {Map<string,string>} Map of validation errors (empty if valid).
|
|
21
|
+
*/
|
|
22
|
+
validate(body?: any): Map<string, string>;
|
|
23
|
+
/**
|
|
24
|
+
* Requires input via UI adapter. Validates fields according to the static `Body` schema.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} ui - UI adapter with input/output capabilities.
|
|
27
|
+
* @param {InputAdapter} ui.input - Input adapter for prompts.
|
|
28
|
+
* @param {OutputAdapter} [ui.output] - Optional output adapter for rendering.
|
|
29
|
+
* @returns {Promise<T>} Resolves with updated body or rejects if cancelled.
|
|
30
|
+
* @throws {CancelError} When user cancels the input process.
|
|
31
|
+
*/
|
|
32
|
+
requireInput(ui: {
|
|
33
|
+
input: InputAdapter;
|
|
34
|
+
output?: import("./OutputAdapter.js").default | undefined;
|
|
35
|
+
}): Promise<T>;
|
|
36
|
+
}
|
|
37
|
+
export type MessageBodySchema = import("@nan0web/co").MessageBodySchema;
|
|
38
|
+
export type InputAdapter = import("./InputAdapter.js").default;
|
|
39
|
+
export type OutputAdapter = import("./OutputAdapter.js").default;
|
|
40
|
+
import { Message } from "@nan0web/co";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** @typedef {import("../InputAdapter.js").default} InputAdapter */
|
|
2
|
+
/**
|
|
3
|
+
* Alert component for CLI rendering.
|
|
4
|
+
*
|
|
5
|
+
* @this {InputAdapter}
|
|
6
|
+
* @param {Object} input - Component props.
|
|
7
|
+
* @param {string} [input.variant="info"] - Alert variant (maps to console method).
|
|
8
|
+
* @param {string} [input.content=""] - Alert message content.
|
|
9
|
+
* @throws {Error} If variant maps to undefined console method.
|
|
10
|
+
*/
|
|
11
|
+
export default function _default(this: import("../InputAdapter.js").default, input?: {
|
|
12
|
+
variant?: string | undefined;
|
|
13
|
+
content?: string | undefined;
|
|
14
|
+
}): void;
|
|
15
|
+
export type InputAdapter = import("../InputAdapter.js").default;
|
package/types/index.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
export { str2argv } from "./utils/parse.js";
|
|
2
|
+
export { generateForm } from "./ui/form.js";
|
|
2
3
|
export const renderers: Map<string, (data: any) => string>;
|
|
3
|
-
export default
|
|
4
|
+
export default CLiInputAdapter;
|
|
4
5
|
export type CommandHelpField = import("./CommandHelp.js").CommandHelpField;
|
|
5
6
|
import CLI from "./CLI.js";
|
|
6
|
-
import
|
|
7
|
+
import CLiInputAdapter from "./InputAdapter.js";
|
|
7
8
|
import { CancelError } from "@nan0web/ui/core";
|
|
9
|
+
import OutputAdapter from "./OutputAdapter.js";
|
|
8
10
|
import Command from "./Command.js";
|
|
9
11
|
import CommandError from "./CommandError.js";
|
|
10
12
|
import CommandMessage from "./CommandMessage.js";
|
|
11
13
|
import CommandParser from "./CommandParser.js";
|
|
12
14
|
import CommandHelp from "./CommandHelp.js";
|
|
13
|
-
export { CLI,
|
|
15
|
+
export { CLI, CLiInputAdapter, CancelError, OutputAdapter, Command, CommandError, CommandMessage, CommandParser, CommandHelp };
|
|
14
16
|
export { select, next, pause, createInput, ask, Input } from "./ui/index.js";
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {object} PlaygroundTestConfig
|
|
3
|
+
* @property {NodeJS.ProcessEnv} env Environment variables for the child process.
|
|
4
|
+
* @property {{ includeDebugger?: boolean }} [config={}] Configuration options.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* Utility class to run playground demos and capture output.
|
|
8
|
+
*
|
|
9
|
+
* Updated behaviour:
|
|
10
|
+
* – When `PLAY_DEMO_SEQUENCE` is defined, the values are streamed to the
|
|
11
|
+
* child process **asynchronously** with a short delay between writes.
|
|
12
|
+
* – Errors caused by writing to a closed stdin (EPIPE) are ignored, allowing
|
|
13
|
+
* the child process to exit cleanly when a demo cancels early.
|
|
14
|
+
* – After execution, leading whitespace on each output line is stripped so
|
|
15
|
+
* that the test suite can compare raw lines without dealing with logger
|
|
16
|
+
* formatting (e.g. logger prefixes, indentation).
|
|
17
|
+
*/
|
|
18
|
+
export default class PlaygroundTest {
|
|
19
|
+
/**
|
|
20
|
+
* @param {NodeJS.ProcessEnv} env Environment variables for the child process.
|
|
21
|
+
* @param {{ includeDebugger?: boolean, includeEmptyLines?: boolean }} [config={}] Configuration options.
|
|
22
|
+
*/
|
|
23
|
+
constructor(env: NodeJS.ProcessEnv, config?: {
|
|
24
|
+
includeDebugger?: boolean | undefined;
|
|
25
|
+
includeEmptyLines?: boolean | undefined;
|
|
26
|
+
} | undefined);
|
|
27
|
+
env: NodeJS.ProcessEnv;
|
|
28
|
+
/** @type {boolean} Include debugger lines in output (default: false). */
|
|
29
|
+
includeDebugger: boolean;
|
|
30
|
+
incldeEmptyLines: boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Subscribe to an event.
|
|
33
|
+
*/
|
|
34
|
+
on(event: any, fn: any): void;
|
|
35
|
+
/**
|
|
36
|
+
* Unsubscribe from an event.
|
|
37
|
+
*/
|
|
38
|
+
off(event: any, fn: any): void;
|
|
39
|
+
/**
|
|
40
|
+
* Emit an event.
|
|
41
|
+
*/
|
|
42
|
+
emit(event: any, data: any): Promise<EventContext<any>>;
|
|
43
|
+
/**
|
|
44
|
+
* Filter debugger related lines.
|
|
45
|
+
*/
|
|
46
|
+
filterDebugger(str: any): any;
|
|
47
|
+
/**
|
|
48
|
+
* Slice lines from stdout or stderr.
|
|
49
|
+
*/
|
|
50
|
+
slice(target: any, start: any, end: any): any;
|
|
51
|
+
/**
|
|
52
|
+
* Executes the playground script.
|
|
53
|
+
*
|
|
54
|
+
* @param {string[]} [args=["play/main.js"]] Arguments passed to the node process.
|
|
55
|
+
*/
|
|
56
|
+
run(args?: string[] | undefined): Promise<{
|
|
57
|
+
stdout: any;
|
|
58
|
+
stderr: any;
|
|
59
|
+
exitCode: any;
|
|
60
|
+
}>;
|
|
61
|
+
recentResult: {
|
|
62
|
+
stdout: any;
|
|
63
|
+
stderr: any;
|
|
64
|
+
exitCode: any;
|
|
65
|
+
} | undefined;
|
|
66
|
+
#private;
|
|
67
|
+
}
|
|
68
|
+
export type PlaygroundTestConfig = {
|
|
69
|
+
/**
|
|
70
|
+
* Environment variables for the child process.
|
|
71
|
+
*/
|
|
72
|
+
env: NodeJS.ProcessEnv;
|
|
73
|
+
/**
|
|
74
|
+
* Configuration options.
|
|
75
|
+
*/
|
|
76
|
+
config?: {
|
|
77
|
+
includeDebugger?: boolean | undefined;
|
|
78
|
+
} | undefined;
|
|
79
|
+
};
|
|
80
|
+
import { EventContext } from "@nan0web/event";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a UiForm instance from a Body class static schema.
|
|
3
|
+
*
|
|
4
|
+
* @param {Function} BodyClass Class containing static field definitions.
|
|
5
|
+
* @param {Object} [options={}] Options.
|
|
6
|
+
* @param {Object} [options.initialState={}] Initial values for the form fields.
|
|
7
|
+
* @param {Function} [options.t] Optional translation function.
|
|
8
|
+
* @returns {UiForm} UiForm populated with fields derived from the schema.
|
|
9
|
+
*
|
|
10
|
+
* The function inspects static properties of `BodyClass` (e.g., `static username = { … }`)
|
|
11
|
+
* and maps each to a {@link FormInput}. The generated {@link UiForm} title defaults
|
|
12
|
+
* to `BodyClass.name` unless overridden via the schema.
|
|
13
|
+
*/
|
|
14
|
+
export function generateForm(BodyClass: Function, options?: {
|
|
15
|
+
initialState?: any;
|
|
16
|
+
t?: Function | undefined;
|
|
17
|
+
} | undefined): UiForm;
|
|
18
|
+
/**
|
|
19
|
+
* CLI-specific form handler that introspects a model class for static field schemas.
|
|
20
|
+
*
|
|
21
|
+
* @class
|
|
22
|
+
*/
|
|
23
|
+
export default class Form {
|
|
24
|
+
/**
|
|
25
|
+
* Creates a {@link Form} instance directly from a Body schema.
|
|
26
|
+
*
|
|
27
|
+
* @param {typeof Object} BodyClass Class with static schema definitions.
|
|
28
|
+
* @param {Object} [initialModel={}] Optional initial model data.
|
|
29
|
+
* @param {Object} [options={}] Same options as the constructor.
|
|
30
|
+
* @returns {Form} New Form instance.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const form = Form.createFromBodySchema(UserBody, { username: "bob" })
|
|
34
|
+
*/
|
|
35
|
+
static createFromBodySchema(BodyClass: typeof Object, initialModel?: any, options?: any): Form;
|
|
36
|
+
/**
|
|
37
|
+
* @param {Object} model - Model instance (e.g., new User({ username: argv[3] })).
|
|
38
|
+
* @param {Object} [options={}] - Options.
|
|
39
|
+
* @param {string[]} [options.stops=["quit", "cancel", "exit"]] - Stop words.
|
|
40
|
+
* @param {(prompt: string) => Promise<Input>} [options.inputFn] - Custom input function.
|
|
41
|
+
* @throws {TypeError} If model is not an object with a constructor.
|
|
42
|
+
*/
|
|
43
|
+
constructor(model: any, options?: {
|
|
44
|
+
stops?: string[] | undefined;
|
|
45
|
+
inputFn?: ((prompt: string) => Promise<Input>) | undefined;
|
|
46
|
+
} | undefined);
|
|
47
|
+
/** @type {Function} Input handler with cancellation support. */
|
|
48
|
+
handler: Function;
|
|
49
|
+
/**
|
|
50
|
+
* Prompts for selection using the provided configuration.
|
|
51
|
+
*
|
|
52
|
+
* @param {Object} config - Selection configuration.
|
|
53
|
+
* @returns {Promise<{index:number, value:any}>} Selected option.
|
|
54
|
+
*/
|
|
55
|
+
select(config: any): Promise<{
|
|
56
|
+
index: number;
|
|
57
|
+
value: any;
|
|
58
|
+
}>;
|
|
59
|
+
/**
|
|
60
|
+
* Prompts for input using the internal handler.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} prompt - Input prompt.
|
|
63
|
+
* @returns {Promise<Input>} Input result.
|
|
64
|
+
*/
|
|
65
|
+
input(prompt: string): Promise<Input>;
|
|
66
|
+
/**
|
|
67
|
+
* Prompts for input, validates, and updates the model.
|
|
68
|
+
* Uses `ask` for text fields and `select` for option-based fields.
|
|
69
|
+
* Supports cancellation via stop words.
|
|
70
|
+
*
|
|
71
|
+
* @returns {Promise<{cancelled:boolean}>} Result indicating if cancelled.
|
|
72
|
+
* @throws {Error} Propagates non-cancellation errors.
|
|
73
|
+
*/
|
|
74
|
+
requireInput(): Promise<{
|
|
75
|
+
cancelled: boolean;
|
|
76
|
+
}>;
|
|
77
|
+
/**
|
|
78
|
+
* Converts raw input value based on field schema.
|
|
79
|
+
*
|
|
80
|
+
* @param {Object} field - Field config.
|
|
81
|
+
* @param {string} value - Raw string value.
|
|
82
|
+
* @returns {string|number|boolean} Typed value.
|
|
83
|
+
*/
|
|
84
|
+
convertValue(field: any, value: string): string | number | boolean;
|
|
85
|
+
/** @returns {Object} The updated model instance. */
|
|
86
|
+
get body(): any;
|
|
87
|
+
#private;
|
|
88
|
+
}
|
|
89
|
+
import { UiForm } from "@nan0web/ui";
|
|
90
|
+
import { Input } from "./input.js";
|