@optique/inquirer 1.0.0-dev.664 → 1.0.0-dev.670

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/dist/index.cjs CHANGED
@@ -69,6 +69,42 @@ function getPromptFunctions() {
69
69
  function isExitPromptError(error) {
70
70
  return typeof error === "object" && error != null && "name" in error && error.name === "ExitPromptError";
71
71
  }
72
+ const deferredPromptValueKey = Symbol.for("@optique/inquirer/deferredPromptValue");
73
+ const deferPromptUntilConfigResolvesKey = Symbol.for("@optique/config/deferPromptUntilResolved");
74
+ const inheritParentAnnotationsKey = Symbol.for("@optique/core/inheritParentAnnotations");
75
+ var DeferredPromptValue = class {
76
+ [deferredPromptValueKey] = true;
77
+ };
78
+ function shouldDeferPrompt(parser, state) {
79
+ const maybeShouldDefer = Reflect.get(parser, deferPromptUntilConfigResolvesKey);
80
+ return typeof maybeShouldDefer === "function" && maybeShouldDefer(state) === true;
81
+ }
82
+ function deferredPromptResult() {
83
+ return {
84
+ success: true,
85
+ value: new DeferredPromptValue()
86
+ };
87
+ }
88
+ function withAnnotationView(state, annotations, run) {
89
+ const annotatedState = new Proxy(state, {
90
+ get(target, key) {
91
+ if (key === __optique_core_annotations.annotationKey) return annotations;
92
+ const value = Reflect.get(target, key, target);
93
+ return typeof value === "function" ? value.bind(target) : value;
94
+ },
95
+ has(target, key) {
96
+ return key === __optique_core_annotations.annotationKey || Reflect.has(target, key);
97
+ }
98
+ });
99
+ return run(annotatedState);
100
+ }
101
+ function withAnnotatedInnerState(sourceState, innerState, run) {
102
+ const annotations = (0, __optique_core_annotations.getAnnotations)(sourceState);
103
+ if (annotations == null || innerState == null || typeof innerState !== "object" || typeof innerState === "object" && __optique_core_annotations.annotationKey in innerState) return run(innerState);
104
+ const inheritedState = (0, __optique_core_annotations.inheritAnnotations)(sourceState, innerState);
105
+ if (inheritedState !== innerState) return run(inheritedState);
106
+ return withAnnotationView(innerState, annotations, (annotatedState) => run(annotatedState));
107
+ }
72
108
  /**
73
109
  * Wraps a parser with an interactive Inquirer.js prompt fallback.
74
110
  *
@@ -110,6 +146,16 @@ function prompt(parser, config) {
110
146
  hasCliValue = false;
111
147
  };
112
148
  let promptCache = null;
149
+ function shouldAttemptInnerCompletion(cliState, state) {
150
+ if (cliState == null || cliState instanceof PromptBindInitialStateClass) return false;
151
+ const cliStateHasAnnotations = typeof cliState === "object" && __optique_core_annotations.annotationKey in cliState;
152
+ if (cliStateHasAnnotations) return true;
153
+ if ((0, __optique_core_annotations.getAnnotations)(state) == null || typeof cliState !== "object") return false;
154
+ if ("hasCliValue" in cliState) return true;
155
+ if (Array.isArray(cliState)) return false;
156
+ const prototype = Object.getPrototypeOf(cliState);
157
+ return prototype !== Object.prototype && prototype !== null;
158
+ }
113
159
  /**
114
160
  * Executes the configured prompt and normalizes its result.
115
161
  *
@@ -223,11 +269,16 @@ function prompt(parser, config) {
223
269
  throw error;
224
270
  }
225
271
  }
226
- return {
272
+ function usePromptOrDefer(state, result) {
273
+ if (result.success) return Promise.resolve(result);
274
+ return shouldDeferPrompt(parser, state) ? Promise.resolve(deferredPromptResult()) : executePrompt();
275
+ }
276
+ const promptedParser = {
227
277
  $mode: "async",
228
278
  $valueType: parser.$valueType,
229
279
  $stateType: parser.$stateType,
230
280
  priority: parser.priority,
281
+ [inheritParentAnnotationsKey]: true,
231
282
  usage: [{
232
283
  type: "optional",
233
284
  terms: parser.usage
@@ -238,13 +289,9 @@ function prompt(parser, config) {
238
289
  parse: (context) => {
239
290
  const annotations = (0, __optique_core_annotations.getAnnotations)(context.state);
240
291
  const innerState = isPromptBindState(context.state) ? context.state.hasCliValue ? context.state.cliState : parser.initialState : context.state;
241
- const innerStateWithAnnotations = annotations != null && (innerState == null || typeof innerState === "object" && !(__optique_core_annotations.annotationKey in innerState)) ? {
242
- ...innerState != null && typeof innerState === "object" ? innerState : {},
243
- [__optique_core_annotations.annotationKey]: annotations
244
- } : innerState;
245
- const innerContext = innerStateWithAnnotations !== context.state ? {
292
+ const baseInnerContext = innerState !== context.state ? {
246
293
  ...context,
247
- state: innerStateWithAnnotations
294
+ state: innerState
248
295
  } : context;
249
296
  const processResult = (result$1) => {
250
297
  if (result$1.success) {
@@ -273,19 +320,25 @@ function prompt(parser, config) {
273
320
  return {
274
321
  success: true,
275
322
  next: {
276
- ...innerContext,
323
+ ...baseInnerContext,
277
324
  state: nextState
278
325
  },
279
326
  consumed: []
280
327
  };
281
328
  };
282
- const result = parser.parse(innerContext);
329
+ const result = withAnnotatedInnerState(context.state, innerState, (annotatedInnerState) => {
330
+ const innerContext = annotatedInnerState !== context.state ? {
331
+ ...context,
332
+ state: annotatedInnerState
333
+ } : context;
334
+ return parser.parse(innerContext);
335
+ });
283
336
  if (result instanceof Promise) return result.then(processResult);
284
337
  return Promise.resolve(processResult(result));
285
338
  },
286
339
  complete: (state) => {
287
340
  if (isPromptBindState(state) && state.hasCliValue) {
288
- const r = parser.complete(state.cliState);
341
+ const r = withAnnotatedInnerState(state, state.cliState, (annotatedInnerState) => parser.complete(annotatedInnerState));
289
342
  if (r instanceof Promise) return r;
290
343
  return Promise.resolve(r);
291
344
  }
@@ -295,10 +348,8 @@ function prompt(parser, config) {
295
348
  promptCache = null;
296
349
  return cached;
297
350
  }
298
- const innerState = parser.initialState;
299
- const r = parser.complete(innerState);
300
- const fallback = (res) => res.success ? Promise.resolve(res) : executePrompt();
301
- const cachedResult = r instanceof Promise ? r.then(fallback) : fallback(r);
351
+ const r = withAnnotatedInnerState(state, parser.initialState, (annotatedInnerState) => parser.complete(annotatedInnerState));
352
+ const cachedResult = r instanceof Promise ? r.then((res) => usePromptOrDefer(state, res)) : usePromptOrDefer(state, r);
302
353
  promptCache = {
303
354
  state,
304
355
  result: cachedResult
@@ -306,15 +357,17 @@ function prompt(parser, config) {
306
357
  return cachedResult;
307
358
  }
308
359
  const cliState = isPromptBindState(state) ? state.cliState : void 0;
309
- const cliStateHasAnnotations = cliState != null && typeof cliState === "object" && __optique_core_annotations.annotationKey in cliState;
310
- if (cliStateHasAnnotations) {
311
- const innerState = cliState;
312
- const r = parser.complete(innerState);
313
- const fallback = (res) => res.success ? Promise.resolve(res) : executePrompt();
314
- if (r instanceof Promise) return r.then(fallback);
315
- return fallback(r);
360
+ const cliStateIsInjectedAnnotationWrapper = cliState != null && typeof cliState === "object" && (0, __optique_core_annotations.unwrapInjectedAnnotationWrapper)(cliState) !== cliState;
361
+ if (shouldAttemptInnerCompletion(cliState, state)) {
362
+ const useCompleteResultOrPrompt = (result) => {
363
+ if (result.success && result.value === void 0 && cliStateIsInjectedAnnotationWrapper) return executePrompt();
364
+ return usePromptOrDefer(state, result);
365
+ };
366
+ const r = withAnnotatedInnerState(state, cliState, (annotatedInnerState) => parser.complete(annotatedInnerState));
367
+ if (r instanceof Promise) return r.then(useCompleteResultOrPrompt);
368
+ return useCompleteResultOrPrompt(r);
316
369
  }
317
- return executePrompt();
370
+ return shouldDeferPrompt(parser, state) ? Promise.resolve(deferredPromptResult()) : executePrompt();
318
371
  },
319
372
  suggest: (context, prefix) => {
320
373
  const innerState = isPromptBindState(context.state) ? context.state.hasCliValue ? context.state.cliState : parser.initialState : context.state;
@@ -333,6 +386,7 @@ function prompt(parser, config) {
333
386
  return parser.getDocFragments(state, defaultValue);
334
387
  }
335
388
  };
389
+ return promptedParser;
336
390
  }
337
391
  /** Normalize choices to the format Inquirer.js expects. */
338
392
  function normalizeChoices(choices) {
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { Separator, checkbox, confirm, editor, expand, input, number, password, rawlist, select } from "@inquirer/prompts";
2
- import { annotationKey, getAnnotations } from "@optique/core/annotations";
2
+ import { annotationKey, getAnnotations, inheritAnnotations, unwrapInjectedAnnotationWrapper } from "@optique/core/annotations";
3
3
  import { message } from "@optique/core/message";
4
4
 
5
5
  //#region src/index.ts
@@ -46,6 +46,42 @@ function getPromptFunctions() {
46
46
  function isExitPromptError(error) {
47
47
  return typeof error === "object" && error != null && "name" in error && error.name === "ExitPromptError";
48
48
  }
49
+ const deferredPromptValueKey = Symbol.for("@optique/inquirer/deferredPromptValue");
50
+ const deferPromptUntilConfigResolvesKey = Symbol.for("@optique/config/deferPromptUntilResolved");
51
+ const inheritParentAnnotationsKey = Symbol.for("@optique/core/inheritParentAnnotations");
52
+ var DeferredPromptValue = class {
53
+ [deferredPromptValueKey] = true;
54
+ };
55
+ function shouldDeferPrompt(parser, state) {
56
+ const maybeShouldDefer = Reflect.get(parser, deferPromptUntilConfigResolvesKey);
57
+ return typeof maybeShouldDefer === "function" && maybeShouldDefer(state) === true;
58
+ }
59
+ function deferredPromptResult() {
60
+ return {
61
+ success: true,
62
+ value: new DeferredPromptValue()
63
+ };
64
+ }
65
+ function withAnnotationView(state, annotations, run) {
66
+ const annotatedState = new Proxy(state, {
67
+ get(target, key) {
68
+ if (key === annotationKey) return annotations;
69
+ const value = Reflect.get(target, key, target);
70
+ return typeof value === "function" ? value.bind(target) : value;
71
+ },
72
+ has(target, key) {
73
+ return key === annotationKey || Reflect.has(target, key);
74
+ }
75
+ });
76
+ return run(annotatedState);
77
+ }
78
+ function withAnnotatedInnerState(sourceState, innerState, run) {
79
+ const annotations = getAnnotations(sourceState);
80
+ if (annotations == null || innerState == null || typeof innerState !== "object" || typeof innerState === "object" && annotationKey in innerState) return run(innerState);
81
+ const inheritedState = inheritAnnotations(sourceState, innerState);
82
+ if (inheritedState !== innerState) return run(inheritedState);
83
+ return withAnnotationView(innerState, annotations, (annotatedState) => run(annotatedState));
84
+ }
49
85
  /**
50
86
  * Wraps a parser with an interactive Inquirer.js prompt fallback.
51
87
  *
@@ -87,6 +123,16 @@ function prompt(parser, config) {
87
123
  hasCliValue = false;
88
124
  };
89
125
  let promptCache = null;
126
+ function shouldAttemptInnerCompletion(cliState, state) {
127
+ if (cliState == null || cliState instanceof PromptBindInitialStateClass) return false;
128
+ const cliStateHasAnnotations = typeof cliState === "object" && annotationKey in cliState;
129
+ if (cliStateHasAnnotations) return true;
130
+ if (getAnnotations(state) == null || typeof cliState !== "object") return false;
131
+ if ("hasCliValue" in cliState) return true;
132
+ if (Array.isArray(cliState)) return false;
133
+ const prototype = Object.getPrototypeOf(cliState);
134
+ return prototype !== Object.prototype && prototype !== null;
135
+ }
90
136
  /**
91
137
  * Executes the configured prompt and normalizes its result.
92
138
  *
@@ -200,11 +246,16 @@ function prompt(parser, config) {
200
246
  throw error;
201
247
  }
202
248
  }
203
- return {
249
+ function usePromptOrDefer(state, result) {
250
+ if (result.success) return Promise.resolve(result);
251
+ return shouldDeferPrompt(parser, state) ? Promise.resolve(deferredPromptResult()) : executePrompt();
252
+ }
253
+ const promptedParser = {
204
254
  $mode: "async",
205
255
  $valueType: parser.$valueType,
206
256
  $stateType: parser.$stateType,
207
257
  priority: parser.priority,
258
+ [inheritParentAnnotationsKey]: true,
208
259
  usage: [{
209
260
  type: "optional",
210
261
  terms: parser.usage
@@ -215,13 +266,9 @@ function prompt(parser, config) {
215
266
  parse: (context) => {
216
267
  const annotations = getAnnotations(context.state);
217
268
  const innerState = isPromptBindState(context.state) ? context.state.hasCliValue ? context.state.cliState : parser.initialState : context.state;
218
- const innerStateWithAnnotations = annotations != null && (innerState == null || typeof innerState === "object" && !(annotationKey in innerState)) ? {
219
- ...innerState != null && typeof innerState === "object" ? innerState : {},
220
- [annotationKey]: annotations
221
- } : innerState;
222
- const innerContext = innerStateWithAnnotations !== context.state ? {
269
+ const baseInnerContext = innerState !== context.state ? {
223
270
  ...context,
224
- state: innerStateWithAnnotations
271
+ state: innerState
225
272
  } : context;
226
273
  const processResult = (result$1) => {
227
274
  if (result$1.success) {
@@ -250,19 +297,25 @@ function prompt(parser, config) {
250
297
  return {
251
298
  success: true,
252
299
  next: {
253
- ...innerContext,
300
+ ...baseInnerContext,
254
301
  state: nextState
255
302
  },
256
303
  consumed: []
257
304
  };
258
305
  };
259
- const result = parser.parse(innerContext);
306
+ const result = withAnnotatedInnerState(context.state, innerState, (annotatedInnerState) => {
307
+ const innerContext = annotatedInnerState !== context.state ? {
308
+ ...context,
309
+ state: annotatedInnerState
310
+ } : context;
311
+ return parser.parse(innerContext);
312
+ });
260
313
  if (result instanceof Promise) return result.then(processResult);
261
314
  return Promise.resolve(processResult(result));
262
315
  },
263
316
  complete: (state) => {
264
317
  if (isPromptBindState(state) && state.hasCliValue) {
265
- const r = parser.complete(state.cliState);
318
+ const r = withAnnotatedInnerState(state, state.cliState, (annotatedInnerState) => parser.complete(annotatedInnerState));
266
319
  if (r instanceof Promise) return r;
267
320
  return Promise.resolve(r);
268
321
  }
@@ -272,10 +325,8 @@ function prompt(parser, config) {
272
325
  promptCache = null;
273
326
  return cached;
274
327
  }
275
- const innerState = parser.initialState;
276
- const r = parser.complete(innerState);
277
- const fallback = (res) => res.success ? Promise.resolve(res) : executePrompt();
278
- const cachedResult = r instanceof Promise ? r.then(fallback) : fallback(r);
328
+ const r = withAnnotatedInnerState(state, parser.initialState, (annotatedInnerState) => parser.complete(annotatedInnerState));
329
+ const cachedResult = r instanceof Promise ? r.then((res) => usePromptOrDefer(state, res)) : usePromptOrDefer(state, r);
279
330
  promptCache = {
280
331
  state,
281
332
  result: cachedResult
@@ -283,15 +334,17 @@ function prompt(parser, config) {
283
334
  return cachedResult;
284
335
  }
285
336
  const cliState = isPromptBindState(state) ? state.cliState : void 0;
286
- const cliStateHasAnnotations = cliState != null && typeof cliState === "object" && annotationKey in cliState;
287
- if (cliStateHasAnnotations) {
288
- const innerState = cliState;
289
- const r = parser.complete(innerState);
290
- const fallback = (res) => res.success ? Promise.resolve(res) : executePrompt();
291
- if (r instanceof Promise) return r.then(fallback);
292
- return fallback(r);
337
+ const cliStateIsInjectedAnnotationWrapper = cliState != null && typeof cliState === "object" && unwrapInjectedAnnotationWrapper(cliState) !== cliState;
338
+ if (shouldAttemptInnerCompletion(cliState, state)) {
339
+ const useCompleteResultOrPrompt = (result) => {
340
+ if (result.success && result.value === void 0 && cliStateIsInjectedAnnotationWrapper) return executePrompt();
341
+ return usePromptOrDefer(state, result);
342
+ };
343
+ const r = withAnnotatedInnerState(state, cliState, (annotatedInnerState) => parser.complete(annotatedInnerState));
344
+ if (r instanceof Promise) return r.then(useCompleteResultOrPrompt);
345
+ return useCompleteResultOrPrompt(r);
293
346
  }
294
- return executePrompt();
347
+ return shouldDeferPrompt(parser, state) ? Promise.resolve(deferredPromptResult()) : executePrompt();
295
348
  },
296
349
  suggest: (context, prefix) => {
297
350
  const innerState = isPromptBindState(context.state) ? context.state.hasCliValue ? context.state.cliState : parser.initialState : context.state;
@@ -310,6 +363,7 @@ function prompt(parser, config) {
310
363
  return parser.getDocFragments(state, defaultValue);
311
364
  }
312
365
  };
366
+ return promptedParser;
313
367
  }
314
368
  /** Normalize choices to the format Inquirer.js expects. */
315
369
  function normalizeChoices(choices) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/inquirer",
3
- "version": "1.0.0-dev.664+4cd3da03",
3
+ "version": "1.0.0-dev.670+a142c6c9",
4
4
  "description": "Interactive prompt support for Optique via Inquirer.js",
5
5
  "keywords": [
6
6
  "CLI",
@@ -56,13 +56,13 @@
56
56
  "sideEffects": false,
57
57
  "dependencies": {
58
58
  "@inquirer/prompts": "^8.3.0",
59
- "@optique/core": "1.0.0-dev.664+4cd3da03"
59
+ "@optique/core": "1.0.0-dev.670+a142c6c9"
60
60
  },
61
61
  "devDependencies": {
62
62
  "@types/node": "^20.19.9",
63
63
  "tsdown": "^0.13.0",
64
64
  "typescript": "^5.8.3",
65
- "@optique/env": "1.0.0-dev.664+4cd3da03"
65
+ "@optique/env": "1.0.0-dev.670+a142c6c9"
66
66
  },
67
67
  "scripts": {
68
68
  "build": "tsdown",