@optique/git 0.9.0-dev.252 → 0.9.0-dev.256

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/dist/index.cjs CHANGED
@@ -46,25 +46,25 @@ const METAVAR_REMOTE = "REMOTE";
46
46
  function getRepoDir(dirOption) {
47
47
  return dirOption ?? (typeof node_process.default !== "undefined" ? node_process.default.cwd() : ".");
48
48
  }
49
- function formatChoiceList(choices) {
50
- let result = [];
51
- for (let i = 0; i < choices.length; i++) {
52
- if (i > 0) result = [...result, ...__optique_core_message.message`, `];
53
- result = [...result, ...__optique_core_message.message`${choices[i]}`];
54
- }
55
- return result;
49
+ function hasErrorCode(error, code) {
50
+ return typeof error === "object" && error !== null && "code" in error && error.code === code;
51
+ }
52
+ function listFailureMessage(error, dir, errors, fallback) {
53
+ if (errors?.listFailed) return errors.listFailed(dir);
54
+ if (hasErrorCode(error, "NotAGitRepositoryError") || hasErrorCode(error, "NotFoundError")) return __optique_core_message.message`${(0, __optique_core_message.value)(dir)} is not a git repository.`;
55
+ return fallback;
56
56
  }
57
57
  function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
58
+ (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
58
59
  return {
59
60
  $mode: "async",
60
61
  metavar,
61
62
  parse(input) {
62
63
  const dir = getRepoDir(options?.dir);
63
- (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
64
64
  return parseFn(dir, input, options?.errors);
65
65
  },
66
- format(value) {
67
- return value;
66
+ format(value$1) {
67
+ return value$1;
68
68
  },
69
69
  async *suggest(prefix) {
70
70
  const dir = getRepoDir(options?.dir);
@@ -108,16 +108,13 @@ function gitBranch(options) {
108
108
  };
109
109
  return {
110
110
  success: false,
111
- error: __optique_core_message.message`Branch ${(0, __optique_core_message.text)(input)} does not exist. Available branches: ${formatChoiceList(branches)}`
112
- };
113
- } catch {
114
- if (errors?.listFailed) return {
115
- success: false,
116
- error: errors.listFailed(dir)
111
+ error: __optique_core_message.message`Branch ${(0, __optique_core_message.value)(input)} does not exist. Available branches: ${(0, __optique_core_message.valueSet)(branches)}`
117
112
  };
113
+ } catch (error) {
114
+ const fallback = __optique_core_message.message`Failed to list branches. Ensure ${(0, __optique_core_message.value)(dir)} is a valid git repository.`;
118
115
  return {
119
116
  success: false,
120
- error: __optique_core_message.message`Failed to list branches. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
117
+ error: listFailureMessage(error, dir, errors, fallback)
121
118
  };
122
119
  }
123
120
  }, async function* suggestBranch(dir, prefix) {
@@ -171,16 +168,13 @@ function gitRemoteBranch(remote, options) {
171
168
  };
172
169
  return {
173
170
  success: false,
174
- error: __optique_core_message.message`Remote branch ${(0, __optique_core_message.text)(input)} does not exist on remote ${(0, __optique_core_message.text)(remote)}. Available branches: ${formatChoiceList(branches)}`
175
- };
176
- } catch {
177
- if (errors?.listFailed) return {
178
- success: false,
179
- error: errors.listFailed(dir)
171
+ error: __optique_core_message.message`Remote branch ${(0, __optique_core_message.value)(input)} does not exist on remote ${(0, __optique_core_message.value)(remote)}. Available branches: ${(0, __optique_core_message.valueSet)(branches)}`
180
172
  };
173
+ } catch (error) {
174
+ const fallback = __optique_core_message.message`Failed to list remote branches. Ensure remote ${(0, __optique_core_message.value)(remote)} exists.`;
181
175
  return {
182
176
  success: false,
183
- error: __optique_core_message.message`Failed to list remote branches. Ensure remote ${(0, __optique_core_message.text)(remote)} exists.`
177
+ error: listFailureMessage(error, dir, errors, fallback)
184
178
  };
185
179
  }
186
180
  }, async function* suggestRemoteBranch(dir, prefix) {
@@ -222,16 +216,13 @@ function gitTag(options) {
222
216
  };
223
217
  return {
224
218
  success: false,
225
- error: __optique_core_message.message`Tag ${(0, __optique_core_message.text)(input)} does not exist. Available tags: ${formatChoiceList(tags)}`
226
- };
227
- } catch {
228
- if (errors?.listFailed) return {
229
- success: false,
230
- error: errors.listFailed(dir)
219
+ error: __optique_core_message.message`Tag ${(0, __optique_core_message.value)(input)} does not exist. Available tags: ${(0, __optique_core_message.valueSet)(tags)}`
231
220
  };
221
+ } catch (error) {
222
+ const fallback = __optique_core_message.message`Failed to list tags. Ensure ${(0, __optique_core_message.value)(dir)} is a valid git repository.`;
232
223
  return {
233
224
  success: false,
234
- error: __optique_core_message.message`Failed to list tags. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
225
+ error: listFailureMessage(error, dir, errors, fallback)
235
226
  };
236
227
  }
237
228
  }, async function* suggestTag(dir, prefix) {
@@ -273,16 +264,13 @@ function gitRemote(options) {
273
264
  };
274
265
  return {
275
266
  success: false,
276
- error: __optique_core_message.message`Remote ${(0, __optique_core_message.text)(input)} does not exist. Available remotes: ${formatChoiceList(names)}`
277
- };
278
- } catch {
279
- if (errors?.listFailed) return {
280
- success: false,
281
- error: errors.listFailed(dir)
267
+ error: __optique_core_message.message`Remote ${(0, __optique_core_message.value)(input)} does not exist. Available remotes: ${(0, __optique_core_message.valueSet)(names)}`
282
268
  };
269
+ } catch (error) {
270
+ const fallback = __optique_core_message.message`Failed to list remotes. Ensure ${(0, __optique_core_message.value)(dir)} is a valid git repository.`;
283
271
  return {
284
272
  success: false,
285
- error: __optique_core_message.message`Failed to list remotes. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
273
+ error: listFailureMessage(error, dir, errors, fallback)
286
274
  };
287
275
  }
288
276
  }, async function* suggestRemote(dir, prefix) {
@@ -320,7 +308,7 @@ function gitCommit(options) {
320
308
  };
321
309
  return {
322
310
  success: false,
323
- error: __optique_core_message.message`Invalid commit SHA: ${(0, __optique_core_message.text)(input)}`
311
+ error: __optique_core_message.message`Invalid commit SHA: ${(0, __optique_core_message.value)(input)}`
324
312
  };
325
313
  }
326
314
  if (input.length < 4 || input.length > 40) {
@@ -330,7 +318,7 @@ function gitCommit(options) {
330
318
  };
331
319
  return {
332
320
  success: false,
333
- error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} must be between 4 and 40 characters.`
321
+ error: __optique_core_message.message`Commit ${(0, __optique_core_message.value)(input)} must be between 4 and 40 characters.`
334
322
  };
335
323
  }
336
324
  try {
@@ -350,7 +338,7 @@ function gitCommit(options) {
350
338
  };
351
339
  return {
352
340
  success: false,
353
- error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} does not exist. Provide a valid commit SHA.`
341
+ error: __optique_core_message.message`Commit ${(0, __optique_core_message.value)(input)} does not exist. Provide a valid commit SHA.`
354
342
  };
355
343
  }
356
344
  });
@@ -396,20 +384,19 @@ function gitRef(options) {
396
384
  };
397
385
  return {
398
386
  success: false,
399
- error: __optique_core_message.message`Reference ${(0, __optique_core_message.text)(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
387
+ error: __optique_core_message.message`Reference ${(0, __optique_core_message.value)(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
400
388
  };
401
389
  }
402
390
  }
403
391
  }, async function* suggestRef(dir, prefix) {
404
392
  try {
405
- const branches = await isomorphic_git.listBranches({
393
+ const [branches, tags] = await Promise.all([isomorphic_git.listBranches({
406
394
  fs: gitFs,
407
395
  dir
408
- });
409
- const tags = await isomorphic_git.listTags({
396
+ }), isomorphic_git.listTags({
410
397
  fs: gitFs,
411
398
  dir
412
- });
399
+ })]);
413
400
  for (const branch of branches) if (branch.startsWith(prefix)) yield {
414
401
  kind: "literal",
415
402
  text: branch
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { message, text } from "@optique/core/message";
1
+ import { message, value, valueSet } from "@optique/core/message";
2
2
  import { ensureNonEmptyString } from "@optique/core/nonempty";
3
3
  import * as git from "isomorphic-git";
4
4
  import { expandOid, listBranches, listRemotes, listTags, readObject, resolveRef } from "isomorphic-git";
@@ -24,25 +24,25 @@ const METAVAR_REMOTE = "REMOTE";
24
24
  function getRepoDir(dirOption) {
25
25
  return dirOption ?? (typeof process !== "undefined" ? process.cwd() : ".");
26
26
  }
27
- function formatChoiceList(choices) {
28
- let result = [];
29
- for (let i = 0; i < choices.length; i++) {
30
- if (i > 0) result = [...result, ...message`, `];
31
- result = [...result, ...message`${choices[i]}`];
32
- }
33
- return result;
27
+ function hasErrorCode(error, code) {
28
+ return typeof error === "object" && error !== null && "code" in error && error.code === code;
29
+ }
30
+ function listFailureMessage(error, dir, errors, fallback) {
31
+ if (errors?.listFailed) return errors.listFailed(dir);
32
+ if (hasErrorCode(error, "NotAGitRepositoryError") || hasErrorCode(error, "NotFoundError")) return message`${value(dir)} is not a git repository.`;
33
+ return fallback;
34
34
  }
35
35
  function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
36
+ ensureNonEmptyString(metavar);
36
37
  return {
37
38
  $mode: "async",
38
39
  metavar,
39
40
  parse(input) {
40
41
  const dir = getRepoDir(options?.dir);
41
- ensureNonEmptyString(metavar);
42
42
  return parseFn(dir, input, options?.errors);
43
43
  },
44
- format(value) {
45
- return value;
44
+ format(value$1) {
45
+ return value$1;
46
46
  },
47
47
  async *suggest(prefix) {
48
48
  const dir = getRepoDir(options?.dir);
@@ -86,16 +86,13 @@ function gitBranch(options) {
86
86
  };
87
87
  return {
88
88
  success: false,
89
- error: message`Branch ${text(input)} does not exist. Available branches: ${formatChoiceList(branches)}`
90
- };
91
- } catch {
92
- if (errors?.listFailed) return {
93
- success: false,
94
- error: errors.listFailed(dir)
89
+ error: message`Branch ${value(input)} does not exist. Available branches: ${valueSet(branches)}`
95
90
  };
91
+ } catch (error) {
92
+ const fallback = message`Failed to list branches. Ensure ${value(dir)} is a valid git repository.`;
96
93
  return {
97
94
  success: false,
98
- error: message`Failed to list branches. Ensure ${text(dir)} is a valid git repository.`
95
+ error: listFailureMessage(error, dir, errors, fallback)
99
96
  };
100
97
  }
101
98
  }, async function* suggestBranch(dir, prefix) {
@@ -149,16 +146,13 @@ function gitRemoteBranch(remote, options) {
149
146
  };
150
147
  return {
151
148
  success: false,
152
- error: message`Remote branch ${text(input)} does not exist on remote ${text(remote)}. Available branches: ${formatChoiceList(branches)}`
153
- };
154
- } catch {
155
- if (errors?.listFailed) return {
156
- success: false,
157
- error: errors.listFailed(dir)
149
+ error: message`Remote branch ${value(input)} does not exist on remote ${value(remote)}. Available branches: ${valueSet(branches)}`
158
150
  };
151
+ } catch (error) {
152
+ const fallback = message`Failed to list remote branches. Ensure remote ${value(remote)} exists.`;
159
153
  return {
160
154
  success: false,
161
- error: message`Failed to list remote branches. Ensure remote ${text(remote)} exists.`
155
+ error: listFailureMessage(error, dir, errors, fallback)
162
156
  };
163
157
  }
164
158
  }, async function* suggestRemoteBranch(dir, prefix) {
@@ -200,16 +194,13 @@ function gitTag(options) {
200
194
  };
201
195
  return {
202
196
  success: false,
203
- error: message`Tag ${text(input)} does not exist. Available tags: ${formatChoiceList(tags)}`
204
- };
205
- } catch {
206
- if (errors?.listFailed) return {
207
- success: false,
208
- error: errors.listFailed(dir)
197
+ error: message`Tag ${value(input)} does not exist. Available tags: ${valueSet(tags)}`
209
198
  };
199
+ } catch (error) {
200
+ const fallback = message`Failed to list tags. Ensure ${value(dir)} is a valid git repository.`;
210
201
  return {
211
202
  success: false,
212
- error: message`Failed to list tags. Ensure ${text(dir)} is a valid git repository.`
203
+ error: listFailureMessage(error, dir, errors, fallback)
213
204
  };
214
205
  }
215
206
  }, async function* suggestTag(dir, prefix) {
@@ -251,16 +242,13 @@ function gitRemote(options) {
251
242
  };
252
243
  return {
253
244
  success: false,
254
- error: message`Remote ${text(input)} does not exist. Available remotes: ${formatChoiceList(names)}`
255
- };
256
- } catch {
257
- if (errors?.listFailed) return {
258
- success: false,
259
- error: errors.listFailed(dir)
245
+ error: message`Remote ${value(input)} does not exist. Available remotes: ${valueSet(names)}`
260
246
  };
247
+ } catch (error) {
248
+ const fallback = message`Failed to list remotes. Ensure ${value(dir)} is a valid git repository.`;
261
249
  return {
262
250
  success: false,
263
- error: message`Failed to list remotes. Ensure ${text(dir)} is a valid git repository.`
251
+ error: listFailureMessage(error, dir, errors, fallback)
264
252
  };
265
253
  }
266
254
  }, async function* suggestRemote(dir, prefix) {
@@ -298,7 +286,7 @@ function gitCommit(options) {
298
286
  };
299
287
  return {
300
288
  success: false,
301
- error: message`Invalid commit SHA: ${text(input)}`
289
+ error: message`Invalid commit SHA: ${value(input)}`
302
290
  };
303
291
  }
304
292
  if (input.length < 4 || input.length > 40) {
@@ -308,7 +296,7 @@ function gitCommit(options) {
308
296
  };
309
297
  return {
310
298
  success: false,
311
- error: message`Commit ${text(input)} must be between 4 and 40 characters.`
299
+ error: message`Commit ${value(input)} must be between 4 and 40 characters.`
312
300
  };
313
301
  }
314
302
  try {
@@ -328,7 +316,7 @@ function gitCommit(options) {
328
316
  };
329
317
  return {
330
318
  success: false,
331
- error: message`Commit ${text(input)} does not exist. Provide a valid commit SHA.`
319
+ error: message`Commit ${value(input)} does not exist. Provide a valid commit SHA.`
332
320
  };
333
321
  }
334
322
  });
@@ -374,20 +362,19 @@ function gitRef(options) {
374
362
  };
375
363
  return {
376
364
  success: false,
377
- error: message`Reference ${text(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
365
+ error: message`Reference ${value(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
378
366
  };
379
367
  }
380
368
  }
381
369
  }, async function* suggestRef(dir, prefix) {
382
370
  try {
383
- const branches = await git.listBranches({
371
+ const [branches, tags] = await Promise.all([git.listBranches({
384
372
  fs: gitFs,
385
373
  dir
386
- });
387
- const tags = await git.listTags({
374
+ }), git.listTags({
388
375
  fs: gitFs,
389
376
  dir
390
- });
377
+ })]);
391
378
  for (const branch of branches) if (branch.startsWith(prefix)) yield {
392
379
  kind: "literal",
393
380
  text: branch
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/git",
3
- "version": "0.9.0-dev.252+3e7b00d9",
3
+ "version": "0.9.0-dev.256+2840ea5d",
4
4
  "description": "Git value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",