@inquirer/core 1.0.4 → 1.1.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.
@@ -29,12 +29,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.createPrompt = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
30
30
  const node_readline_1 = __importDefault(require("node:readline"));
31
31
  const mute_stream_1 = __importDefault(require("mute-stream"));
32
- const screen_manager_mjs_1 = __importDefault(require("./lib/screen-manager.js"));
33
- const options_mjs_1 = require("./lib/options.js");
34
- var prefix_mjs_1 = require("./lib/prefix.js");
32
+ const screen_manager_mjs_1 = __importDefault(require("./lib/screen-manager.mjs"));
33
+ const options_mjs_1 = require("./lib/options.mjs");
34
+ var prefix_mjs_1 = require("./lib/prefix.mjs");
35
35
  Object.defineProperty(exports, "usePrefix", { enumerable: true, get: function () { return prefix_mjs_1.usePrefix; } });
36
- __exportStar(require("./lib/key.js"), exports);
37
- __exportStar(require("./lib/Paginator.js"), exports);
36
+ __exportStar(require("./lib/key.mjs"), exports);
37
+ __exportStar(require("./lib/Paginator.mjs"), exports);
38
+ __exportStar(require("./lib/Separator.mjs"), exports);
38
39
  const hooks = [];
39
40
  const hooksCleanup = [];
40
41
  const hooksEffect = [];
