inquirerer 0.1.3 → 0.4.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/LICENSE +21 -0
- package/README.md +165 -0
- package/esm/index.js +2 -0
- package/esm/keypress.js +71 -0
- package/esm/package.js +26 -0
- package/esm/prompt.js +338 -0
- package/esm/question/index.js +1 -0
- package/esm/utils.js +8 -0
- package/index.d.ts +2 -0
- package/index.js +18 -0
- package/keypress.d.ts +33 -0
- package/keypress.js +75 -0
- package/package.d.ts +1 -0
- package/package.js +30 -0
- package/package.json +30 -35
- package/prompt.d.ts +24 -0
- package/prompt.js +345 -0
- package/question/index.d.ts +1 -0
- package/question/index.js +17 -0
- package/question/types.d.ts +34 -0
- package/question/types.js +2 -0
- package/utils.d.ts +1 -0
- package/utils.js +15 -0
- package/build/index.js +0 -117
- package/build/index.js.map +0 -1
- package/build/test/prompt.test.js +0 -331
- package/build/test/prompt.test.js.map +0 -1
- package/build/types/index.d.ts +0 -21
- package/index.ts +0 -86
- package/readme.md +0 -105
- package/test/prompt.test.ts +0 -236
- package/tsconfig.json +0 -18
- package/yarn.lock +0 -3332
- /package/{build/types/test/prompt.test.d.ts → esm/question/types.js} +0 -0
package/keypress.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Readable } from 'stream';
|
|
3
|
+
type KeyHandler = () => void;
|
|
4
|
+
interface ProcessWrapper {
|
|
5
|
+
exit: (code?: number) => never;
|
|
6
|
+
}
|
|
7
|
+
export declare const KEY_CODES: {
|
|
8
|
+
UP_ARROW: string;
|
|
9
|
+
DOWN_ARROW: string;
|
|
10
|
+
RIGHT_ARROW: string;
|
|
11
|
+
LEFT_ARROW: string;
|
|
12
|
+
ENTER: string;
|
|
13
|
+
SPACE: string;
|
|
14
|
+
CTRL_C: string;
|
|
15
|
+
BACKSPACE: string;
|
|
16
|
+
BACKSPACE_LEGACY: string;
|
|
17
|
+
};
|
|
18
|
+
export declare class TerminalKeypress {
|
|
19
|
+
private listeners;
|
|
20
|
+
private active;
|
|
21
|
+
private noTty;
|
|
22
|
+
private input;
|
|
23
|
+
private proc;
|
|
24
|
+
constructor(noTty?: boolean, input?: Readable, proc?: ProcessWrapper);
|
|
25
|
+
isTTY(): boolean;
|
|
26
|
+
private setupListeners;
|
|
27
|
+
on(key: string, callback: KeyHandler): void;
|
|
28
|
+
off(key: string, callback: KeyHandler): void;
|
|
29
|
+
pause(): void;
|
|
30
|
+
resume(): void;
|
|
31
|
+
destroy(): void;
|
|
32
|
+
}
|
|
33
|
+
export {};
|
package/keypress.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TerminalKeypress = exports.KEY_CODES = void 0;
|
|
4
|
+
const defaultProcessWrapper = {
|
|
5
|
+
exit: (code) => process.exit(code)
|
|
6
|
+
};
|
|
7
|
+
exports.KEY_CODES = {
|
|
8
|
+
UP_ARROW: '\u001b[A',
|
|
9
|
+
DOWN_ARROW: '\u001b[B',
|
|
10
|
+
RIGHT_ARROW: '\u001b[C',
|
|
11
|
+
LEFT_ARROW: '\u001b[D',
|
|
12
|
+
ENTER: '\r',
|
|
13
|
+
SPACE: ' ',
|
|
14
|
+
CTRL_C: '\u0003',
|
|
15
|
+
BACKSPACE: '\x7f',
|
|
16
|
+
BACKSPACE_LEGACY: '\x08' // For compatibility with some systems
|
|
17
|
+
};
|
|
18
|
+
class TerminalKeypress {
|
|
19
|
+
listeners = {};
|
|
20
|
+
active = true;
|
|
21
|
+
noTty;
|
|
22
|
+
input;
|
|
23
|
+
proc;
|
|
24
|
+
constructor(noTty = false, input = process.stdin, proc = defaultProcessWrapper) {
|
|
25
|
+
this.noTty = noTty;
|
|
26
|
+
this.input = input;
|
|
27
|
+
this.proc = proc;
|
|
28
|
+
if (this.isTTY()) {
|
|
29
|
+
this.input.setRawMode(true);
|
|
30
|
+
this.input.resume();
|
|
31
|
+
this.input.setEncoding('utf8');
|
|
32
|
+
}
|
|
33
|
+
this.setupListeners();
|
|
34
|
+
}
|
|
35
|
+
isTTY() {
|
|
36
|
+
return !this.noTty;
|
|
37
|
+
}
|
|
38
|
+
setupListeners() {
|
|
39
|
+
this.input.on('data', (key) => {
|
|
40
|
+
if (!this.active)
|
|
41
|
+
return;
|
|
42
|
+
const handlers = this.listeners[key];
|
|
43
|
+
handlers?.forEach(handler => handler());
|
|
44
|
+
if (key === exports.KEY_CODES.CTRL_C) { // Ctrl+C
|
|
45
|
+
this.proc.exit(0);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
on(key, callback) {
|
|
50
|
+
if (!this.listeners[key]) {
|
|
51
|
+
this.listeners[key] = [];
|
|
52
|
+
}
|
|
53
|
+
this.listeners[key].push(callback);
|
|
54
|
+
}
|
|
55
|
+
off(key, callback) {
|
|
56
|
+
if (this.listeners[key]) {
|
|
57
|
+
const index = this.listeners[key].indexOf(callback);
|
|
58
|
+
if (index !== -1) {
|
|
59
|
+
this.listeners[key].splice(index, 1);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
pause() {
|
|
64
|
+
this.active = false;
|
|
65
|
+
}
|
|
66
|
+
resume() {
|
|
67
|
+
this.active = true;
|
|
68
|
+
}
|
|
69
|
+
destroy() {
|
|
70
|
+
this.input.setRawMode(false);
|
|
71
|
+
this.input.pause();
|
|
72
|
+
this.input.removeAllListeners('data');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.TerminalKeypress = TerminalKeypress;
|
package/package.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function readAndParsePackageJson(): any;
|
package/package.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.readAndParsePackageJson = void 0;
|
|
4
|
+
const fs_1 = require("fs");
|
|
5
|
+
const path_1 = require("path");
|
|
6
|
+
// need to search due to the dist/ folder and src/, etc.
|
|
7
|
+
function findPackageJson(currentDir) {
|
|
8
|
+
const filePath = (0, path_1.join)(currentDir, 'package.json');
|
|
9
|
+
// Check if package.json exists in the current directory
|
|
10
|
+
if ((0, fs_1.existsSync)(filePath)) {
|
|
11
|
+
return filePath;
|
|
12
|
+
}
|
|
13
|
+
// Get the parent directory
|
|
14
|
+
const parentDir = (0, path_1.dirname)(currentDir);
|
|
15
|
+
// If reached the root directory, package.json is not found
|
|
16
|
+
if (parentDir === currentDir) {
|
|
17
|
+
throw new Error('package.json not found in any parent directory');
|
|
18
|
+
}
|
|
19
|
+
// Recursively look in the parent directory
|
|
20
|
+
return findPackageJson(parentDir);
|
|
21
|
+
}
|
|
22
|
+
function readAndParsePackageJson() {
|
|
23
|
+
// Start searching from the current directory
|
|
24
|
+
const pkgPath = findPackageJson(__dirname);
|
|
25
|
+
// Read and parse the package.json
|
|
26
|
+
const str = (0, fs_1.readFileSync)(pkgPath, 'utf8');
|
|
27
|
+
const pkg = JSON.parse(str);
|
|
28
|
+
return pkg;
|
|
29
|
+
}
|
|
30
|
+
exports.readAndParsePackageJson = readAndParsePackageJson;
|
package/package.json
CHANGED
|
@@ -1,44 +1,39 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "inquirerer",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
"types": "
|
|
3
|
+
"version": "0.4.0",
|
|
4
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
5
|
+
"description": "inquirerer",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/pyramation/inquirerer",
|
|
10
|
+
"license": "SEE LICENSE IN LICENSE",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/pyramation/inquirerer"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/pyramation/inquirerer/issues"
|
|
21
|
+
},
|
|
9
22
|
"scripts": {
|
|
10
|
-
"
|
|
23
|
+
"copy": "copyfiles -f ../../LICENSE README.md package.json dist",
|
|
24
|
+
"clean": "del dist/**",
|
|
25
|
+
"prepare": "npm run build",
|
|
26
|
+
"build": "npm run clean; tsc; tsc -p tsconfig.esm.json; npm run copy",
|
|
27
|
+
"dev": "ts-node dev/index",
|
|
11
28
|
"test": "jest",
|
|
12
29
|
"test:watch": "jest --watch"
|
|
13
30
|
},
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"@types/jest": "^21.1.0",
|
|
16
|
-
"@types/node": "^8.0.0",
|
|
17
|
-
"jest": "^22.4.0",
|
|
18
|
-
"ts-jest": "^21.0.0",
|
|
19
|
-
"ts-node": "^5.0.0",
|
|
20
|
-
"tslint": "^5.4.3",
|
|
21
|
-
"typescript": "^2.7.1"
|
|
22
|
-
},
|
|
23
31
|
"dependencies": {
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
32
|
+
"chalk": "^4.1.0",
|
|
33
|
+
"deepmerge": "^4.3.1",
|
|
34
|
+
"js-yaml": "^4.1.0",
|
|
35
|
+
"minimist": "^1.2.8"
|
|
27
36
|
},
|
|
28
|
-
"
|
|
29
|
-
|
|
30
|
-
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
|
|
31
|
-
},
|
|
32
|
-
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
|
|
33
|
-
"moduleFileExtensions": ["ts", "tsx", "js"],
|
|
34
|
-
"coveragePathIgnorePatterns": ["/node_modules/", "/test/"],
|
|
35
|
-
"coverageThreshold": {
|
|
36
|
-
"global": {
|
|
37
|
-
"branches": 90,
|
|
38
|
-
"functions": 95,
|
|
39
|
-
"lines": 95,
|
|
40
|
-
"statements": 95
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
37
|
+
"keywords": [],
|
|
38
|
+
"gitHead": "a8472a9c3d0b5b68dcf647bf68b76e39c4a0c856"
|
|
44
39
|
}
|
package/prompt.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { Writable, Readable } from 'stream';
|
|
3
|
+
import { AutocompleteQuestion, CheckboxQuestion, ConfirmQuestion, Question, TextQuestion, Value } from './question';
|
|
4
|
+
interface PromptContext {
|
|
5
|
+
numTries: number;
|
|
6
|
+
}
|
|
7
|
+
export declare class Inquirerer {
|
|
8
|
+
private rl;
|
|
9
|
+
private keypress;
|
|
10
|
+
private noTty;
|
|
11
|
+
private output;
|
|
12
|
+
constructor(noTty?: boolean, input?: Readable, output?: Writable);
|
|
13
|
+
clearScreen(): void;
|
|
14
|
+
write(message: string): void;
|
|
15
|
+
log(message: string): void;
|
|
16
|
+
prompt<T extends object>(params: T, questions: Question[], usageText?: string): Promise<T>;
|
|
17
|
+
confirm(question: ConfirmQuestion, ctx: PromptContext): Promise<boolean>;
|
|
18
|
+
text(question: TextQuestion, ctx: PromptContext): Promise<string | null>;
|
|
19
|
+
checkbox(question: CheckboxQuestion, ctx: PromptContext): Promise<Value[]>;
|
|
20
|
+
autocomplete(question: AutocompleteQuestion, ctx: PromptContext): Promise<string>;
|
|
21
|
+
filterOptions(options: string[], input: string): string[];
|
|
22
|
+
close(): void;
|
|
23
|
+
}
|
|
24
|
+
export {};
|
package/prompt.js
ADDED
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Inquirerer = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const readline_1 = __importDefault(require("readline"));
|
|
9
|
+
const keypress_1 = require("./keypress");
|
|
10
|
+
const requiredMessage = (question) => chalk_1.default.red(`The field "${question.name}" is required. Please provide a value.\n`);
|
|
11
|
+
function generatePromptMessage(question, ctx) {
|
|
12
|
+
let promptMessage = question.message ? `${question.message}\n> ` : `${question.name}:\n> `;
|
|
13
|
+
if (ctx.numTries > 0 && question.required) {
|
|
14
|
+
promptMessage = requiredMessage(question) + promptMessage;
|
|
15
|
+
}
|
|
16
|
+
switch (question.type) {
|
|
17
|
+
case 'confirm':
|
|
18
|
+
promptMessage += `(y/n)${question.default !== undefined ? ` [${question.default ? 'y' : 'n'}]` : ''}`;
|
|
19
|
+
break;
|
|
20
|
+
case 'text':
|
|
21
|
+
if (question.default) {
|
|
22
|
+
promptMessage += ` [${question.default}]`;
|
|
23
|
+
}
|
|
24
|
+
break;
|
|
25
|
+
case 'autocomplete':
|
|
26
|
+
case 'checkbox':
|
|
27
|
+
// For these types, you might want to show the default selected options if any
|
|
28
|
+
if (question.options && question.default) {
|
|
29
|
+
const defaultOptions = Array.isArray(question.default) ? question.default : [question.default];
|
|
30
|
+
const defaultText = defaultOptions.join(', ');
|
|
31
|
+
promptMessage += ` [${defaultText}]`;
|
|
32
|
+
}
|
|
33
|
+
break;
|
|
34
|
+
}
|
|
35
|
+
return promptMessage;
|
|
36
|
+
}
|
|
37
|
+
class Inquirerer {
|
|
38
|
+
rl;
|
|
39
|
+
keypress;
|
|
40
|
+
noTty;
|
|
41
|
+
output;
|
|
42
|
+
constructor(noTty = false, input = process.stdin, output = process.stdout) {
|
|
43
|
+
this.noTty = noTty;
|
|
44
|
+
this.output = output;
|
|
45
|
+
if (!noTty) {
|
|
46
|
+
this.rl = readline_1.default.createInterface({
|
|
47
|
+
input,
|
|
48
|
+
output
|
|
49
|
+
});
|
|
50
|
+
this.keypress = new keypress_1.TerminalKeypress(noTty, input);
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
this.rl = null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
clearScreen() {
|
|
57
|
+
// same as console.clear()
|
|
58
|
+
this.output.write('\x1Bc'); // This is the escape sequence to clear the terminal screen.
|
|
59
|
+
}
|
|
60
|
+
write(message) {
|
|
61
|
+
this.output.write(message);
|
|
62
|
+
}
|
|
63
|
+
log(message) {
|
|
64
|
+
this.output.write(message + '\n');
|
|
65
|
+
}
|
|
66
|
+
async prompt(params, questions, usageText) {
|
|
67
|
+
const obj = { ...params };
|
|
68
|
+
// when interactive and missing a bunch of stuff, we should display to the user
|
|
69
|
+
if (usageText && Object.values(params).some(value => value === undefined) && !this.noTty) {
|
|
70
|
+
this.log(usageText);
|
|
71
|
+
}
|
|
72
|
+
const needsContinue = (question) => {
|
|
73
|
+
const value = obj[question.name];
|
|
74
|
+
return (value === undefined ||
|
|
75
|
+
value === null ||
|
|
76
|
+
(typeof value === 'string' && value.trim().length === 0) // Check for empty string, safely trimming it
|
|
77
|
+
);
|
|
78
|
+
};
|
|
79
|
+
let index = 0;
|
|
80
|
+
let numTries = 0;
|
|
81
|
+
while (index < questions.length) {
|
|
82
|
+
const question = questions[index];
|
|
83
|
+
const ctx = {
|
|
84
|
+
numTries
|
|
85
|
+
};
|
|
86
|
+
if (needsContinue(question)) {
|
|
87
|
+
switch (question.type) {
|
|
88
|
+
case 'confirm':
|
|
89
|
+
obj[question.name] = await this.confirm(question, ctx);
|
|
90
|
+
break;
|
|
91
|
+
case 'checkbox':
|
|
92
|
+
obj[question.name] = await this.checkbox(question, ctx);
|
|
93
|
+
break;
|
|
94
|
+
case 'autocomplete':
|
|
95
|
+
obj[question.name] = await this.autocomplete(question, ctx);
|
|
96
|
+
break;
|
|
97
|
+
case 'text':
|
|
98
|
+
default:
|
|
99
|
+
obj[question.name] = await this.text(question, ctx); // Use promptText instead of text
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
// Check if the question is required and the response is not adequate
|
|
103
|
+
if (question.required && needsContinue(question)) {
|
|
104
|
+
// Reset the property to undefined to re-trigger the prompt
|
|
105
|
+
numTries++;
|
|
106
|
+
obj[question.name] = undefined;
|
|
107
|
+
continue; // Stay on the same question
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
index++; // Move to the next question
|
|
111
|
+
numTries = 0;
|
|
112
|
+
}
|
|
113
|
+
return obj;
|
|
114
|
+
}
|
|
115
|
+
async confirm(question, ctx) {
|
|
116
|
+
if (this.noTty || !this.rl)
|
|
117
|
+
return question.default ?? false; // Return default if non-interactive
|
|
118
|
+
return new Promise((resolve) => {
|
|
119
|
+
// Construct the prompt with the default option indicated
|
|
120
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
121
|
+
this.rl.question(promptMessage, (answer) => {
|
|
122
|
+
const userInput = answer.trim().toLowerCase();
|
|
123
|
+
if (userInput === '') {
|
|
124
|
+
resolve(question.default ?? false); // Use default value if input is empty
|
|
125
|
+
}
|
|
126
|
+
else if (['yes', 'y'].includes(userInput)) {
|
|
127
|
+
resolve(true);
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
resolve(false);
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async text(question, ctx) {
|
|
136
|
+
if (this.noTty || !this.rl)
|
|
137
|
+
return question.default ?? null; // Return default if non-interactive
|
|
138
|
+
let userInput = '';
|
|
139
|
+
return new Promise((resolve) => {
|
|
140
|
+
this.clearScreen(); // Clear the console at the beginning of each input session
|
|
141
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
142
|
+
this.rl.question(promptMessage, (answer) => {
|
|
143
|
+
userInput = answer;
|
|
144
|
+
if (userInput.trim() !== '') {
|
|
145
|
+
resolve(userInput); // Return input if not empty
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
resolve(null); // Return null if empty and not required
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
async checkbox(question, ctx) {
|
|
154
|
+
if (this.noTty || !this.rl)
|
|
155
|
+
return question.default ?? []; // Return default if non-interactive
|
|
156
|
+
this.keypress.resume();
|
|
157
|
+
const options = question.options || [];
|
|
158
|
+
let input = ''; // Search input
|
|
159
|
+
let filteredOptions = options;
|
|
160
|
+
let selectedIndex = 0;
|
|
161
|
+
let startIndex = 0; // Start index for visible options
|
|
162
|
+
const maxLines = question.maxDisplayLines || options.length; // Use provided max or total options
|
|
163
|
+
const selections = new Array(options.length).fill(false);
|
|
164
|
+
const updateFilteredOptions = () => {
|
|
165
|
+
filteredOptions = options.filter(option => option.toLowerCase().includes(input.toLowerCase()));
|
|
166
|
+
};
|
|
167
|
+
const display = () => {
|
|
168
|
+
this.clearScreen();
|
|
169
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
170
|
+
this.write(promptMessage);
|
|
171
|
+
this.log(`${input}`);
|
|
172
|
+
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
173
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
174
|
+
const option = filteredOptions[i];
|
|
175
|
+
const isSelected = selectedIndex === i;
|
|
176
|
+
const marker = isSelected ? '>' : ' ';
|
|
177
|
+
const isChecked = selections[options.indexOf(option)] ? '◉' : '○'; // Use the original index in options
|
|
178
|
+
const line = `${marker} ${isChecked} ${option}`;
|
|
179
|
+
this.log(isSelected ? chalk_1.default.blue(line) : line);
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
display();
|
|
183
|
+
// Handling BACKSPACE key
|
|
184
|
+
this.keypress.on(keypress_1.KEY_CODES.BACKSPACE, () => {
|
|
185
|
+
input = input.slice(0, -1);
|
|
186
|
+
updateFilteredOptions();
|
|
187
|
+
display();
|
|
188
|
+
});
|
|
189
|
+
// Register alphanumeric keypresses to accumulate input, excluding space
|
|
190
|
+
'abcdefghijklmnopqrstuvwxyz0123456789'.split('').forEach(char => {
|
|
191
|
+
this.keypress.on(char, () => {
|
|
192
|
+
input += char;
|
|
193
|
+
updateFilteredOptions();
|
|
194
|
+
display();
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
|
|
198
|
+
selectedIndex = selectedIndex > 0 ? selectedIndex - 1 : filteredOptions.length - 1;
|
|
199
|
+
if (selectedIndex < startIndex) {
|
|
200
|
+
startIndex = selectedIndex; // Scroll up
|
|
201
|
+
}
|
|
202
|
+
else if (selectedIndex === filteredOptions.length - 1) {
|
|
203
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines); // Jump to the bottom of the list
|
|
204
|
+
}
|
|
205
|
+
display();
|
|
206
|
+
});
|
|
207
|
+
this.keypress.on(keypress_1.KEY_CODES.DOWN_ARROW, () => {
|
|
208
|
+
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
|
|
209
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
210
|
+
startIndex = selectedIndex - maxLines + 1; // Scroll down
|
|
211
|
+
}
|
|
212
|
+
else if (selectedIndex === 0) {
|
|
213
|
+
startIndex = 0; // Jump to the top of the list
|
|
214
|
+
}
|
|
215
|
+
display();
|
|
216
|
+
});
|
|
217
|
+
this.keypress.on(keypress_1.KEY_CODES.SPACE, () => {
|
|
218
|
+
// Map filtered index back to the original index in options
|
|
219
|
+
selections[options.indexOf(filteredOptions[selectedIndex])] = !selections[options.indexOf(filteredOptions[selectedIndex])];
|
|
220
|
+
display();
|
|
221
|
+
});
|
|
222
|
+
return new Promise(resolve => {
|
|
223
|
+
this.keypress.on(keypress_1.KEY_CODES.ENTER, () => {
|
|
224
|
+
this.keypress.pause();
|
|
225
|
+
const result = [];
|
|
226
|
+
if (question.returnFullResults) {
|
|
227
|
+
// Return all options with their selected status
|
|
228
|
+
options.forEach((option, index) => {
|
|
229
|
+
result.push({
|
|
230
|
+
name: option,
|
|
231
|
+
value: selections[index]
|
|
232
|
+
});
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// Return only options that are selected
|
|
237
|
+
options.forEach((option, index) => {
|
|
238
|
+
if (selections[index]) {
|
|
239
|
+
result.push({
|
|
240
|
+
name: option,
|
|
241
|
+
value: selections[index]
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
resolve(result);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
async autocomplete(question, ctx) {
|
|
251
|
+
if (this.noTty || !this.rl)
|
|
252
|
+
return question.default ?? false; // Return default if non-interactive
|
|
253
|
+
this.keypress.resume();
|
|
254
|
+
const options = question.options || [];
|
|
255
|
+
let input = '';
|
|
256
|
+
let filteredOptions = options;
|
|
257
|
+
let selectedIndex = 0;
|
|
258
|
+
let startIndex = 0; // Start index for visible options
|
|
259
|
+
const maxLines = question.maxDisplayLines || options.length; // Use provided max or total options
|
|
260
|
+
const display = () => {
|
|
261
|
+
this.clearScreen();
|
|
262
|
+
const promptMessage = generatePromptMessage(question, ctx);
|
|
263
|
+
this.log(promptMessage);
|
|
264
|
+
// Determine the range of options to display
|
|
265
|
+
const endIndex = Math.min(startIndex + maxLines, filteredOptions.length);
|
|
266
|
+
for (let i = startIndex; i < endIndex; i++) {
|
|
267
|
+
const option = filteredOptions[i];
|
|
268
|
+
if (i === selectedIndex) {
|
|
269
|
+
this.log(chalk_1.default.blue('> ' + option)); // Highlight the selected option with chalk
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
this.log(' ' + option);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
const updateFilteredOptions = () => {
|
|
277
|
+
filteredOptions = this.filterOptions(options, input);
|
|
278
|
+
// Adjust startIndex to keep the selectedIndex in the visible range
|
|
279
|
+
if (selectedIndex < startIndex) {
|
|
280
|
+
startIndex = selectedIndex;
|
|
281
|
+
}
|
|
282
|
+
else if (selectedIndex >= startIndex + maxLines) {
|
|
283
|
+
startIndex = selectedIndex - maxLines + 1;
|
|
284
|
+
}
|
|
285
|
+
if (selectedIndex >= filteredOptions.length) {
|
|
286
|
+
selectedIndex = Math.max(filteredOptions.length - 1, 0);
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
display();
|
|
290
|
+
// Handling BACKSPACE key
|
|
291
|
+
this.keypress.on(keypress_1.KEY_CODES.BACKSPACE, () => {
|
|
292
|
+
input = input.slice(0, -1);
|
|
293
|
+
updateFilteredOptions();
|
|
294
|
+
display();
|
|
295
|
+
});
|
|
296
|
+
// Register alphanumeric and space keypresses to accumulate input
|
|
297
|
+
'abcdefghijklmnopqrstuvwxyz0123456789 '.split('').forEach(char => {
|
|
298
|
+
this.keypress.on(char, () => {
|
|
299
|
+
input += char;
|
|
300
|
+
updateFilteredOptions();
|
|
301
|
+
display();
|
|
302
|
+
});
|
|
303
|
+
});
|
|
304
|
+
// Navigation
|
|
305
|
+
this.keypress.on(keypress_1.KEY_CODES.UP_ARROW, () => {
|
|
306
|
+
selectedIndex = selectedIndex - 1 >= 0 ? selectedIndex - 1 : filteredOptions.length - 1;
|
|
307
|
+
if (selectedIndex < startIndex) {
|
|
308
|
+
startIndex = selectedIndex; // Scroll up
|
|
309
|
+
}
|
|
310
|
+
else if (selectedIndex === filteredOptions.length - 1) {
|
|
311
|
+
startIndex = Math.max(0, filteredOptions.length - maxLines); // Jump to the bottom of the list
|
|
312
|
+
}
|
|
313
|
+
display();
|
|
314
|
+
});
|
|
315
|
+
this.keypress.on(keypress_1.KEY_CODES.DOWN_ARROW, () => {
|
|
316
|
+
selectedIndex = (selectedIndex + 1) % filteredOptions.length;
|
|
317
|
+
if (selectedIndex >= startIndex + maxLines) {
|
|
318
|
+
startIndex = selectedIndex - maxLines + 1; // Scroll down
|
|
319
|
+
}
|
|
320
|
+
else if (selectedIndex === 0) {
|
|
321
|
+
startIndex = 0; // Jump to the top of the list
|
|
322
|
+
}
|
|
323
|
+
display();
|
|
324
|
+
});
|
|
325
|
+
return new Promise(resolve => {
|
|
326
|
+
this.keypress.on(keypress_1.KEY_CODES.ENTER, () => {
|
|
327
|
+
this.keypress.pause();
|
|
328
|
+
resolve(filteredOptions[selectedIndex] || input);
|
|
329
|
+
});
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
filterOptions(options, input) {
|
|
333
|
+
return options
|
|
334
|
+
.filter(option => option.toLowerCase().startsWith(input.toLowerCase()))
|
|
335
|
+
.sort();
|
|
336
|
+
}
|
|
337
|
+
// Method to cleanly close the readline interface
|
|
338
|
+
close() {
|
|
339
|
+
if (this.rl) {
|
|
340
|
+
this.rl.close();
|
|
341
|
+
this.keypress.destroy();
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
exports.Inquirerer = Inquirerer;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './types';
|
|
@@ -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("./types"), exports);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export interface Value {
|
|
2
|
+
name: string;
|
|
3
|
+
value: boolean;
|
|
4
|
+
}
|
|
5
|
+
export interface BaseQuestion {
|
|
6
|
+
name: string;
|
|
7
|
+
type: string;
|
|
8
|
+
default?: any;
|
|
9
|
+
required?: boolean;
|
|
10
|
+
message?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface ConfirmQuestion extends BaseQuestion {
|
|
13
|
+
type: 'confirm';
|
|
14
|
+
default?: boolean;
|
|
15
|
+
}
|
|
16
|
+
export interface AutocompleteQuestion extends BaseQuestion {
|
|
17
|
+
type: 'autocomplete';
|
|
18
|
+
options: string[];
|
|
19
|
+
maxDisplayLines?: number;
|
|
20
|
+
returnFullResults?: boolean;
|
|
21
|
+
allowCustomOptions?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface CheckboxQuestion extends BaseQuestion {
|
|
24
|
+
type: 'checkbox';
|
|
25
|
+
options: string[];
|
|
26
|
+
maxDisplayLines?: number;
|
|
27
|
+
returnFullResults?: boolean;
|
|
28
|
+
default?: Value[];
|
|
29
|
+
}
|
|
30
|
+
export interface TextQuestion extends BaseQuestion {
|
|
31
|
+
type: 'text';
|
|
32
|
+
default?: string;
|
|
33
|
+
}
|
|
34
|
+
export type Question = ConfirmQuestion | AutocompleteQuestion | CheckboxQuestion | TextQuestion;
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function displayVersion(): void;
|
package/utils.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.displayVersion = void 0;
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const package_1 = require("./package");
|
|
9
|
+
// Function to display the version information
|
|
10
|
+
function displayVersion() {
|
|
11
|
+
const pkg = (0, package_1.readAndParsePackageJson)();
|
|
12
|
+
console.log(chalk_1.default.green(`Name: ${pkg.name}`));
|
|
13
|
+
console.log(chalk_1.default.blue(`Version: ${pkg.version}`));
|
|
14
|
+
}
|
|
15
|
+
exports.displayVersion = displayVersion;
|