@atlaskit/ads-cli 0.5.0 → 0.7.0

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.
@@ -28,6 +28,7 @@ import { formatToken } from '../output/format-token';
28
28
  import { parseLintRuleRecords } from '../output/parse-lint-rule-records';
29
29
  import { buildManifest } from './build-manifest';
30
30
  import { getVersion } from './get-version';
31
+ import { normalizeBatchRequest } from './normalize-batch-request';
31
32
  /**
32
33
  * Shared `--limit` flag definition reused across the search-style commands.
33
34
  */
@@ -445,6 +446,65 @@ const resolveUnifiedSearch = input => {
445
446
  };
446
447
  };
447
448
 
449
+ /**
450
+ * Split a batch invocation into argv groups introduced by repeated `--command` flags. Canonical
451
+ * tokenized groups remain unchanged; a complete request in the first token is safely normalized
452
+ * to argv without evaluating shell strings.
453
+ */
454
+ const resolveBatch = input => {
455
+ var _input$rawArgs;
456
+ const rawArgs = (_input$rawArgs = input.rawArgs) !== null && _input$rawArgs !== void 0 ? _input$rawArgs : [];
457
+ const batchIndex = rawArgs.indexOf('batch');
458
+ const tokens = rawArgs.slice(batchIndex + 1).filter(token => token !== '--json');
459
+ if (tokens.length === 0) {
460
+ return {
461
+ error: 'At least one command request is required.'
462
+ };
463
+ }
464
+ if (tokens[0] !== '--command') {
465
+ return {
466
+ error: 'Each batch request must start with `--command`.'
467
+ };
468
+ }
469
+ const requests = [];
470
+ // The leading `--command` is validated above, so every remaining token either closes the open
471
+ // request or appends to it. Seeding the accumulator here keeps it non-nullable, which is what
472
+ // lets the loop stay free of "no request open yet" checks.
473
+ let current = [];
474
+ for (const token of tokens.slice(1)) {
475
+ if (token === '--command') {
476
+ if (current.length === 0) {
477
+ return {
478
+ error: '`--command` must be followed by a non-empty command request.'
479
+ };
480
+ }
481
+ const normalized = normalizeBatchRequest(current);
482
+ if ('error' in normalized) {
483
+ return normalized;
484
+ }
485
+ requests.push(normalized);
486
+ current = [];
487
+ continue;
488
+ }
489
+ current.push(token);
490
+ }
491
+ if (current.length === 0) {
492
+ return {
493
+ error: '`--command` must be followed by a non-empty command request.'
494
+ };
495
+ }
496
+ const normalized = normalizeBatchRequest(current);
497
+ if ('error' in normalized) {
498
+ return normalized;
499
+ }
500
+ requests.push(normalized);
501
+ return {
502
+ tools: [],
503
+ batch: requests,
504
+ meta: {}
505
+ };
506
+ };
507
+
448
508
  /**
449
509
  * The full set of CLI commands. Order here is the order shown by `manifest` and `--help`.
450
510
  */
@@ -505,6 +565,20 @@ export const commands = [{
505
565
  tool: searchKindTools[searchType]
506
566
  });
507
567
  }
