@inquirer/core 0.0.21-alpha.0 → 0.0.22-alpha.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/dist/index.d.ts +2 -2
- package/package.json +5 -2
- package/src/index.ts +0 -163
- package/src/lib/Paginator.ts +0 -45
- package/src/lib/index.d.ts +0 -3
- package/src/lib/key.ts +0 -20
- package/src/lib/options.ts +0 -14
- package/src/lib/prefix.ts +0 -26
- package/src/lib/readline.ts +0 -55
- package/src/lib/screen-manager.ts +0 -99
- package/src/lib/utils.ts +0 -15
- package/test.ts +0 -92
- package/tsconfig.json +0 -7
package/dist/index.d.ts
CHANGED
|
@@ -25,10 +25,10 @@ export declare function useRef<Value>(val: Value): {
|
|
|
25
25
|
};
|
|
26
26
|
export declare type AsyncPromptConfig = {
|
|
27
27
|
message: string | Promise<string> | (() => Promise<string>);
|
|
28
|
-
validate?: () => boolean
|
|
28
|
+
validate?: (value: string) => boolean | string | Promise<string | boolean>;
|
|
29
29
|
};
|
|
30
30
|
export declare type ResolvedPromptConfig = {
|
|
31
31
|
message: string;
|
|
32
|
-
validate: () => boolean
|
|
32
|
+
validate: (value: string) => boolean | string | Promise<string | boolean>;
|
|
33
33
|
};
|
|
34
34
|
export declare function createPrompt<Value, Config extends AsyncPromptConfig>(view: (config: Config & ResolvedPromptConfig, done: (value: Value) => void) => string | [string, string | undefined]): (options: Config, stdio?: StdioOptions) => Promise<Value>;
|
package/package.json
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.0.
|
|
4
|
+
"version": "0.0.22-alpha.0",
|
|
5
5
|
"description": "Core Inquirer prompt API",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"typings": "dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/"
|
|
10
|
+
],
|
|
8
11
|
"repository": "SBoudrias/Inquirer.js",
|
|
9
12
|
"keywords": [
|
|
10
13
|
"cli",
|
|
@@ -32,5 +35,5 @@
|
|
|
32
35
|
"publishConfig": {
|
|
33
36
|
"access": "public"
|
|
34
37
|
},
|
|
35
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "1d5e7e3d15c86851f2e5df2bb9f55084ba231c54"
|
|
36
39
|
}
|
package/src/index.ts
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import readline from 'node:readline';
|
|
2
|
-
import MuteStream from 'mute-stream';
|
|
3
|
-
import ScreenManager from './lib/screen-manager.js';
|
|
4
|
-
import { getPromptConfig } from './lib/options.js';
|
|
5
|
-
|
|
6
|
-
export { usePrefix } from './lib/prefix.js';
|
|
7
|
-
export * from './lib/key.js';
|
|
8
|
-
export * from './lib/Paginator.js';
|
|
9
|
-
|
|
10
|
-
type StdioOptions = {
|
|
11
|
-
input?: NodeJS.ReadableStream;
|
|
12
|
-
output?: NodeJS.WritableStream;
|
|
13
|
-
};
|
|
14
|
-
|
|
15
|
-
export type InquirerReadline = readline.ReadLine & {
|
|
16
|
-
output: MuteStream;
|
|
17
|
-
input: NodeJS.ReadableStream;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export type KeypressEvent = {
|
|
21
|
-
name: string;
|
|
22
|
-
ctrl: boolean;
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
let sessionRl: InquirerReadline | void;
|
|
26
|
-
let hooks: any[] = [];
|
|
27
|
-
const hooksCleanup: any[] = [];
|
|
28
|
-
let index = 0;
|
|
29
|
-
let handleChange = () => {};
|
|
30
|
-
|
|
31
|
-
const cleanupHook = (index: number) => {
|
|
32
|
-
const cleanFn = hooksCleanup[index];
|
|
33
|
-
if (typeof cleanFn === 'function') {
|
|
34
|
-
cleanFn();
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
export function useState<Value>(defaultValue: Value): [Value, (newValue: Value) => void] {
|
|
39
|
-
const _idx = index;
|
|
40
|
-
const value = _idx in hooks ? hooks[_idx] : defaultValue;
|
|
41
|
-
|
|
42
|
-
index++;
|
|
43
|
-
|
|
44
|
-
return [
|
|
45
|
-
value,
|
|
46
|
-
(newValue) => {
|
|
47
|
-
hooks[_idx] = newValue;
|
|
48
|
-
|
|
49
|
-
// Trigger re-render
|
|
50
|
-
handleChange();
|
|
51
|
-
},
|
|
52
|
-
];
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export function useEffect(cb: () => void | (() => void), depArray: unknown[]): void {
|
|
56
|
-
const _idx = index;
|
|
57
|
-
|
|
58
|
-
const oldDeps = hooks[_idx];
|
|
59
|
-
let hasChanged = true;
|
|
60
|
-
if (oldDeps) {
|
|
61
|
-
hasChanged = depArray.some((dep, i) => !Object.is(dep, oldDeps[i]));
|
|
62
|
-
}
|
|
63
|
-
if (hasChanged) {
|
|
64
|
-
cleanupHook(_idx);
|
|
65
|
-
hooksCleanup[_idx] = cb();
|
|
66
|
-
}
|
|
67
|
-
hooks[_idx] = depArray;
|
|
68
|
-
|
|
69
|
-
index++;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export function useKeypress(
|
|
73
|
-
userHandler: (event: KeypressEvent, rl: InquirerReadline) => void
|
|
74
|
-
) {
|
|
75
|
-
const rl = sessionRl;
|
|
76
|
-
|
|
77
|
-
if (!rl) {
|
|
78
|
-
throw new Error('useKeypress must be used within a prompt');
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
useEffect(() => {
|
|
82
|
-
const handler = (_input: string, event: KeypressEvent) => {
|
|
83
|
-
userHandler(event, rl);
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
rl.input.on('keypress', handler);
|
|
87
|
-
return () => {
|
|
88
|
-
rl.input.removeListener('keypress', handler);
|
|
89
|
-
};
|
|
90
|
-
}, [userHandler]);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export function useRef<Value>(val: Value): { current: Value } {
|
|
94
|
-
return useState({ current: val })[0];
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
export type AsyncPromptConfig = {
|
|
98
|
-
message: string | Promise<string> | (() => Promise<string>);
|
|
99
|
-
validate?: () => boolean;
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export type ResolvedPromptConfig = {
|
|
103
|
-
message: string;
|
|
104
|
-
validate: () => boolean;
|
|
105
|
-
};
|
|
106
|
-
|
|
107
|
-
export function createPrompt<Value, Config extends AsyncPromptConfig>(
|
|
108
|
-
view: (
|
|
109
|
-
config: Config & ResolvedPromptConfig,
|
|
110
|
-
done: (value: Value) => void
|
|
111
|
-
) => string | [string, string | undefined]
|
|
112
|
-
) {
|
|
113
|
-
return async function (options: Config, stdio?: StdioOptions): Promise<Value> {
|
|
114
|
-
// Default `input` to stdin
|
|
115
|
-
const input = stdio?.input ?? process.stdin;
|
|
116
|
-
|
|
117
|
-
// Add mute capabilities to the output
|
|
118
|
-
const output = new MuteStream();
|
|
119
|
-
output.pipe(stdio?.output ?? process.stdout);
|
|
120
|
-
|
|
121
|
-
const rl = readline.createInterface({
|
|
122
|
-
terminal: true,
|
|
123
|
-
input,
|
|
124
|
-
output,
|
|
125
|
-
}) as InquirerReadline;
|
|
126
|
-
const screen = new ScreenManager(rl);
|
|
127
|
-
|
|
128
|
-
// TODO: we should display a loader while we get the default options.
|
|
129
|
-
const resolvedConfig = await getPromptConfig(options);
|
|
130
|
-
|
|
131
|
-
return new Promise((resolve) => {
|
|
132
|
-
const done = (value: Value) => {
|
|
133
|
-
let len = cleanupHook.length;
|
|
134
|
-
while (len--) {
|
|
135
|
-
cleanupHook(len);
|
|
136
|
-
}
|
|
137
|
-
screen.done();
|
|
138
|
-
|
|
139
|
-
// Reset hooks state
|
|
140
|
-
hooks = [];
|
|
141
|
-
index = 0;
|
|
142
|
-
sessionRl = undefined;
|
|
143
|
-
|
|
144
|
-
// Finally we resolve our promise
|
|
145
|
-
resolve(value);
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
hooks = [];
|
|
149
|
-
const workLoop = (config: Config & ResolvedPromptConfig) => {
|
|
150
|
-
sessionRl = rl;
|
|
151
|
-
index = 0;
|
|
152
|
-
handleChange = () => workLoop(config);
|
|
153
|
-
|
|
154
|
-
const nextView = view(config, done);
|
|
155
|
-
const [content, bottomContent] =
|
|
156
|
-
typeof nextView === 'string' ? [nextView] : nextView;
|
|
157
|
-
screen.render(content, bottomContent);
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
workLoop(resolvedConfig);
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
}
|
package/src/lib/Paginator.ts
DELETED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import cliWidth from 'cli-width';
|
|
3
|
-
import { breakLines } from './utils.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* The paginator keeps track of a pointer index in a list and returns
|
|
7
|
-
* a subset of the choices if the list is too long.
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
export class Paginator {
|
|
11
|
-
pointer = 0;
|
|
12
|
-
lastIndex = 0;
|
|
13
|
-
|
|
14
|
-
paginate(output: string, active: number, pageSize: number = 7) {
|
|
15
|
-
const middleOfList = Math.floor(pageSize / 2);
|
|
16
|
-
|
|
17
|
-
// TODO: I've remove the dependency on readline here. But we should refactor the
|
|
18
|
-
// paginator to also rely on hook.
|
|
19
|
-
const width = cliWidth({ defaultWidth: 80, output: process.stdout });
|
|
20
|
-
const lines = breakLines(output, width).split('\n');
|
|
21
|
-
|
|
22
|
-
// Make sure there's enough lines to paginate
|
|
23
|
-
if (lines.length <= pageSize) {
|
|
24
|
-
return output;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
// Move the pointer only when the user go down and limit it to the middle of the list
|
|
28
|
-
if (
|
|
29
|
-
this.pointer < middleOfList &&
|
|
30
|
-
this.lastIndex < active &&
|
|
31
|
-
active - this.lastIndex < pageSize
|
|
32
|
-
) {
|
|
33
|
-
this.pointer = Math.min(middleOfList, this.pointer + active - this.lastIndex);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
this.lastIndex = active;
|
|
37
|
-
|
|
38
|
-
// Duplicate the lines so it give an infinite list look
|
|
39
|
-
const infinite = [lines, lines, lines].flat();
|
|
40
|
-
const topIndex = Math.max(0, active + lines.length - this.pointer);
|
|
41
|
-
|
|
42
|
-
const section = infinite.splice(topIndex, pageSize).join('\n');
|
|
43
|
-
return section + '\n' + chalk.dim('(Move up and down to reveal more choices)');
|
|
44
|
-
}
|
|
45
|
-
}
|
package/src/lib/index.d.ts
DELETED
package/src/lib/key.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
type KeypressEvent = {
|
|
2
|
-
name: string;
|
|
3
|
-
ctrl: boolean;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export const isUpKey = (key: KeypressEvent): boolean =>
|
|
7
|
-
key.name === 'up' || key.name === 'k' || (key.name === 'p' && key.ctrl);
|
|
8
|
-
|
|
9
|
-
export const isDownKey = (key: KeypressEvent): boolean =>
|
|
10
|
-
key.name === 'down' || key.name === 'j' || (key.name === 'n' && key.ctrl);
|
|
11
|
-
|
|
12
|
-
export const isSpaceKey = (key: KeypressEvent): boolean => key.name === 'space';
|
|
13
|
-
|
|
14
|
-
export const isBackspaceKey = (key: KeypressEvent): boolean => key.name === 'backspace';
|
|
15
|
-
|
|
16
|
-
export const isNumberKey = (key: KeypressEvent): boolean =>
|
|
17
|
-
'123456789'.includes(key.name);
|
|
18
|
-
|
|
19
|
-
export const isEnterKey = (key: KeypressEvent): boolean =>
|
|
20
|
-
key.name === 'enter' || key.name === 'return';
|
package/src/lib/options.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import type { AsyncPromptConfig, ResolvedPromptConfig } from '../index.js';
|
|
2
|
-
|
|
3
|
-
export async function getPromptConfig<In extends AsyncPromptConfig>(
|
|
4
|
-
option: In
|
|
5
|
-
): Promise<In & ResolvedPromptConfig> {
|
|
6
|
-
const message =
|
|
7
|
-
typeof option.message === 'function' ? option.message() : option.message;
|
|
8
|
-
|
|
9
|
-
return {
|
|
10
|
-
validate: () => true,
|
|
11
|
-
...option,
|
|
12
|
-
message: await message,
|
|
13
|
-
};
|
|
14
|
-
}
|
package/src/lib/prefix.ts
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import chalk from 'chalk';
|
|
2
|
-
import spinners from 'cli-spinners';
|
|
3
|
-
import { useState, useEffect } from '../index.js';
|
|
4
|
-
|
|
5
|
-
const spinner = spinners.dots;
|
|
6
|
-
|
|
7
|
-
export function usePrefix(isLoading: boolean = false): string {
|
|
8
|
-
const [tick, setTick] = useState(0);
|
|
9
|
-
|
|
10
|
-
useEffect((): void | (() => unknown) => {
|
|
11
|
-
if (isLoading) {
|
|
12
|
-
const timeout = setTimeout(() => {
|
|
13
|
-
setTick(tick + 1);
|
|
14
|
-
}, spinner.interval);
|
|
15
|
-
|
|
16
|
-
return () => clearTimeout(timeout);
|
|
17
|
-
}
|
|
18
|
-
}, [isLoading, tick]);
|
|
19
|
-
|
|
20
|
-
if (isLoading) {
|
|
21
|
-
const frame = tick % spinner.frames.length;
|
|
22
|
-
return chalk.yellow(spinner.frames[frame]);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return chalk.green('?');
|
|
26
|
-
}
|
package/src/lib/readline.ts
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import ansiEscapes from 'ansi-escapes';
|
|
2
|
-
import type { InquirerReadline } from '../index.js';
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Move cursor left by `x`
|
|
6
|
-
* @param {Readline} rl - Readline instance
|
|
7
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
8
|
-
*/
|
|
9
|
-
export function left(rl: InquirerReadline, x: number) {
|
|
10
|
-
if (x > 0) {
|
|
11
|
-
rl.output.write(ansiEscapes.cursorBackward(x));
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Move cursor right by `x`
|
|
17
|
-
* @param {Readline} rl - Readline instance
|
|
18
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
19
|
-
*/
|
|
20
|
-
export function right(rl: InquirerReadline, x: number) {
|
|
21
|
-
if (x > 0) {
|
|
22
|
-
rl.output.write(ansiEscapes.cursorForward(x));
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Move cursor up by `x`
|
|
28
|
-
* @param {Readline} rl - Readline instance
|
|
29
|
-
* @param {Number} x - How far to go up (default to 1)
|
|
30
|
-
*/
|
|
31
|
-
export function up(rl: InquirerReadline, x: number) {
|
|
32
|
-
if (x > 0) {
|
|
33
|
-
rl.output.write(ansiEscapes.cursorUp(x));
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Move cursor down by `x`
|
|
39
|
-
* @param {Readline} rl - Readline instance
|
|
40
|
-
* @param {Number} x - How far to go down (default to 1)
|
|
41
|
-
*/
|
|
42
|
-
export function down(rl: InquirerReadline, x: number) {
|
|
43
|
-
if (x > 0) {
|
|
44
|
-
rl.output.write(ansiEscapes.cursorDown(x));
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Clear current line
|
|
50
|
-
* @param {Readline} rl - Readline instance
|
|
51
|
-
* @param {Number} len - number of line to delete
|
|
52
|
-
*/
|
|
53
|
-
export function clearLine(rl: InquirerReadline, len: number) {
|
|
54
|
-
rl.output.write(ansiEscapes.eraseLines(len));
|
|
55
|
-
}
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import cliWidth from 'cli-width';
|
|
2
|
-
import stripAnsi from 'strip-ansi';
|
|
3
|
-
import stringWidth from 'string-width';
|
|
4
|
-
import ansiEscapes from 'ansi-escapes';
|
|
5
|
-
import * as util from './readline.js';
|
|
6
|
-
import { breakLines } from './utils.js';
|
|
7
|
-
import { InquirerReadline } from '../index.js';
|
|
8
|
-
|
|
9
|
-
const height = (content: string): number => content.split('\n').length;
|
|
10
|
-
const lastLine = (content: string): string => content.split('\n').pop() ?? '';
|
|
11
|
-
|
|
12
|
-
export default class ScreenManager {
|
|
13
|
-
// These variables are keeping information to allow correct prompt re-rendering
|
|
14
|
-
private height: number = 0;
|
|
15
|
-
private extraLinesUnderPrompt: number = 0;
|
|
16
|
-
|
|
17
|
-
constructor(private readonly rl: InquirerReadline) {
|
|
18
|
-
this.rl = rl;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
render(content: string, bottomContent: string = '') {
|
|
22
|
-
this.rl.output.unmute();
|
|
23
|
-
this.clean(this.extraLinesUnderPrompt);
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Write message to screen and setPrompt to control backspace
|
|
27
|
-
*/
|
|
28
|
-
|
|
29
|
-
const promptLine = lastLine(content);
|
|
30
|
-
const rawPromptLine = stripAnsi(promptLine);
|
|
31
|
-
|
|
32
|
-
// Remove the rl.line from our prompt. We can't rely on the content of
|
|
33
|
-
// rl.line (mainly because of the password prompt), so just rely on it's
|
|
34
|
-
// length.
|
|
35
|
-
let prompt = rawPromptLine;
|
|
36
|
-
if (this.rl.line.length) {
|
|
37
|
-
prompt = prompt.slice(0, -this.rl.line.length);
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.rl.setPrompt(prompt);
|
|
41
|
-
|
|
42
|
-
// SetPrompt will change cursor position, now we can get correct value
|
|
43
|
-
const cursorPos = (this.rl as any)._getCursorPos();
|
|
44
|
-
const width = cliWidth({ defaultWidth: 80, output: this.rl.output });
|
|
45
|
-
|
|
46
|
-
content = breakLines(content, width);
|
|
47
|
-
bottomContent = breakLines(bottomContent, width);
|
|
48
|
-
|
|
49
|
-
// Manually insert an extra line if we're at the end of the line.
|
|
50
|
-
// This prevent the cursor from appearing at the beginning of the
|
|
51
|
-
// current line.
|
|
52
|
-
if (rawPromptLine.length % width === 0) {
|
|
53
|
-
content += '\n';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const fullContent = content + (bottomContent ? '\n' + bottomContent : '');
|
|
57
|
-
this.rl.output.write(fullContent);
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Re-adjust the cursor at the correct position.
|
|
61
|
-
*/
|
|
62
|
-
|
|
63
|
-
// We need to consider parts of the prompt under the cursor as part of the bottom
|
|
64
|
-
// content in order to correctly cleanup and re-render.
|
|
65
|
-
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
66
|
-
const bottomContentHeight =
|
|
67
|
-
promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
68
|
-
|
|
69
|
-
// Return cursor to the input position (on top of the bottomContent)
|
|
70
|
-
util.up(this.rl, bottomContentHeight);
|
|
71
|
-
// Move cursor at the start of the line, then return to the initial left offset.
|
|
72
|
-
util.left(this.rl, stringWidth(lastLine(fullContent)));
|
|
73
|
-
util.right(this.rl, cursorPos.cols);
|
|
74
|
-
|
|
75
|
-
/**
|
|
76
|
-
* Set up state for next re-rendering
|
|
77
|
-
*/
|
|
78
|
-
this.extraLinesUnderPrompt = bottomContentHeight;
|
|
79
|
-
this.height = height(fullContent);
|
|
80
|
-
|
|
81
|
-
this.rl.output.mute();
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
clean(extraLines: number) {
|
|
85
|
-
util.down(this.rl, extraLines);
|
|
86
|
-
util.clearLine(this.rl, this.height);
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
done() {
|
|
90
|
-
// Reset the cursor at the end of the previously displayed content
|
|
91
|
-
util.down(this.rl, this.extraLinesUnderPrompt);
|
|
92
|
-
|
|
93
|
-
this.rl.setPrompt('');
|
|
94
|
-
this.rl.output.unmute();
|
|
95
|
-
this.rl.output.write('\n');
|
|
96
|
-
this.rl.output.write(ansiEscapes.cursorShow);
|
|
97
|
-
this.rl.close();
|
|
98
|
-
}
|
|
99
|
-
}
|
package/src/lib/utils.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import wrapAnsi from 'wrap-ansi';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Force line returns at specific width. This function is ANSI code friendly and it'll
|
|
5
|
-
* ignore invisible codes during width calculation.
|
|
6
|
-
* @param {string} content
|
|
7
|
-
* @param {number} width
|
|
8
|
-
* @return {string}
|
|
9
|
-
*/
|
|
10
|
-
export const breakLines = (content: string, width: number): string =>
|
|
11
|
-
content
|
|
12
|
-
.split('\n')
|
|
13
|
-
// @ts-expect-error broken type check from library
|
|
14
|
-
.map((line) => wrapAnsi(line, width, { trim: false, hard: true }).split('\n'))
|
|
15
|
-
.join('\n');
|
package/test.ts
DELETED
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
import MuteStream from 'mute-stream';
|
|
2
|
-
import { jest } from '@jest/globals';
|
|
3
|
-
import { Stream } from 'node:stream';
|
|
4
|
-
import {
|
|
5
|
-
createPrompt,
|
|
6
|
-
useKeypress,
|
|
7
|
-
useState,
|
|
8
|
-
isDownKey,
|
|
9
|
-
isUpKey,
|
|
10
|
-
isEnterKey,
|
|
11
|
-
type KeypressEvent,
|
|
12
|
-
} from './src/index.js';
|
|
13
|
-
|
|
14
|
-
describe('createPrompt()', () => {
|
|
15
|
-
it('handle async function message', async () => {
|
|
16
|
-
const render = jest.fn(() => '');
|
|
17
|
-
const input = new MuteStream();
|
|
18
|
-
const prompt = createPrompt(render);
|
|
19
|
-
const promise = Promise.resolve('Async message:');
|
|
20
|
-
prompt({ message: () => promise }, { input });
|
|
21
|
-
|
|
22
|
-
// Initially, we leave a few ms for message to resolve
|
|
23
|
-
expect(render).not.toHaveBeenCalled();
|
|
24
|
-
|
|
25
|
-
await promise;
|
|
26
|
-
await Promise.resolve(); // Wait one extra tick
|
|
27
|
-
expect(render).toHaveBeenLastCalledWith(
|
|
28
|
-
expect.objectContaining({ message: 'Async message:' }),
|
|
29
|
-
expect.any(Function)
|
|
30
|
-
);
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('handle deferred message', async () => {
|
|
34
|
-
const render = jest.fn(() => '');
|
|
35
|
-
const input = new MuteStream();
|
|
36
|
-
const prompt = createPrompt(render);
|
|
37
|
-
const promise = Promise.resolve('Async message:');
|
|
38
|
-
prompt({ message: promise }, { input });
|
|
39
|
-
|
|
40
|
-
// Initially, we leave a few ms for message to resolve
|
|
41
|
-
expect(render).not.toHaveBeenCalled();
|
|
42
|
-
|
|
43
|
-
await promise;
|
|
44
|
-
await Promise.resolve(); // Wait one extra tick
|
|
45
|
-
expect(render).toHaveBeenLastCalledWith(
|
|
46
|
-
expect.objectContaining({ message: 'Async message:' }),
|
|
47
|
-
expect.any(Function)
|
|
48
|
-
);
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it('onKeypress: allow to implement custom behavior on keypress', async () => {
|
|
52
|
-
const Prompt = (config: { message: string }, done: (value: string) => void) => {
|
|
53
|
-
const [value, setValue] = useState('');
|
|
54
|
-
|
|
55
|
-
useKeypress((key: KeypressEvent) => {
|
|
56
|
-
if (isEnterKey(key)) {
|
|
57
|
-
done(value);
|
|
58
|
-
} else if (isDownKey(key)) {
|
|
59
|
-
setValue('down');
|
|
60
|
-
} else if (isUpKey(key)) {
|
|
61
|
-
setValue('up');
|
|
62
|
-
}
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
return `${config.message} ${value}`;
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
const prompt = createPrompt(Prompt);
|
|
69
|
-
const input = new MuteStream();
|
|
70
|
-
const data = jest.fn();
|
|
71
|
-
const output = new Stream.Writable({
|
|
72
|
-
write(chunk, _encoding, next) {
|
|
73
|
-
data(chunk.toString());
|
|
74
|
-
next();
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
const answer = prompt({ message: 'Question' }, { input, output });
|
|
78
|
-
|
|
79
|
-
// Wait for event listeners to be ready
|
|
80
|
-
await Promise.resolve();
|
|
81
|
-
await Promise.resolve();
|
|
82
|
-
|
|
83
|
-
input.emit('keypress', null, { name: 'down' });
|
|
84
|
-
expect(data).toHaveBeenCalledWith('Question down');
|
|
85
|
-
expect(data).not.toHaveBeenCalledWith('Question up');
|
|
86
|
-
input.emit('keypress', null, { name: 'up' });
|
|
87
|
-
expect(data).toHaveBeenCalledWith('Question up');
|
|
88
|
-
input.emit('keypress', null, { name: 'enter' });
|
|
89
|
-
|
|
90
|
-
await expect(answer).resolves.toEqual('up');
|
|
91
|
-
});
|
|
92
|
-
});
|