@jacebenson/jsn 4.3.12 → 4.3.13-dev.100.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.
Files changed (75) hide show
  1. package/README.md +55 -0
  2. package/package.json +2 -1
  3. package/skills/servicenow/SKILL.md +20 -172
  4. package/src/app.js +29 -40
  5. package/src/auth-recovery.js +83 -0
  6. package/src/auth.js +2 -0
  7. package/src/capabilities.js +9 -0
  8. package/src/cli-adapter.js +24 -4
  9. package/src/cli.js +39 -9
  10. package/src/command-audience.js +35 -0
  11. package/src/commands/_generic.js +58 -37
  12. package/src/commands/_simple.js +30 -33
  13. package/src/commands/_ticket.js +38 -31
  14. package/src/commands/attachments.js +88 -0
  15. package/src/commands/auth.js +94 -19
  16. package/src/commands/b4rules.js +8 -3
  17. package/src/commands/changes.js +2 -4
  18. package/src/commands/columns.js +2 -1
  19. package/src/commands/customizations.js +13 -7
  20. package/src/commands/discover.js +54 -0
  21. package/src/commands/docs/db.js +9 -1
  22. package/src/commands/docs/docs.js +25 -2
  23. package/src/commands/docs/ingest.js +20 -3
  24. package/src/commands/docs/install.js +86 -0
  25. package/src/commands/docs/locks.js +34 -0
  26. package/src/commands/docs/refresh.js +22 -2
  27. package/src/commands/docs/replace.js +30 -0
  28. package/src/commands/docs/snapshot-check.js +11 -0
  29. package/src/commands/docs/snapshot.js +130 -0
  30. package/src/commands/docs/sync.js +16 -6
  31. package/src/commands/forms.js +22 -97
  32. package/src/commands/groups.js +11 -8
  33. package/src/commands/history.js +57 -0
  34. package/src/commands/incidents.js +2 -4
  35. package/src/commands/midservers.js +117 -10
  36. package/src/commands/records.js +64 -119
  37. package/src/commands/requests.js +10 -16
  38. package/src/commands/rest.js +15 -2
  39. package/src/commands/scopes.js +1 -1
  40. package/src/commands/setup.js +46 -9
  41. package/src/commands/skill.js +72 -76
  42. package/src/commands/tasks.js +2 -4
  43. package/src/commands/tickets.js +31 -22
  44. package/src/commands/uipolicies.js +2 -27
  45. package/src/commands/updatesets.js +65 -13
  46. package/src/commands/users.js +11 -8
  47. package/src/commands/version.js +14 -8
  48. package/src/constrained-query.js +20 -0
  49. package/src/context.js +6 -6
  50. package/src/customization-guidance.js +31 -0
  51. package/src/customizations-report.js +6 -0
  52. package/src/customizations.js +28 -13
  53. package/src/errors.js +23 -1
  54. package/src/essential-records.js +40 -0
  55. package/src/field-behavior.js +15 -0
  56. package/src/help.js +10 -2
  57. package/src/helpers.js +44 -0
  58. package/src/metadata/records.js +249 -0
  59. package/src/midserver-linux-remote.js +210 -0
  60. package/src/midserver-remote-recipe.js +49 -0
  61. package/src/midserver-remote.js +221 -0
  62. package/src/midserver-windows-remote.js +318 -0
  63. package/src/midserver-zip.js +2 -2
  64. package/src/mutation-context.js +74 -0
  65. package/src/mutation-history.js +75 -0
  66. package/src/mutations.js +3 -2
  67. package/src/output.js +34 -2
  68. package/src/record-command-profiles.js +70 -0
  69. package/src/record-profiles.js +93 -0
  70. package/src/records-engine.js +57 -0
  71. package/src/related-lists.js +153 -0
  72. package/src/resolve-record.js +50 -28
  73. package/src/sdk-batch.js +130 -0
  74. package/src/sdk.js +157 -18
  75. package/src/update-set-xml.js +89 -0
@@ -1,10 +1,11 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
3
- import { formatCommandList, formatRecordForDisplay, buildQuerySuffix, parseDataArg, getStringField, interactiveList, resolveFieldsParam, checkDerivedFields, confirmDelete, assertSafeExactMatch, verifyWriteBack } from '../helpers.js';
1
+ import { formatCommandList, formatRecordForDisplay, buildQuerySuffix, buildRecordPayload, getStringField, interactiveList, resolveFieldsParam, checkDerivedFields, confirmDelete, verifyWriteBack } from '../helpers.js';
2
+ import { listRecords, recordListParams, getRecord, createRecord, updateRecord, deleteRecord } from '../records-engine.js';
3
+ import { unwrapSysId } from '../resolve-record.js';
4
+ import { errUsage } from '../errors.js';
4
5
  import { declareCapabilities } from '../capabilities.js';