568
+ }, {
569
+ name: 'batch',
570
+ description: 'Run multiple ADS CLI queries in one process. Tokenized child requests are canonical; complete-request strings are also accepted.',
571
+ usage: 'batch [--json] --command <command> [args...] [--command <command> [args...]]...',
572
+ arguments: [],
573
+ flags: [{
574
+ name: 'command',
575
+ type: 'string[]',
576
+ description: 'Canonical: a tokenized child command and its arguments. A complete quoted request is also accepted. Repeat once per child command.'
577
+ }],
578
+ examples: ['batch --command search button --type icon --command token space.200', 'batch --command "search button" --type icon --command "token space.200"', 'batch --json --command component Button --command docs spacing'],
579
+ responseTypes: ['ads-cli/batch'],
580
+ envelopeType: () => 'batch',
581
+ resolve: resolveBatch
508
582
  }, makeItemCommand({
509
583
  kind: 'components',
510
584
  name: 'component',
@@ -0,0 +1,142 @@
1
+ /**
2
+ * Normalise one batch child request without involving a shell.
3
+ *
4
+ * The canonical form is already-tokenized argv, for example
5
+ * `['search', 'Grid', '--type', 'component']`. For compatibility with agent-generated commands,
6
+ * the first token may instead contain a complete request, for example `['search Grid', '--type',
7
+ * 'component']`. Only that first token is split; trailing tokens have already been tokenized by the
8
+ * caller and must remain unchanged so multi-word arguments keep their boundaries.
9
+ */
10
+
11
+ const shellSyntaxError = syntax => ({
12
+ error: `Shell syntax ${syntax} is not supported in batch requests. Pass child commands as argv only; pipes, redirects, expansion, and command substitution are not allowed.`
13
+ });
14
+
15
+ /**
16
+ * Reject syntax that would have special meaning to a shell even though this parser never invokes
17
+ * one. Failing closed gives callers an actionable error instead of accepting input that only looks
18
+ * safe because it currently remains an argv array.
19
+ */
20
+ const findShellSyntax = value => {
21
+ var _value$match;
22
+ if (value.includes('$(')) {
23
+ return 'command substitution (`$(...)`)';
24
+ }
25
+ if (value.includes('`')) {
26
+ return 'command substitution (backticks)';
27
+ }
28
+ if (value.includes('$')) {
29
+ return 'environment expansion (`$`)';
30
+ }
31
+ if (/\r|\n/.test(value)) {
32
+ return 'a command separator (line break)';
33
+ }
34
+ if (value.includes('&&')) {
35
+ return 'the `&&` operator';
36
+ }
37
+ if (value.includes('||')) {
38
+ return 'the `||` operator';
39
+ }
40
+ const operator = (_value$match = value.match(/[|&;<>()]/)) === null || _value$match === void 0 ? void 0 : _value$match[0];
41
+ return operator ? `the \`${operator}\` operator` : null;
42
+ };
43
+
44
+ /**
45
+ * Split a complete child request into argv using quoting and escaping rules only. This is
46
+ * intentionally not a shell grammar: it performs no expansion, substitution, redirection, or
47
+ * operator evaluation.
48
+ */
49
+ const tokenizeCompleteRequest = value => {
50
+ const argv = [];
51
+ let current = '';
52
+ let quote = null;
53
+ let escaped = false;
54
+ let tokenStarted = false;
55
+ for (const character of value) {
56
+ if (escaped) {
57
+ current += character;
58
+ escaped = false;
59
+ tokenStarted = true;
60
+ continue;
61
+ }
62
+ if (character === '\\') {
63
+ escaped = true;
64
+ tokenStarted = true;
65
+ continue;
66
+ }
67
+ if (quote !== null) {
68
+ if (character === quote) {
69
+ quote = null;
70
+ } else {
71
+ current += character;
72
+ }
73
+ continue;
74
+ }
75
+ if (character === "'" || character === '"') {
76
+ quote = character;
77
+ tokenStarted = true;
78
+ continue;
79
+ }
80
+ if (/\s/.test(character)) {
81
+ if (tokenStarted) {
82
+ argv.push(current);
83
+ current = '';
84
+ tokenStarted = false;
85
+ }
86
+ continue;
87
+ }
88
+ current += character;
89
+ tokenStarted = true;
90
+ }
91
+ if (escaped) {
92
+ return {
93
+ error: 'Invalid complete batch request: it ends with an escape character.'
94
+ };
95
+ }
96
+ if (quote !== null) {
97
+ return {
98
+ error: `Invalid complete batch request: unterminated ${quote} quote.`
99
+ };
100
+ }
101
+ if (tokenStarted) {
102
+ argv.push(current);
103
+ }
104
+ if (argv.length === 0) {
105
+ return {
106
+ error: 'Invalid complete batch request: a command name is required.'
107
+ };
108
+ }
109
+ return {
110
+ argv
111
+ };
112
+ };
113
+ export const normalizeBatchRequest = argv => {
114
+ for (const token of argv) {
115
+ const syntax = findShellSyntax(token);
116
+ if (syntax !== null) {
117
+ return shellSyntaxError(syntax);
118
+ }
119
+ }
120
+ const [first, ...trailing] = argv;
121
+ if (first === undefined) {
122
+ return {
123
+ error: '`--command` must be followed by a non-empty command request.'
124
+ };
125
+ }
126
+
127
+ // A normal command name cannot contain whitespace, quotes, or escapes. Their presence in the
128
+ // first argv entry therefore identifies the complete-string compatibility form. Tokenized
129
+ // trailing entries are never re-split.
130
+ if (!/[\s'"\\]/.test(first)) {
131
+ return {
132
+ argv: [...argv]
133
+ };
134
+ }
135
+ const normalized = tokenizeCompleteRequest(first);
136
+ if ('error' in normalized) {
137
+ return normalized;
138
+ }
139
+ return {
140
+ argv: [...normalized.argv, ...trailing]
141
+ };
142
+ };
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Generic last-resort human formatting for a successful command result.
3
+ */
4
+
5
+ import chalk from 'chalk';
6
+
7
+ /**
8
+ * Render a successful result with the generic last-resort formatting, used when a command
9
+ * declares neither a compact result kind nor its own `formatHuman`.
10
+ *
11
+ * Structured data is pretty-printed as JSON (the underlying tools already return rich objects),
12
+ * while plain-string payloads such as guideline markdown are returned verbatim.
13
+ *
14
+ * Kept separate from the writer so callers that compose text rather than write it — notably
15
+ * `batch`, which concatenates one section per child — produce byte-identical output without
16
+ * duplicating these rules.
17
+ */
18
+ export const formatHumanResult = ({
19
+ data
20
+ }) => {
21
+ if (typeof data === 'string') {
22
+ return data;
23
+ }
24
+ if (data === null || data === undefined) {
25
+ return chalk.yellow('No results.');
26
+ }
27
+ return JSON.stringify(data, null, 2);
28
+ };
@@ -2,24 +2,23 @@
2
2
  * Human-readable rendering of a successful command result.
3
3
  */
4
4
 
5
- import chalk from 'chalk';
5
+ import { formatHumanResult } from './format-human-result';
6
6
  /**
7
7
  * Emit a human-readable rendering of a successful result to stdout.
8
8
  *
9
- * Structured data is pretty-printed as JSON (the underlying tools already return rich
10
- * objects), while plain-string payloads such as guideline markdown are printed verbatim.
9
+ * An absent payload is a diagnostic rather than data, so it goes to stderr to keep stdout clean
10
+ * for piping.
11
11
  */
12
12
  export const writeHumanResult = ({
13
13
  data,
14
14
  writer
15
15
  }) => {
16
- if (typeof data === 'string') {
17
- writer.out(data);
18
- return;
19
- }
16
+ const text = formatHumanResult({
17
+ data
18
+ });
20
19
  if (data === null || data === undefined) {
21
- writer.err(chalk.yellow('No results.'));
20
+ writer.err(text);
22
21
  return;
23
22
  }
24
- writer.out(JSON.stringify(data, null, 2));
23
+ writer.out(text);
25
24
  };