inquirerer 4.8.1 → 4.9.1

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/esm/prompt.js CHANGED
@@ -134,6 +134,17 @@ function generatePromptMessage(question, ctx) {
134
134
  }
135
135
  return lines.join('\n') + '\n';
136
136
  }
137
+ export class PromptTimeoutError extends Error {
138
+ currentQuestion;
139
+ allQuestions;
140
+ constructor(message, currentQuestion, allQuestions) {
141
+ super(message);
142
+ this.name = 'PromptTimeoutError';
143
+ this.currentQuestion = currentQuestion;
144
+ this.allQuestions = allQuestions;
145
+ }
146
+ }
147
+ const DEFAULT_NON_TTY_TIMEOUT = 15_000;
137
148
  export class Inquirerer {
138
149
  rl;
139
150
  keypress;
@@ -144,9 +155,13 @@ export class Inquirerer {
144
155
  globalMaxLines;
145
156
  mutateArgs;
146
157
  resolverRegistry;
158
+ timeout;
159
+ _inactivityTimer = null;
160
+ _inactivityReject = null;
161
+ _inactivityContext = null;
147
162
  handledKeys = new Set();
148
163
  constructor(options) {
149
- const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false, globalMaxLines = 10, mutateArgs = true, resolverRegistry = globalResolverRegistry } = options ?? {};
164
+ const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false, globalMaxLines = 10, mutateArgs = true, resolverRegistry = globalResolverRegistry, timeout } = options ?? {};
150
165
  this.useDefaults = useDefaults;
151
166
  this.noTty = noTty;
152
167
  this.output = output;
@@ -154,6 +169,12 @@ export class Inquirerer {
154
169
  this.input = input;
155
170
  this.globalMaxLines = globalMaxLines;
156
171
  this.resolverRegistry = resolverRegistry;
172
+ if (timeout !== undefined) {
173
+ this.timeout = timeout;
174
+ }
175
+ else if (!noTty) {
176
+ this.timeout = DEFAULT_NON_TTY_TIMEOUT;
177
+ }
157
178
  if (!noTty) {
158
179
  this.rl = readline.createInterface({
159
180
  input,
@@ -383,7 +404,18 @@ export class Inquirerer {
383
404
  continue; // Skip to the next question since the default is applied
384
405
  }
385
406
  while (ctx.needsInput) {
386
- obj[question.name] = await this.handleQuestionType(question, ctx);
407
+ const timeoutPromise = this.startInactivityTimeout(question, ordered);
408
+ const removeActivityListener = this.setupInputActivityListener();
409
+ try {
410
+ const questionPromise = this.handleQuestionType(question, ctx);
411
+ obj[question.name] = timeoutPromise
412
+ ? await Promise.race([questionPromise, timeoutPromise])
413
+ : await questionPromise;
414
+ }
415
+ finally {
416
+ removeActivityListener();
417
+ this.clearInactivityTimeout();
418
+ }
387
419
  if (!this.isValid(question, obj, ctx)) {
388
420
  if (this.noTty) {
389
421
  // If you're not valid and here with noTty, you're out!
@@ -1177,6 +1209,90 @@ export class Inquirerer {
1177
1209
  // }
1178
1210
  return Math.min(this.globalMaxLines, defaultLength);
1179
1211
  }
1212
+ startInactivityTimeout(question, allQuestions) {
1213
+ if (this.timeout === undefined)
1214
+ return null;
1215
+ const ms = this.timeout;
1216
+ this._inactivityContext = { question, allQuestions, ms };
1217
+ return new Promise((_, reject) => {
1218
+ this._inactivityReject = reject;
1219
+ this._inactivityTimer = setTimeout(() => {
1220
+ reject(new PromptTimeoutError(this.formatTimeoutError(question, allQuestions, ms), question, allQuestions));
1221
+ }, ms);
1222
+ });
1223
+ }
1224
+ resetInactivityTimeout() {
1225
+ if (this._inactivityTimer === null || this._inactivityReject === null || this._inactivityContext === null)
1226
+ return;
1227
+ clearTimeout(this._inactivityTimer);
1228
+ const { question, allQuestions, ms } = this._inactivityContext;
1229
+ const rejectFn = this._inactivityReject;
1230
+ this._inactivityTimer = setTimeout(() => {
1231
+ rejectFn(new PromptTimeoutError(this.formatTimeoutError(question, allQuestions, ms), question, allQuestions));
1232
+ }, ms);
1233
+ }
1234
+ clearInactivityTimeout() {
1235
+ if (this._inactivityTimer !== null) {
1236
+ clearTimeout(this._inactivityTimer);
1237
+ this._inactivityTimer = null;
1238
+ }
1239
+ this._inactivityReject = null;
1240
+ this._inactivityContext = null;
1241
+ }
1242
+ setupInputActivityListener() {
1243
+ if (this.timeout === undefined)
1244
+ return () => { };
1245
+ const onData = () => this.resetInactivityTimeout();
1246
+ this.input.on('data', onData);
1247
+ return () => this.input.removeListener('data', onData);
1248
+ }
1249
+ formatTimeoutError(currentQuestion, allQuestions, ms) {
1250
+ const seconds = Math.round(ms / 1000);
1251
+ const lines = [];
1252
+ lines.push('');
1253
+ lines.push(`PROMPT TIMEOUT: No input received for "${currentQuestion.name}" after ${seconds}s.`);
1254
+ lines.push('');
1255
+ lines.push('No input was received before the inactivity timeout.');
1256
+ lines.push('');
1257
+ lines.push('REQUIRED ARGUMENTS:');
1258
+ for (const q of allQuestions) {
1259
+ const flag = `--${q.name}`;
1260
+ const aliases = q.alias
1261
+ ? (Array.isArray(q.alias) ? q.alias : [q.alias])
1262
+ .map(a => a.length === 1 ? `-${a}` : `--${a}`)
1263
+ .join(', ')
1264
+ : '';
1265
+ const aliasStr = aliases ? ` (${aliases})` : '';
1266
+ const label = q.message || q.name;
1267
+ const req = q.required ? ' [REQUIRED]' : '';
1268
+ const def = 'default' in q && q.default !== undefined ? ` (default: ${JSON.stringify(q.default)})` : '';
1269
+ lines.push(` ${flag}${aliasStr} ${label}${req}${def}`);
1270
+ }
1271
+ lines.push('');
1272
+ lines.push('HOW TO FIX:');
1273
+ lines.push(' 1. Pass all required arguments as CLI flags:');
1274
+ const requiredFlags = allQuestions
1275
+ .filter(q => q.required)
1276
+ .map(q => `--${q.name} <value>`);
1277
+ if (requiredFlags.length > 0) {
1278
+ lines.push(` $ command ${requiredFlags.join(' ')}`);
1279
+ }
1280
+ else {
1281
+ lines.push(' $ command --flag1 <value> --flag2 <value>');
1282
+ }
1283
+ lines.push('');
1284
+ lines.push(' 2. Or enable non-interactive mode:');
1285
+ lines.push(' new Inquirerer({ noTty: true, useDefaults: true })');
1286
+ lines.push('');
1287
+ lines.push(' 3. Or pass --no-tty if the CLI supports it.');
1288
+ lines.push('');
1289
+ lines.push('WHY THIS HAPPENED:');
1290
+ lines.push(' The prompt waited for input but none was received within the');
1291
+ lines.push(' timeout window. Pass the required arguments as CLI flags');
1292
+ lines.push(' to avoid interactive prompts.');
1293
+ lines.push('');
1294
+ return lines.join('\n');
1295
+ }
1180
1296
  // Method to cleanly close the readline interface
1181
1297
  // NOTE: use exit() to close!
1182
1298
  close() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "inquirerer",
3
- "version": "4.8.1",
3
+ "version": "4.9.1",
4
4
  "author": "Constructive <developers@constructive.io>",
5
5
  "description": "TypeScript-first library for building beautiful CLI interfaces with interactive prompts",
6
6
  "main": "index.js",
@@ -46,5 +46,5 @@
46
46
  "makage": "0.1.10",
47
47
  "string-width": "4.2.3"
48
48
  },
49
- "gitHead": "ea7144ba7becb4bcabfc5044214eafe25b24b876"
49
+ "gitHead": "e4fa1ffdded38205e01b42c5f12171a5601cdac6"
50
50
  }
package/prompt.d.ts CHANGED
@@ -22,6 +22,11 @@ declare class PromptContext {
22
22
  nextQuestion(): void;
23
23
  process(validation: Validation | boolean): Validation;
24
24
  }
25
+ export declare class PromptTimeoutError extends Error {
26
+ readonly currentQuestion: Question;
27
+ readonly allQuestions: Question[];
28
+ constructor(message: string, currentQuestion: Question, allQuestions: Question[]);
29
+ }
25
30
  export interface InquirererOptions {
26
31
  noTty?: boolean;
27
32
  input?: Readable;
@@ -30,6 +35,7 @@ export interface InquirererOptions {
30
35
  globalMaxLines?: number;
31
36
  mutateArgs?: boolean;
32
37
  resolverRegistry?: DefaultResolverRegistry;
38
+ timeout?: number;
33
39
  }
34
40
  export declare class Inquirerer {
35
41
  private rl;
@@ -41,6 +47,10 @@ export declare class Inquirerer {
41
47
  private globalMaxLines;
42
48
  private mutateArgs;
43
49
  private resolverRegistry;
50
+ private timeout;
51
+ private _inactivityTimer;
52
+ private _inactivityReject;
53
+ private _inactivityContext;
44
54
  private handledKeys;
45
55
  constructor(options?: InquirererOptions);
46
56
  private clearScreen;
@@ -119,6 +129,11 @@ export declare class Inquirerer {
119
129
  private sanitizeOptions;
120
130
  private filterOptions;
121
131
  private getMaxLines;
132
+ private startInactivityTimeout;
133
+ private resetInactivityTimeout;
134
+ private clearInactivityTimeout;
135
+ private setupInputActivityListener;
136
+ private formatTimeoutError;
122
137
  close(): void;
123
138
  }
124
139
  export {};
package/prompt.js CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Inquirerer = void 0;
6
+ exports.Inquirerer = exports.PromptTimeoutError = void 0;
7
7
  exports.reorderQuestionsByDeps = reorderQuestionsByDeps;
8
8
  const yanse_1 = require("yanse");
9
9
  const readline_1 = __importDefault(require("readline"));
@@ -141,6 +141,18 @@ function generatePromptMessage(question, ctx) {
141
141
  }
142
142
  return lines.join('\n') + '\n';
143
143
  }
144
+ class PromptTimeoutError extends Error {
145
+ currentQuestion;
146
+ allQuestions;
147
+ constructor(message, currentQuestion, allQuestions) {
148
+ super(message);
149
+ this.name = 'PromptTimeoutError';
150
+ this.currentQuestion = currentQuestion;
151
+ this.allQuestions = allQuestions;
152
+ }
153
+ }
154
+ exports.PromptTimeoutError = PromptTimeoutError;
155
+ const DEFAULT_NON_TTY_TIMEOUT = 15_000;
144
156
  class Inquirerer {
145
157
  rl;
146
158
  keypress;
@@ -151,9 +163,13 @@ class Inquirerer {
151
163
  globalMaxLines;
152
164
  mutateArgs;
153
165
  resolverRegistry;
166
+ timeout;
167
+ _inactivityTimer = null;
168
+ _inactivityReject = null;
169
+ _inactivityContext = null;
154
170
  handledKeys = new Set();
155
171
  constructor(options) {
156
- const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false, globalMaxLines = 10, mutateArgs = true, resolverRegistry = resolvers_1.globalResolverRegistry } = options ?? {};
172
+ const { noTty = false, input = process.stdin, output = process.stdout, useDefaults = false, globalMaxLines = 10, mutateArgs = true, resolverRegistry = resolvers_1.globalResolverRegistry, timeout } = options ?? {};
157
173
  this.useDefaults = useDefaults;
158
174
  this.noTty = noTty;
159
175
  this.output = output;
@@ -161,6 +177,12 @@ class Inquirerer {
161
177
  this.input = input;
162
178
  this.globalMaxLines = globalMaxLines;
163
179
  this.resolverRegistry = resolverRegistry;
180
+ if (timeout !== undefined) {
181
+ this.timeout = timeout;
182
+ }
183
+ else if (!noTty) {
184
+ this.timeout = DEFAULT_NON_TTY_TIMEOUT;
185
+ }
164
186
  if (!noTty) {
165
187
  this.rl = readline_1.default.createInterface({
166
188
  input,
@@ -390,7 +412,18 @@ class Inquirerer {
390
412
  continue; // Skip to the next question since the default is applied
391
413
  }
392
414
  while (ctx.needsInput) {
393
- obj[question.name] = await this.handleQuestionType(question, ctx);
415
+ const timeoutPromise = this.startInactivityTimeout(question, ordered);
416
+ const removeActivityListener = this.setupInputActivityListener();
417
+ try {
418
+ const questionPromise = this.handleQuestionType(question, ctx);
419
+ obj[question.name] = timeoutPromise
420
+ ? await Promise.race([questionPromise, timeoutPromise])
421
+ : await questionPromise;
422
+ }
423
+ finally {
424
+ removeActivityListener();
425
+ this.clearInactivityTimeout();
426
+ }
394
427
  if (!this.isValid(question, obj, ctx)) {
395
428
  if (this.noTty) {
396
429
  // If you're not valid and here with noTty, you're out!
@@ -1184,6 +1217,90 @@ class Inquirerer {
1184
1217
  // }
1185
1218
  return Math.min(this.globalMaxLines, defaultLength);
1186
1219
  }
1220
+ startInactivityTimeout(question, allQuestions) {
1221
+ if (this.timeout === undefined)
1222
+ return null;
1223
+ const ms = this.timeout;
1224
+ this._inactivityContext = { question, allQuestions, ms };
1225
+ return new Promise((_, reject) => {
1226
+ this._inactivityReject = reject;
1227
+ this._inactivityTimer = setTimeout(() => {
1228
+ reject(new PromptTimeoutError(this.formatTimeoutError(question, allQuestions, ms), question, allQuestions));
1229
+ }, ms);
1230
+ });
1231
+ }
1232
+ resetInactivityTimeout() {
1233
+ if (this._inactivityTimer === null || this._inactivityReject === null || this._inactivityContext === null)
1234
+ return;
1235
+ clearTimeout(this._inactivityTimer);
1236
+ const { question, allQuestions, ms } = this._inactivityContext;
1237
+ const rejectFn = this._inactivityReject;
1238
+ this._inactivityTimer = setTimeout(() => {
1239
+ rejectFn(new PromptTimeoutError(this.formatTimeoutError(question, allQuestions, ms), question, allQuestions));
1240
+ }, ms);
1241
+ }
1242
+ clearInactivityTimeout() {
1243
+ if (this._inactivityTimer !== null) {
1244
+ clearTimeout(this._inactivityTimer);
1245
+ this._inactivityTimer = null;
1246
+ }
1247
+ this._inactivityReject = null;
1248
+ this._inactivityContext = null;
1249
+ }
1250
+ setupInputActivityListener() {
1251
+ if (this.timeout === undefined)
1252
+ return () => { };
1253
+ const onData = () => this.resetInactivityTimeout();
1254
+ this.input.on('data', onData);
1255
+ return () => this.input.removeListener('data', onData);
1256
+ }
1257
+ formatTimeoutError(currentQuestion, allQuestions, ms) {
1258
+ const seconds = Math.round(ms / 1000);
1259
+ const lines = [];
1260
+ lines.push('');
1261
+ lines.push(`PROMPT TIMEOUT: No input received for "${currentQuestion.name}" after ${seconds}s.`);
1262
+ lines.push('');
1263
+ lines.push('No input was received before the inactivity timeout.');
1264
+ lines.push('');
1265
+ lines.push('REQUIRED ARGUMENTS:');
1266
+ for (const q of allQuestions) {
1267
+ const flag = `--${q.name}`;
1268
+ const aliases = q.alias
1269
+ ? (Array.isArray(q.alias) ? q.alias : [q.alias])
1270
+ .map(a => a.length === 1 ? `-${a}` : `--${a}`)
1271
+ .join(', ')
1272
+ : '';
1273
+ const aliasStr = aliases ? ` (${aliases})` : '';
1274
+ const label = q.message || q.name;
1275
+ const req = q.required ? ' [REQUIRED]' : '';
1276
+ const def = 'default' in q && q.default !== undefined ? ` (default: ${JSON.stringify(q.default)})` : '';
1277
+ lines.push(` ${flag}${aliasStr} ${label}${req}${def}`);
1278
+ }
1279
+ lines.push('');
1280
+ lines.push('HOW TO FIX:');
1281
+ lines.push(' 1. Pass all required arguments as CLI flags:');
1282
+ const requiredFlags = allQuestions
1283
+ .filter(q => q.required)
1284
+ .map(q => `--${q.name} <value>`);
1285
+ if (requiredFlags.length > 0) {
1286
+ lines.push(` $ command ${requiredFlags.join(' ')}`);
1287
+ }
1288
+ else {
1289
+ lines.push(' $ command --flag1 <value> --flag2 <value>');
1290
+ }
1291
+ lines.push('');
1292
+ lines.push(' 2. Or enable non-interactive mode:');
1293
+ lines.push(' new Inquirerer({ noTty: true, useDefaults: true })');
1294
+ lines.push('');
1295
+ lines.push(' 3. Or pass --no-tty if the CLI supports it.');
1296
+ lines.push('');
1297
+ lines.push('WHY THIS HAPPENED:');
1298
+ lines.push(' The prompt waited for input but none was received within the');
1299
+ lines.push(' timeout window. Pass the required arguments as CLI flags');
1300
+ lines.push(' to avoid interactive prompts.');
1301
+ lines.push('');
1302
+ return lines.join('\n');
1303
+ }
1187
1304
  // Method to cleanly close the readline interface
1188
1305
  // NOTE: use exit() to close!
1189
1306
  close() {