@optique/clack 1.2.0-dev.2196 → 1.2.0-dev.2200
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/dist/index.cjs +195 -0
- package/dist/index.d.cts +159 -0
- package/dist/index.js +7 -2
- package/package.json +12 -7
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
10
|
+
key = keys[i];
|
|
11
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
12
|
+
get: ((k) => from[k]).bind(null, key),
|
|
13
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
19
|
+
value: mod,
|
|
20
|
+
enumerable: true
|
|
21
|
+
}) : target, mod));
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
const __clack_prompts = __toESM(require("@clack/prompts"));
|
|
25
|
+
const __optique_core_message = __toESM(require("@optique/core/message"));
|
|
26
|
+
const __optique_prompt = __toESM(require("@optique/prompt"));
|
|
27
|
+
|
|
28
|
+
//#region src/index.ts
|
|
29
|
+
const promptFunctionsOverrideSymbol = Symbol.for("@optique/clack/prompt-functions");
|
|
30
|
+
const defaultPromptFunctions = {
|
|
31
|
+
text: __clack_prompts.text,
|
|
32
|
+
password: __clack_prompts.password,
|
|
33
|
+
confirm: __clack_prompts.confirm,
|
|
34
|
+
select: __clack_prompts.select,
|
|
35
|
+
multiselect: __clack_prompts.multiselect,
|
|
36
|
+
isCancel: __clack_prompts.isCancel
|
|
37
|
+
};
|
|
38
|
+
function promptFunctionKeys() {
|
|
39
|
+
return Object.keys(defaultPromptFunctions);
|
|
40
|
+
}
|
|
41
|
+
function assignPromptFunctionOverride(override, key, candidate) {
|
|
42
|
+
if (typeof candidate === "function") override[key] = candidate;
|
|
43
|
+
}
|
|
44
|
+
function getPromptFunctionsOverride(value) {
|
|
45
|
+
if (typeof value !== "object" || value == null) return void 0;
|
|
46
|
+
const override = {};
|
|
47
|
+
for (const key of promptFunctionKeys()) assignPromptFunctionOverride(override, key, Reflect.get(value, key));
|
|
48
|
+
return override;
|
|
49
|
+
}
|
|
50
|
+
function getPromptFunctions() {
|
|
51
|
+
const override = getPromptFunctionsOverride(Reflect.get(globalThis, promptFunctionsOverrideSymbol));
|
|
52
|
+
return override != null ? {
|
|
53
|
+
...defaultPromptFunctions,
|
|
54
|
+
...override
|
|
55
|
+
} : defaultPromptFunctions;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Wraps a parser with an interactive Clack prompt fallback.
|
|
59
|
+
*
|
|
60
|
+
* @param parser Inner parser that reads CLI values.
|
|
61
|
+
* @param config Type-safe Clack prompt configuration.
|
|
62
|
+
* @returns A parser with interactive prompt fallback, always in async mode.
|
|
63
|
+
* @throws {Error} If prompt execution fails with an unexpected error or if the
|
|
64
|
+
* inner parser throws while parsing or completing.
|
|
65
|
+
* @since 1.2.0
|
|
66
|
+
*/
|
|
67
|
+
function prompt(parser, config) {
|
|
68
|
+
const promptWithAdapter = (0, __optique_prompt.createPromptAdapter)({
|
|
69
|
+
execute: (cfg) => executePromptRaw(cfg),
|
|
70
|
+
getDefaultValue: getConfigDefault
|
|
71
|
+
});
|
|
72
|
+
return promptWithAdapter(parser, config);
|
|
73
|
+
}
|
|
74
|
+
function getConfigDefault(config) {
|
|
75
|
+
if (config != null && typeof config === "object" && "initialValue" in config) return config.initialValue;
|
|
76
|
+
return void 0;
|
|
77
|
+
}
|
|
78
|
+
async function executePromptRaw(config) {
|
|
79
|
+
const cfg = config;
|
|
80
|
+
const type = cfg.type;
|
|
81
|
+
if (!isPromptType(type)) throw new TypeError(`Unsupported prompt type: ${String(type)}.`);
|
|
82
|
+
const prompts = getPromptFunctions();
|
|
83
|
+
if ("prompter" in cfg && cfg.prompter != null) {
|
|
84
|
+
const value = await cfg.prompter();
|
|
85
|
+
if (prompts.isCancel(value)) return {
|
|
86
|
+
success: false,
|
|
87
|
+
error: __optique_core_message.message`Prompt cancelled.`
|
|
88
|
+
};
|
|
89
|
+
if (cfg.type === "number") return normalizeNumberResult(value);
|
|
90
|
+
if (cfg.type === "multiselect") return normalizeMultiselectResult(value, cfg);
|
|
91
|
+
return {
|
|
92
|
+
success: true,
|
|
93
|
+
value
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
const result = await executeClackPrompt(cfg, prompts);
|
|
97
|
+
if (prompts.isCancel(result)) return {
|
|
98
|
+
success: false,
|
|
99
|
+
error: __optique_core_message.message`Prompt cancelled.`
|
|
100
|
+
};
|
|
101
|
+
if (cfg.type === "number") return normalizeNumberResult(result);
|
|
102
|
+
if (cfg.type === "multiselect") return normalizeMultiselectResult(result, cfg);
|
|
103
|
+
return {
|
|
104
|
+
success: true,
|
|
105
|
+
value: result
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function isPromptType(value) {
|
|
109
|
+
return value === "text" || value === "password" || value === "confirm" || value === "number" || value === "select" || value === "multiselect";
|
|
110
|
+
}
|
|
111
|
+
function executeClackPrompt(cfg, prompts) {
|
|
112
|
+
switch (cfg.type) {
|
|
113
|
+
case "text": return prompts.text({
|
|
114
|
+
message: cfg.message,
|
|
115
|
+
...cfg.placeholder !== void 0 ? { placeholder: cfg.placeholder } : {},
|
|
116
|
+
...cfg.initialValue !== void 0 ? { initialValue: cfg.initialValue } : {},
|
|
117
|
+
...cfg.validate !== void 0 ? { validate: cfg.validate } : {}
|
|
118
|
+
});
|
|
119
|
+
case "password": return prompts.password({
|
|
120
|
+
message: cfg.message,
|
|
121
|
+
...cfg.mask !== void 0 ? { mask: cfg.mask } : {},
|
|
122
|
+
...cfg.validate !== void 0 ? { validate: cfg.validate } : {}
|
|
123
|
+
});
|
|
124
|
+
case "confirm": return prompts.confirm({
|
|
125
|
+
message: cfg.message,
|
|
126
|
+
...cfg.initialValue !== void 0 ? { initialValue: cfg.initialValue } : {}
|
|
127
|
+
});
|
|
128
|
+
case "number": return prompts.text({
|
|
129
|
+
message: cfg.message,
|
|
130
|
+
...cfg.placeholder !== void 0 ? { placeholder: cfg.placeholder } : {},
|
|
131
|
+
...cfg.initialValue !== void 0 ? { initialValue: String(cfg.initialValue) } : {},
|
|
132
|
+
validate: async (value) => {
|
|
133
|
+
const parsed = parseNumberPromptValue(value);
|
|
134
|
+
if (parsed == null) return "Enter a number.";
|
|
135
|
+
if (cfg.min !== void 0 && parsed < cfg.min) return `Must be at least ${cfg.min}.`;
|
|
136
|
+
if (cfg.max !== void 0 && parsed > cfg.max) return `Must be at most ${cfg.max}.`;
|
|
137
|
+
return await cfg.validate?.(parsed);
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
case "select": return prompts.select({
|
|
141
|
+
message: cfg.message,
|
|
142
|
+
options: normalizeOptions(cfg.options),
|
|
143
|
+
...cfg.initialValue !== void 0 ? { initialValue: cfg.initialValue } : {}
|
|
144
|
+
});
|
|
145
|
+
case "multiselect": return prompts.multiselect({
|
|
146
|
+
message: cfg.message,
|
|
147
|
+
options: normalizeOptions(cfg.options),
|
|
148
|
+
...cfg.required !== void 0 ? { required: cfg.required } : {}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
function normalizeNumberResult(result) {
|
|
153
|
+
const parsed = typeof result === "number" && Number.isFinite(result) ? result : typeof result === "string" ? parseNumberPromptValue(result) : null;
|
|
154
|
+
if (parsed == null) return {
|
|
155
|
+
success: false,
|
|
156
|
+
error: __optique_core_message.message`No number provided.`
|
|
157
|
+
};
|
|
158
|
+
return {
|
|
159
|
+
success: true,
|
|
160
|
+
value: parsed
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
function normalizeMultiselectResult(result, config) {
|
|
164
|
+
const values = Array.isArray(result) ? result : [];
|
|
165
|
+
if (config.required === true && values.length < 1) return {
|
|
166
|
+
success: false,
|
|
167
|
+
error: __optique_core_message.message`No option selected.`
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
success: true,
|
|
171
|
+
value: values
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function parseNumberPromptValue(value) {
|
|
175
|
+
if (value.trim() === "") return null;
|
|
176
|
+
const parsed = Number(value);
|
|
177
|
+
return Number.isFinite(parsed) ? parsed : null;
|
|
178
|
+
}
|
|
179
|
+
function normalizeOptions(options) {
|
|
180
|
+
return options.map((option) => {
|
|
181
|
+
if (typeof option === "string") return {
|
|
182
|
+
value: option,
|
|
183
|
+
label: option
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
value: option.value,
|
|
187
|
+
...option.label !== void 0 ? { label: option.label } : {},
|
|
188
|
+
...option.hint !== void 0 ? { hint: option.hint } : {},
|
|
189
|
+
...option.disabled !== void 0 ? { disabled: option.disabled } : {}
|
|
190
|
+
};
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
//#endregion
|
|
195
|
+
exports.prompt = prompt;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { FluentParser } from "@optique/core/fluent";
|
|
2
|
+
import { Mode, Parser } from "@optique/core/parser";
|
|
3
|
+
|
|
4
|
+
//#region src/index.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A choice item for `select` and `multiselect` prompts.
|
|
8
|
+
*
|
|
9
|
+
* @since 1.2.0
|
|
10
|
+
*/
|
|
11
|
+
interface Option {
|
|
12
|
+
/**
|
|
13
|
+
* The value returned when this option is selected.
|
|
14
|
+
*/
|
|
15
|
+
readonly value: string;
|
|
16
|
+
/**
|
|
17
|
+
* Display label shown in the prompt. Defaults to `value`.
|
|
18
|
+
*/
|
|
19
|
+
readonly label?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Additional hint shown next to the option.
|
|
22
|
+
*/
|
|
23
|
+
readonly hint?: string;
|
|
24
|
+
/**
|
|
25
|
+
* If truthy, the option cannot be selected.
|
|
26
|
+
*/
|
|
27
|
+
readonly disabled?: boolean | string;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Configuration for a `text` prompt.
|
|
31
|
+
*
|
|
32
|
+
* @since 1.2.0
|
|
33
|
+
*/
|
|
34
|
+
interface TextConfig {
|
|
35
|
+
readonly type: "text";
|
|
36
|
+
/** The question to display to the user. */
|
|
37
|
+
readonly message: string;
|
|
38
|
+
/** Placeholder text shown before input. */
|
|
39
|
+
readonly placeholder?: string;
|
|
40
|
+
/** Initial value pre-filled in the prompt. */
|
|
41
|
+
readonly initialValue?: string;
|
|
42
|
+
/** Validation function called when the user submits. */
|
|
43
|
+
readonly validate?: (value: string) => string | void | Promise<string | void>;
|
|
44
|
+
/** Override the prompt execution. Useful for testing. */
|
|
45
|
+
readonly prompter?: () => Promise<string>;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Configuration for a `password` prompt.
|
|
49
|
+
*
|
|
50
|
+
* @since 1.2.0
|
|
51
|
+
*/
|
|
52
|
+
interface PasswordConfig {
|
|
53
|
+
readonly type: "password";
|
|
54
|
+
/** The question to display to the user. */
|
|
55
|
+
readonly message: string;
|
|
56
|
+
/** Mask character shown while typing. */
|
|
57
|
+
readonly mask?: string;
|
|
58
|
+
/** Validation function called when the user submits. */
|
|
59
|
+
readonly validate?: (value: string) => string | void | Promise<string | void>;
|
|
60
|
+
/** Override the prompt execution. Useful for testing. */
|
|
61
|
+
readonly prompter?: () => Promise<string>;
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Configuration for a `confirm` prompt.
|
|
65
|
+
*
|
|
66
|
+
* @since 1.2.0
|
|
67
|
+
*/
|
|
68
|
+
interface ConfirmConfig {
|
|
69
|
+
readonly type: "confirm";
|
|
70
|
+
/** The question to display to the user. */
|
|
71
|
+
readonly message: string;
|
|
72
|
+
/** Initial Boolean value. */
|
|
73
|
+
readonly initialValue?: boolean;
|
|
74
|
+
/** Override the prompt execution. Useful for testing. */
|
|
75
|
+
readonly prompter?: () => Promise<boolean>;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Configuration for a `number` prompt.
|
|
79
|
+
*
|
|
80
|
+
* Clack does not provide a dedicated number prompt, so *@optique/clack* uses a
|
|
81
|
+
* text prompt and converts the submitted value to a number.
|
|
82
|
+
*
|
|
83
|
+
* @since 1.2.0
|
|
84
|
+
*/
|
|
85
|
+
interface NumberPromptConfig {
|
|
86
|
+
readonly type: "number";
|
|
87
|
+
/** The question to display to the user. */
|
|
88
|
+
readonly message: string;
|
|
89
|
+
/** Placeholder text shown before input. */
|
|
90
|
+
readonly placeholder?: string;
|
|
91
|
+
/** Initial numeric value. */
|
|
92
|
+
readonly initialValue?: number;
|
|
93
|
+
/** Minimum accepted value. */
|
|
94
|
+
readonly min?: number;
|
|
95
|
+
/** Maximum accepted value. */
|
|
96
|
+
readonly max?: number;
|
|
97
|
+
/** Additional validation after numeric conversion. */
|
|
98
|
+
readonly validate?: (value: number) => string | void | Promise<string | void>;
|
|
99
|
+
/** Override the prompt execution. Useful for testing. */
|
|
100
|
+
readonly prompter?: () => Promise<number | undefined>;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Configuration for a `select` prompt.
|
|
104
|
+
*
|
|
105
|
+
* @since 1.2.0
|
|
106
|
+
*/
|
|
107
|
+
interface SelectConfig {
|
|
108
|
+
readonly type: "select";
|
|
109
|
+
/** The question to display to the user. */
|
|
110
|
+
readonly message: string;
|
|
111
|
+
/** Available options. */
|
|
112
|
+
readonly options: readonly (string | Option)[];
|
|
113
|
+
/** Initially selected option value. */
|
|
114
|
+
readonly initialValue?: string;
|
|
115
|
+
/** Override the prompt execution. Useful for testing. */
|
|
116
|
+
readonly prompter?: () => Promise<string>;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Configuration for a `multiselect` prompt.
|
|
120
|
+
*
|
|
121
|
+
* @since 1.2.0
|
|
122
|
+
*/
|
|
123
|
+
interface MultiselectConfig {
|
|
124
|
+
readonly type: "multiselect";
|
|
125
|
+
/** The question to display to the user. */
|
|
126
|
+
readonly message: string;
|
|
127
|
+
/** Available options. */
|
|
128
|
+
readonly options: readonly (string | Option)[];
|
|
129
|
+
/** Whether at least one option must be selected. */
|
|
130
|
+
readonly required?: boolean;
|
|
131
|
+
/** Override the prompt execution. Useful for testing. */
|
|
132
|
+
readonly prompter?: () => Promise<readonly string[]>;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* A union of all string-valued prompt configurations.
|
|
136
|
+
*
|
|
137
|
+
* @since 1.2.0
|
|
138
|
+
*/
|
|
139
|
+
type StringPromptConfig = TextConfig | PasswordConfig | SelectConfig;
|
|
140
|
+
/**
|
|
141
|
+
* Type-safe Clack prompt configuration for a given parser value type `T`.
|
|
142
|
+
*
|
|
143
|
+
* @since 1.2.0
|
|
144
|
+
*/
|
|
145
|
+
type PromptConfig<T> = BasePromptConfig<Exclude<T, null | undefined>>;
|
|
146
|
+
type BasePromptConfig<T> = T extends boolean ? ConfirmConfig : T extends number ? NumberPromptConfig : T extends string ? StringPromptConfig : T extends readonly string[] ? MultiselectConfig : never;
|
|
147
|
+
/**
|
|
148
|
+
* Wraps a parser with an interactive Clack prompt fallback.
|
|
149
|
+
*
|
|
150
|
+
* @param parser Inner parser that reads CLI values.
|
|
151
|
+
* @param config Type-safe Clack prompt configuration.
|
|
152
|
+
* @returns A parser with interactive prompt fallback, always in async mode.
|
|
153
|
+
* @throws {Error} If prompt execution fails with an unexpected error or if the
|
|
154
|
+
* inner parser throws while parsing or completing.
|
|
155
|
+
* @since 1.2.0
|
|
156
|
+
*/
|
|
157
|
+
declare function prompt<M extends Mode, TValue, TState>(parser: Parser<M, TValue, TState>, config: PromptConfig<TValue>): FluentParser<"async", TValue, TState>;
|
|
158
|
+
//#endregion
|
|
159
|
+
export { ConfirmConfig, MultiselectConfig, NumberPromptConfig, Option, PasswordConfig, PromptConfig, SelectConfig, StringPromptConfig, TextConfig, prompt };
|
package/dist/index.js
CHANGED
|
@@ -59,6 +59,10 @@ async function executePromptRaw(config) {
|
|
|
59
59
|
const prompts = getPromptFunctions();
|
|
60
60
|
if ("prompter" in cfg && cfg.prompter != null) {
|
|
61
61
|
const value = await cfg.prompter();
|
|
62
|
+
if (prompts.isCancel(value)) return {
|
|
63
|
+
success: false,
|
|
64
|
+
error: message`Prompt cancelled.`
|
|
65
|
+
};
|
|
62
66
|
if (cfg.type === "number") return normalizeNumberResult(value);
|
|
63
67
|
if (cfg.type === "multiselect") return normalizeMultiselectResult(value, cfg);
|
|
64
68
|
return {
|
|
@@ -134,13 +138,14 @@ function normalizeNumberResult(result) {
|
|
|
134
138
|
};
|
|
135
139
|
}
|
|
136
140
|
function normalizeMultiselectResult(result, config) {
|
|
137
|
-
|
|
141
|
+
const values = Array.isArray(result) ? result : [];
|
|
142
|
+
if (config.required === true && values.length < 1) return {
|
|
138
143
|
success: false,
|
|
139
144
|
error: message`No option selected.`
|
|
140
145
|
};
|
|
141
146
|
return {
|
|
142
147
|
success: true,
|
|
143
|
-
value:
|
|
148
|
+
value: values
|
|
144
149
|
};
|
|
145
150
|
}
|
|
146
151
|
function parseNumberPromptValue(value) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@optique/clack",
|
|
3
|
-
"version": "1.2.0-dev.
|
|
3
|
+
"version": "1.2.0-dev.2200",
|
|
4
4
|
"description": "Interactive prompt support for Optique via Clack",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"CLI",
|
|
@@ -41,11 +41,16 @@
|
|
|
41
41
|
],
|
|
42
42
|
"type": "module",
|
|
43
43
|
"module": "./dist/index.js",
|
|
44
|
+
"main": "./dist/index.cjs",
|
|
44
45
|
"types": "./dist/index.d.ts",
|
|
45
46
|
"exports": {
|
|
46
47
|
".": {
|
|
47
|
-
"types":
|
|
48
|
-
|
|
48
|
+
"types": {
|
|
49
|
+
"import": "./dist/index.d.ts",
|
|
50
|
+
"require": "./dist/index.d.cts"
|
|
51
|
+
},
|
|
52
|
+
"import": "./dist/index.js",
|
|
53
|
+
"require": "./dist/index.cjs"
|
|
49
54
|
}
|
|
50
55
|
},
|
|
51
56
|
"imports": {
|
|
@@ -57,16 +62,16 @@
|
|
|
57
62
|
"sideEffects": false,
|
|
58
63
|
"dependencies": {
|
|
59
64
|
"@clack/prompts": "^1.6.0",
|
|
60
|
-
"@optique/
|
|
61
|
-
"@optique/
|
|
65
|
+
"@optique/prompt": "1.2.0-dev.2200+9ff07852",
|
|
66
|
+
"@optique/core": "1.2.0-dev.2200+9ff07852"
|
|
62
67
|
},
|
|
63
68
|
"devDependencies": {
|
|
64
69
|
"@types/node": "^24.0.0",
|
|
65
70
|
"fast-check": "^4.7.0",
|
|
66
71
|
"tsdown": "^0.13.0",
|
|
67
72
|
"typescript": "^5.8.3",
|
|
68
|
-
"@optique/env": "1.2.0-dev.
|
|
69
|
-
"@optique/run": "1.2.0-dev.
|
|
73
|
+
"@optique/env": "1.2.0-dev.2200+9ff07852",
|
|
74
|
+
"@optique/run": "1.2.0-dev.2200+9ff07852"
|
|
70
75
|
},
|
|
71
76
|
"scripts": {
|
|
72
77
|
"build": "tsdown",
|