@reliverse/relinka 1.1.7 → 1.1.8

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 (121) hide show
  1. package/dist-npm/components/anykey/index.js +57 -77
  2. package/dist-npm/components/block/block.js +48 -61
  3. package/dist-npm/components/checkbox/index.js +178 -211
  4. package/dist-npm/components/confirm/confirm-main.js +85 -122
  5. package/dist-npm/components/confirm/confirm-three.js +24 -27
  6. package/dist-npm/components/confirm/index.js +36 -45
  7. package/dist-npm/components/core/Separator.js +15 -17
  8. package/dist-npm/components/core/create-prompt.js +101 -124
  9. package/dist-npm/components/core/errors.js +19 -15
  10. package/dist-npm/components/core/hook-engine.js +91 -120
  11. package/dist-npm/components/core/index.js +12 -12
  12. package/dist-npm/components/core/key.js +16 -6
  13. package/dist-npm/components/core/lines.js +45 -54
  14. package/dist-npm/components/core/make-theme.js +21 -30
  15. package/dist-npm/components/core/position.js +27 -23
  16. package/dist-npm/components/core/promise-polyfill.js +11 -14
  17. package/dist-npm/components/core/screen-manager.js +59 -74
  18. package/dist-npm/components/core/theme.js +22 -32
  19. package/dist-npm/components/core/use-effect.js +9 -15
  20. package/dist-npm/components/core/use-keypress.js +19 -23
  21. package/dist-npm/components/core/use-memo.js +10 -16
  22. package/dist-npm/components/core/use-pagination.js +33 -32
  23. package/dist-npm/components/core/use-prefix.js +40 -38
  24. package/dist-npm/components/core/use-ref.js +2 -5
  25. package/dist-npm/components/core/use-state.js +15 -23
  26. package/dist-npm/components/core/useKeyPress.js +14 -17
  27. package/dist-npm/components/core/usePromptState.js +8 -14
  28. package/dist-npm/components/core/utils.js +7 -16
  29. package/dist-npm/components/date/date.js +173 -204
  30. package/dist-npm/components/editor/index.js +71 -92
  31. package/dist-npm/components/expand/index.js +96 -124
  32. package/dist-npm/components/figures/index.js +283 -294
  33. package/dist-npm/components/input/index.js +61 -87
  34. package/dist-npm/components/input/text-main.js +97 -124
  35. package/dist-npm/components/input/text.js +24 -28
  36. package/dist-npm/components/instance/basic.js +17 -25
  37. package/dist-npm/components/instance/browser.js +14 -18
  38. package/dist-npm/components/instance/reporter/basic.js +46 -65
  39. package/dist-npm/components/instance/reporter/browser.js +44 -47
  40. package/dist-npm/components/instance/reporter/fancy.js +83 -96
  41. package/dist-npm/components/instance/shared.js +2 -2
  42. package/dist-npm/components/mono/mono.js +52 -62
  43. package/dist-npm/components/mono/monoTwo.js +35 -49
  44. package/dist-npm/components/multiselect/group-multiselect.js +55 -71
  45. package/dist-npm/components/multiselect/multi-select-two.js +97 -130
  46. package/dist-npm/components/multiselect/multi-select.js +43 -49
  47. package/dist-npm/components/multiselect/multiselect-main.d.ts +1 -0
  48. package/dist-npm/components/multiselect/multiselect-main.js +158 -145
  49. package/dist-npm/components/multiselect/num-multi-select.js +97 -130
  50. package/dist-npm/components/multiselect/num-multiselect-main.js +24 -35
  51. package/dist-npm/components/next-steps/next-steps.js +23 -25
  52. package/dist-npm/components/number/index.js +78 -112
  53. package/dist-npm/components/number/number-main.js +95 -2
  54. package/dist-npm/components/password/index.js +54 -73
  55. package/dist-npm/components/password/password-main.js +119 -2
  56. package/dist-npm/components/password/password-three.js +26 -30
  57. package/dist-npm/components/progressbar/ProgressBar.js +45 -64
  58. package/dist-npm/components/progressbar/helper.js +33 -40
  59. package/dist-npm/components/progressbar/index.js +1 -1
  60. package/dist-npm/components/prompts/create.js +29 -44
  61. package/dist-npm/components/prompts/index.d.ts +1 -0
  62. package/dist-npm/components/prompts/index.js +46 -45
  63. package/dist-npm/components/prompts/prompt.js +211 -260
  64. package/dist-npm/components/prompts/promptTwo.js +561 -605
  65. package/dist-npm/components/prompts/relinka.js +237 -295
  66. package/dist-npm/components/range/range.js +247 -294
  67. package/dist-npm/components/rawlist/index.js +87 -107
  68. package/dist-npm/components/results/results.js +31 -37
  69. package/dist-npm/components/search/index.js +148 -193
  70. package/dist-npm/components/select/index.js +148 -186
  71. package/dist-npm/components/select/num-select-main.js +27 -27
  72. package/dist-npm/components/select/num-select.js +124 -5
  73. package/dist-npm/components/select/select-key.js +24 -25
  74. package/dist-npm/components/select/select-main.d.ts +1 -0
  75. package/dist-npm/components/select/select-main.js +143 -133
  76. package/dist-npm/components/select/select-three.js +32 -36
  77. package/dist-npm/components/select/select-two.js +94 -87
  78. package/dist-npm/components/spinner/index.js +107 -136
  79. package/dist-npm/components/st-end/end.js +34 -26
  80. package/dist-npm/components/st-end/start.js +29 -15
  81. package/dist-npm/components/toggle/index.js +113 -137
  82. package/dist-npm/components/visual/animate/animate.js +53 -10
  83. package/dist-npm/components/visual/ascii-art/ascii-art.js +12 -1
  84. package/dist-npm/main.js +1 -0
  85. package/dist-npm/testing/index.js +58 -83
  86. package/dist-npm/types/general.d.ts +1 -1
  87. package/dist-npm/types/general.js +0 -1
  88. package/dist-npm/types/index.js +2 -3
  89. package/dist-npm/types/keypress.js +35 -36
  90. package/dist-npm/types/readline.js +0 -1
  91. package/dist-npm/types/relinka.js +0 -1
  92. package/dist-npm/types/utils.js +0 -1
  93. package/dist-npm/utils/box.js +135 -137
  94. package/dist-npm/utils/color.js +65 -74
  95. package/dist-npm/utils/colorize.js +124 -156
  96. package/dist-npm/utils/component.js +532 -657
  97. package/dist-npm/utils/constants.js +64 -63
  98. package/dist-npm/utils/core.js +2 -3
  99. package/dist-npm/utils/decoder.js +244 -223
  100. package/dist-npm/utils/error.js +4 -9
  101. package/dist-npm/utils/errors.js +14 -4
  102. package/dist-npm/utils/format.js +19 -24
  103. package/dist-npm/utils/keypress.js +316 -414
  104. package/dist-npm/utils/log.js +11 -15
  105. package/dist-npm/utils/mapping.js +45 -52
  106. package/dist-npm/utils/messages.js +196 -183
  107. package/dist-npm/utils/platforms.js +16 -20
  108. package/dist-npm/utils/prompt-tmp.js +235 -286
  109. package/dist-npm/utils/prompt-two.js +235 -286
  110. package/dist-npm/utils/readline.js +5 -7
  111. package/dist-npm/utils/skeleton.js +130 -170
  112. package/dist-npm/utils/stream.js +2 -2
  113. package/dist-npm/utils/string.js +44 -58
  114. package/dist-npm/utils/terminal.js +23 -34
  115. package/dist-npm/utils/tree.js +30 -41
  116. package/dist-npm/utils/types.js +0 -1
  117. package/dist-npm/utils/utils.js +8 -8
  118. package/dist-npm/utils/variants.js +36 -44
  119. package/package.json +14 -14
  120. package/dist-npm/mod.js +0 -2
  121. /package/dist-npm/{mod.d.ts → main.d.ts} +0 -0
