@reliverse/relinka 1.1.4 → 1.1.6

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.
Files changed (118) hide show
  1. package/dist-npm/components/anykey/index.js +77 -57
  2. package/dist-npm/components/block/block.js +61 -48
  3. package/dist-npm/components/checkbox/index.js +211 -178
  4. package/dist-npm/components/confirm/confirm-main.js +122 -86
  5. package/dist-npm/components/confirm/confirm-three.js +27 -24
  6. package/dist-npm/components/confirm/index.js +45 -36
  7. package/dist-npm/components/core/Separator.js +17 -15
  8. package/dist-npm/components/core/create-prompt.js +124 -101
  9. package/dist-npm/components/core/errors.js +15 -19
  10. package/dist-npm/components/core/hook-engine.js +120 -91
  11. package/dist-npm/components/core/index.js +12 -12
  12. package/dist-npm/components/core/key.js +6 -16
  13. package/dist-npm/components/core/lines.js +54 -45
  14. package/dist-npm/components/core/make-theme.js +30 -21
  15. package/dist-npm/components/core/position.js +23 -27
  16. package/dist-npm/components/core/promise-polyfill.js +14 -11
  17. package/dist-npm/components/core/screen-manager.js +74 -59
  18. package/dist-npm/components/core/theme.js +32 -22
  19. package/dist-npm/components/core/use-effect.js +15 -9
  20. package/dist-npm/components/core/use-keypress.js +23 -19
  21. package/dist-npm/components/core/use-memo.js +16 -10
  22. package/dist-npm/components/core/use-pagination.js +32 -33
  23. package/dist-npm/components/core/use-prefix.js +38 -40
  24. package/dist-npm/components/core/use-ref.js +5 -2
  25. package/dist-npm/components/core/use-state.js +23 -15
  26. package/dist-npm/components/core/useKeyPress.js +17 -14
  27. package/dist-npm/components/core/usePromptState.js +14 -8
  28. package/dist-npm/components/core/utils.js +16 -7
  29. package/dist-npm/components/date/date.js +204 -168
  30. package/dist-npm/components/editor/index.js +92 -71
  31. package/dist-npm/components/expand/index.js +124 -96
  32. package/dist-npm/components/figures/index.js +294 -283
  33. package/dist-npm/components/input/index.js +87 -61
  34. package/dist-npm/components/input/text-main.js +124 -86
  35. package/dist-npm/components/input/text.js +28 -24
  36. package/dist-npm/components/instance/basic.js +25 -17
  37. package/dist-npm/components/instance/browser.js +18 -14
  38. package/dist-npm/components/instance/reporter/basic.js +65 -46
  39. package/dist-npm/components/instance/reporter/browser.js +47 -44
  40. package/dist-npm/components/instance/reporter/fancy.js +96 -83
  41. package/dist-npm/components/instance/shared.js +2 -2
  42. package/dist-npm/components/mono/mono.js +62 -52
  43. package/dist-npm/components/mono/monoTwo.js +49 -35
  44. package/dist-npm/components/multiselect/group-multiselect.js +71 -55
  45. package/dist-npm/components/multiselect/multi-select-two.js +130 -98
  46. package/dist-npm/components/multiselect/multi-select.js +49 -43
  47. package/dist-npm/components/multiselect/multiselect-main.js +146 -120
  48. package/dist-npm/components/multiselect/num-multi-select.js +130 -98
  49. package/dist-npm/components/multiselect/num-multiselect-main.js +35 -24
  50. package/dist-npm/components/next-steps/next-steps.js +25 -24
  51. package/dist-npm/components/number/index.js +112 -78
  52. package/dist-npm/components/number/number-main.js +2 -96
  53. package/dist-npm/components/password/index.js +73 -54
  54. package/dist-npm/components/password/password-main.js +2 -120
  55. package/dist-npm/components/password/password-three.js +30 -26
  56. package/dist-npm/components/progressbar/ProgressBar.js +64 -45
  57. package/dist-npm/components/progressbar/helper.js +40 -33
  58. package/dist-npm/components/progressbar/index.js +1 -1
  59. package/dist-npm/components/prompts/create.js +44 -29
  60. package/dist-npm/components/prompts/index.js +45 -45
  61. package/dist-npm/components/prompts/prompt.js +260 -211
  62. package/dist-npm/components/prompts/promptTwo.js +605 -561
  63. package/dist-npm/components/prompts/relinka.js +295 -237
  64. package/dist-npm/components/range/range.js +294 -247
  65. package/dist-npm/components/rawlist/index.js +107 -87
  66. package/dist-npm/components/results/results.js +37 -31
  67. package/dist-npm/components/search/index.js +193 -148
  68. package/dist-npm/components/select/index.js +186 -148
  69. package/dist-npm/components/select/num-select-main.js +27 -27
  70. package/dist-npm/components/select/num-select.js +5 -125
  71. package/dist-npm/components/select/select-key.js +25 -24
  72. package/dist-npm/components/select/select-main.js +133 -109
  73. package/dist-npm/components/select/select-three.js +36 -32
  74. package/dist-npm/components/select/select-two.js +87 -95
  75. package/dist-npm/components/spinner/index.js +136 -107
  76. package/dist-npm/components/st-end/end.d.ts +1 -1
  77. package/dist-npm/components/st-end/end.js +26 -35
  78. package/dist-npm/components/st-end/start.d.ts +1 -1
  79. package/dist-npm/components/st-end/start.js +15 -30
  80. package/dist-npm/components/toggle/index.js +137 -113
  81. package/dist-npm/components/visual/animate/animate.js +10 -53
  82. package/dist-npm/components/visual/ascii-art/ascii-art.js +1 -12
  83. package/dist-npm/mod.js +2 -1
  84. package/dist-npm/testing/index.js +83 -58
  85. package/dist-npm/types/general.d.ts +2 -1
  86. package/dist-npm/types/general.js +1 -0
  87. package/dist-npm/types/index.js +3 -2
  88. package/dist-npm/types/keypress.js +36 -35
  89. package/dist-npm/types/readline.js +1 -0
  90. package/dist-npm/types/relinka.js +1 -0
  91. package/dist-npm/types/utils.js +1 -0
  92. package/dist-npm/utils/box.js +137 -135
  93. package/dist-npm/utils/color.js +74 -65
  94. package/dist-npm/utils/colorize.js +156 -124
  95. package/dist-npm/utils/component.js +657 -532
  96. package/dist-npm/utils/constants.js +63 -64
  97. package/dist-npm/utils/core.js +3 -2
  98. package/dist-npm/utils/decoder.js +223 -244
  99. package/dist-npm/utils/error.js +9 -4
  100. package/dist-npm/utils/errors.js +4 -14
  101. package/dist-npm/utils/format.js +24 -19
  102. package/dist-npm/utils/keypress.js +414 -316
  103. package/dist-npm/utils/log.js +15 -11
  104. package/dist-npm/utils/mapping.js +52 -45
  105. package/dist-npm/utils/messages.js +183 -183
  106. package/dist-npm/utils/platforms.js +20 -16
  107. package/dist-npm/utils/prompt-tmp.js +286 -235
  108. package/dist-npm/utils/prompt-two.js +286 -235
  109. package/dist-npm/utils/readline.js +7 -5
  110. package/dist-npm/utils/skeleton.js +170 -130
  111. package/dist-npm/utils/stream.js +2 -2
  112. package/dist-npm/utils/string.js +58 -44
  113. package/dist-npm/utils/terminal.js +34 -23
  114. package/dist-npm/utils/tree.js +41 -30
  115. package/dist-npm/utils/types.js +1 -0
  116. package/dist-npm/utils/utils.js +8 -8
  117. package/dist-npm/utils/variants.js +44 -36
  118. package/package.json +20 -28
