@trenskow/arguments-parser 0.3.141 → 0.3.143

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.
@@ -89,10 +89,50 @@ const autocomplete = () => {
89
89
  };
90
90
  }
91
91
 
92
+ if (typeof item?.type !== 'string') {
93
+ throw new Error('Autocomplete item must have a type.');
94
+ }
95
+
96
+ if (!['command', 'option', 'files'].includes(item.type)) {
97
+ throw new Error('Autocomplete item type must be one of: `command`, `option`, `files`.');
98
+ }
99
+
100
+ if (item.type !== 'files') {
101
+
102
+ if (typeof item?.argument !== 'string') {
103
+ throw new Error('Autocomplete item must have an argument.');
104
+ }
105
+
106
+ if (typeof item?.description !== 'undefined' && typeof item?.description !== 'string') {
107
+ throw new Error('Autocomplete item description must be a string.');
108
+ }
109
+
110
+ }
111
+
92
112
  return item;
93
113
 
94
114
  });
95
115
 
116
+ const resolveExternal = async (external, current) => {
117
+
118
+ let autocomplete = [{
119
+ type: 'files'
120
+ }];
121
+
122
+ if (typeof external !== 'undefined') {
123
+
124
+ autocomplete = external;
125
+
126
+ if (typeof autocomplete === 'function') {
127
+ autocomplete = await autocomplete(current);
128
+ }
129
+
130
+ }
131
+
132
+ return autocomplete;
133
+
134
+ };
135
+
96
136
  const filter = (items) => normalize(items)
97
137
  .filter((item) => {
98
138
  switch (item.type) {
@@ -203,9 +243,7 @@ const autocomplete = () => {
203
243
 
204
244
  const {
205
245
  variadic,
206
- autocomplete = () => [{
207
- type: 'files'
208
- }]
246
+ autocomplete: variadicAutocomplete
209
247
  } = options.validationOptions;
210
248
 
211
249
  if (args
@@ -273,7 +311,9 @@ const autocomplete = () => {
273
311
  }
274
312
 
275
313
  output(
276
- await schemas[autocompleteIndex].autocomplete?.(current) || []);
314
+ await resolveExternal(
315
+ schemas[autocompleteIndex].autocomplete,
316
+ current));
277
317
 
278
318
  } else {
279
319
 
@@ -281,7 +321,9 @@ const autocomplete = () => {
281
321
 
282
322
  if (variadic === 'allow') {
283
323
  items = items.concat(
284
- await autocomplete?.(current) || []);
324
+ await resolveExternal(
325
+ variadicAutocomplete,
326
+ current));
285
327
  }
286
328
 
287
329
  if (!current.startsWith('--')) {
@@ -15,9 +15,7 @@ export default () => ({
15
15
  validate: (data) => data,
16
16
  formalize: (data) => {
17
17
 
18
- if (!(Array.isArray(data) && data.every((data) => typeof data === 'string')) && typeof data !== 'function') {
19
- throw new Error('Must be a string or a function.');
20
- }
18
+ if (!Array.isArray(data)) data = [data];
21
19
 
22
20
  return data;
23
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trenskow/arguments-parser",
3
- "version": "0.3.141",
3
+ "version": "0.3.143",
4
4
  "description": "Yet another arguments parser.",
5
5
  "main": "index.js",
6
6
  "type": "module",