@@ -1,59 +1,56 @@
1
- const isString = (a) => typeof a === 'string';
2
-
3
1
  export class BrowserReporter {
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
- };
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;
21
25
  }
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;
26
+ if (level === 1) {
27
+ return console.__warn || console.warn;
33
28
  }
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 = `
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 = `
42
37
  background: ${color};
43
38
  border-radius: 0.5em;
44
39
  color: white;
45
40
  font-weight: bold;
46
41
  padding: 2px 0.5em;
47
42
  `;
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
- 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
- }
49
+ "",
50
+ ...logObj.args.slice(1)
51
+ );
52
+ } else {
53
+ consoleLogFn(badge, style, ...logObj.args);
58
54
  }
55
+ }
59
56
  }
@@ -1,114 +1,101 @@
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
-
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";
11
8
  export const TYPE_COLOR_MAP = {
12
- info: 'cyan',
13
- fail: 'red',
14
- success: 'green',
15
- ready: 'green',
16
- start: 'magenta',
9
+ info: "cyan",
10
+ fail: "red",
11
+ success: "green",
12
+ ready: "green",
13
+ start: "magenta"
17
14
  };
18
15
  export const LEVEL_COLOR_MAP = {
19
- 0: 'red',
20
- 1: 'yellow',
16
+ 0: "red",
17
+ 1: "yellow"
21
18
  };
22
19
  const unicode = isUnicodeSupported();
23
20
  const s = (c, fallback) => unicode ? c : fallback;
24
-
25
21
  const TYPE_ICONS = {
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: '',
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: ""
37
33
  };
38
-
39
34
  function stringWidth(str) {
40
- if (!Intl.Segmenter) {
41
- return stripAnsi(str).length;
42
- }
43
-
44
- return _stringWidth(str);
35
+ if (!Intl.Segmenter) {
36
+ return stripAnsi(str).length;
37
+ }
38
+ return _stringWidth(str);
45
39
  }
46
-
47
40
  export class FancyReporter extends BasicReporter {
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');
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
+ );
55
53
  }
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()} `));
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
62
70
  }