@@ -1,56 +1,59 @@
1
+ const isString = (a) => typeof a === 'string';
2
+
1
3
  export class BrowserReporter {
2
- options;
3
- defaultColor;
4
- levelColorMap;
5
- typeColorMap;
6
- constructor(options) {
7
- this.options = { ...options };
8
- this.defaultColor = "#7f8c8d";
9
- this.levelColorMap = {
10
- 0: "#c0392b",
11
-
12
- 1: "#f39c12",
13
-
14
- 3: "#00BCD4"
15
-
16
- };
17
- this.typeColorMap = {
18
- success: "#2ecc71"
19
-
20
- };
21
- }
22
- _getLogFn(level) {
23
- if (level < 1) {
24
- return console.__error || console.error;
4
+ options;
5
+ defaultColor;
6
+ levelColorMap;
7
+ typeColorMap;
8
+ constructor(options) {
9
+ this.options = {...options};
10
+ this.defaultColor = '#7f8c8d';
11
+ this.levelColorMap = {
12
+ 0: '#c0392b',
13
+
14
+ 1: '#f39c12',
15
+
16
+ 3: '#00BCD4',
17
+ };
18
+ this.typeColorMap = {
19
+ success: '#2ecc71',
20
+ };
25
21
  }
26
- if (level === 1) {
27
- return console.__warn || console.warn;
22
+
23
+ _getLogFn(level) {
24
+ if (level < 1) {
25
+ return console.__error || console.error;
26
+ }
27
+
28
+ if (level === 1) {
29
+ return console.__warn || console.warn;
30
+ }
31
+
32
+ return console.__log || console.log;
28
33
  }
29
- return console.__log || console.log;
30
- }
31
- log(logObj) {
32
- const consoleLogFn = this._getLogFn(logObj.level);
33
- const type = logObj.type === "log" ? "" : logObj.type;
34
- const tag = logObj.tag || "";
35
- const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
36
- const style = `
34
+
35
+ log(logObj) {
36
+ const consoleLogFn = this._getLogFn(logObj.level);
37
+ const type = logObj.type === 'log' ? '' : logObj.type;
38
+ const tag = logObj.tag || '';
39
+ const color = this.typeColorMap[logObj.type] || this.levelColorMap[logObj.level] || this.defaultColor;
40
+
41
+ const style = `
37
42
  background: ${color};
38
43
  border-radius: 0.5em;
39
44
  color: white;
40
45
  font-weight: bold;
41
46
  padding: 2px 0.5em;
42
47
  `;
43
- const badge = `%c${[tag, type].filter(Boolean).join(":")}`;
44
- if (typeof logObj.args[0] === "string") {
45
- consoleLogFn(
46
- `${badge}%c ${logObj.args[0]}`,
47
- style,
48
48
 
49
- "",
50
- ...logObj.args.slice(1)
51
- );
52
- } else {
53
- consoleLogFn(badge, style, ...logObj.args);
49
+ const badge = `%c${[tag, type]
50
+ .filter(Boolean)
51
+ .join(':')}`;
52
+
53
+ if (isString(logObj.args[0])) {
54
+ consoleLogFn(`${badge}%c ${logObj.args[0]}`, style, '', ...logObj.args.slice(1));
55
+ } else {
56
+ consoleLogFn(badge, style, ...logObj.args);
57
+ }
54
58
  }
55
- }
56
59
  }
@@ -1,101 +1,114 @@
1
- import isUnicodeSupported from "is-unicode-supported";
2
- import _stringWidth from "string-width";
3
- import { box } from "../../../utils/box.js";
4
- import { colors } from "../../../utils/color.js";
5
- import { parseStack } from "../../../utils/error.js";
6
- import { stripAnsi } from "../../../utils/utils.js";
7
- import { BasicReporter } from "./basic.js";
1
+ import isUnicodeSupported from 'is-unicode-supported';
2
+ import _stringWidth from 'string-width';
3
+ import {box} from '../../../utils/box.js';
4
+ import {colors} from '../../../utils/color.js';
5
+ import {parseStack} from '../../../utils/error.js';
6
+ import {stripAnsi} from '../../../utils/utils.js';
7
+ import {BasicReporter} from './basic.js';
8
+
9
+ const isString = (a) => typeof a === 'string';
10
+
8
11
  export const TYPE_COLOR_MAP = {
9
- info: "cyan",
10
- fail: "red",
11
- success: "green",
12
- ready: "green",
13
- start: "magenta"
12
+ info: 'cyan',
13
+ fail: 'red',
14
+ success: 'green',
15
+ ready: 'green',
16
+ start: 'magenta',
14
17
  };
15
18
  export const LEVEL_COLOR_MAP = {
16
- 0: "red",
17
- 1: "yellow"
19
+ 0: 'red',
20
+ 1: 'yellow',
18
21
  };
19
22
  const unicode = isUnicodeSupported();
20
23
  const s = (c, fallback) => unicode ? c : fallback;
24
+
21
25
  const TYPE_ICONS = {
22
- error: s("\u2716", "\xD7"),
23
- fatal: s("\u2716", "\xD7"),
24
- ready: s("\u2714", "\u221A"),
25
- warn: s("\u26A0", "\u203C"),
26
- info: s("\u2139", "i"),
27
- success: s("\u2714", "\u221A"),
28
- debug: s("\u2699", "D"),
29
- trace: s("\u2192", "\u2192"),
30
- fail: s("\u2716", "\xD7"),
31
- start: s("\u25D0", "o"),
32
- log: ""
26
+ error: s('\u2716', '\xD7'),
27
+ fatal: s('\u2716', '\xD7'),
28
+ ready: s('\u2714', '\u221A'),
29
+ warn: s('\u26A0', '\u203C'),
30
+ info: s('\u2139', 'i'),
31
+ success: s('\u2714', '\u221A'),
32
+ debug: s('\u2699', 'D'),
33
+ trace: s('\u2192', '\u2192'),
34
+ fail: s('\u2716', '\xD7'),
35
+ start: s('\u25D0', 'o'),
36
+ log: '',
33
37
  };
38
+
34
39
  function stringWidth(str) {
35
- if (!Intl.Segmenter) {
36
- return stripAnsi(str).length;
37
- }
38
- return _stringWidth(str);
40
+ if (!Intl.Segmenter) {
41
+ return stripAnsi(str).length;
42
+ }
43
+
44
+ return _stringWidth(str);
39
45
  }
46
+
40
47
  export class FancyReporter extends BasicReporter {
41
- // @ts-expect-error TODO: fix ts
42
- formatStack(stack) {
43
- return "\n" + parseStack(stack).map(
44
- (line) => " " + line.replace(/^at +/, (m) => colors.gray(m)).replace(/\((.+)\)/, (_, m) => `(${colors.cyan(m)})`)
45
- ).join("\n");
46
- }
47
- formatType(logObj, isBadge, opts) {
48
- const typeColor = TYPE_COLOR_MAP[logObj.type] || LEVEL_COLOR_MAP[logObj.level] || "gray";
49
- if (isBadge) {
50
- return getBgColor(typeColor)(
51
- colors.black(` ${logObj.type.toUpperCase()} `)
52
- );
48
+ // @ts-expect-error TODO: fix ts
49
+ formatStack(stack) {
50
+ return '\n' + parseStack(stack)
51
+ .map((line) => ' ' + line
52
+ .replace(/^at +/, (m) => colors.gray(m))
53
+ .replace(/\((.+)\)/, (_, m) => `(${colors.cyan(m)})`))
54
+ .join('\n');
53
55
  }
54
- const _type = typeof TYPE_ICONS[logObj.type] === "string" ? TYPE_ICONS[logObj.type] : logObj.icon || logObj.type;
55
- return _type ? getColor(typeColor)(_type) : "";
56
- }
57
- // @ts-expect-error TODO: fix ts
58
- formatLogObj(logObj, opts) {
59
- const [message, ...additional] = this.formatArgs(logObj.args, opts).split(
60
- "\n"
61
- );
62
- if (logObj.type === "box") {
63
- return box(
64
- characterFormat(
65
- message + (additional.length > 0 ? "\n" + additional.join("\n") : "")
66
- ),
67
- {
68
- title: logObj.title ? characterFormat(logObj.title) : void 0,
69
- style: logObj.style
56
+
57
+ formatType(logObj, isBadge) {
58
+ const typeColor = TYPE_COLOR_MAP[logObj.type] || LEVEL_COLOR_MAP[logObj.level] || 'gray';
59
+
60
+ if (isBadge) {
61
+ return getBgColor(typeColor)(colors.black(` ${logObj.type.toUpperCase()} `));
70
62
  }
71
- );
63
+
64
+ const _type = isString(TYPE_ICONS[logObj.type]) ? TYPE_ICONS[logObj.type] : logObj.icon || logObj.type;
65
+
66
+ return _type ? getColor(typeColor)(_type) : '';
72
67
  }
73
- const date = this.formatDate(logObj.date, opts);
74
- const coloredDate = date && colors.gray(date);
75
- const isBadge = logObj.badge ?? logObj.level < 2;
76
- const type = this.formatType(logObj, isBadge, opts);
77
- const tag = logObj.tag ? colors.gray(logObj.tag) : "";
78
- let line;
79
- const left = this.filterAndJoin([type, characterFormat(message)]);
80
- const right = this.filterAndJoin(opts.columns ? [tag, coloredDate] : [tag]);
81
- const space = (opts.columns || 0) - stringWidth(left) - stringWidth(right) - 2;
82
- line = space > 0 && (opts.columns || 0) >= 80 ? left + " ".repeat(space) + right : (right ? `${colors.gray(`[${right}]`)} ` : "") + left;
83
- line += characterFormat(
84
- additional.length > 0 ? "\n" + additional.join("\n") : ""
85
- );
86
- if (logObj.type === "trace") {
87
- const _err = new Error("Trace: " + logObj.message);
88
- line += this.formatStack(_err.stack || "");
68
+ // @ts-expect-error TODO: fix ts
69
+ formatLogObj(logObj, opts) {
70
+ const [message, ...additional] = this
71
+ .formatArgs(logObj.args, opts)
72
+ .split('\n');
73
+
74
+ if (logObj.type === 'box') {
75
+ return box(characterFormat(message + (additional.length > 0 ? '\n' + additional.join('\n') : '')), {
76
+ title: logObj.title ? characterFormat(logObj.title) : void 0,
77
+ style: logObj.style,
78
+ });
79
+ }
80
+
81
+ const date = this.formatDate(logObj.date, opts);
82
+ const coloredDate = date && colors.gray(date);
83
+ const isBadge = logObj.badge ?? logObj.level < 2;
84
+ const type = this.formatType(isBadge, opts);
85
+ const tag = logObj.tag ? colors.gray(logObj.tag) : '';
86
+ let line;
87
+ const left = this.filterAndJoin([type, characterFormat(message)]);
88
+ const right = this.filterAndJoin(opts.columns ? [tag, coloredDate] : [tag]);
89
+ const space = (opts.columns || 0) - stringWidth(left) - stringWidth(right) - 2;
90
+
91
+ line = space > 0 && (opts.columns || 0) >= 80 ? left + ' '.repeat(space) + right : (right ? `${colors.gray(`[${right}]`)} ` : '') + left;
92
+ line += characterFormat(additional.length > 0 ? '\n' + additional.join('\n') : '');
93
+ if (logObj.type === 'trace') {
94
+ const _err = Error('Trace: ' + logObj.message);
95
+ line += this.formatStack(_err.stack || '');
96
+ }
97
+
98
+ return isBadge ? `
99
+ ${line}
100
+ ` : line;
89
101
  }
90
- return isBadge ? "\n" + line + "\n" : line;
91
- }
92
- }
93
- function characterFormat(str) {
94
- return str.replace(/`([^`]+)`/gm, (_, m) => colors.cyan(m)).replace(/\s+_([^_]+)_\s+/gm, (_, m) => ` ${colors.underline(m)} `);
102
+ }function characterFormat(str) {
103
+ return str
104
+ .replace(/`([^`]+)`/gm, (_, m) => colors.cyan(m))
105
+ .replace(/\s+_([^_]+)_\s+/gm, (_, m) => ` ${colors.underline(m)} `);
95
106
  }
96
- function getColor(color = "white") {
97
- return colors[color] || colors.white;
107
+
108
+ function getColor(color = 'white') {
109
+ return colors[color] || colors.white;
98
110
  }
99
- function getBgColor(color = "bgWhite") {
100
- return colors[`bg${color[0].toUpperCase()}${color.slice(1)}`] || colors.bgWhite;
111
+
112
+ function getBgColor(color = 'bgWhite') {
113
+ return colors[`bg${color[0].toUpperCase()}${color.slice(1)}`] || colors.bgWhite;
101
114
  }
@@ -1,2 +1,2 @@
1
- export { LogLevels, LogTypes } from "../../utils/constants.js";
2
- export { Relinka } from "../../components/prompts/relinka.js";
1
+ export {LogLevels, LogTypes} from '../../utils/constants.js';
2
+ export {Relinka} from '../../components/prompts/relinka.js';
@@ -1,64 +1,74 @@
1
- import { confirmPrompt } from "../../components/confirm/confirm-main.js";
2
- import { datePrompt } from "../../components/date/date.js";
3
- import { textPrompt } from "../../components/input/text-main.js";
4
- import { nextStepsPrompt } from "../../components/next-steps/next-steps.js";
5
- import { numberPrompt } from "../../components/number/number-main.js";
6
- import { passwordPrompt } from "../../components/password/password-main.js";
7
- import { numSelectPrompt } from "../../components/select/num-select.js";
8
- import { endPrompt } from "../../components/st-end/end.js";
9
- import { startPrompt } from "../../components/st-end/start.js";
1
+ import {confirmPrompt} from '../../components/confirm/confirm-main.js';
2
+ import {datePrompt} from '../../components/date/date.js';
3
+ import {textPrompt} from '../../components/input/text-main.js';
4
+ import {nextStepsPrompt} from '../../components/next-steps/next-steps.js';
5
+ import {numberPrompt} from '../../components/number/number-main.js';
6
+ import {passwordPrompt} from '../../components/password/password-main.js';
7
+ import {numSelectPrompt} from '../../components/select/num-select.js';
8
+ import {endPrompt} from '../../components/st-end/end.js';
9
+ import {startPrompt} from '../../components/st-end/start.js';
10
+
10
11
  export async function prompt(options) {
11
- const { type, id, action } = options;
12
- let value;
13
- switch (type) {
14
- case "start":
15
- await startPrompt(options);
16
- value = null;
17
- break;
18
- case "text":
19
- value = await textPrompt(options);
20
- break;
21
- case "number":
22
- value = await numberPrompt(options);
23
- break;
24
- case "confirm":
25
- value = await confirmPrompt(options);
26
- break;
27
- case "numSelect":
28
- value = await numSelectPrompt(options);
29
- break;
12
+ const {
13
+ type,
14
+ id,
15
+ action,
16
+ } = options;
17
+ let value;
30
18
 
19
+ switch(type) {
20
+ case 'start':
21
+ await startPrompt(options);
22
+ value = null;
23
+ break;
31
24
 
25
+ case 'text':
26
+ value = await textPrompt(options);
27
+ break;
32
28
 
29
+ case 'number':
30
+ value = await numberPrompt(options);
31
+ break;
33
32
 
33
+ case 'confirm':
34
+ value = await confirmPrompt(options);
35
+ break;
34
36
 
37
+ case 'numSelect':
38
+ value = await numSelectPrompt(options);
39
+ break;
35
40
 
41
+ case 'password':
42
+ value = await passwordPrompt(options);
43
+ break;
36
44
 
45
+ case 'date':
46
+ value = await datePrompt({
47
+ ...options,
48
+ dateFormat: 'DD/MM/YYYY',
49
+ dateKind: 'other',
50
+ });
51
+ break;
37
52
 
53
+ case 'nextSteps':
54
+ await nextStepsPrompt(options);
55
+ value = null;
56
+ break;
57
+
58
+ case 'end':
59
+ await endPrompt(options);
60
+ value = null;
61
+ break;
38
62
 
39
- case "password":
40
- value = await passwordPrompt(options);
41
- break;
42
- case "date":
43
- value = await datePrompt({
44
- ...options,
45
- dateFormat: "DD/MM/YYYY",
46
- dateKind: "other"
47
- });
48
- break;
49
- case "nextSteps":
50
- await nextStepsPrompt(options);
51
- value = null;
52
- break;
53
- case "end":
54
- await endPrompt(options);
55
- value = null;
56
- break;
57
63
  default:
58
- throw new Error(`Unknown prompt type: ${type}`);
59
- }
60
- if (action) {
61
- await action();
62
- }
63
- return { [id]: value };
64
+ throw Error(`Unknown prompt type: ${type}`);
65
+ }
66
+
67
+ if (action) {
68
+ await action();
69
+ }
70
+
71
+ return {
72
+ [id]: value,
73
+ };
64
74
  }
@@ -1,37 +1,51 @@
1
- import { text, confirm, select, multiselect } from "../prompts/promptTwo.js";
1
+ import {
2
+ text,
3
+ confirm,
4
+ select,
5
+ multiselect,
6
+ } from '../prompts/promptTwo.js';
7
+
8
+ const isString = (a) => typeof a === 'string';
9
+
2
10
  export async function prompt(message, opts = {}) {
3
- if (!opts.type || opts.type === "text") {
4
- return await text({
5
- message,
6
- defaultValue: opts.default,
7
- placeholder: opts.placeholder,
8
- initialValue: opts.initial
9
- });
10
- }
11
- if (opts.type === "confirm") {
12
- return await confirm({
13
- message,
14
- initialValue: opts.initial
15
- });
16
- }
17
- if (opts.type === "select") {
18
- return await select({
19
- message,
20
- options: opts.options.map(
21
- (o) => typeof o === "string" ? { value: o, label: o } : o
22
- ),
23
- initialValue: opts.initial
24
- });
25
- }
26
- if (opts.type === "multiselect") {
27
- return await multiselect({
28
- message,
29
- options: opts.options.map(
30
- (o) => typeof o === "string" ? { value: o, label: o } : o
31
- ),
32
- required: opts.required,
33
- initialValues: opts.initial
34
- });
35
- }
36
- throw new Error(`Unknown prompt type: ${opts.type}`);
11
+ if (!opts.type || opts.type === 'text') {
12
+ return await text({
13
+ message,
14
+ defaultValue: opts.default,
15
+ placeholder: opts.placeholder,
16
+ initialValue: opts.initial,
17
+ });
18
+ }
19
+
20
+ if (opts.type === 'confirm') {
21
+ return await confirm({
22
+ message,
23
+ initialValue: opts.initial,
24
+ });
25
+ }
26
+
27
+ if (opts.type === 'select') {
28
+ return await select({
29
+ message,
30
+ options: opts.options.map((o) => isString(o) ? {
31
+ value: o,
32
+ label: o,
33
+ } : o),
34
+ initialValue: opts.initial,
35
+ });
36
+ }
37
+
38
+ if (opts.type === 'multiselect') {
39
+ return await multiselect({
40
+ message,
41
+ options: opts.options.map((o) => isString(o) ? {
42
+ value: o,
43
+ label: o,
44
+ } : o),
45
+ required: opts.required,
46
+ initialValues: opts.initial,
47
+ });
48
+ }
49
+
50
+ throw Error(`Unknown prompt type: ${opts.type}`);
37
51
  }
@@ -1,58 +1,74 @@
1
- import Prompt from "../prompts/prompt.js";
1
+ import Prompt from '../prompts/prompt.js';
2
+
2
3
  export default class GroupMultiSelectPrompt extends Prompt {
3
- options;
4
- cursor = 0;
5
- getGroupItems(group) {
6
- return this.options.filter((o) => o.group === group);
7
- }
8
- isGroupSelected(group) {
9
- const items = this.getGroupItems(group);
10
- return items.every((i) => this.value.includes(i.value));
11
- }
12
- toggleValue() {
13
- const item = this.options[this.cursor];
14
- if (item.group === true) {
15
- const group = item.value;
16
- const groupedItems = this.getGroupItems(group);
17
- if (this.isGroupSelected(group)) {
18
- this.value = this.value.filter(
19
- (v) => groupedItems.findIndex((i) => i.value === v) === -1
20
- );
21
- } else {
22
- this.value = [...this.value, ...groupedItems.map((i) => i.value)];
23
- }
24
- this.value = Array.from(new Set(this.value));
25
- } else {
26
- const selected = this.value.includes(item.value);
27
- this.value = selected ? this.value.filter((v) => v !== item.value) : [...this.value, item.value];
4
+ options;
5
+ cursor = 0;
6
+ getGroupItems(group) {
7
+ return this.options.filter((o) => o.group === group);
8
+ }
9
+
10
+ isGroupSelected(group) {
11
+ const items = this.getGroupItems(group);
12
+ return items.every((i) => this.value.includes(i.value));
13
+ }
14
+
15
+ toggleValue() {
16
+ const item = this.options[this.cursor];
17
+
18
+ if (item.group) {
19
+ const group = item.value;
20
+ const groupedItems = this.getGroupItems(group);
21
+
22
+ if (this.isGroupSelected(group)) {
23
+ this.value = this.value.filter((v) => groupedItems.findIndex((i) => i.value === v) === -1);
24
+ } else {
25
+ this.value = [
26
+ ...this.value,
27
+ ...groupedItems.map((i) => i.value),
28
+ ];
29
+ }
30
+
31
+ this.value = Array.from(new Set(this.value));
32
+ } else {
33
+ const selected = this.value.includes(item.value);
34
+ this.value = selected ? this.value.filter((v) => v !== item.value) : [
35
+ ...this.value,
36
+ item.value,
37
+ ];
38
+ }
39
+ }
40
+
41
+ constructor(opts) {
42
+ super(opts, false);
43
+ const {options} = opts;
44
+
45
+ this.options = Object
46
+ .entries(options)
47
+ .flatMap(([key, option]) => [{
48
+ value: key,
49
+ group: true,
50
+ label: key,
51
+ },
52
+ ...option.map((opt) => ({ ...opt,
53
+ group: key}))]);
54
+ this.value = opts.initialValues ?? [].slice();
55
+ this.cursor = Math.max(this.options.findIndex(({value}) => value === opts.cursorAt), 0);
56
+ this.on('cursor', (key) => {
57
+ switch(key) {
58
+ case 'left':
59
+ case 'up':
60
+ this.cursor = !this.cursor ? this.options.length - 1 : this.cursor - 1;
61
+ break;
62
+
63
+ case 'down':
64
+ case 'right':
65
+ this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
66
+ break;
67
+
68
+ case 'space':
69
+ this.toggleValue();
70
+ break;
71
+ }
72
+ });
28
73
  }
29
- }
30
- constructor(opts) {
31
- super(opts, false);
32
- const { options } = opts;
33
- this.options = Object.entries(options).flatMap(([key, option]) => [
34
- { value: key, group: true, label: key },
35
- ...option.map((opt) => ({ ...opt, group: key }))
36
- ]);
37
- this.value = [...opts.initialValues ?? []];
38
- this.cursor = Math.max(
39
- this.options.findIndex(({ value }) => value === opts.cursorAt),
40
- 0
41
- );
42
- this.on("cursor", (key) => {
43
- switch (key) {
44
- case "left":
45
- case "up":
46
- this.cursor = this.cursor === 0 ? this.options.length - 1 : this.cursor - 1;
47
- break;
48
- case "down":
49
- case "right":
50
- this.cursor = this.cursor === this.options.length - 1 ? 0 : this.cursor + 1;
51
- break;
52
- case "space":
53
- this.toggleValue();
54
- break;
55
- }
56
- });
57
- }
58
74
  }