5
6
 
6
7
  declareCapabilities('records', {
7
- mutationSubcommands: ['create', 'update', 'delete', 'bulk', 'attachments add'],
8
+ mutationSubcommands: ['create', 'update', 'delete', 'bulk'],
8
9
  });
9
10
 
10
11
  const tableDefaultColumns = {
@@ -67,6 +68,15 @@ function getDefaultColumns(table) {
67
68
  return tableDefaultColumns[table] || ['sys_id'];
68
69
  }
69
70
 
71
+ function recordLookup(argv) {
72
+ const sysId = argv['sys-id'];
73
+ if ((sysId === undefined) === (argv.identifier === undefined)) throw errUsage('Supply exactly one of --sys-id or --identifier.');
74
+ if (sysId !== undefined && argv.identifierField && argv.identifierField !== 'sys_id') throw errUsage('--sys-id cannot be combined with a different --identifier-field.');
75
+ return { table: argv.table, identifier: sysId ?? argv.identifier,
76
+ identifierField: sysId !== undefined ? 'sys_id' : (argv.identifierField || 'sys_id'),
77
+ displayValues: argv.displayValues, resource: 'Record' };
78
+ }
79
+
70
80
  export function recordsCmd(wrap) {
71
81
  return {
72
82
  command: 'records [subcommand]',
@@ -83,7 +93,16 @@ export function recordsCmd(wrap) {
83
93
  .option('columns', { alias: ['c', 'fields'], type: 'string', describe: 'Comma-separated columns (e.g. "number,short_description")' })
84
94
  .option('limit', { type: 'number', default: 20, describe: 'Max records' })
85
95
  .option('offset', { type: 'number', default: 0, describe: 'Offset' })
86
- .option('count', { type: 'boolean', default: true, describe: 'Include the total-matching count (use --no-count to opt out)' }),
96
+ .option('display-values', { type: 'string', choices: ['raw', 'display', 'both'], describe: 'Reference values: raw, display or both' })
97
+ .option('count', { type: 'boolean', default: true, describe: 'Include the total-matching count (use --no-count to opt out)' })
98
+ .epilogue([
99
+ 'In an interactive terminal with automatic output, browse a picker (10 visible items).',
100
+ 'Use arrow keys to browse, type to search sys_id on the server, scroll to load more,',
101
+ 'and press Enter to select or Esc to cancel. --sys-id skips the picker.',
102
+ 'Use --json for noninteractive structured output; pipes and explicit formats also skip the picker.',
103
+ 'Example: jsn records list --table incident --query "active=true" --limit 5 --json',
104
+ 'Discover fields with: jsn columns --table incident',
105
+ ].join('\n')),
87
106
  handler: wrap(async (argv, app) => {
88
107
  const table = argv.table;
89
108
  const columns = argv.columns ? argv.columns.split(',') : getDefaultColumns(table);
@@ -122,15 +141,10 @@ export function recordsCmd(wrap) {
122
141
  }
123
142
 
124
143
  // Text/table fallback (or sys-id direct lookup)
125
- const params = new URLSearchParams();
126
- params.set('sysparm_limit', argv['sys-id'] ? '1' : String(argv.limit));
127
- params.set('sysparm_offset', String(argv.offset));
128
- params.set('sysparm_display_value', 'all');
129
144
  const fields = resolveFieldsParam(columns);
130
- if (fields) params.set('sysparm_fields', fields);
131
- if (query) params.set('sysparm_query', query);
132
- const records = await app.sdk.list(table, params);
133
- const displayRecords = fields ? records.map(r => formatRecordForDisplay(r, columns)) : records;
145
+ const params = recordListParams({ limit: argv['sys-id'] ? 1 : argv.limit, offset: argv.offset, fields, query });
146
+ const records = await listRecords(app.sdk, { table, params, displayValues: argv.displayValues });
147
+ const displayRecords = fields && !argv.displayValues ? records.map(r => formatRecordForDisplay(r, columns)) : records;
134
148
 
135
149
  // include_counts: default opt-in on every list. Opt out per-profile
136
150
  // (config include_counts:false) or per-invocation (--no-count).
@@ -142,7 +156,7 @@ export function recordsCmd(wrap) {
142
156
  }
143
157
 
144
158
  const breadcrumbs = [
145
- { action: 'create', cmd: `jsn records create --table ${table} --data '{...}'`, description: 'Create a new record' },
159
+ { action: 'create', cmd: `jsn records create --table ${table} -f name=value`, description: 'Create a new record' },
146
160
  { action: 'filter', cmd: `jsn records list --table ${table} --query "priority=1"`, description: 'Filter: priority 1 only' },
147
161
  { action: 'columns', cmd: `jsn columns --table ${table}`, description: 'View available columns' },
148
162
  ];
@@ -182,27 +196,23 @@ export function recordsCmd(wrap) {
182
196
  })
183
197
  .command({
184
198
  command: 'get',
185
- describe: 'Get a single record by sys_id',
199
+ describe: 'Get one record by sys_id or an explicit identifier field',
186
200
  builder: (y) => y
187
201
  .option('table', { type: 'string', demandOption: true, describe: 'Table name' })
188
- .option('sys-id', { type: 'string', demandOption: true, describe: 'Record sys_id' })
202
+ .option('sys-id', { type: 'string', describe: 'Full or unambiguous partial record sys_id (including legacy IDs)' })
203
+ .option('identifier', { type: 'string', describe: 'Identifier value; use --identifier-field number/name/etc for human identifiers' })
204
+ .option('identifier-field', { type: 'string', describe: 'Exact identifier field (default sys_id); disambiguates names and legacy sys_ids' })
205
+ .option('display-values', { type: 'string', choices: ['raw', 'display', 'both'], describe: 'Reference values: raw, display or both' })
189
206
  .option('columns', { alias: ['c', 'fields'], type: 'string', describe: 'Comma-separated columns (e.g. "number,short_description")' })
190
- .option('attachments', { type: 'boolean', default: false, describe: 'Also list the record\'s attachments' }),
207
+ .option('attachments', { type: 'boolean', default: false, describe: 'Also list the record\'s attachments' })
208
+ .epilogue('Exact lookup precedes sys_id prefix, then suffix. Ambiguous matches fail.\nExample: jsn records get --table incident --sys-id <sys_id> --json\nExample: jsn records get --table incident --identifier INC0010001 --identifier-field number --json'),
191
209
  handler: wrap(async (argv, app) => {
192
- assertSafeExactMatch(argv['sys-id']);
193
- const params = new URLSearchParams();
194
- params.set('sysparm_query', `sys_id=${argv['sys-id']}`);
195
- params.set('sysparm_limit', '1');
196
- params.set('sysparm_display_value', 'true');
197
- if (argv.columns && argv.columns !== '*') params.set('sysparm_fields', `sys_id,${argv.columns}`);
198
- const records = await app.sdk.list(argv.table, params);
199
- if (records.length === 0) {
200
- throw new Error(`Record not found: ${argv['sys-id']}`);
201
- }
202
- const record = records[0];
210
+ const lookup = recordLookup(argv);
211
+ const fields = argv.columns && argv.columns !== '*' ? argv.columns.split(',') : undefined;
212
+ const record = await getRecord(app.sdk, { ...lookup, fields, displayValue: 'true' });
203
213
  if (argv.attachments) {
204
214
  try {
205
- const atts = await app.sdk.listAttachments(argv['sys-id']);
215
+ const atts = await app.sdk.listAttachments(unwrapSysId(record));
206
216
  record._attachments = atts;
207
217
  } catch {
208
218
  record._attachments = [];
@@ -221,17 +231,18 @@ export function recordsCmd(wrap) {
221
231
  describe: 'Create a new record',
222
232
  builder: (y) => y
223
233
  .option('table', { type: 'string', demandOption: true, describe: 'Table name' })
224
- .option('data', { type: 'string', describe: 'JSON fields (e.g. \'{"state":"2"}\')' })
234
+ .option('data', { type: 'string', describe: 'JSON fields' })
225
235
  .option('data-file', { type: 'string', describe: 'Read JSON payload from file' })
226
236
  .option('data-stdin', { type: 'boolean', describe: 'Read JSON payload from stdin (pipe-friendly)' })
237
+ .option('field', { alias: 'f', type: 'string', array: true, describe: 'Set a field as name=value; repeatable' })
227
238
  .option('strict', { type: 'boolean', default: false, describe: 'Exit non-zero if any supplied field was not persisted (write-back check)' }),
228
239
  handler: wrap(async (argv, app) => {
229
- const recordData = parseDataArg(argv);
240
+ const recordData = buildRecordPayload(argv);
230
241
  const warnings = checkDerivedFields(argv.table, recordData);
231
242
  for (const w of warnings) {
232
243
  process.stderr.write(`⚠ ${w.hint}\n`);
233
244
  }
234
- const record = await app.sdk.create(argv.table, recordData);
245
+ const record = await createRecord(app.sdk, { table: argv.table, data: recordData });
235
246
  const sysID = getStringField(record, 'sys_id');
236
247
  const mismatches = await verifyWriteBack(app, argv.table, sysID, recordData);
237
248
  for (const m of mismatches) {
@@ -248,19 +259,24 @@ export function recordsCmd(wrap) {
248
259
  describe: 'Update an existing record',
249
260
  builder: (y) => y
250
261
  .option('table', { type: 'string', demandOption: true, describe: 'Table name' })
251
- .option('sys-id', { type: 'string', demandOption: true, describe: 'Record sys_id' })
252
- .option('data', { type: 'string', describe: 'JSON fields (e.g. \'{"state":"2"}\')' })
262
+ .option('sys-id', { type: 'string', describe: 'Full or unambiguous partial record sys_id (including legacy IDs)' })
263
+ .option('identifier', { type: 'string', describe: 'Identifier value; use --identifier-field number/name/etc for human identifiers' })
264
+ .option('identifier-field', { type: 'string', describe: 'Exact identifier field (default sys_id); disambiguates names and legacy sys_ids' })
265
+ .option('data', { type: 'string', describe: 'JSON fields' })
253
266
  .option('data-file', { type: 'string', describe: 'Read JSON payload from file' })
254
267
  .option('data-stdin', { type: 'boolean', describe: 'Read JSON payload from stdin (pipe-friendly)' })
268
+ .option('field', { alias: 'f', type: 'string', array: true, describe: 'Set a field as name=value; repeatable' })
255
269
  .option('strict', { type: 'boolean', default: false, describe: 'Exit non-zero if any supplied field was not persisted (write-back check)' }),
256
270
  handler: wrap(async (argv, app) => {
257
- const recordData = parseDataArg(argv);
271
+ const recordData = buildRecordPayload(argv);
258
272
  const warnings = checkDerivedFields(argv.table, recordData);
259
273
  for (const w of warnings) {
260
274
  process.stderr.write(`⚠ ${w.hint}\n`);
261
275
  }
262
- const record = await app.sdk.update(argv.table, argv['sys-id'], recordData);
263
- const mismatches = await verifyWriteBack(app, argv.table, argv['sys-id'], recordData);
276
+ const lookup = recordLookup(argv);
277
+ const resolved = await getRecord(app.sdk, lookup);
278
+ const record = await updateRecord(app.sdk, { table: argv.table, record: resolved, data: recordData });
279
+ const mismatches = await verifyWriteBack(app, argv.table, unwrapSysId(resolved), recordData);
264
280
  for (const m of mismatches) {
265
281
  process.stderr.write(`⚠ field "${m.field}" was not persisted (sent: "${m.sent}", got: "${m.got}")\n`);
266
282
  }
@@ -275,82 +291,17 @@ export function recordsCmd(wrap) {
275
291
  describe: 'Delete a record',
276
292
  builder: (y) => y
277
293
  .option('table', { type: 'string', demandOption: true, describe: 'Table name' })
278
- .option('sys-id', { type: 'string', demandOption: true, describe: 'Record sys_id' })
294
+ .option('sys-id', { type: 'string', describe: 'Full or unambiguous partial record sys_id (including legacy IDs)' })
295
+ .option('identifier', { type: 'string', describe: 'Identifier value; use --identifier-field number/name/etc for human identifiers' })
296
+ .option('identifier-field', { type: 'string', describe: 'Exact identifier field (default sys_id); disambiguates names and legacy sys_ids' })
279
297
  .option('force', { type: 'boolean', default: false, describe: 'Skip confirmation' }),
280
298
  handler: wrap(async (argv, app) => {
281
- await confirmDelete(app, argv, `Delete record from ${argv.table} (${argv['sys-id']})`);
282
- await app.sdk.delete(argv.table, argv['sys-id']);
283
- app.ok({ message: 'Record deleted', table: argv.table, sys_id: argv['sys-id'] }, { summary: `Deleted record from ${argv.table}` });
299
+ const lookup = recordLookup(argv);
300
+ await confirmDelete(app, argv, `Delete record from ${argv.table} (${lookup.identifier})`);
301
+ const sysID = await deleteRecord(app.sdk, lookup);
302
+ app.ok({ message: 'Record deleted', table: argv.table, sys_id: sysID }, { summary: `Deleted record from ${argv.table}` });
284
303
  }),
285
304
  })
286
- .command({
287
- command: 'attachments',
288
- describe: 'Work with attachments on a record',
289
- builder: (y) => y
290
- .command({
291
- command: 'list',
292
- describe: 'List attachments on a record',
293
- builder: (yy) => yy
294
- .option('sys-id', { alias: 's', type: 'string', demandOption: true, describe: 'Parent record sys_id' })
295
- .option('table', { type: 'string', describe: 'Parent table name (context only)' }),
296
- handler: wrap(async (argv, app) => {
297
- app.requireInstance();
298
- const attachments = await app.sdk.listAttachments(argv['sys-id']);
299
- app.ok({
300
- sys_id: argv['sys-id'],
301
- table: argv.table || '',
302
- count: attachments.length,
303
- attachments,
304
- context: { instance_url: app.getEffectiveInstance() },
305
- }, { summary: `${attachments.length} attachment(s) on record ${argv['sys-id']}` });
306
- }),
307
- })
308
- .command({
309
- command: 'get <attachment-id>',
310
- describe: 'Download an attachment to a local file',
311
- builder: (yy) => yy
312
- .positional('attachment-id', { describe: 'Attachment sys_id', type: 'string' })
313
- .option('sys-id', { alias: 's', type: 'string', demandOption: true, describe: 'Parent record sys_id (used to resolve the file name)' })
314
- .option('out', { alias: 'o', type: 'string', describe: 'Output file path (default: attachment file name in cwd)' }),
315
- handler: wrap(async (argv, app) => {
316
- app.requireInstance();
317
- const buf = await app.sdk.getAttachment(argv['attachment-id']);
318
- let name = argv.out;
319
- if (!name) {
320
- // Best-effort: derive the file name from the attachment row.
321
- let fileName = `${argv['attachment-id']}.bin`;
322
- try {
323
- const rows = await app.sdk.listAttachments(argv['sys-id']);
324
- const hit = (rows || []).find((a) => getStringField(a, 'sys_id') === argv['attachment-id']);
325
- const fn = getStringField(hit, 'file_name');
326
- if (fn) fileName = fn;
327
- } catch { /* keep default */ }
328
- name = fileName;
329
- }
330
- await fs.promises.writeFile(name, buf);
331
- app.ok({ sys_id: argv['attachment-id'], out: name, bytes: buf.length }, { summary: `Wrote ${buf.length} bytes to ${name}` });
332
- }),
333
- })
334
- .command({
335
- command: 'add <file>',
336
- describe: 'Upload a file as a new attachment on a record (mutation)',
337
- builder: (yy) => yy
338
- .positional('file', { describe: 'Local file path to upload', type: 'string' })
339
- .option('sys-id', { alias: 's', type: 'string', demandOption: true, describe: 'Parent record sys_id' })
340
- .option('table', { type: 'string', demandOption: true, describe: 'Parent table for the record (e.g. incident)' })
341
- .option('name', { type: 'string', describe: 'Display file name (default: basename of --file)' })
342
- .option('force', { type: 'boolean', default: false, describe: 'Skip confirmation' }),
343
- handler: wrap(async (argv, app) => {
344
- app.requireInstance();
345
- await confirmDelete(app, argv, `Upload "${argv.file}" as an attachment on ${argv.table} ${argv['sys-id']}?`);
346
- const content = await fs.promises.readFile(argv.file);
347
- const fileName = argv.name || path.basename(argv.file);
348
- const created = await app.sdk.addAttachment(argv.table, argv['sys-id'], content, fileName);
349
- app.ok({ sys_id: created?.sys_id, file_name: fileName, table: argv.table, recordsys_id: argv['sys-id'] }, { summary: `Attached ${fileName}` });
350
- }),
351
- })
352
- .demandCommand(1, 'Specify an attachment action: list, get, or add'),
353
- })
354
305
  .command({
355
306
  command: 'aggregate',
356
307
  describe: 'Run ServiceNow Stats API aggregation with optional grouping',
@@ -358,6 +309,7 @@ export function recordsCmd(wrap) {
358
309
  .option('table', { type: 'string', demandOption: true, describe: 'Table name' })
359
310
  .option('query', { type: 'string', describe: 'Encoded query to constrain the aggregation' })
360
311
  .option('group-by', { type: 'string', describe: 'Comma-separated fields to group by' })
312
+ .option('display-values', { type: 'string', choices: ['raw', 'display', 'both'], describe: 'Reference values: raw, display or both' })
361
313
  .option('count', { type: 'boolean', default: true, describe: 'Include record counts' })
362
314
  .option('avg-fields', { type: 'string', describe: 'Comma-separated fields to average' })
363
315
  .option('sum-fields', { type: 'string', describe: 'Comma-separated fields to sum' })
@@ -401,22 +353,15 @@ export function recordsCmd(wrap) {
401
353
  builder: (y) => y
402
354
  .option('table', { type: 'string', demandOption: true, describe: 'Table name' })
403
355
  .option('query', { type: 'string', demandOption: true, describe: 'Encoded query selecting the records to update (e.g. "priority=1^state=1")' })
404
- .option('set', { type: 'string', demandOption: true, describe: 'JSON object of fields to set (e.g. \'{"state":"3"}\')' })
356
+ .option('set', { type: 'string', describe: 'JSON object of fields to set' })
357
+ .option('field', { alias: 'f', type: 'string', array: true, describe: 'Set a field as name=value; repeatable' })
405
358
  .option('dry-run', { type: 'boolean', default: true, describe: 'Preview the count + a sample without mutating (default)' })
406
359
  .option('execute', { type: 'boolean', default: false, describe: 'Perform the update after confirmation' })
407
360
  .option('limit', { type: 'number', default: 200, describe: 'Max records to update when executing' })
408
361
  .option('force', { type: 'boolean', default: false, describe: 'Skip confirmation (with --execute)' }),
409
362
  handler: wrap(async (argv, app) => {
410
363
  app.requireInstance();
411
- let set;
412
- try {
413
- set = JSON.parse(argv.set);
414
- } catch (e) {
415
- throw new Error(`Invalid --set JSON: ${e.message}`, { cause: e });
416
- }
417
- if (!set || typeof set !== 'object' || Array.isArray(set)) {
418
- throw new Error('--set must be a JSON object of field=value pairs');
419
- }
364
+ const set = buildRecordPayload({ ...argv, data: argv.set }, { required: true });
420
365
  if (!argv['dry-run'] && !argv.execute) {
421
366
  argv.execute = true; // explicit --no-dry-run implies execute intent
422
367
  }
@@ -1,11 +1,12 @@
1
- import { buildTicketCommands } from './_ticket.js';
2
- import { getStringField, assertSafeExactMatch } from '../helpers.js';
1
+ import { enrichRelatedLists, relatedListOptions } from '../related-lists.js';
2
+ import { getRecord, recordOptions } from '../records-engine.js';
3
+ import { unwrapSysId } from '../resolve-record.js';
4
+ import { buildProfileCommand } from '../record-profiles.js';
3
5
 
4
- const requestDefaultColumns = ['number', 'short_description', 'stage', 'assigned_to'];
5
6
 
6
7
  export function requestsCmd(wrap) {
7
8
  // Get the base command definition from the shared ticket builder
8
- const baseCmd = buildTicketCommands('sc_req_item', 'requests', 'req', requestDefaultColumns, {}, null, wrap);
9
+ const baseCmd = buildProfileCommand('requests', wrap);
9
10
 
10
11
  // Override the builder to inject our enriched show handler.
11
12
  // yargs allows re-registration — the last .command() call wins the slot.
@@ -16,6 +17,7 @@ export function requestsCmd(wrap) {
16
17
  command: 'show <number>',
17
18
  aliases: ['get'],
18
19
  describe: 'Show a specific request with attachments and catalog variables',
20
+ builder: y => relatedListOptions(recordOptions(y)),
19
21
  handler: wrap(enrichedShowHandler),
20
22
  });
21
23
  };
@@ -25,17 +27,8 @@ export function requestsCmd(wrap) {
25
27
 
26
28
  async function enrichedShowHandler(argv, app) {
27
29
  const number = argv.number;
28
- assertSafeExactMatch(number);
29
- const params = new URLSearchParams();
30
- params.set('sysparm_query', `number=${number}`);
31
- params.set('sysparm_display_value', 'all');
32
- params.set('sysparm_limit', '1');
33
- const records = await app.sdk.list('sc_req_item', params);
34
- if (records.length === 0) {
35
- throw new Error(`Request not found: ${number}`);
36
- }
37
- const record = records[0];
38
- const sysID = getStringField(record, 'sys_id');
30
+ const record = await getRecord(app.sdk, { table: 'sc_req_item', identifier: number, matchField: 'number', identifierField: argv.identifierField, displayValues: argv.displayValues, resource: 'Request' });
31
+ const sysID = unwrapSysId(record);
39
32
 
40
33
  // Fetch attachments and catalog variables in parallel
41
34
  const [attachments, variables] = await Promise.all([
@@ -50,10 +43,11 @@ async function enrichedShowHandler(argv, app) {
50
43
  table: 'sc_req_item',
51
44
  };
52
45
 
46
+ await enrichRelatedLists(record, { table: 'sc_req_item', argv, app });
53
47
  app.ok(record, {
54
48
  summary: `Request ${number}`,
55
49
  breadcrumbs: [
56
- { action: 'update', cmd: `jsn req update ${number} --data '{...}'`, description: 'Update this request' },
50
+ { action: 'update', cmd: `jsn req update ${number} -f state=2`, description: 'Update this request' },
57
51
  { action: 'list', cmd: `jsn req list`, description: 'Back to all requests' },
58
52
  ],
59
53
  });
@@ -2,7 +2,7 @@ import { declareCapabilities } from '../capabilities.js';
2
2
 
3
3
  // Raw REST passthrough — can hit any endpoint with any method, so the whole
4
4
  // command is a mutation surface on read-only profiles.
5
- declareCapabilities('rest', { mutationSubcommands: [''] });
5
+ declareCapabilities('rest', { mutationSubcommands: [''], mutationContextReadMethods: ['GET', 'HEAD', 'OPTIONS'] });
6
6
 
7
7
  export function restCmd(wrap) {
8
8
  return {
@@ -10,6 +10,10 @@ export function restCmd(wrap) {
10
10
  describe: 'Make raw REST API calls',
11
11
  builder: (yargs) => {
12
12
  return yargs
13
+ .positional('endpoint', {
14
+ type: 'string',
15
+ describe: 'API path, optionally including query parameters; with --table, a record sys_id',
16
+ })
13
17
  .option('method', {
14
18
  alias: 'X',
15
19
  type: 'string',
@@ -34,7 +38,16 @@ export function restCmd(wrap) {
34
38
  .option('query', {
35
39
  type: 'string',
36
40
  describe: 'Query parameters string',
37
- });
41
+ })
42
+ .epilogue([
43
+ 'Prefer a specific jsn command or records for normal table operations.',
44
+ 'Raw REST is blocked on read-only profiles for every method, including GET.',
45
+ 'Examples:',
46
+ ' jsn rest "/api/now/table/incident?sysparm_limit=3" --json',
47
+ ' jsn rest --table incident --query "sysparm_limit=3" --json',
48
+ ' jsn rest /stats.do --raw',
49
+ 'Use either query parameters in the endpoint or --query, rather than both.',
50
+ ].join('\n'));
38
51
  },
39
52
  handler: wrap(async (argv, app) => {
40
53
  const validMethods = ['GET', 'POST', 'PUT', 'DELETE', 'PATCH'];
@@ -3,7 +3,7 @@ import { requireCurrentUserSysId, setCurrentApplication } from '../context.js';
3
3
  import { declareCapabilities } from '../capabilities.js';
4
4
  import { unwrapSysId } from '../resolve-record.js';
5
5
 
6
- declareCapabilities('scopes', { mutationSubcommands: ['create', 'set'] });
6
+ declareCapabilities('scopes', { mutationSubcommands: ['create', 'set'], mutationContextExemptSubcommands: ['create', 'set'] });
7
7
 
8
8
  /**
9
9
  * Resolve a scope identifier to a single sys_scope record.
@@ -1,5 +1,5 @@
1
1
  import { loginWizard, loginProfile, showAuthStatus, switchProfile, modifyProfile, logoutProfile, removeConnection } from './auth.js';
2
- import { errUsage } from '../errors.js';
2
+ import { errUsageHint } from '../errors.js';
3
3
  import { declareCapabilities } from '../capabilities.js';
4
4
 
5
5
  // Setup manages local profile config — runs without a configured instance.
@@ -7,15 +7,36 @@ import { declareCapabilities } from '../capabilities.js';
7
7
  declareCapabilities('setup', { noInstance: true, skipContextHeader: true });
8
8
 
9
9
  const HUB_CHOICES = [
10
- { name: 'Add a connection', value: 'add' },
11
- { name: 'Sign in again', value: 'login' },
12
- { name: 'Check connections', value: 'status' },
13
- { name: 'Choose the default profile', value: 'switch' },
14
- { name: 'Change settings', value: 'modify' },
15
- { name: 'Sign out (keep the profile)', value: 'logout' },
16
- { name: 'Remove a connection', value: 'remove' },
10
+ { name: 'Add a connection', value: 'add', command: 'jsn auth login <instance> --name <name> --basic' },
11
+ { name: 'Sign in again', value: 'login', command: 'jsn auth login --profile <name> (with the authentication options below)' },
12
+ { name: 'Check connections', value: 'status', command: 'jsn auth status --details --json (use --local to skip instance probes)' },
13
+ { name: 'Choose the default profile', value: 'switch', command: 'jsn auth switch <name>' },
14
+ { name: 'Change settings', value: 'modify', command: 'jsn auth modify --profile <name> --read-only --confirm-deletes --no-include-counts' },
15
+ { name: 'Sign out (keep the profile)', value: 'logout', command: 'jsn auth logout --profile <name> --force (only after explicit approval)' },
16
+ { name: 'Remove a connection', value: 'remove', command: 'jsn auth remove <name>' },
17
+ { name: 'Install optional offline documentation', value: 'docs', command: 'jsn docs install --source snapshot|github|skip' },
17
18
  ];
18
19
 
20
+ const setupHelp = () => [
21
+ 'With no profiles, setup opens the add-connection wizard; otherwise it opens the management menu.',
22
+ 'The wizard and these scriptable commands share the same authentication and configuration operations:',
23
+ ...HUB_CHOICES.map(choice => ` ${choice.name}: ${choice.command}`),
24
+ '',
25
+ 'Authentication input without secrets in command arguments:',
26
+ ' Basic: set SN_USERNAME and SN_PASSWORD through your environment, then use --basic.',
27
+ ' OAuth: jsn auth login <instance> --name <name> --oauth --print-url --wait-file <code-file> --json',
28
+ ' Browser session: jsn auth login <instance> --name <name> --headers-file <session-file> --json',
29
+ ' Piped browser session: jsn auth login <instance> --name <name> --headers-file - --json',
30
+ ' A browser window can also be requested explicitly with --browser; it requires a human to sign in.',
31
+ ' Refresh OAuth: jsn auth refresh --profile <name>',
32
+ ' Renew captured browser headers: jsn auth refresh --profile <name> --headers-file <session-file>',
33
+ '',
34
+ 'New-profile preferences: --read-only, --confirm-deletes, --include-counts and --make-default.',
35
+ 'Use --no-read-only, --no-confirm-deletes, --no-include-counts or --no-make-default for the opposite value.',
36
+ 'Use --json for structured output. Explicit flags avoid implicit pickers; ambiguous targets return an error.',
37
+ 'Discover all options with jsn auth <command> --help.',
38
+ ].join('\n');
39
+
19
40
  /** The setup hub prompt; q and Escape resolve with no selected action. */
20
41
  export const authHubPrompt = async (...args) => {
21
42
  const { createPrompt, useKeypress, usePagination, useState, isDownKey, isEnterKey, isUpKey } = await import('@inquirer/core');
@@ -81,19 +102,35 @@ export function setupCmd(wrap) {
81
102
  return {
82
103
  command: 'setup',
83
104
  describe: 'Set up and manage your ServiceNow instances (interactive)',
105
+ builder: y => y.option('docs-source', { type: 'string', choices: ['snapshot', 'github', 'skip'], describe: 'Install optional documentation directly without the authentication wizard' }).epilogue(setupHelp()),
84
106
  handler: wrap(async (argv, app) => {
107
+ if (argv['docs-source']) {
108
+ const { installDocumentation } = await import('./docs/install.js');
109
+ app.ok(await installDocumentation(argv['docs-source']));
110
+ return;
111
+ }
85
112
  const { canUsePicker } = await import('../helpers.js');
86
113
  if (!app.isInteractive() || !canUsePicker(app, argv)) {
87
- throw errUsage('"jsn setup" is interactive. Use the specific commands:\n jsn auth login <instance>\n jsn auth refresh\n jsn auth status');
114
+ throw errUsageHint('"jsn setup" is interactive. Use explicit auth commands instead.', setupHelp());
88
115
  }
89
116
  const profileNames = Object.keys(app.config.profiles || {});
90
117
  if (profileNames.length === 0) {
91
118
  const result = await loginWizard(app, argv);
92
119
  await showAuthStatus({ ...argv, profile: result.profileName }, app);
120
+ const { docsDbExists } = await import('./docs/db.js');
121
+ if (!docsDbExists()) {
122
+ const { offerDocumentation } = await import('./docs/install.js');
123
+ app.ok(await offerDocumentation(app, argv));
124
+ }
93
125
  return;
94
126
  }
95
127
  const action = await authHubMenu();
96
128
  if (action === undefined) return;
129
+ if (action === 'docs') {
130
+ const { offerDocumentation } = await import('./docs/install.js');
131
+ app.ok(await offerDocumentation(app, argv));
132
+ return;
133
+ }
97
134
  const result = await dispatchSetupAction(app, argv, action);
98
135
  if (action !== 'status') await showAuthStatus({ ...argv, profile: result?.profile || result?.profileName }, app);
99
136
  }),