63
-
64
- const _type = isString(TYPE_ICONS[logObj.type]) ? TYPE_ICONS[logObj.type] : logObj.icon || logObj.type;
65
-
66
- return _type ? getColor(typeColor)(_type) : '';
71
+ );
67
72
  }
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;
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 || "");
101
89
  }
102
- }function characterFormat(str) {
103
- return str
104
- .replace(/`([^`]+)`/gm, (_, m) => colors.cyan(m))
105
- .replace(/\s+_([^_]+)_\s+/gm, (_, m) => ` ${colors.underline(m)} `);
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)} `);
106
95
  }
107
-
108
- function getColor(color = 'white') {
109
- return colors[color] || colors.white;
96
+ function getColor(color = "white") {
97
+ return colors[color] || colors.white;
110
98
  }
111
-
112
- function getBgColor(color = 'bgWhite') {
113
- return colors[`bg${color[0].toUpperCase()}${color.slice(1)}`] || colors.bgWhite;
99
+ function getBgColor(color = "bgWhite") {
100
+ return colors[`bg${color[0].toUpperCase()}${color.slice(1)}`] || colors.bgWhite;
114
101
  }
@@ -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,74 +1,64 @@
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
-
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";
11
10
  export async function prompt(options) {
12
- const {
13
- type,
14
- id,
15
- action,
16
- } = options;
17
- let value;
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;
18
30
 
19
- switch(type) {
20
- case 'start':
21
- await startPrompt(options);
22
- value = null;
23
- break;
24
31
 
25
- case 'text':
26
- value = await textPrompt(options);
27
- break;
28
32
 
29
- case 'number':
30
- value = await numberPrompt(options);
31
- break;
32
33
 
33
- case 'confirm':
34
- value = await confirmPrompt(options);
35
- break;
36
34
 
37
- case 'numSelect':
38
- value = await numSelectPrompt(options);
39
- break;
40
35
 
41
- case 'password':
42
- value = await passwordPrompt(options);
43
- break;
44
36
 
45
- case 'date':
46
- value = await datePrompt({
47
- ...options,
48
- dateFormat: 'DD/MM/YYYY',
49
- dateKind: 'other',
50
- });
51
- break;
52
37
 
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;
62
38
 
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;
63
57
  default:
64
- throw Error(`Unknown prompt type: ${type}`);
65
- }
66
-
67
- if (action) {
68
- await action();
69
- }
70
-
71
- return {
72
- [id]: value,
73
- };
58
+ throw new Error(`Unknown prompt type: ${type}`);
59
+ }
60
+ if (action) {
61
+ await action();
62
+ }
63
+ return { [id]: value };
74
64
  }
@@ -1,51 +1,37 @@
1
- import {
2
- text,
3
- confirm,
4
- select,
5
- multiselect,
6
- } from '../prompts/promptTwo.js';
7
-
8
- const isString = (a) => typeof a === 'string';
9
-
1
+ import { text, confirm, select, multiselect } from "../prompts/promptTwo.js";
10
2
  export async function prompt(message, opts = {}) {
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}`);
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}`);
51
37
  }
@@ -1,74 +1,58 @@
1
- import Prompt from '../prompts/prompt.js';
2
-
1
+ import Prompt from "../prompts/prompt.js";
3
2
  export default class GroupMultiSelectPrompt extends Prompt {
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
- });
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];
73
28
  }
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
+ }
74
58
  }