@inquirer/rawlist 0.0.4-alpha.0 → 0.0.7-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.
@@ -0,0 +1,13 @@
1
+ import { AsyncPromptConfig } from '@inquirer/core';
2
+ declare type RawlistConfig = AsyncPromptConfig & {
3
+ choices: {
4
+ value: string;
5
+ name?: string;
6
+ key?: string;
7
+ }[];
8
+ };
9
+ declare const _default: (options: RawlistConfig, stdio?: {
10
+ input?: NodeJS.ReadableStream | undefined;
11
+ output?: NodeJS.WritableStream | undefined;
12
+ } | undefined) => Promise<string>;
13
+ export default _default;
package/dist/index.js ADDED
@@ -0,0 +1,61 @@
1
+ import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, } from '@inquirer/core';
2
+ import chalk from 'chalk';
3
+ const numberRegex = /[0-9]+/;
4
+ export default createPrompt((config, done) => {
5
+ const { choices } = config;
6
+ const [status, setStatus] = useState('pending');
7
+ const [value, setValue] = useState('');
8
+ const [errorMsg, setError] = useState(undefined);
9
+ const prefix = usePrefix();
10
+ useKeypress((key, rl) => {
11
+ if (isEnterKey(key)) {
12
+ let selectedChoice;
13
+ if (numberRegex.test(value)) {
14
+ const answer = parseInt(value, 10) - 1;
15
+ selectedChoice = choices[answer];
16
+ }
17
+ else {
18
+ const answer = value.toLowerCase();
19
+ selectedChoice = choices.find(({ key }) => key === answer);
20
+ }
21
+ if (selectedChoice) {
22
+ const finalValue = selectedChoice.value || selectedChoice.name;
23
+ setValue(finalValue);
24
+ setStatus('done');
25
+ done(finalValue);
26
+ }
27
+ else if (value === '') {
28
+ setError('Please input a value');
29
+ }
30
+ else {
31
+ setError(`"${chalk.red(value)}" isn't an available option`);
32
+ }
33
+ }
34
+ else {
35
+ setValue(rl.line);
36
+ setError(undefined);
37
+ }
38
+ });
39
+ const message = chalk.bold(config.message);
40
+ if (status === 'done') {
41
+ return `${prefix} ${message} ${chalk.cyan(value)}`;
42
+ }
43
+ const choicesStr = choices
44
+ .map((choice, index) => {
45
+ const humanIndex = index + 1;
46
+ const line = ` ${choice.key || humanIndex}) ${choice.name || choice.value}`;
47
+ if (choice.key === value.toLowerCase() || String(humanIndex) === value) {
48
+ return chalk.cyan(line);
49
+ }
50
+ return line;
51
+ })
52
+ .join('\n');
53
+ let error = '';
54
+ if (errorMsg) {
55
+ error = chalk.red(`> ${errorMsg}`);
56
+ }
57
+ return [
58
+ `${prefix} ${message} ${value}`,
59
+ [choicesStr, error].filter(Boolean).join('\n'),
60
+ ];
61
+ });
package/package.json CHANGED
@@ -1,9 +1,13 @@
1
1
  {
2
2
  "name": "@inquirer/rawlist",
3
3
  "type": "module",
4
- "version": "0.0.4-alpha.0",
4
+ "version": "0.0.7-alpha.0",
5
5
  "description": "Inquirer rawlist prompt",
6
- "main": "index.js",
6
+ "main": "dist/index.js",
7
+ "typings": "dist/index.d.ts",
8
+ "files": [
9
+ "dist/"
10
+ ],
7
11
  "repository": "SBoudrias/Inquirer.js",
8
12
  "keywords": [
9
13
  "cli",
@@ -19,11 +23,14 @@
19
23
  "license": "MIT",
20
24
  "homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/rawlist/README.md",
21
25
  "dependencies": {
22
- "@inquirer/core": "^0.0.21-alpha.0",
26
+ "@inquirer/core": "^0.0.24-alpha.0",
23
27
  "chalk": "^5.0.1"
24
28
  },
29
+ "scripts": {
30
+ "tsc": "tsc"
31
+ },
25
32
  "publishConfig": {
26
33
  "access": "public"
27
34
  },
28
- "gitHead": "cd0d1465327c0e98a89c937a80b7c473b6d00f74"
35
+ "gitHead": "0e054fd0b58bf56e94088136dfd36b74246a5187"
29
36
  }
package/demo.js DELETED
@@ -1,55 +0,0 @@
1
- import rawlist from './index.js';
2
-
3
- (async () => {
4
- let answer;
5
-
6
- answer = await rawlist({
7
- message: '(no keys) Conflict on `file.js`:',
8
- choices: [
9
- {
10
- name: 'Overwrite',
11
- value: 'overwrite',
12
- },
13
- {
14
- name: 'Overwrite this one and all next',
15
- value: 'overwrite_all',
16
- },
17
- {
18
- name: 'Show diff',
19
- value: 'diff',
20
- },
21
- {
22
- name: 'Abort',
23
- value: 'abort',
24
- },
25
- ],
26
- });
27
- console.log('Answer:', answer);
28
-
29
- answer = await rawlist({
30
- message: '(with keys) Conflict on `file.js`:',
31
- choices: [
32
- {
33
- key: 'y',
34
- name: 'Overwrite',
35
- value: 'overwrite',
36
- },
37
- {
38
- key: 'a',
39
- name: 'Overwrite this one and all next',
40
- value: 'overwrite_all',
41
- },
42
- {
43
- key: 'd',
44
- name: 'Show diff',
45
- value: 'diff',
46
- },
47
- {
48
- key: 'x',
49
- name: 'Abort',
50
- value: 'abort',
51
- },
52
- ],
53
- });
54
- console.log('Answer:', answer);
55
- })();
package/index.js DELETED
@@ -1,74 +0,0 @@
1
- import {
2
- createPrompt,
3
- useState,
4
- useKeypress,
5
- usePrefix,
6
- isEnterKey,
7
- } from '@inquirer/core';
8
- import chalk from 'chalk';
9
-
10
- const numberRegex = /[0-9]+/;
11
-
12
- export default createPrompt((config, done) => {
13
- const { choices } = config;
14
- const [status, setStatus] = useState('pending');
15
- const [value, setValue] = useState('');
16
- const [errorMsg, setError] = useState();
17
- const prefix = usePrefix();
18
-
19
- useKeypress((key, rl) => {
20
- if (isEnterKey(key)) {
21
- let selectedChoice;
22
- if (numberRegex.test(value)) {
23
- const answer = parseInt(value, 10) - 1;
24
- selectedChoice = choices[answer];
25
- } else {
26
- const answer = value.toLowerCase();
27
- selectedChoice = choices.find(({ key }) => key === answer);
28
- }
29
-
30
- if (selectedChoice) {
31
- const finalValue = selectedChoice.value || selectedChoice.name;
32
- setValue(finalValue);
33
- setStatus('done');
34
- done(finalValue);
35
- } else if (value === '') {
36
- setError('Please input a value');
37
- } else {
38
- setError(`"${chalk.red(value)}" isn't an available option`);
39
- }
40
- } else {
41
- setValue(rl.line);
42
- setError(undefined);
43
- }
44
- });
45
-
46
- const message = chalk.bold(config.message);
47
-
48
- if (status === 'done') {
49
- return `${prefix} ${message} ${chalk.cyan(value)}`;
50
- }
51
-
52
- const choicesStr = choices
53
- .map((choice, index) => {
54
- const humanIndex = index + 1;
55
- const line = ` ${choice.key || humanIndex}) ${choice.name || choice.value}`;
56
-
57
- if (choice.key === value.toLowerCase() || String(humanIndex) === value) {
58
- return chalk.cyan(line);
59
- }
60
-
61
- return line;
62
- })
63
- .join('\n');
64
-
65
- let error = '';
66
- if (errorMsg) {
67
- error = chalk.red(`> ${errorMsg}`);
68
- }
69
-
70
- return [
71
- `${prefix} ${message} ${value}`,
72
- [choicesStr, error].filter(Boolean).join('\n'),
73
- ];
74
- });