@jacebenson/jsn 5.5.7 → 5.5.8-dev.220.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/README.md +16 -1
- package/package.json +5 -6
- package/skills/servicenow/SKILL.md +9 -1
- package/src/app.js +4 -79
- package/src/auth/credential-store.js +33 -8
- package/src/auth/windows-credential-store.js +85 -0
- package/src/auth/windows-credential-store.ps1 +163 -0
- package/src/bounded-reads.js +13 -0
- package/src/capabilities.js +8 -3
- package/src/cli-adapter.js +26 -3
- package/src/cli.js +13 -14
- package/src/command-families.js +68 -0
- package/src/commands/_generic.js +1 -0
- package/src/commands/_ticket.js +1 -1
- package/src/commands/actions.js +9 -4
- package/src/commands/approvals.js +1 -1
- package/src/commands/atf.js +1 -1
- package/src/commands/attachments.js +1 -1
- package/src/commands/auth.js +21 -2
- package/src/commands/codesearch.js +11 -2
- package/src/commands/compare.js +22 -0
- package/src/commands/customizations.js +3 -4
- package/src/commands/decisiontables.js +4 -0
- package/src/commands/docs/docs.js +5 -5
- package/src/commands/domains.js +1 -1
- package/src/commands/eval.js +11 -5
- package/src/commands/flows.js +24 -7
- package/src/commands/groupmembers.js +1 -1
- package/src/commands/grouproles.js +1 -1
- package/src/commands/groups.js +1 -1
- package/src/commands/midservers.js +4 -11
- package/src/commands/perf.js +2 -3
- package/src/commands/plugins.js +9 -0
- package/src/commands/records.js +12 -2
- package/src/commands/scopes.js +1 -1
- package/src/commands/scrapi.js +2 -2
- package/src/commands/setup.js +4 -34
- package/src/commands/skipped-updates.js +2 -3
- package/src/commands/store.js +4 -1
- package/src/commands/subscriptions.js +47 -0
- package/src/commands/tickets.js +1 -1
- package/src/commands/users.js +1 -1
- package/src/commands/workflows.js +3 -5
- package/src/config.js +1 -0
- package/src/context.js +8 -56
- package/src/docs-links.js +28 -0
- package/src/flow-create.js +9 -0
- package/src/help.js +4 -0
- package/src/helpers.js +17 -24
- package/src/instance-inventory.js +184 -0
- package/src/intended-context.js +68 -0
- package/src/mutation-context.js +69 -46
- package/src/output.js +1 -1
- package/src/paginated-search.js +146 -178
- package/src/picker-ui.js +20 -0
- package/src/prompt-engine.js +176 -0
- package/src/prompts.js +293 -43
- package/src/records/inspect.js +30 -16
- package/src/records-engine.js +2 -2
- package/src/schedule-cache.js +96 -0
- package/src/schedules/itom-2026-04-09.json +121 -0
- package/src/schedules/public-inventory.json +6224 -0
- package/src/sdk-batch.js +2 -1
- package/src/sdk.js +42 -16
- package/src/setup-preferences.js +2 -2
- package/src/subscriptions.js +176 -0
package/src/paginated-search.js
CHANGED
|
@@ -1,195 +1,163 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Paginated
|
|
2
|
+
* Paginated, searchable record picker built on src/prompt-engine.js.
|
|
3
|
+
*
|
|
4
|
+
* Contract (unchanged from the Inquirer version):
|
|
5
|
+
* paginatedSearch({ message, pageSize, totalCount, source }, context?)
|
|
6
|
+
* source(term, offset, { signal }) -> [{ name, value }]
|
|
7
|
+
* Enter resolves the active choice object; Escape resolves undefined.
|
|
8
|
+
* Down at the last loaded row loads more while loaded < totalCount;
|
|
9
|
+
* a null totalCount is unknown and continues until a successful empty page.
|
|
3
10
|
*/
|
|
4
11
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
53
|
-
};
|
|
54
|
-
load();
|
|
55
|
-
return () => controller.abort();
|
|
56
|
-
}, []);
|
|
57
|
-
|
|
58
|
-
// Load more when scrolling past end
|
|
59
|
-
async function loadMore() {
|
|
60
|
-
if (loadedRef.current >= totalCountRef.current || searchModeRef.current) return;
|
|
61
|
-
setStatus('loading');
|
|
62
|
-
try {
|
|
63
|
-
const newItems = await source('', loadedRef.current, { signal: new AbortController().signal });
|
|
64
|
-
const updated = choicesRef.current.concat(newItems);
|
|
65
|
-
choicesRef.current = updated;
|
|
66
|
-
loadedRef.current = updated.length;
|
|
67
|
-
setChoices([...updated]);
|
|
68
|
-
setLoaded(updated.length);
|
|
69
|
-
} catch (err) {
|
|
70
|
-
console.error('Scroll load error:', err.message || err);
|
|
71
|
-
loadedRef.current = totalCountRef.current;
|
|
72
|
-
setLoaded(totalCountRef.current);
|
|
12
|
+
import { pickerRows, pickerFooter } from './picker-ui.js';
|
|
13
|
+
import { runPrompt, normalizeChoices, escapeResult } from './prompt-engine.js';
|
|
14
|
+
|
|
15
|
+
export const paginatedSearch = async (config = {}, context = {}) => {
|
|
16
|
+
const { message, pageSize = 10, totalCount = 0, source } = config;
|
|
17
|
+
let request;
|
|
18
|
+
const state = {
|
|
19
|
+
status: 'loading',
|
|
20
|
+
term: '',
|
|
21
|
+
choices: [],
|
|
22
|
+
active: 0,
|
|
23
|
+
loaded: 0,
|
|
24
|
+
searchMode: false,
|
|
25
|
+
token: 0,
|
|
26
|
+
error: '',
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const canLoadMore = () => !state.searchMode && !state.exhausted
|
|
30
|
+
&& (totalCount == null || state.loaded < totalCount);
|
|
31
|
+
|
|
32
|
+
// Every request gets a token. A response may only be applied while its
|
|
33
|
+
// token is still current, so a stale (slower) search can never replace
|
|
34
|
+
// newer results or be selected by a later Enter.
|
|
35
|
+
const isCurrent = token => token === state.token && state.status !== 'cancelled';
|
|
36
|
+
|
|
37
|
+
async function load(term, offset, { append = false } = {}) {
|
|
38
|
+
request?.abort();
|
|
39
|
+
request = new AbortController();
|
|
40
|
+
const signal = context.signal ? AbortSignal.any([request.signal, context.signal]) : request.signal;
|
|
41
|
+
const token = ++state.token;
|
|
42
|
+
state.status = 'loading';
|
|
43
|
+
state.error = '';
|
|
44
|
+
try {
|
|
45
|
+
const items = normalizeChoices(await source(term || undefined, offset, { signal }) || []);
|
|
46
|
+
if (!isCurrent(token)) return;
|
|
47
|
+
state.choices = append ? state.choices.concat(items) : items;
|
|
48
|
+
state.loaded = state.choices.length;
|
|
49
|
+
state.exhausted = items.length === 0;
|
|
50
|
+
state.active = Math.min(state.active, Math.max(0, state.choices.length - 1));
|
|
51
|
+
state.status = 'done';
|
|
52
|
+
} catch (error) {
|
|
53
|
+
if (!isCurrent(token)) return;
|
|
54
|
+
state.status = 'done';
|
|
55
|
+
state.error = error?.message || String(error);
|
|
56
|
+
if (!append) {
|
|
57
|
+
state.choices = [];
|
|
58
|
+
state.loaded = 0;
|
|
73
59
|
}
|
|
74
|
-
|
|
60
|
+
// A failed load-more keeps the rows already on screen.
|
|
75
61
|
}
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
setActive(0);
|
|
92
|
-
setStatus('done');
|
|
93
|
-
}
|
|
94
|
-
}).catch(() => {
|
|
95
|
-
if (!controller.signal.aborted) setStatus('done');
|
|
96
|
-
});
|
|
97
|
-
return () => controller.abort();
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const controller = new AbortController();
|
|
101
|
-
setStatus('loading');
|
|
102
|
-
setSearchMode(true);
|
|
103
|
-
searchModeRef.current = true;
|
|
104
|
-
|
|
105
|
-
source(searchTerm, undefined, { signal: controller.signal }).then(items => {
|
|
106
|
-
if (!controller.signal.aborted) {
|
|
107
|
-
choicesRef.current = items;
|
|
108
|
-
loadedRef.current = items.length;
|
|
109
|
-
activeRef.current = 0;
|
|
110
|
-
setChoices(items);
|
|
111
|
-
setLoaded(items.length);
|
|
112
|
-
setActive(0);
|
|
113
|
-
setStatus('done');
|
|
114
|
-
}
|
|
115
|
-
}).catch(() => {
|
|
116
|
-
if (!controller.signal.aborted) {
|
|
117
|
-
setStatus('done');
|
|
118
|
-
setChoices([]);
|
|
119
|
-
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let api;
|
|
65
|
+
const prompt = runPrompt({ ...config, message }, context, {
|
|
66
|
+
escapeValue: escapeResult(undefined),
|
|
67
|
+
init() {
|
|
68
|
+
// Kick off the first page. Enter is ignored while status is 'loading',
|
|
69
|
+
// so nothing is selectable until this resolves.
|
|
70
|
+
state.pending = load('', 0);
|
|
71
|
+
return state;
|
|
72
|
+
},
|
|
73
|
+
onReady(handle) {
|
|
74
|
+
api = handle;
|
|
75
|
+
state.pending.then(() => {
|
|
76
|
+
if (state.status !== 'cancelled') api.redraw();
|
|
120
77
|
});
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
done(choicesRef.current[activeRef.current]);
|
|
132
|
-
}
|
|
133
|
-
return;
|
|
78
|
+
},
|
|
79
|
+
key(_state, key, handle) {
|
|
80
|
+
api = handle;
|
|
81
|
+
if (key.name === 'return' || key.name === 'enter') {
|
|
82
|
+
// No selecting a row while a request is in flight: the list on
|
|
83
|
+
// screen may be about to change.
|
|
84
|
+
if (state.status === 'loading' || !state.choices.length) return;
|
|
85
|
+
const choice = state.choices[state.active];
|
|
86
|
+
if (!choice || choice.disabled) return;
|
|
87
|
+
return { done: choice };
|
|
134
88
|
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
89
|
+
if (key.name === 'up') {
|
|
90
|
+
if (state.active > 0) state.active -= 1;
|
|
91
|
+
api?.redraw();
|
|
138
92
|
return;
|
|
139
93
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
if (
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
} else if (!searchModeRef.current && loadedRef.current < totalCountRef.current) {
|
|
154
|
-
loadMore().then(() => {
|
|
155
|
-
activeRef.current++;
|
|
156
|
-
setActive(activeRef.current);
|
|
94
|
+
if (key.name === 'down') {
|
|
95
|
+
if (state.status === 'loading') return;
|
|
96
|
+
if (state.active < state.choices.length - 1) {
|
|
97
|
+
state.active += 1;
|
|
98
|
+
api?.redraw();
|
|
99
|
+
} else if (canLoadMore()) {
|
|
100
|
+
const before = state.loaded;
|
|
101
|
+
const pending = load('', state.loaded, { append: true });
|
|
102
|
+
const token = state.token;
|
|
103
|
+
void pending.then(() => {
|
|
104
|
+
if (!isCurrent(token)) return;
|
|
105
|
+
if (state.status === 'done' && state.loaded > before) state.active = before;
|
|
106
|
+
api?.redraw();
|
|
157
107
|
});
|
|
108
|
+
api?.redraw();
|
|
158
109
|
}
|
|
159
110
|
return;
|
|
160
111
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
if (
|
|
165
|
-
|
|
112
|
+
const previous = state.term;
|
|
113
|
+
if (key.name === 'backspace') state.term = state.term.slice(0, -1);
|
|
114
|
+
else if (key.name === 'space') state.term += ' ';
|
|
115
|
+
else if (key.text) state.term += key.text;
|
|
116
|
+
else return;
|
|
117
|
+
if (state.term === previous) return;
|
|
118
|
+
|
|
119
|
+
if (!state.term) {
|
|
120
|
+
state.searchMode = false;
|
|
121
|
+
state.active = 0;
|
|
122
|
+
void load('', 0).then(() => api?.redraw());
|
|
123
|
+
api?.redraw();
|
|
124
|
+
return;
|
|
166
125
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
pageSize
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
126
|
+
state.searchMode = true;
|
|
127
|
+
state.active = 0;
|
|
128
|
+
void load(state.term, undefined).then(() => api?.redraw());
|
|
129
|
+
api?.redraw();
|
|
130
|
+
},
|
|
131
|
+
render() {
|
|
132
|
+
const pages = Math.max(1, Math.ceil(state.choices.length / pageSize));
|
|
133
|
+
const current = Math.min(pages - 1, Math.floor(state.active / pageSize));
|
|
134
|
+
const start = current * pageSize;
|
|
135
|
+
const visible = state.choices.slice(start, start + pageSize);
|
|
136
|
+
const header = totalCount > 0 && !state.searchMode
|
|
137
|
+
? `${message} (${state.loaded} of ${totalCount} loaded)`
|
|
138
|
+
: message;
|
|
139
|
+
const lines = [header ?? '', `Search: ${state.term}${state.status === 'loading' ? ' ...' : ''}`];
|
|
140
|
+
if (state.error) lines.push(` ${state.error}`);
|
|
141
|
+
else if (!state.choices.length) lines.push(state.status === 'loading' ? ' Loading...' : ' No matches');
|
|
142
|
+
lines.push(...pickerRows(visible, state.active - start));
|
|
143
|
+
lines.push(...pickerFooter({ more: canLoadMore() }));
|
|
144
|
+
return lines;
|
|
145
|
+
},
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
return prompt.then(value => {
|
|
149
|
+
// Mark the prompt so an in-flight source response cannot redraw or
|
|
150
|
+
// apply after the picker closed.
|
|
151
|
+
state.status = 'cancelled';
|
|
152
|
+
state.token += 1;
|
|
153
|
+
request?.abort();
|
|
154
|
+
return value;
|
|
155
|
+
}, error => {
|
|
156
|
+
state.status = 'cancelled';
|
|
157
|
+
state.token += 1;
|
|
158
|
+
request?.abort();
|
|
159
|
+
throw error;
|
|
160
|
+
}).then(value => value);
|
|
193
161
|
};
|
|
194
162
|
|
|
195
163
|
export default paginatedSearch;
|
package/src/picker-ui.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const PICKER_HELP = '↑↓ navigate • ⏎ select • type to search • Esc cancel';
|
|
2
|
+
|
|
3
|
+
export function pickerRows(choices, active, { picked } = {}) {
|
|
4
|
+
const lines = [];
|
|
5
|
+
choices.forEach((choice, index) => {
|
|
6
|
+
if (choice.section && choice.section !== choices[index - 1]?.section) {
|
|
7
|
+
if (index) lines.push('');
|
|
8
|
+
lines.push(` ${choice.section}`);
|
|
9
|
+
}
|
|
10
|
+
if (choice.separator) { lines.push(` ${choice.name}`); return; }
|
|
11
|
+
const check = picked ? `[${picked.has(choice) ? 'x' : ' '}] ` : '';
|
|
12
|
+
lines.push(`${index === active ? '❯' : ' '} ${check}${choice.name}${choice.disabled ? ' (disabled)' : ''}`);
|
|
13
|
+
if (index === active && choice.description) lines.push(` ${choice.description}`);
|
|
14
|
+
});
|
|
15
|
+
return lines;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function pickerFooter({ checkbox = false, more = false } = {}) {
|
|
19
|
+
return ['', `${PICKER_HELP}${checkbox ? ' • Space toggle' : ''}${more ? ' • ↓ load more' : ''}`];
|
|
20
|
+
}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small terminal prompt engine built on node:readline keypress events.
|
|
3
|
+
*
|
|
4
|
+
* This is deliberately not a general prompt library: it exists so JSN's
|
|
5
|
+
* observed prompt surface (input, password, confirm, select, search,
|
|
6
|
+
* checkbox, editor, paginated pickers) runs on Node built-ins with no
|
|
7
|
+
* runtime dependency, while keeping the existing caller contract
|
|
8
|
+
* `prompt(config, { input, output, signal })`.
|
|
9
|
+
*
|
|
10
|
+
* Every prompt restores raw mode and removes its listener in `finally`,
|
|
11
|
+
* on success, Escape, Ctrl+C and thrown errors alike.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import readline from 'node:readline';
|
|
15
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
16
|
+
|
|
17
|
+
export class PromptCancelled extends Error {
|
|
18
|
+
constructor() {
|
|
19
|
+
super('Cancelled.');
|
|
20
|
+
this.name = 'PromptCancelled';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Choices may be strings or { name, value, disabled }. */
|
|
25
|
+
export function normalizeChoices(choices = []) {
|
|
26
|
+
return choices.map(choice => {
|
|
27
|
+
if (choice && typeof choice === 'object') {
|
|
28
|
+
return {
|
|
29
|
+
name: String(choice.name ?? choice.value ?? ''),
|
|
30
|
+
value: 'value' in choice ? choice.value : choice.name,
|
|
31
|
+
disabled: Boolean(choice.disabled) || choice.type === 'separator',
|
|
32
|
+
...(choice.type === 'separator' ? { separator: true } : {}),
|
|
33
|
+
...(choice.section !== undefined ? { section: choice.section } : {}),
|
|
34
|
+
...(choice.description !== undefined ? { description: choice.description } : {}),
|
|
35
|
+
checked: Boolean(choice.checked),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return { name: String(choice), value: choice, disabled: false };
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function truncate(text, width = 70) {
|
|
43
|
+
const value = String(text ?? '');
|
|
44
|
+
return value.length > width ? `${value.slice(0, width - 3)}...` : value;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Run one interactive prompt.
|
|
49
|
+
*
|
|
50
|
+
* @param {object} config prompt config ({ message, ... })
|
|
51
|
+
* @param {object} context { input, output, signal }
|
|
52
|
+
* @param {object} spec
|
|
53
|
+
* init(config, context) -> state
|
|
54
|
+
* key(state, key, api) -> directive | undefined | Promise<...>
|
|
55
|
+
* directive: { done: value } finishes, { cancel: true } throws
|
|
56
|
+
* PromptCancelled, { escape: true } resolves undefined.
|
|
57
|
+
* render(config, state) -> string | string[]
|
|
58
|
+
* final(config, value, state) -> string | null — the settled answer line
|
|
59
|
+
* (Inquirer prints `✔ <message> <answer>`). Only written on a TTY after
|
|
60
|
+
* the live frame is erased, so one prompt yields exactly one final line
|
|
61
|
+
* and piped output keeps its existing byte contract.
|
|
62
|
+
* escapeValue — when the caller treats Escape as "leave this screen",
|
|
63
|
+
* pass a sentinel produced by `escapeResult(value)`.
|
|
64
|
+
*/
|
|
65
|
+
export async function runPrompt(config, context, spec) {
|
|
66
|
+
const input = context.input || process.stdin;
|
|
67
|
+
const output = context.output || process.stdout;
|
|
68
|
+
const canRaw = typeof input.setRawMode === 'function';
|
|
69
|
+
const previousRaw = canRaw ? Boolean(input.isRaw) : false;
|
|
70
|
+
|
|
71
|
+
let state;
|
|
72
|
+
let height = 0;
|
|
73
|
+
let settled = false;
|
|
74
|
+
let resolveDone;
|
|
75
|
+
let rejectDone;
|
|
76
|
+
|
|
77
|
+
const done = new Promise((resolve, reject) => { resolveDone = resolve; rejectDone = reject; });
|
|
78
|
+
|
|
79
|
+
const draw = () => {
|
|
80
|
+
if (settled) return;
|
|
81
|
+
const rendered = spec.render(state);
|
|
82
|
+
const text = Array.isArray(rendered) ? rendered.join('\n') : String(rendered ?? '');
|
|
83
|
+
if (output.isTTY && height > 0) output.write(`\x1b[${height}A\x1b[0J`);
|
|
84
|
+
output.write(`${text}\n`);
|
|
85
|
+
// Count wrapped terminal rows as well as explicit newlines: the compact
|
|
86
|
+
// footer can still wrap in a narrow terminal.
|
|
87
|
+
const columns = output.isTTY && output.columns > 0 ? output.columns : Infinity;
|
|
88
|
+
height = text.split('\n').reduce((rows, line) => rows + Math.max(1, Math.ceil(Array.from(stripVTControlCharacters(line)).length / columns)), 0);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const finish = value => {
|
|
92
|
+
if (settled) return;
|
|
93
|
+
settled = true;
|
|
94
|
+
// The settled answer replaces the live frame rather than appending to it:
|
|
95
|
+
// rewind the frame height first so a terminal reader sees one final line.
|
|
96
|
+
if (spec.final && output.isTTY) {
|
|
97
|
+
const line = spec.final(config, value, state);
|
|
98
|
+
if (line) {
|
|
99
|
+
if (height > 0) output.write(`\x1b[${height}A\x1b[0J`);
|
|
100
|
+
output.write(`${line}\n`);
|
|
101
|
+
height = 0;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
resolveDone(value);
|
|
105
|
+
};
|
|
106
|
+
const fail = error => {
|
|
107
|
+
if (settled) return;
|
|
108
|
+
settled = true;
|
|
109
|
+
rejectDone(error);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const api = { finish, fail, redraw: draw };
|
|
113
|
+
let queue = Promise.resolve();
|
|
114
|
+
const onKeypress = (_chunk, key) => {
|
|
115
|
+
if (!key || settled) return;
|
|
116
|
+
// Cancellation cannot wait behind validation or a source request.
|
|
117
|
+
if (key.name === 'escape') {
|
|
118
|
+
if (spec.escapeValue !== undefined) finish(spec.escapeValue.value);
|
|
119
|
+
else fail(new PromptCancelled());
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
if (key.ctrl && (key.name === 'c' || key.name === 'd')) {
|
|
123
|
+
fail(new PromptCancelled());
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
key = { ...key, text: !key.ctrl && !key.meta && typeof _chunk === 'string' && Array.from(_chunk).every(char => char.codePointAt(0) >= 32 && char.codePointAt(0) !== 127) ? _chunk : '' };
|
|
127
|
+
// Serialize handling: a chunk can carry several keys at once, and async
|
|
128
|
+
// handlers (search/load-more) must not reorder Enter ahead of arrows.
|
|
129
|
+
queue = queue.then(async () => {
|
|
130
|
+
if (settled) return;
|
|
131
|
+
|
|
132
|
+
const directive = await spec.key(state, key, api);
|
|
133
|
+
if (!directive || settled) return;
|
|
134
|
+
if ('done' in directive) finish(directive.done);
|
|
135
|
+
else if (directive.cancel) fail(new PromptCancelled());
|
|
136
|
+
else if (directive.escape) finish(undefined);
|
|
137
|
+
}).catch(error => fail(error));
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const onAbort = () => fail(context.signal.reason || new PromptCancelled());
|
|
141
|
+
const onEnd = () => fail(new PromptCancelled());
|
|
142
|
+
|
|
143
|
+
try {
|
|
144
|
+
readline.emitKeypressEvents(input);
|
|
145
|
+
if (canRaw) input.setRawMode(true);
|
|
146
|
+
input.on('keypress', onKeypress);
|
|
147
|
+
input.on('end', onEnd);
|
|
148
|
+
input.on('close', onEnd);
|
|
149
|
+
input.on('error', fail);
|
|
150
|
+
if (context.signal) {
|
|
151
|
+
if (context.signal.aborted) onAbort();
|
|
152
|
+
else context.signal.addEventListener('abort', onAbort, { once: true });
|
|
153
|
+
}
|
|
154
|
+
state = spec.init(config, context);
|
|
155
|
+
if (input.readableEnded || input.destroyed) onEnd();
|
|
156
|
+
if (typeof input.resume === 'function') input.resume();
|
|
157
|
+
draw();
|
|
158
|
+
spec.onReady?.(api);
|
|
159
|
+
return await done;
|
|
160
|
+
} finally {
|
|
161
|
+
input.removeListener('keypress', onKeypress);
|
|
162
|
+
input.removeListener('end', onEnd);
|
|
163
|
+
input.removeListener('close', onEnd);
|
|
164
|
+
input.removeListener('error', fail);
|
|
165
|
+
if (context.signal) context.signal.removeEventListener('abort', onAbort);
|
|
166
|
+
if (canRaw) {
|
|
167
|
+
try { input.setRawMode(previousRaw); } catch { /* stream already closed */ }
|
|
168
|
+
}
|
|
169
|
+
if (typeof input.pause === 'function' && input.isTTY) input.pause();
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** Escape resolves `value` instead of cancelling (record pickers). */
|
|
174
|
+
export function escapeResult(value) {
|
|
175
|
+
return { value };
|
|
176
|
+
}
|