@jacebenson/jsn 5.5.7-dev.214.1 → 5.5.7-dev.215.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/package.json +1 -1
- package/src/commands/midservers.js +1 -6
- package/src/commands/setup.js +3 -30
- package/src/helpers.js +17 -24
- package/src/paginated-search.js +13 -13
- package/src/picker-ui.js +20 -0
- package/src/prompt-engine.js +9 -2
- package/src/prompts.js +54 -66
package/package.json
CHANGED
|
@@ -302,12 +302,7 @@ async function promptMidCredentials(app, argv) {
|
|
|
302
302
|
const validation = await validateMidCredentials(app.getEffectiveInstance(), username, password);
|
|
303
303
|
return { username, password, releaseName: validation.releaseName };
|
|
304
304
|
} catch (error) {
|
|
305
|
-
|
|
306
|
-
if (answer.trim().toLowerCase() === 'q') {
|
|
307
|
-
const quit = new Error('MID Server installation cancelled');
|
|
308
|
-
quit.code = 'cancelled';
|
|
309
|
-
throw quit;
|
|
310
|
-
}
|
|
305
|
+
await input({ message: `${error.message}. Press Enter to retry or Esc to cancel:` });
|
|
311
306
|
username = argv.username || '';
|
|
312
307
|
}
|
|
313
308
|
}
|
package/src/commands/setup.js
CHANGED
|
@@ -41,37 +41,10 @@ const setupHelp = () => [
|
|
|
41
41
|
'Configure settings directly: jsn setup --configure-defaults or jsn setup --configure-docs.',
|
|
42
42
|
].join('\n');
|
|
43
43
|
|
|
44
|
-
/** The setup hub
|
|
44
|
+
/** The setup hub shares searchable local menus; Escape leaves the hub. */
|
|
45
45
|
export const authHubPrompt = async (config = {}, context = {}) => {
|
|
46
|
-
const {
|
|
47
|
-
|
|
48
|
-
const state = { active: 0 };
|
|
49
|
-
return runPrompt(config, context, {
|
|
50
|
-
escapeValue: escapeResult(undefined),
|
|
51
|
-
init: () => state,
|
|
52
|
-
key(_state, key, api) {
|
|
53
|
-
if (key.name === 'q') return { done: undefined };
|
|
54
|
-
if (key.name === 'return' || key.name === 'enter') {
|
|
55
|
-
const choice = choices[state.active];
|
|
56
|
-
if (choice) return { done: choice.value };
|
|
57
|
-
return;
|
|
58
|
-
}
|
|
59
|
-
if (key.name === 'up' && state.active > 0) { state.active -= 1; api.redraw(); return; }
|
|
60
|
-
if (key.name === 'down' && state.active < choices.length - 1) { state.active += 1; api.redraw(); }
|
|
61
|
-
},
|
|
62
|
-
render() {
|
|
63
|
-
// Section headings are rendered inline; the pagination budget the
|
|
64
|
-
// Inquirer version needed no longer applies to a local renderer.
|
|
65
|
-
const lines = [config.message];
|
|
66
|
-
choices.forEach((choice, index) => {
|
|
67
|
-
const heading = choices[index - 1]?.section !== choice.section ? `${index ? '\n' : ''} ${choice.section}` : null;
|
|
68
|
-
if (heading) lines.push(heading);
|
|
69
|
-
lines.push(`${index === state.active ? '❯' : ' '} ${choice.name}`);
|
|
70
|
-
});
|
|
71
|
-
lines.push('', '↑↓ navigate • ⏎ select • q/Esc exit');
|
|
72
|
-
return lines;
|
|
73
|
-
},
|
|
74
|
-
});
|
|
46
|
+
const { select, escapeResult } = await import('../prompts.js');
|
|
47
|
+
return select({ ...config, escapeValue: escapeResult(undefined) }, context);
|
|
75
48
|
};
|
|
76
49
|
|
|
77
50
|
/** Interactive hub: what do you want to do? Returns an action or undefined on exit. */
|
package/src/helpers.js
CHANGED
|
@@ -448,31 +448,24 @@ export async function interactiveList({ app, table, singular, columns, limit = 5
|
|
|
448
448
|
|
|
449
449
|
// Paginated source adapter: (term, offset, { signal }) => choices[]
|
|
450
450
|
async function serverSource(term, offset, { signal: _signal } = {}) {
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
params.set('sysparm_offset', String(offset));
|
|
461
|
-
}
|
|
462
|
-
const records = await app.sdk.list(table, params);
|
|
463
|
-
if (!Array.isArray(records)) {
|
|
464
|
-
console.error('SDK non-array:', typeof records);
|
|
465
|
-
return [];
|
|
466
|
-
}
|
|
467
|
-
return records.map(r => ({
|
|
468
|
-
name: formatLabel ? formatLabel(r) : (getStringField(r, labelField) || getStringField(r, 'sys_id')),
|
|
469
|
-
value: formatValue ? formatValue(r) : r,
|
|
470
|
-
}));
|
|
471
|
-
} catch (err) {
|
|
472
|
-
if (isTerminalAuthError(err)) throw err;
|
|
473
|
-
console.error('serverSource error:', err.message || err);
|
|
474
|
-
return [];
|
|
451
|
+
const params = new URLSearchParams();
|
|
452
|
+
params.set('sysparm_limit', String(limit));
|
|
453
|
+
params.set('sysparm_display_value', 'all');
|
|
454
|
+
if (pickerFields) params.set('sysparm_fields', pickerFields);
|
|
455
|
+
if (term) {
|
|
456
|
+
params.set('sysparm_query', `${baseQuery}${labelField}LIKE${term}^ORDERBYDESCsys_updated_on`);
|
|
457
|
+
} else {
|
|
458
|
+
params.set('sysparm_query', baseQuery + 'ORDERBYDESCsys_updated_on');
|
|
459
|
+
params.set('sysparm_offset', String(offset));
|
|
475
460
|
}
|
|
461
|
+
// Only a successful empty page means exhausted. Let the picker render
|
|
462
|
+
// failures and retry this offset; preserve terminal auth errors unchanged.
|
|
463
|
+
const records = await app.sdk.list(table, params);
|
|
464
|
+
if (!Array.isArray(records)) throw new Error(`Could not list ${table}: list returned a non-array result`);
|
|
465
|
+
return records.map(r => ({
|
|
466
|
+
name: formatLabel ? formatLabel(r) : (getStringField(r, labelField) || getStringField(r, 'sys_id')),
|
|
467
|
+
value: formatValue ? formatValue(r) : r,
|
|
468
|
+
}));
|
|
476
469
|
}
|
|
477
470
|
|
|
478
471
|
const selected = await prompt({
|
package/src/paginated-search.js
CHANGED
|
@@ -5,10 +5,12 @@
|
|
|
5
5
|
* paginatedSearch({ message, pageSize, totalCount, source }, context?)
|
|
6
6
|
* source(term, offset, { signal }) -> [{ name, value }]
|
|
7
7
|
* Enter resolves the active choice object; Escape resolves undefined.
|
|
8
|
-
* Down at the last loaded row loads more while loaded < totalCount
|
|
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.
|
|
9
10
|
*/
|
|
10
11
|
|
|
11
|
-
import {
|
|
12
|
+
import { pickerRows, pickerFooter } from './picker-ui.js';
|
|
13
|
+
import { runPrompt, normalizeChoices, escapeResult } from './prompt-engine.js';
|
|
12
14
|
|
|
13
15
|
export const paginatedSearch = async (config = {}, context = {}) => {
|
|
14
16
|
const { message, pageSize = 10, totalCount = 0, source } = config;
|
|
@@ -24,6 +26,9 @@ export const paginatedSearch = async (config = {}, context = {}) => {
|
|
|
24
26
|
error: '',
|
|
25
27
|
};
|
|
26
28
|
|
|
29
|
+
const canLoadMore = () => !state.searchMode && !state.exhausted
|
|
30
|
+
&& (totalCount == null || state.loaded < totalCount);
|
|
31
|
+
|
|
27
32
|
// Every request gets a token. A response may only be applied while its
|
|
28
33
|
// token is still current, so a stale (slower) search can never replace
|
|
29
34
|
// newer results or be selected by a later Enter.
|
|
@@ -41,6 +46,7 @@ export const paginatedSearch = async (config = {}, context = {}) => {
|
|
|
41
46
|
if (!isCurrent(token)) return;
|
|
42
47
|
state.choices = append ? state.choices.concat(items) : items;
|
|
43
48
|
state.loaded = state.choices.length;
|
|
49
|
+
state.exhausted = items.length === 0;
|
|
44
50
|
state.active = Math.min(state.active, Math.max(0, state.choices.length - 1));
|
|
45
51
|
state.status = 'done';
|
|
46
52
|
} catch (error) {
|
|
@@ -77,7 +83,7 @@ export const paginatedSearch = async (config = {}, context = {}) => {
|
|
|
77
83
|
// screen may be about to change.
|
|
78
84
|
if (state.status === 'loading' || !state.choices.length) return;
|
|
79
85
|
const choice = state.choices[state.active];
|
|
80
|
-
if (!choice) return;
|
|
86
|
+
if (!choice || choice.disabled) return;
|
|
81
87
|
return { done: choice };
|
|
82
88
|
}
|
|
83
89
|
if (key.name === 'up') {
|
|
@@ -90,7 +96,7 @@ export const paginatedSearch = async (config = {}, context = {}) => {
|
|
|
90
96
|
if (state.active < state.choices.length - 1) {
|
|
91
97
|
state.active += 1;
|
|
92
98
|
api?.redraw();
|
|
93
|
-
} else if (
|
|
99
|
+
} else if (canLoadMore()) {
|
|
94
100
|
const before = state.loaded;
|
|
95
101
|
const pending = load('', state.loaded, { append: true });
|
|
96
102
|
const token = state.token;
|
|
@@ -130,17 +136,11 @@ export const paginatedSearch = async (config = {}, context = {}) => {
|
|
|
130
136
|
const header = totalCount > 0 && !state.searchMode
|
|
131
137
|
? `${message} (${state.loaded} of ${totalCount} loaded)`
|
|
132
138
|
: message;
|
|
133
|
-
const lines = [
|
|
139
|
+
const lines = [header ?? '', `Search: ${state.term}${state.status === 'loading' ? ' ...' : ''}`];
|
|
134
140
|
if (state.error) lines.push(` ${state.error}`);
|
|
135
141
|
else if (!state.choices.length) lines.push(state.status === 'loading' ? ' Loading...' : ' No matches');
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
lines.push(`${absolute === state.active ? '>' : ' '} ${truncate(choice.name)}`);
|
|
139
|
-
});
|
|
140
|
-
const help = state.loaded < totalCount && !state.searchMode
|
|
141
|
-
? '(scroll to load more, type to search, Esc to cancel)'
|
|
142
|
-
: '(type to search, Esc to cancel)';
|
|
143
|
-
lines.push('', help);
|
|
142
|
+
lines.push(...pickerRows(visible, state.active - start));
|
|
143
|
+
lines.push(...pickerFooter({ more: canLoadMore() }));
|
|
144
144
|
return lines;
|
|
145
145
|
},
|
|
146
146
|
});
|
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
|
+
}
|
package/src/prompt-engine.js
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import readline from 'node:readline';
|
|
15
|
+
import { stripVTControlCharacters } from 'node:util';
|
|
15
16
|
|
|
16
17
|
export class PromptCancelled extends Error {
|
|
17
18
|
constructor() {
|
|
@@ -27,7 +28,10 @@ export function normalizeChoices(choices = []) {
|
|
|
27
28
|
return {
|
|
28
29
|
name: String(choice.name ?? choice.value ?? ''),
|
|
29
30
|
value: 'value' in choice ? choice.value : choice.name,
|
|
30
|
-
disabled: Boolean(choice.disabled),
|
|
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 } : {}),
|
|
31
35
|
checked: Boolean(choice.checked),
|
|
32
36
|
};
|
|
33
37
|
}
|
|
@@ -78,7 +82,10 @@ export async function runPrompt(config, context, spec) {
|
|
|
78
82
|
const text = Array.isArray(rendered) ? rendered.join('\n') : String(rendered ?? '');
|
|
79
83
|
if (output.isTTY && height > 0) output.write(`\x1b[${height}A\x1b[0J`);
|
|
80
84
|
output.write(`${text}\n`);
|
|
81
|
-
|
|
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);
|
|
82
89
|
};
|
|
83
90
|
|
|
84
91
|
const finish = value => {
|
package/src/prompts.js
CHANGED
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
* paginated-search.js and resolve undefined instead.
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
-
import {
|
|
10
|
+
import { pickerRows, pickerFooter } from './picker-ui.js';
|
|
11
|
+
import { runPrompt, normalizeChoices, PromptCancelled, escapeResult } from './prompt-engine.js';
|
|
11
12
|
export { PromptCancelled };
|
|
12
13
|
|
|
13
14
|
const ESC_HINT = ' (Esc to cancel)';
|
|
@@ -113,33 +114,59 @@ export async function confirm(config = {}, context = {}) {
|
|
|
113
114
|
}
|
|
114
115
|
|
|
115
116
|
export async function select(config = {}, context = {}) {
|
|
117
|
+
return localPicker(config, context);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function localPicker(config, context, multiple = false) {
|
|
116
121
|
const choices = normalizeChoices(config.choices || []);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
122
|
+
const state = {
|
|
123
|
+
term: '', choices, active: firstEnabled(choices), error: '',
|
|
124
|
+
picked: new Set(choices.filter(choice => choice.checked && !choice.disabled)),
|
|
125
|
+
};
|
|
126
|
+
const defaultIndex = choices.findIndex(choice => !choice.disabled && choice.value === config.default);
|
|
127
|
+
if (defaultIndex >= 0) state.active = defaultIndex;
|
|
128
|
+
return runPrompt(config, context, {
|
|
129
|
+
...(config.escapeValue ? { escapeValue: config.escapeValue } : {}),
|
|
130
|
+
init: () => state,
|
|
131
|
+
async key(_state, key, api) {
|
|
132
|
+
const visible = state.choices;
|
|
133
|
+
const choice = visible[state.active];
|
|
120
134
|
if (key.name === 'return' || key.name === 'enter') {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
135
|
+
if (!multiple) {
|
|
136
|
+
if (choice && !choice.disabled) return { done: choice.value };
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const values = choices.filter(choice => state.picked.has(choice)).map(choice => choice.value);
|
|
140
|
+
state.error = config.required && !values.length ? 'Select at least one option.' : await validateAnswer(config.validate, values);
|
|
141
|
+
if (state.error) { api.redraw(); return; }
|
|
142
|
+
return { done: values };
|
|
124
143
|
}
|
|
125
|
-
if (key.name === '
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
|
|
144
|
+
if (multiple && key.name === 'space') {
|
|
145
|
+
if (choice && !choice.disabled) {
|
|
146
|
+
if (state.picked.has(choice)) state.picked.delete(choice);
|
|
147
|
+
else state.picked.add(choice);
|
|
148
|
+
state.error = '';
|
|
149
|
+
}
|
|
150
|
+
} else if (multiple && key.ctrl && key.name === 'a') {
|
|
151
|
+
visible.forEach(choice => { if (!choice.disabled) state.picked.add(choice); });
|
|
152
|
+
} else if (key.name === 'up' || key.name === 'down') {
|
|
153
|
+
state.active = move(visible, state.active, key.name === 'up' ? -1 : 1);
|
|
154
|
+
} else {
|
|
155
|
+
const previous = state.term;
|
|
156
|
+
if (key.name === 'backspace') state.term = Array.from(state.term).slice(0, -1).join('');
|
|
157
|
+
else if (key.text) state.term += key.text;
|
|
158
|
+
if (previous !== state.term) {
|
|
159
|
+
state.choices = state.term ? choices.filter(choice => !choice.separator && choice.name.toLocaleLowerCase().includes(state.term.toLocaleLowerCase())) : choices;
|
|
160
|
+
state.active = firstEnabled(state.choices);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
api.redraw();
|
|
138
164
|
},
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
165
|
+
render() {
|
|
166
|
+
return [config.message ?? '', `Search: ${state.term}`, ...pickerRows(state.choices, state.active, multiple ? { picked: state.picked } : {}),
|
|
167
|
+
...(!state.choices.length ? [' No matches'] : []), ...(state.error ? [` ${state.error}`] : []), ...pickerFooter({ checkbox: multiple })];
|
|
142
168
|
},
|
|
169
|
+
final: (_cfg, value) => value === undefined ? null : `✔ ${config.message ?? ''} ${multiple ? choices.filter(choice => value.includes(choice.value)).map(choice => choice.name).join(', ') : choices.find(choice => choice.value === value)?.name ?? value ?? ''}`,
|
|
143
170
|
});
|
|
144
171
|
}
|
|
145
172
|
|
|
@@ -190,11 +217,11 @@ export async function search(config = {}, context = {}) {
|
|
|
190
217
|
if (state.term !== previous) void load(api);
|
|
191
218
|
},
|
|
192
219
|
render() {
|
|
193
|
-
const lines = [
|
|
220
|
+
const lines = [config.message ?? '', `Search: ${state.term}${state.loading ? ' ...' : ''}`];
|
|
194
221
|
if (state.error) lines.push(` ${state.error}`);
|
|
195
222
|
else if (!state.choices.length) lines.push(state.loading ? ' Loading...' : ' No matches');
|
|
196
|
-
else state.choices
|
|
197
|
-
lines.push(
|
|
223
|
+
else lines.push(...pickerRows(state.choices, state.active));
|
|
224
|
+
lines.push(...pickerFooter());
|
|
198
225
|
return lines;
|
|
199
226
|
},
|
|
200
227
|
});
|
|
@@ -205,46 +232,7 @@ export async function search(config = {}, context = {}) {
|
|
|
205
232
|
}
|
|
206
233
|
|
|
207
234
|
export async function checkbox(config = {}, context = {}) {
|
|
208
|
-
|
|
209
|
-
return runPrompt({ ...config, message: withHint(config) }, context, {
|
|
210
|
-
init: () => ({ active: firstEnabled(choices), picked: new Set(choices.flatMap((choice, index) => choice.checked && !choice.disabled ? [index] : [])), error: '' }),
|
|
211
|
-
key(state, key, api) {
|
|
212
|
-
if (key.name === 'return' || key.name === 'enter') {
|
|
213
|
-
const values = choices.filter((_choice, index) => state.picked.has(index)).map(choice => choice.value);
|
|
214
|
-
if (config.required && values.length === 0) {
|
|
215
|
-
state.error = 'Select at least one option.';
|
|
216
|
-
api.redraw();
|
|
217
|
-
return;
|
|
218
|
-
}
|
|
219
|
-
return { done: values };
|
|
220
|
-
}
|
|
221
|
-
if (key.name === 'space') {
|
|
222
|
-
if (choices[state.active] && !choices[state.active].disabled) {
|
|
223
|
-
if (state.picked.has(state.active)) state.picked.delete(state.active);
|
|
224
|
-
else state.picked.add(state.active);
|
|
225
|
-
state.error = '';
|
|
226
|
-
api.redraw();
|
|
227
|
-
}
|
|
228
|
-
return;
|
|
229
|
-
}
|
|
230
|
-
if (key.name === 'a' && key.ctrl) {
|
|
231
|
-
choices.forEach((choice, index) => { if (!choice.disabled) state.picked.add(index); });
|
|
232
|
-
api.redraw();
|
|
233
|
-
return;
|
|
234
|
-
}
|
|
235
|
-
if (key.name === 'up') { state.active = move(choices, state.active, -1); api.redraw(); return; }
|
|
236
|
-
if (key.name === 'down') { state.active = move(choices, state.active, 1); api.redraw(); return; }
|
|
237
|
-
},
|
|
238
|
-
render(state) {
|
|
239
|
-
const lines = [withHint(config)];
|
|
240
|
-
choices.forEach((choice, index) => {
|
|
241
|
-
lines.push(`${index === state.active ? '>' : ' '} [${state.picked.has(index) ? 'x' : ' '}] ${choice.name}`);
|
|
242
|
-
});
|
|
243
|
-
if (state.error) lines.push(` ${state.error}`);
|
|
244
|
-
lines.push('', ' Space toggles, arrows move, Enter confirms.');
|
|
245
|
-
return lines;
|
|
246
|
-
},
|
|
247
|
-
});
|
|
235
|
+
return localPicker(config, context, true);
|
|
248
236
|
}
|
|
249
237
|
|
|
250
238
|
function firstEnabled(choices) {
|