@jacebenson/jsn 5.5.3-dev.183.1 → 5.5.3-dev.184.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.5.3-dev.183.1",
3
+ "version": "5.5.3-dev.184.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
package/src/cli.js CHANGED
@@ -100,10 +100,11 @@ import { graphqlCmd } from './commands/graphql.js';
100
100
  *
101
101
  * Precedence is the middleware's, unchanged: the shortcut flags beat `--format`,
102
102
  * and among the shortcuts the one applied last wins
103
- * (csv > markdown > styled > quiet > json).
103
+ * (tsv > csv > markdown > styled > quiet > json).
104
104
  */
105
105
  export function resolveRequestedFormat(argv) {
106
106
  if (!argv) return '';
107
+ if (argv.tsv) return 'tsv';
107
108
  if (argv.csv) return 'csv';
108
109
  if (argv.markdown) return 'markdown';
109
110
  if (argv.styled) return 'styled';
@@ -175,8 +176,8 @@ export function buildCLI() {
175
176
  },
176
177
  format: {
177
178
  type: 'string',
178
- description: 'Output format: auto, json, markdown, styled, quiet, csv',
179
- choices: ['auto', 'json', 'markdown', 'styled', 'quiet', 'csv'],
179
+ description: 'Output format: auto, json, markdown, styled, quiet, csv, tsv',
180
+ choices: ['auto', 'json', 'markdown', 'styled', 'quiet', 'csv', 'tsv'],
180
181
  },
181
182
  json: {
182
183
  type: 'boolean',
@@ -184,7 +185,11 @@ export function buildCLI() {
184
185
  },
185
186
  csv: {
186
187
  type: 'boolean',
187
- description: 'Output in CSV format (shortcut for --format=csv)',
188
+ description: 'CSV output for compact, flat record lists',
189
+ },
190
+ tsv: {
191
+ type: 'boolean',
192
+ description: 'TSV output for compact, tab-separated record lists',
188
193
  },
189
194
  get: {
190
195
  alias: 'select',
package/src/help.js CHANGED
@@ -14,11 +14,11 @@ const COMMON_COMMANDS = [
14
14
  'incidents', 'requests', 'tasks', 'changes', 'approvals', 'knowledge', 'records', 'setup', 'discover',
15
15
  ];
16
16
 
17
- const CONCISE_FLAGS = ['instance', 'profile', 'json'];
17
+ const CONCISE_FLAGS = ['instance', 'profile', 'json', 'csv', 'tsv'];
18
18
 
19
19
  const CONCISE_EXAMPLES = [
20
20
  'jsn incidents list --query "active=true" --limit 5',
21
- 'jsn records list --table incident --limit 5 --json',
21
+ 'jsn records list --table incident --columns sys_id,number,state --limit 5 --csv',
22
22
  'jsn discover catalog',
23
23
  ];
24
24
 
@@ -242,6 +242,8 @@ function renderFlags(options, { full = false, width = helpWidth() } = {}) {
242
242
  * examples, and pointers to the exhaustive reference and local discovery.
243
243
  */
244
244
  function renderConciseHelp(config, registry, width) {
245
+ // Keep the short page consistent between ordinary 80-column terminals and pipes.
246
+ width = Math.min(width, 80);
245
247
  const commands = Array.isArray(registry?.commands) ? registry.commands : [];
246
248
  const byName = new Map(commands.map((command) => [command.name, command]));
247
249
  const common = COMMON_COMMANDS
@@ -259,9 +261,16 @@ function renderConciseHelp(config, registry, width) {
259
261
  renderGroup('COMMON COMMANDS', common, width),
260
262
  '',
261
263
  'EXAMPLES',
262
- ...CONCISE_EXAMPLES.map((example) => ` ${example}`),
264
+ ...CONCISE_EXAMPLES.map((example) => wrapText(example, { width: width - 2, firstIndent: 2 }).join(' \\\n')),
263
265
  renderFlags(Array.isArray(registry?.options) ? registry.options : [], { width }),
264
266
  '',
267
+ 'OUTPUT',
268
+ ...[
269
+ 'For smaller list responses, select fields with --columns and bound rows with --limit.',
270
+ 'CSV and TSV avoid repeated field names; JSON preserves metadata and nested structure.',
271
+ 'Use JSON for pagination metadata or both reference IDs and display labels.',
272
+ ].flatMap(text => wrapText(text, { width, firstIndent: 2 })),
273
+ '',
265
274
  'MORE',
266
275
  renderRows(CONCISE_MORE, { width, indent: 2 }),
267
276
  '',
package/src/output.js CHANGED
@@ -9,6 +9,7 @@ export const FormatMarkdown = 'markdown';
9
9
  export const FormatStyled = 'styled';
10
10
  export const FormatQuiet = 'quiet';
11
11
  export const FormatCSV = 'csv';
12
+ export const FormatTSV = 'tsv';
12
13
 
13
14
  /**
14
15
  * Resolve a dotpath (`.data[0].number`, `data.records.0.sys_id`) against an
@@ -154,6 +155,8 @@ export class OutputWriter {
154
155
  return this.writeQuiet(data);
155
156
  case FormatCSV:
156
157
  return this.writeCSV(data, opts);
158
+ case FormatTSV:
159
+ return this.writeDelimited(data, '\t', tsvEscape, 'TSV');
157
160
  case FormatStyled:
158
161
  return this.writeStyled(data, opts);
159
162
  default:
@@ -291,12 +294,16 @@ export class OutputWriter {
291
294
  }
292
295
 
293
296
  writeCSV(data, _opts) {
294
- // CSV stdout contains only the table, even on a terminal. Pagination is
297
+ return this.writeDelimited(data, ',', csvEscape, 'CSV');
298
+ }
299
+
300
+ writeDelimited(data, delimiter, escape, label) {
301
+ // Delimited stdout contains only the table, even on a terminal. Pagination is
295
302
  // diagnostic output, not an extra row that spreadsheet readers must skip.
296
303
  if (Array.isArray(data)) {
297
- this.writeCSVTable(data);
304
+ this.writeDelimitedTable(data, delimiter, escape);
298
305
  } else if (data && typeof data === 'object' && Array.isArray(data.records)) {
299
- this.writeCSVTable(data.records);
306
+ this.writeDelimitedTable(data.records, delimiter, escape);
300
307
  const page = data.pagination;
301
308
  if (page?.hasMore === true) {
302
309
  const count = data.records.length;
@@ -309,32 +316,36 @@ export class OutputWriter {
309
316
  && Number.isSafeInteger(page.limit) && page.limit > 0
310
317
  && Number.isSafeInteger(page.offset + page.limit)
311
318
  ? `; next offset ${page.offset + page.limit}` : '';
312
- this.errorWriter.write(`CSV pagination: returned ${count}${total} records${unknown}; more available${nextOffset}.\n`);
319
+ this.errorWriter.write(`${label} pagination: returned ${count}${total} records${unknown}; more available${nextOffset}.\n`);
313
320
  }
314
321
  } else {
315
- this.writeCSVTable([data]);
322
+ this.writeDelimitedTable([data], delimiter, escape);
316
323
  }
317
324
  }
318
325
 
319
326
  writeCSVTable(rows) {
327
+ return this.writeDelimitedTable(rows, ',', csvEscape);
328
+ }
329
+
330
+ writeDelimitedTable(rows, delimiter, escape) {
320
331
  if (!Array.isArray(rows) || rows.length === 0) {
321
332
  this.writer.write('');
322
333
  return;
323
334
  }
324
335
  const columns = Object.keys(rows[0]);
325
- this.writer.write(columns.map(csvEscape).join(',') + '\n');
336
+ this.writer.write(columns.map(escape).join(delimiter) + '\n');
326
337
  for (const row of rows) {
327
338
  const cells = columns.map((col) => {
328
339
  const val = row[col];
329
340
  if (val == null) return '';
330
341
  if (typeof val === 'object') {
331
- if (val.display_value != null) return csvEscape(val.display_value);
332
- if (val.value != null) return csvEscape(val.value);
333
- return csvEscape(JSON.stringify(val));
342
+ if (val.display_value != null) return escape(val.display_value);
343
+ if (val.value != null) return escape(val.value);
344
+ return escape(JSON.stringify(val));
334
345
  }
335
- return csvEscape(val);
346
+ return escape(val);
336
347
  });
337
- this.writer.write(cells.join(',') + '\n');
348
+ this.writer.write(cells.join(delimiter) + '\n');
338
349
  }
339
350
  }
340
351
 
@@ -700,6 +711,11 @@ export function getDisplayValue(val) {
700
711
  return String(val);
701
712
  }
702
713
 
714
+ // Backslash escapes keep each TSV record on one physical line, including headers.
715
+ export function tsvEscape(val) {
716
+ return String(val ?? '').replace(/[\\\t\n\r]/g, ch => ({ '\\': '\\\\', '\t': '\\t', '\n': '\\n', '\r': '\\r' })[ch]);
717
+ }
718
+
703
719
  /** Escape a single CSV field: quote when it contains a delimiter, quote, or newline. */
704
720
  export function csvEscape(val) {
705
721
  if (val == null) return '';