@inquirer/core 0.0.26-alpha.0 → 0.0.30-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/README.md ADDED
@@ -0,0 +1,139 @@
1
+ # `@inquirer/core`
2
+
3
+ The `@inquirer/core` package is the library enabling the creation of Inquirer prompts.
4
+
5
+ It aims to implements a lightweight API similar to React hooks - but without JSX.
6
+
7
+ # Installation
8
+
9
+ ```sh
10
+ npm install @inquirer/core
11
+
12
+ yarn add @inquirer/core
13
+ ```
14
+
15
+ # Usage
16
+
17
+ ```ts
18
+ import chalk from 'chalk';
19
+ import {
20
+ createPrompt,
21
+ useState,
22
+ useKeypress,
23
+ isEnterKey,
24
+ usePrefix,
25
+ } from '@inquirer/core';
26
+
27
+ const confirm = createPrompt<boolean, { message: string; default?: boolean }>(
28
+ (config, done) => {
29
+ const [status, setStatus] = useState('pending');
30
+ const [value, setValue] = useState('');
31
+ const prefix = usePrefix();
32
+
33
+ useKeypress((key, rl) => {
34
+ if (isEnterKey(key)) {
35
+ const answer = value ? /^y(es)?/i.test(value) : config.default !== false;
36
+ setValue(answer ? 'yes' : 'no');
37
+ setStatus('done');
38
+ done(answer);
39
+ } else {
40
+ setValue(rl.line);
41
+ }
42
+ });
43
+
44
+ let formattedValue = value;
45
+ let defaultValue = '';
46
+ if (status === 'done') {
47
+ formattedValue = chalk.cyan(value);
48
+ } else {
49
+ defaultValue = chalk.dim(config.default === false ? ' (y/N)' : ' (Y/n)');
50
+ }
51
+
52
+ const message = chalk.bold(config.message);
53
+ return `${prefix} ${message}${defaultValue} ${formattedValue}`;
54
+ }
55
+ );
56
+
57
+ /**
58
+ * Which then can be used like this:
59
+ */
60
+ const answer = await confirm({ message: 'Do you want to continue?' });
61
+ ```
62
+
63
+ See more examples:
64
+
65
+ - [Confirm Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/confirm/src/index.ts)
66
+ - [Input Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/input/src/index.ts)
67
+ - [Password Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/password/src/index.ts)
68
+ - [Editor Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/editor/src/index.ts)
69
+ - [Select Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/select/src/index.ts)
70
+ - [Checkbox Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/checkbox/src/index.ts)
71
+ - [Rawlist Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/rawlist/src/index.ts)
72
+ - [Expand Prompt](https://github.com/SBoudrias/Inquirer.js/blob/master/packages/expand/src/index.ts)
73
+
74
+ ## API
75
+
76
+ ### `createPrompt(config, context)`
77
+
78
+ The `createPrompt` function returns an asynchronous function that returns a promise that'll resolve to the valid answer a user submit. This prompt function takes the prompt configuration as its first argument (this is defined by each prompt), and the context options as a second argument.
79
+
80
+ The context options are:
81
+
82
+ | Property | Type | Required | Description |
83
+ | ----------------- | ----------------------- | -------- | ------------------------------------------------------------ |
84
+ | input | `NodeJS.ReadableStream` | no | The stdin stream (defaults to `process.stdin`) |
85
+ | output | `NodeJS.WritableStream` | no | The stdout stream (defaults to `process.stdout`) |
86
+ | clearPromptOnDone | `boolean` | no | If true, we'll clear the screen after the prompt is answered |
87
+
88
+ #### Typescript
89
+
90
+ If using typescript, `createPrompt` takes 2 generic arguments (ex `createPrompt<string, { message: string }>()`)
91
+
92
+ The first one is the type of the resolved value; `function createPrompt<Value>(): Promise<Value> {}`
93
+
94
+ The second one is the type of the prompt config; in other words the interface the created prompt will provide to users.
95
+
96
+ ### Hooks
97
+
98
+ Hooks can only be called within the prompt function and are used to handle state and events.
99
+
100
+ Those hooks are matching the React hooks API:
101
+
102
+ - `useState`
103
+ - `useRef`
104
+ - `useEffect`
105
+
106
+ And those are custom utilities from Inquirer:
107
+
108
+ - `useKeypress`
109
+ - `usePrefix`
110
+
111
+ ### Key utilities
112
+
113
+ Listening for keypress events inside an inquirer prompt is a very common pattern. To ease this, we export a few utility functions taking in the keypress event object and return a boolean:
114
+
115
+ - `isEnterKey()`
116
+ - `isBackspaceKey()`
117
+ - `isSpaceKey()`
118
+ - `isUpKey()` - Note: this utility will handle vim and emacs keybindings (up, `k`, and `ctrl+p`)
119
+ - `isDownKey()` - Note: this utility will handle vim and emacs keybindings (down, `j`, and `ctrl+n`)
120
+ - `isNumberKey()` one of 1, 2, 3, 4, 5, 6, 7, 8, 9, 0
121
+
122
+ ### `Paginator`
123
+
124
+ When looping through a long list of options (like in the `select` prompt), paginating the results appearing on the screen at once can be necessary. The `Paginator` utility is there to help this use case.
125
+
126
+ ```js
127
+ export default createPrompt((config, done) => {
128
+ const paginator = useRef(new Paginator()).current;
129
+
130
+ const windowedChoices = paginator.paginate(allChoices, cursorPosition, config.pageSize);
131
+
132
+ return '...';
133
+ });
134
+ ```
135
+
136
+ # License
137
+
138
+ Copyright (c) 2022 Simon Boudrias (twitter: [@vaxilart](https://twitter.com/Vaxilart))
139
+ Licensed under the MIT license.
package/dist/index.d.ts CHANGED
@@ -1,15 +1,11 @@
1
1
  /// <reference types="node" resolution-mode="require"/>
2
2
  /// <reference types="node" resolution-mode="require"/>
3
3
  import readline from 'node:readline';
4
+ import type { Prompt } from '@inquirer/type';
4
5
  import MuteStream from 'mute-stream';
5
6
  export { usePrefix } from './lib/prefix.js';
6
7
  export * from './lib/key.js';
7
8
  export * from './lib/Paginator.js';
8
- declare type Context = {
9
- input?: NodeJS.ReadableStream;
10
- output?: NodeJS.WritableStream;
11
- clearPromptOnDone?: boolean;
12
- };
13
9
  export declare type InquirerReadline = readline.ReadLine & {
14
10
  output: MuteStream;
15
11
  input: NodeJS.ReadableStream;
@@ -32,5 +28,4 @@ export declare type ResolvedPromptConfig = {
32
28
  message: string;
33
29
  validate: (value: string) => boolean | string | Promise<string | boolean>;
34
30
  };
35
- export declare type Prompt<Value, Config> = (options: Config, context?: Context) => Promise<Value>;
36
31
  export declare function createPrompt<Value, Config extends AsyncPromptConfig>(view: (config: Config & ResolvedPromptConfig, done: (value: Value) => void) => string | [string, string | undefined]): Prompt<Value, Config>;
package/dist/index.js CHANGED
@@ -64,7 +64,7 @@ export function useRef(val) {
64
64
  return useState({ current: val })[0];
65
65
  }
66
66
  export function createPrompt(view) {
67
- const prompt = async (options, context) => {
67
+ const prompt = async (config, context) => {
68
68
  // Default `input` to stdin
69
69
  const input = context?.input ?? process.stdin;
70
70
  // Add mute capabilities to the output
@@ -77,7 +77,7 @@ export function createPrompt(view) {
77
77
  });
78
78
  const screen = new ScreenManager(rl);
79
79
  // TODO: we should display a loader while we get the default options.
80
- const resolvedConfig = await getPromptConfig(options);
80
+ const resolvedConfig = await getPromptConfig(config);
81
81
  return new Promise((resolve) => {
82
82
  const done = (value) => {
83
83
  let len = hooksCleanup.length;
@@ -100,15 +100,15 @@ export function createPrompt(view) {
100
100
  };
101
101
  index = 0;
102
102
  hooks = [];
103
- const workLoop = (config) => {
103
+ const workLoop = () => {
104
104
  sessionRl = rl;
105
105
  index = 0;
106
- handleChange = () => workLoop(config);
107
- const nextView = view(config, done);
106
+ handleChange = () => workLoop();
107
+ const nextView = view(resolvedConfig, done);
108
108
  const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
109
109
  screen.render(content, bottomContent);
110
110
  };
111
- workLoop(resolvedConfig);
111
+ workLoop();
112
112
  });
113
113
  };
114
114
  return prompt;
package/dist/lib/key.js CHANGED
@@ -1,5 +1,17 @@
1
- export const isUpKey = (key) => key.name === 'up' || key.name === 'k' || (key.name === 'p' && key.ctrl);
2
- export const isDownKey = (key) => key.name === 'down' || key.name === 'j' || (key.name === 'n' && key.ctrl);
1
+ export const isUpKey = (key) =>
2
+ // The up key
3
+ key.name === 'up' ||
4
+ // Vim keybinding
5
+ key.name === 'k' ||
6
+ // Emacs keybinding
7
+ (key.ctrl && key.name === 'p');
8
+ export const isDownKey = (key) =>
9
+ // The down key
10
+ key.name === 'down' ||
11
+ // Vim keybinding
12
+ key.name === 'j' ||
13
+ // Emacs keybinding
14
+ (key.ctrl && key.name === 'n');
3
15
  export const isSpaceKey = (key) => key.name === 'space';
4
16
  export const isBackspaceKey = (key) => key.name === 'backspace';
5
17
  export const isNumberKey = (key) => '123456789'.includes(key.name);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
3
  "type": "module",
4
- "version": "0.0.26-alpha.0",
4
+ "version": "0.0.30-alpha.0",
5
5
  "description": "Core Inquirer prompt API",
6
6
  "main": "dist/index.js",
7
7
  "typings": "dist/index.d.ts",
@@ -10,16 +10,52 @@
10
10
  ],
11
11
  "repository": "SBoudrias/Inquirer.js",
12
12
  "keywords": [
13
+ "answer",
14
+ "answers",
15
+ "ask",
16
+ "base",
13
17
  "cli",
14
- "prompt",
18
+ "cli",
19
+ "command",
20
+ "command-line",
21
+ "confirm",
22
+ "enquirer",
23
+ "generate",
24
+ "generator",
25
+ "hyper",
26
+ "input",
27
+ "inquire",
15
28
  "inquirer",
16
- "command-line"
29
+ "interface",
30
+ "iterm",
31
+ "javascript",
32
+ "menu",
33
+ "node",
34
+ "nodejs",
35
+ "prompt",
36
+ "promptly",
37
+ "prompts",
38
+ "question",
39
+ "readline",
40
+ "scaffold",
41
+ "scaffolder",
42
+ "scaffolding",
43
+ "stdin",
44
+ "stdout",
45
+ "terminal",
46
+ "tty",
47
+ "ui",
48
+ "yeoman",
49
+ "yo",
50
+ "zsh"
17
51
  ],
18
52
  "author": "Simon Boudrias",
19
53
  "license": "MIT",
54
+ "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/core/README.md",
20
55
  "dependencies": {
21
- "ansi-escapes": "^5.0.0",
22
- "chalk": "^5.0.1",
56
+ "@inquirer/type": "^0.0.4-alpha.0",
57
+ "ansi-escapes": "^6.0.0",
58
+ "chalk": "^5.1.2",
23
59
  "cli-spinners": "^2.7.0",
24
60
  "cli-width": "^4.0.0",
25
61
  "lodash": "^4.17.21",
@@ -30,7 +66,7 @@
30
66
  "wrap-ansi": "^8.0.1"
31
67
  },
32
68
  "devDependencies": {
33
- "@inquirer/testing": "^0.0.2-alpha.0"
69
+ "@inquirer/testing": "^0.0.5-alpha.0"
34
70
  },
35
71
  "scripts": {
36
72
  "tsc": "tsc"
@@ -38,5 +74,5 @@
38
74
  "publishConfig": {
39
75
  "access": "public"
40
76
  },
41
- "gitHead": "3d7124020ee2a316a601f6006880018eec01f3a6"
77
+ "gitHead": "f532af8be2d622caa5e54adf4c084d588c9ffad6"
42
78
  }