@@ -51,7 +52,12 @@ function useState(defaultValue) {
51
52
  const _idx = index;
52
53
  index++;
53
54
  if (!(_idx in hooks)) {
54
- hooks[_idx] = defaultValue;
55
+ if (typeof defaultValue === 'function') {
56
+ hooks[_idx] = defaultValue();
57
+ }
58
+ else {
59
+ hooks[_idx] = defaultValue;
60
+ }
55
61
  }
56
62
  return [
57
63
  hooks[_idx],
@@ -74,7 +80,11 @@ function useEffect(cb, depArray) {
74
80
  if (hasChanged) {
75
81
  hooksEffect.push(() => {
76
82
  cleanupHook(_idx);
77
- hooksCleanup[_idx] = cb();
83
+ const cleanFn = cb();
84
+ if (cleanFn != null && typeof cleanFn !== 'function') {
85
+ throw new Error('useEffect return value must be a cleanup function or nothing.');
86
+ }
87
+ hooksCleanup[_idx] = cleanFn;
78
88
  });
79
89
  }
80
90
  hooks[_idx] = depArray;
@@ -119,25 +129,35 @@ function createPrompt(view) {
119
129
  sessionRl = rl;
120
130
  // TODO: we should display a loader while we get the default options.
121
131
  const resolvedConfig = yield (0, options_mjs_1.getPromptConfig)(config);
122
- return new Promise((resolve) => {
132
+ return new Promise((resolve, reject) => {
123
133
  const done = (value) => {
124
- let len = hooksCleanup.length;
125
- while (len--) {
126
- cleanupHook(len);
127
- }
128
- if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
129
- screen.clean();
130
- }
131
- else {
132
- screen.clearContent();
133
- }
134
- screen.done();
135
- // Reset hooks state
136
- index = 0;
137
- hooks.length = 0;
138
- sessionRl = undefined;
139
- // Finally we resolve our promise
140
- resolve(value);
134
+ // Delay execution to let time to the hookCleanup functions to registers.
135
+ setImmediate(() => {
136
+ try {
137
+ let len = hooksCleanup.length;
138
+ while (len--) {
139
+ cleanupHook(len);
140
+ }
141
+ }
142
+ catch (err) {
143
+ reject(err);
144
+ }
145
+ if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
146
+ screen.clean();
147
+ }
148
+ else {
149
+ screen.clearContent();
150
+ }
151
+ screen.done();
152
+ // Reset hooks state
153
+ index = 0;
154
+ hooks.length = 0;
155
+ hooksEffect.length = 0;
156
+ hooksCleanup.length = 0;
157
+ sessionRl = undefined;
158
+ // Finally we resolve our promise
159
+ resolve(value);
160
+ });
141
161
  };
142
162
  const workLoop = () => {
143
163
  index = 0;
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Paginator = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  const cli_width_1 = __importDefault(require("cli-width"));
9
- const utils_mjs_1 = require("./utils.js");
9
+ const utils_mjs_1 = require("./utils.mjs");
10
10
  /**
11
11
  * The paginator keeps track of a pointer index in a list and returns
12
12
  * a subset of the choices if the list is too long.
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Separator = void 0;
7
+ const chalk_1 = __importDefault(require("chalk"));
8
+ const figures_1 = __importDefault(require("figures"));
9
+ class Separator {
10
+ constructor(separator) {
11
+ Object.defineProperty(this, "separator", {
12
+ enumerable: true,
13
+ configurable: true,
14
+ writable: true,
15
+ value: chalk_1.default.dim(new Array(15).join(figures_1.default.line))
16
+ });
17
+ Object.defineProperty(this, "type", {
18
+ enumerable: true,
19
+ configurable: true,
20
+ writable: true,
21
+ value: 'separator'
22
+ });
23
+ if (separator) {
24
+ this.separator = separator;
25
+ }
26
+ }
27
+ }
28
+ exports.Separator = Separator;
@@ -6,7 +6,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.usePrefix = void 0;
7
7
  const chalk_1 = __importDefault(require("chalk"));
8
8
  const cli_spinners_1 = __importDefault(require("cli-spinners"));
9
- const index_mjs_1 = require("../index.js");
9
+ const index_mjs_1 = require("../index.mjs");
10
10
  const spinner = cli_spinners_1.default.dots;
11
11
  function usePrefix(isLoading = false) {
12
12
  const [tick, setTick] = (0, index_mjs_1.useState)(0);
@@ -30,8 +30,8 @@ const cli_width_1 = __importDefault(require("cli-width"));
30
30
  const strip_ansi_1 = __importDefault(require("strip-ansi"));
31
31
  const string_width_1 = __importDefault(require("string-width"));
32
32
  const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
33
- const util = __importStar(require("./readline.js"));
34
- const utils_mjs_1 = require("./utils.js");
33
+ const util = __importStar(require("./readline.mjs"));
34
+ const utils_mjs_1 = require("./utils.mjs");
35
35
  const height = (content) => content.split('\n').length;
36
36
  const lastLine = (content) => { var _a; return (_a = content.split('\n').pop()) !== null && _a !== void 0 ? _a : ''; };
37
37
  class ScreenManager {
@@ -6,6 +6,7 @@ import MuteStream from 'mute-stream';
6
6
  export { usePrefix } from './lib/prefix.mjs';
7
7
  export * from './lib/key.mjs';
8
8
  export * from './lib/Paginator.mjs';
9
+ export * from './lib/Separator.mjs';
9
10
  export type InquirerReadline = readline.ReadLine & {
10
11
  output: MuteStream;
11
12
  input: NodeJS.ReadableStream;
@@ -14,7 +15,8 @@ export type KeypressEvent = {
14
15
  name: string;
15
16
  ctrl: boolean;
16
17
  };
17
- export declare function useState<Value>(defaultValue: Value): [Value, (newValue: Value) => void];
18
+ type NotFunction<T> = T extends Function ? never : T;
19
+ export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
18
20
  export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
19
21
  export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
20
22
  export declare function useRef<Value>(val: Value): {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Separator object
3
+ * Used to space/separate choices group
4
+ */
5
+ export type SeparatorType = {
6
+ type: 'separator';
7
+ separator: string;
8
+ };
9
+ export declare class Separator {
10
+ separator: string;
11
+ readonly type = "separator";
12
+ constructor(separator?: string);
13
+ }
@@ -5,6 +5,7 @@ import { getPromptConfig } from './lib/options.mjs';
5
5
  export { usePrefix } from './lib/prefix.mjs';
6
6
  export * from './lib/key.mjs';
7
7
  export * from './lib/Paginator.mjs';
8
+ export * from './lib/Separator.mjs';
8
9
  const hooks = [];
9
10
  const hooksCleanup = [];
10
11
  const hooksEffect = [];
@@ -21,7 +22,12 @@ export function useState(defaultValue) {
21
22
  const _idx = index;
22
23
  index++;
23
24
  if (!(_idx in hooks)) {
24
- hooks[_idx] = defaultValue;
25
+ if (typeof defaultValue === 'function') {
26
+ hooks[_idx] = defaultValue();
27
+ }
28
+ else {
29
+ hooks[_idx] = defaultValue;
30
+ }
25
31
  }
26
32
  return [
27
33
  hooks[_idx],
@@ -43,7 +49,11 @@ export function useEffect(cb, depArray) {
43
49
  if (hasChanged) {
44
50
  hooksEffect.push(() => {
45
51
  cleanupHook(_idx);
46
- hooksCleanup[_idx] = cb();
52
+ const cleanFn = cb();
53
+ if (cleanFn != null && typeof cleanFn !== 'function') {
54
+ throw new Error('useEffect return value must be a cleanup function or nothing.');
55
+ }
56
+ hooksCleanup[_idx] = cleanFn;
47
57
  });
48
58
  }
49
59
  hooks[_idx] = depArray;
@@ -84,25 +94,35 @@ export function createPrompt(view) {
84
94
  sessionRl = rl;
85
95
  // TODO: we should display a loader while we get the default options.
86
96
  const resolvedConfig = await getPromptConfig(config);
87
- return new Promise((resolve) => {
97
+ return new Promise((resolve, reject) => {
88
98
  const done = (value) => {
89
- let len = hooksCleanup.length;
90
- while (len--) {
91
- cleanupHook(len);
92
- }
93
- if (context?.clearPromptOnDone) {
94
- screen.clean();
95
- }
96
- else {
97
- screen.clearContent();
98
- }
99
- screen.done();
100
- // Reset hooks state
101
- index = 0;
102
- hooks.length = 0;
103
- sessionRl = undefined;
104
- // Finally we resolve our promise
105
- resolve(value);
99
+ // Delay execution to let time to the hookCleanup functions to registers.
100
+ setImmediate(() => {
101
+ try {
102
+ let len = hooksCleanup.length;
103
+ while (len--) {
104
+ cleanupHook(len);
105
+ }
106
+ }
107
+ catch (err) {
108
+ reject(err);
109
+ }
110
+ if (context?.clearPromptOnDone) {
111
+ screen.clean();
112
+ }
113
+ else {
114
+ screen.clearContent();
115
+ }
116
+ screen.done();
117
+ // Reset hooks state
118
+ index = 0;
119
+ hooks.length = 0;
120
+ hooksEffect.length = 0;
121
+ hooksCleanup.length = 0;
122
+ sessionRl = undefined;
123
+ // Finally we resolve our promise
124
+ resolve(value);
125
+ });
106
126
  };
107
127
  const workLoop = () => {
108
128
  index = 0;
@@ -0,0 +1,11 @@
1
+ import chalk from 'chalk';
2
+ import figures from 'figures';
3
+ export class Separator {
4
+ separator = chalk.dim(new Array(15).join(figures.line));
5
+ type = 'separator';
6
+ constructor(separator) {
7
+ if (separator) {
8
+ this.separator = separator;
9
+ }
10
+ }
11
+ }
@@ -6,6 +6,7 @@ import MuteStream from 'mute-stream';
6
6
  export { usePrefix } from './lib/prefix.mjs';
7
7
  export * from './lib/key.mjs';
8
8
  export * from './lib/Paginator.mjs';
9
+ export * from './lib/Separator.mjs';
9
10
  export type InquirerReadline = readline.ReadLine & {
10
11
  output: MuteStream;
11
12
  input: NodeJS.ReadableStream;
@@ -14,7 +15,8 @@ export type KeypressEvent = {
14
15
  name: string;
15
16
  ctrl: boolean;
16
17
  };
17
- export declare function useState<Value>(defaultValue: Value): [Value, (newValue: Value) => void];
18
+ type NotFunction<T> = T extends Function ? never : T;
19
+ export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
18
20
  export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
19
21
  export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
20
22
  export declare function useRef<Value>(val: Value): {
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Separator object
3
+ * Used to space/separate choices group
4
+ */
5
+ export type SeparatorType = {
6
+ type: 'separator';
7
+ separator: string;
8
+ };
9
+ export declare class Separator {
10
+ separator: string;
11
+ readonly type = "separator";
12
+ constructor(separator?: string);
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@inquirer/core",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "engines": {
5
5
  "node": ">=14.18.0"
6
6
  },
@@ -62,7 +62,7 @@
62
62
  "chalk": "^4.1.2",
63
63
  "cli-spinners": "^2.8.0",
64
64
  "cli-width": "^4.0.0",
65
- "lodash": "^4.17.21",
65
+ "figures": "^3.2.0",
66
66
  "mute-stream": "^1.0.0",
67
67
  "run-async": "^2.3.0",
68
68
  "string-width": "^4.2.3",
@@ -94,5 +94,5 @@
94
94
  }
95
95
  }
96
96
  },
97
- "gitHead": "12fecef5292da253564c845ef5be67b37441e381"
97
+ "gitHead": "09f738e0ab2b52c0b33a3973f37d5256bca9a6ab"
98
98
  }
File without changes
File without changes
File without changes
File without changes