@wocker/utils 1.0.11 → 2.0.0-beta.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 +73 -2
- package/lib/index.d.ts +3 -8
- package/lib/index.js +3 -8
- package/lib/messages.d.ts +15 -0
- package/lib/messages.js +18 -0
- package/lib/prompts/index.d.ts +3 -0
- package/lib/prompts/index.js +19 -0
- package/lib/prompts/promptConfirm.d.ts +4 -0
- package/lib/prompts/promptConfirm.js +75 -0
- package/lib/prompts/promptInput.d.ts +15 -0
- package/lib/prompts/promptInput.js +179 -0
- package/lib/prompts/promptSelect.d.ts +15 -0
- package/lib/prompts/promptSelect.js +162 -0
- package/lib/tools/index.d.ts +4 -0
- package/lib/tools/index.js +20 -0
- package/lib/{normalizeOptions.d.ts → tools/normalizeOptions.d.ts} +6 -8
- package/lib/tools/normalizeOptions.js +26 -0
- package/lib/{volumeFormat.js → tools/volumeFormat.js} +1 -1
- package/lib/types/KeypressEvent.d.ts +5 -0
- package/lib/types/KeypressEvent.js +2 -0
- package/lib/types/PromptConfig.d.ts +8 -0
- package/lib/types/PromptConfig.js +2 -0
- package/lib/types/PromptValidationConfig.d.ts +10 -0
- package/lib/types/PromptValidationConfig.js +2 -0
- package/lib/types/Theme.d.ts +6 -0
- package/lib/types/Theme.js +2 -0
- package/lib/types/Validation.d.ts +2 -0
- package/lib/types/Validation.js +2 -0
- package/lib/types/ValidationError.d.ts +4 -0
- package/lib/types/ValidationError.js +2 -0
- package/lib/types/ValidationResult.d.ts +1 -0
- package/lib/types/ValidationResult.js +2 -0
- package/lib/types/ValidationRule.d.ts +3 -0
- package/lib/types/ValidationRule.js +2 -0
- package/lib/types/ValidationValue.d.ts +1 -0
- package/lib/types/ValidationValue.js +2 -0
- package/lib/types/ValidationValueMessage.d.ts +5 -0
- package/lib/types/ValidationValueMessage.js +2 -0
- package/lib/validation/normalizeRule.d.ts +3 -0
- package/lib/validation/normalizeRule.js +16 -0
- package/lib/validation/validatePrompt.d.ts +3 -0
- package/lib/validation/validatePrompt.js +101 -0
- package/lib/validators/index.d.ts +1 -0
- package/lib/validators/index.js +17 -0
- package/lib/validators/validateVolume.d.ts +2 -0
- package/lib/validators/validateVolume.js +20 -0
- package/package.json +18 -7
- package/test/makes/CustomMuteStream.ts +116 -0
- package/test/makes/PromptSimulator.ts +55 -0
- package/test/tools/render.ts +32 -0
- package/lib/normalizeOptions.js +0 -23
- package/lib/promptConfig.d.ts +0 -14
- package/lib/promptConfig.js +0 -76
- package/lib/promptConfirm.d.ts +0 -6
- package/lib/promptConfirm.js +0 -27
- package/lib/promptGroup.d.ts +0 -14
- package/lib/promptGroup.js +0 -55
- package/lib/promptSelect.d.ts +0 -14
- package/lib/promptSelect.js +0 -58
- package/lib/promptText.d.ts +0 -11
- package/lib/promptText.js +0 -62
- /package/lib/{demuxOutput.d.ts → tools/demuxOutput.d.ts} +0 -0
- /package/lib/{demuxOutput.js → tools/demuxOutput.js} +0 -0
- /package/lib/{volumeFormat.d.ts → tools/volumeFormat.d.ts} +0 -0
- /package/lib/{volumeParse.d.ts → tools/volumeParse.d.ts} +0 -0
- /package/lib/{volumeParse.js → tools/volumeParse.js} +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeOptions = void 0;
|
|
4
|
+
const normalizeOptions = (rawOptions) => {
|
|
5
|
+
if (Array.isArray(rawOptions)) {
|
|
6
|
+
return rawOptions.map((option) => {
|
|
7
|
+
if (typeof option === "string") {
|
|
8
|
+
return {
|
|
9
|
+
label: option,
|
|
10
|
+
value: option
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
return {
|
|
14
|
+
label: option.label || option.value || "",
|
|
15
|
+
value: option.value || ""
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
return Object.keys(rawOptions).map((value) => {
|
|
20
|
+
return {
|
|
21
|
+
label: rawOptions[value],
|
|
22
|
+
value
|
|
23
|
+
};
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
exports.normalizeOptions = normalizeOptions;
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.volumeFormat = void 0;
|
|
4
4
|
const volumeFormat = (volume) => {
|
|
5
|
-
const { source, destination, options } = volume;
|
|
5
|
+
const { source = "/", destination = "/", options } = volume;
|
|
6
6
|
return `${source}:${destination}` + (options ? `:${options}` : "");
|
|
7
7
|
};
|
|
8
8
|
exports.volumeFormat = volumeFormat;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { PartialDeep } from "@inquirer/type";
|
|
2
|
+
import { Theme } from "./Theme";
|
|
3
|
+
import { PromptValidationConfig } from "./PromptValidationConfig";
|
|
4
|
+
export type PromptConfig<V = unknown, C = object, T extends Theme = Theme> = C & PromptValidationConfig<V> & {
|
|
5
|
+
message?: string;
|
|
6
|
+
default?: V;
|
|
7
|
+
theme?: PartialDeep<T>;
|
|
8
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ValidationRule } from "./ValidationRule";
|
|
2
|
+
import { Validation } from "./Validation";
|
|
3
|
+
export type PromptValidationConfig<V = unknown> = {
|
|
4
|
+
required?: boolean | string;
|
|
5
|
+
min?: ValidationRule<number>;
|
|
6
|
+
max?: ValidationRule<number>;
|
|
7
|
+
minLength?: ValidationRule<number>;
|
|
8
|
+
maxLength?: ValidationRule<number>;
|
|
9
|
+
validate?: Validation<V> | Record<string, Validation<V>>;
|
|
10
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ValidationResult = string | string[] | boolean | undefined;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type ValidationValue = boolean | number | string | RegExp;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeRule = void 0;
|
|
4
|
+
const normalizeRule = (rule) => {
|
|
5
|
+
if (typeof rule === "undefined") {
|
|
6
|
+
return [undefined, undefined];
|
|
7
|
+
}
|
|
8
|
+
if (typeof rule === "object" && rule !== null && !(rule instanceof RegExp)) {
|
|
9
|
+
return [rule.value, rule.message];
|
|
10
|
+
}
|
|
11
|
+
return [
|
|
12
|
+
rule,
|
|
13
|
+
undefined
|
|
14
|
+
];
|
|
15
|
+
};
|
|
16
|
+
exports.normalizeRule = normalizeRule;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { PromptValidationConfig } from "../types/PromptValidationConfig";
|
|
2
|
+
import { ValidationError } from "../types/ValidationError";
|
|
3
|
+
export declare const validatePrompt: <T = unknown>(value: any, config: PromptValidationConfig<T>) => Promise<undefined | ValidationError>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.validatePrompt = void 0;
|
|
13
|
+
const normalizeRule_1 = require("./normalizeRule");
|
|
14
|
+
const messages_1 = require("../messages");
|
|
15
|
+
const validatePrompt = (value, config) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
const { required, min, max, minLength, maxLength, validate } = config;
|
|
17
|
+
const isEmpty = typeof value === "undefined" ||
|
|
18
|
+
(typeof value === "boolean" && !value) ||
|
|
19
|
+
(typeof value === "string" && value === "") ||
|
|
20
|
+
(typeof value === "object" && value === null) ||
|
|
21
|
+
(typeof value === "number" && isNaN(value)) ||
|
|
22
|
+
(Array.isArray(value) && value.length === 0);
|
|
23
|
+
const isRequired = (typeof required === "boolean" && required) ||
|
|
24
|
+
typeof required === "string";
|
|
25
|
+
if (isRequired && isEmpty) {
|
|
26
|
+
return {
|
|
27
|
+
type: "required",
|
|
28
|
+
message: typeof required === "string" ? required : messages_1.messages.required
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
const [minValue, minMessage] = (0, normalizeRule_1.normalizeRule)(min);
|
|
32
|
+
if (!isEmpty && typeof minValue !== "undefined") {
|
|
33
|
+
if (typeof value === "number" && value < minValue) {
|
|
34
|
+
return {
|
|
35
|
+
type: "min",
|
|
36
|
+
message: minMessage || messages_1.messages.min(minValue)
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const [maxValue, maxMessage] = (0, normalizeRule_1.normalizeRule)(max);
|
|
41
|
+
if (!isEmpty && typeof maxValue !== "undefined") {
|
|
42
|
+
if (typeof value === "number" && value > maxValue) {
|
|
43
|
+
return {
|
|
44
|
+
type: "max",
|
|
45
|
+
message: maxMessage || messages_1.messages.max(maxValue)
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const [minLengthValue, minLengthMessage] = (0, normalizeRule_1.normalizeRule)(minLength);
|
|
50
|
+
if (!isEmpty && typeof minLengthValue !== "undefined") {
|
|
51
|
+
const validateValue = typeof value === "number"
|
|
52
|
+
? value.toString()
|
|
53
|
+
: value;
|
|
54
|
+
if (typeof validateValue === "string" && validateValue.length < minLengthValue) {
|
|
55
|
+
return {
|
|
56
|
+
type: "minLength",
|
|
57
|
+
message: minLengthMessage || messages_1.messages.minLength(minLengthValue)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (Array.isArray(value) && value.length < minLengthValue) {
|
|
61
|
+
return {
|
|
62
|
+
type: "minLength",
|
|
63
|
+
message: minLengthMessage || messages_1.messages.minArrayLength(minLengthValue)
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
const [maxLengthValue, maxLengthMessage = ""] = (0, normalizeRule_1.normalizeRule)(maxLength);
|
|
68
|
+
if (!isEmpty && typeof maxLengthValue === "number") {
|
|
69
|
+
const validateValue = typeof value === "number"
|
|
70
|
+
? value.toString()
|
|
71
|
+
: value;
|
|
72
|
+
if (typeof validateValue === "string" && validateValue.length > maxLengthValue) {
|
|
73
|
+
return {
|
|
74
|
+
type: "maxLength",
|
|
75
|
+
message: maxLengthMessage || messages_1.messages.maxLength(maxLengthValue)
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
if (Array.isArray(value) && value.length > maxLengthValue) {
|
|
79
|
+
return {
|
|
80
|
+
type: "maxLength",
|
|
81
|
+
message: maxLengthMessage || messages_1.messages.maxArrayLength(maxLengthValue)
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
if (validate) {
|
|
86
|
+
const rules = typeof validate === "function"
|
|
87
|
+
? { validate }
|
|
88
|
+
: validate;
|
|
89
|
+
for (const name in rules) {
|
|
90
|
+
const result = yield rules[name](value);
|
|
91
|
+
if (result === false || typeof result === "string") {
|
|
92
|
+
return {
|
|
93
|
+
message: typeof result === "string" ? result : messages_1.messages.custom,
|
|
94
|
+
type: "custom"
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return undefined;
|
|
100
|
+
});
|
|
101
|
+
exports.validatePrompt = validatePrompt;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./validateVolume";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./validateVolume"), exports);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateVolume = void 0;
|
|
4
|
+
const messages_1 = require("../messages");
|
|
5
|
+
const validateVolume = (value) => {
|
|
6
|
+
if (typeof value === "undefined" || value === null || value === "") {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (typeof value !== "string") {
|
|
10
|
+
return messages_1.messages.volume.notAString;
|
|
11
|
+
}
|
|
12
|
+
if (value.length > 255) {
|
|
13
|
+
return messages_1.messages.volume.tooLong;
|
|
14
|
+
}
|
|
15
|
+
if (!/^[A-Za-z0-9]+(?:[-_]+[A-Za-z0-9]+)*$/.test(value)) {
|
|
16
|
+
return messages_1.messages.volume.invalidCharacters;
|
|
17
|
+
}
|
|
18
|
+
return true;
|
|
19
|
+
};
|
|
20
|
+
exports.validateVolume = validateVolume;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wocker/utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0-beta.0",
|
|
4
4
|
"author": "Kris Papercut <krispcut@gmail.com>",
|
|
5
5
|
"description": "Utils for @wocker",
|
|
6
6
|
"license": "MIT",
|
|
@@ -20,16 +20,27 @@
|
|
|
20
20
|
"url": "https://github.com/kearisp/wocker-utils/issues"
|
|
21
21
|
},
|
|
22
22
|
"scripts": {
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"test": "
|
|
23
|
+
"prepublishOnly": "npm run build",
|
|
24
|
+
"build": "tsc --project tsconfig.build.json",
|
|
25
|
+
"watch": "tsc -w --project tsconfig.build.json",
|
|
26
|
+
"test": "jest",
|
|
27
|
+
"posttest": "jest-coverage-badges --output ./coverage/badges",
|
|
28
|
+
"test-watch": "jest --watchAll"
|
|
27
29
|
},
|
|
28
30
|
"dependencies": {
|
|
29
|
-
"inquirer": "^
|
|
31
|
+
"@inquirer/core": "^10.1.10"
|
|
30
32
|
},
|
|
31
33
|
"devDependencies": {
|
|
32
|
-
"@
|
|
34
|
+
"@inquirer/testing": "^2.1.46",
|
|
35
|
+
"@inquirer/type": "^3.0.6",
|
|
36
|
+
"@types/jest": "^29.5.14",
|
|
37
|
+
"@types/mute-stream": "^0.0.4",
|
|
38
|
+
"@types/node": "^22.14.1",
|
|
39
|
+
"jest": "^29.7.0",
|
|
40
|
+
"jest-coverage-badges": "^1.1.2",
|
|
41
|
+
"mute-stream": "^2.0.0",
|
|
42
|
+
"ts-jest": "^29.3.2",
|
|
43
|
+
"ts-node": "^10.9.2",
|
|
33
44
|
"typescript": "^5.8.3"
|
|
34
45
|
}
|
|
35
46
|
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import MuteStream from "mute-stream";
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
export class CustomMuteStream extends MuteStream {
|
|
5
|
+
protected _listeners: {
|
|
6
|
+
[event: string]: Map<any, any>;
|
|
7
|
+
} = {};
|
|
8
|
+
protected _waiters: {
|
|
9
|
+
[event: string]: {
|
|
10
|
+
completed: number;
|
|
11
|
+
resolve: (value: void | PromiseLike<void>) => void;
|
|
12
|
+
};
|
|
13
|
+
} = {};
|
|
14
|
+
|
|
15
|
+
protected getOverride(event: string, listener: any) {
|
|
16
|
+
if(!this._listeners) {
|
|
17
|
+
this._listeners = {};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if(!this._listeners[event]) {
|
|
21
|
+
this._listeners[event] = new Map();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
let override = this._listeners[event].get(listener);
|
|
25
|
+
|
|
26
|
+
if(!override) {
|
|
27
|
+
override = async (...args: any[]) => {
|
|
28
|
+
const result = await listener(...args);
|
|
29
|
+
|
|
30
|
+
if(!this._waiters) {
|
|
31
|
+
this._waiters = {};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if(this._waiters[event]) {
|
|
35
|
+
this._waiters[event].completed++;
|
|
36
|
+
|
|
37
|
+
if(this._waiters[event].completed === this._listeners[event].size) {
|
|
38
|
+
this._waiters[event].resolve();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return result;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
this._listeners[event].set(listener, override);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return override;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
protected hasOverride(event: string, listener: any) {
|
|
52
|
+
return this._listeners && this._listeners[event] && this._listeners[event].has(listener);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected removeOverride(event: string, listener: any) {
|
|
56
|
+
if(!this.hasOverride(event, listener)) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
this._listeners[event].delete(listener);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public on(event: any, listener: any): this {
|
|
64
|
+
const override = this.getOverride(event, listener);
|
|
65
|
+
|
|
66
|
+
super.on(event, override);
|
|
67
|
+
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
public addListener(event: any, listener: any): this {
|
|
72
|
+
const override = this.getOverride(event, listener);
|
|
73
|
+
|
|
74
|
+
super.addListener(event, override);
|
|
75
|
+
|
|
76
|
+
return this;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public off(event: any, listener: any): this {
|
|
80
|
+
if(this.hasOverride(event, listener)) {
|
|
81
|
+
const override = this.getOverride(event, listener);
|
|
82
|
+
super.off(event, override);
|
|
83
|
+
this.removeOverride(event, listener);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
super.off(event, listener);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
public removeListener(event: any, listener: any): this {
|
|
93
|
+
if(this.hasOverride(event, listener)) {
|
|
94
|
+
const override = this.getOverride(event, listener);
|
|
95
|
+
|
|
96
|
+
super.removeListener(event, override);
|
|
97
|
+
this.removeOverride(event, listener);
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
super.removeListener(event, listener);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return this;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
public async wait(event: string, data: any, key: any): Promise<void> {
|
|
107
|
+
return new Promise<void>((resolve) => {
|
|
108
|
+
this._waiters[event] = {
|
|
109
|
+
resolve,
|
|
110
|
+
completed: 0
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
this.emit(event, data, key);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {CustomMuteStream} from "./CustomMuteStream";
|
|
2
|
+
import {KeypressEvent} from "../../src/types/KeypressEvent";
|
|
3
|
+
import EventEmitter from "node:events";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export class PromptSimulator {
|
|
7
|
+
protected emitter: EventEmitter;
|
|
8
|
+
|
|
9
|
+
constructor(
|
|
10
|
+
protected readonly input: CustomMuteStream
|
|
11
|
+
) {
|
|
12
|
+
this.emitter = new EventEmitter();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public async keypress(key: string | KeypressEvent) {
|
|
16
|
+
const event = typeof key === "string" ? {
|
|
17
|
+
name: key
|
|
18
|
+
} : key;
|
|
19
|
+
|
|
20
|
+
return this.input.wait("keypress", null, event);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
public async type(text: string) {
|
|
24
|
+
this.input.write(text);
|
|
25
|
+
|
|
26
|
+
for(const char of text) {
|
|
27
|
+
await this.input.wait("keypress", null, {
|
|
28
|
+
name: char
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public async process(events: (string | KeypressEvent)[]): Promise<void> {
|
|
34
|
+
const typeReg = /^type:(.*)$/;
|
|
35
|
+
|
|
36
|
+
for(const event of events) {
|
|
37
|
+
if(typeof event === "string" && typeReg.test(event)) {
|
|
38
|
+
const [, text = ""] = typeReg.exec(event) || [];
|
|
39
|
+
|
|
40
|
+
await this.type(text);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
await this.keypress(event);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public onAbort(handle: () => void) {
|
|
49
|
+
this.emitter.on("abort", handle);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
public abort() {
|
|
53
|
+
this.emitter.emit("abort");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {render} from "@inquirer/testing";
|
|
2
|
+
import {Prompt, Context} from "@inquirer/type";
|
|
3
|
+
import {CustomMuteStream} from "../makes/CustomMuteStream";
|
|
4
|
+
import {PromptSimulator} from "../makes/PromptSimulator";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export const customRender = async <const C, const V>(
|
|
8
|
+
prompt: Prompt<V, C>,
|
|
9
|
+
config: C,
|
|
10
|
+
options?: Omit<Context, "signal" | "input">
|
|
11
|
+
) => {
|
|
12
|
+
const input = new CustomMuteStream(),
|
|
13
|
+
events = new PromptSimulator(input),
|
|
14
|
+
abortController = new AbortController();
|
|
15
|
+
|
|
16
|
+
const data = await render(prompt, config, {
|
|
17
|
+
...options || {},
|
|
18
|
+
input,
|
|
19
|
+
signal: abortController.signal
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
events.onAbort(() => {
|
|
23
|
+
data.answer.catch(() => undefined);
|
|
24
|
+
abortController.abort();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
...data,
|
|
29
|
+
input,
|
|
30
|
+
events
|
|
31
|
+
};
|
|
32
|
+
};
|
package/lib/normalizeOptions.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.normalizeOptions = void 0;
|
|
4
|
-
const normalizeOptions = (rawOptions) => {
|
|
5
|
-
return Array.isArray(rawOptions)
|
|
6
|
-
? rawOptions.map((option) => {
|
|
7
|
-
return {
|
|
8
|
-
label: typeof option === "string"
|
|
9
|
-
? option
|
|
10
|
-
: option.label || option.value || "",
|
|
11
|
-
value: typeof option === "string"
|
|
12
|
-
? option
|
|
13
|
-
: option.value || ""
|
|
14
|
-
};
|
|
15
|
-
})
|
|
16
|
-
: Object.keys(rawOptions).map((value) => {
|
|
17
|
-
return {
|
|
18
|
-
label: rawOptions[value],
|
|
19
|
-
value
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
};
|
|
23
|
-
exports.normalizeOptions = normalizeOptions;
|
package/lib/promptConfig.d.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { PromptConfirmOptions } from "./promptConfirm";
|
|
2
|
-
import { PromptSelectOptions } from "./promptSelect";
|
|
3
|
-
import { PromptTextOptions } from "./promptText";
|
|
4
|
-
type ConfirmOptions = PromptConfirmOptions & {
|
|
5
|
-
type: "boolean";
|
|
6
|
-
};
|
|
7
|
-
type SelectOptions = PromptSelectOptions & {
|
|
8
|
-
type: "select";
|
|
9
|
-
};
|
|
10
|
-
type Options = {
|
|
11
|
-
[key: string]: PromptTextOptions | ConfirmOptions | SelectOptions;
|
|
12
|
-
};
|
|
13
|
-
export declare const promptConfig: (options: Options, values?: any) => Promise<any>;
|
|
14
|
-
export {};
|