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

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,6 +46,14 @@ 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 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
+ }
49
57
  function formatChoiceList(choices) {
50
58
  let result = [];
51
59
  for (let i = 0; i < choices.length; i++) {
@@ -55,16 +63,16 @@ function formatChoiceList(choices) {
55
63
  return result;
56
64
  }
57
65
  function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
66
+ (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
58
67
  return {
59
68
  $mode: "async",
60
69
  metavar,
61
70
  parse(input) {
62
71
  const dir = getRepoDir(options?.dir);
63
- (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
64
72
  return parseFn(dir, input, options?.errors);
65
73
  },
66
- format(value) {
67
- return value;
74
+ format(value$1) {
75
+ return value$1;
68
76
  },
69
77
  async *suggest(prefix) {
70
78
  const dir = getRepoDir(options?.dir);
@@ -108,16 +116,13 @@ function gitBranch(options) {
108
116
  };
109
117
  return {
110
118
  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)
119
+ error: __optique_core_message.message`Branch ${(0, __optique_core_message.value)(input)} does not exist. Available branches: ${formatChoiceList(branches)}`
117
120
  };
121
+ } catch (error) {
122
+ const fallback = __optique_core_message.message`Failed to list branches. Ensure ${(0, __optique_core_message.value)(dir)} is a valid git repository.`;
118
123
  return {
119
124
  success: false,
120
- error: __optique_core_message.message`Failed to list branches. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
125
+ error: listFailureMessage(error, dir, errors, fallback)
121
126
  };
122
127
  }
123
128
  }, async function* suggestBranch(dir, prefix) {
@@ -171,16 +176,13 @@ function gitRemoteBranch(remote, options) {
171
176
  };
172
177
  return {
173
178
  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)
179
+ 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: ${formatChoiceList(branches)}`
180
180
  };
181
+ } catch (error) {
182
+ const fallback = __optique_core_message.message`Failed to list remote branches. Ensure remote ${(0, __optique_core_message.value)(remote)} exists.`;
181
183
  return {
182
184
  success: false,
183
- error: __optique_core_message.message`Failed to list remote branches. Ensure remote ${(0, __optique_core_message.text)(remote)} exists.`
185
+ error: listFailureMessage(error, dir, errors, fallback)
184
186
  };
185
187
  }
186
188
  }, async function* suggestRemoteBranch(dir, prefix) {
@@ -222,16 +224,13 @@ function gitTag(options) {
222
224
  };
223
225
  return {
224
226
  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)
227
+ error: __optique_core_message.message`Tag ${(0, __optique_core_message.value)(input)} does not exist. Available tags: ${formatChoiceList(tags)}`
231
228
  };
229
+ } catch (error) {
230
+ const fallback = __optique_core_message.message`Failed to list tags. Ensure ${(0, __optique_core_message.value)(dir)} is a valid git repository.`;
232
231
  return {
233
232
  success: false,
234
- error: __optique_core_message.message`Failed to list tags. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
233
+ error: listFailureMessage(error, dir, errors, fallback)
235
234
  };
236
235
  }
237
236
  }, async function* suggestTag(dir, prefix) {
@@ -273,16 +272,13 @@ function gitRemote(options) {
273
272
  };
274
273
  return {
275
274
  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)
275
+ error: __optique_core_message.message`Remote ${(0, __optique_core_message.value)(input)} does not exist. Available remotes: ${formatChoiceList(names)}`
282
276
  };
277
+ } catch (error) {
278
+ const fallback = __optique_core_message.message`Failed to list remotes. Ensure ${(0, __optique_core_message.value)(dir)} is a valid git repository.`;
283
279
  return {
284
280
  success: false,
285
- error: __optique_core_message.message`Failed to list remotes. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
281
+ error: listFailureMessage(error, dir, errors, fallback)
286
282
  };
287
283
  }
288
284
  }, async function* suggestRemote(dir, prefix) {
@@ -320,7 +316,7 @@ function gitCommit(options) {
320
316
  };
321
317
  return {
322
318
  success: false,
323
- error: __optique_core_message.message`Invalid commit SHA: ${(0, __optique_core_message.text)(input)}`
319
+ error: __optique_core_message.message`Invalid commit SHA: ${(0, __optique_core_message.value)(input)}`
324
320
  };
325
321
  }
326
322
  if (input.length < 4 || input.length > 40) {
@@ -330,7 +326,7 @@ function gitCommit(options) {
330
326
  };
331
327
  return {
332
328
  success: false,
333
- error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} must be between 4 and 40 characters.`
329
+ error: __optique_core_message.message`Commit ${(0, __optique_core_message.value)(input)} must be between 4 and 40 characters.`
334
330
  };
335
331
  }
336
332
  try {
@@ -350,7 +346,7 @@ function gitCommit(options) {
350
346
  };
351
347
  return {
352
348
  success: false,
353
- error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} does not exist. Provide a valid commit SHA.`
349
+ error: __optique_core_message.message`Commit ${(0, __optique_core_message.value)(input)} does not exist. Provide a valid commit SHA.`
354
350
  };
355
351
  }
356
352
  });
@@ -396,20 +392,19 @@ function gitRef(options) {
396
392
  };
397
393
  return {
398
394
  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.`
395
+ error: __optique_core_message.message`Reference ${(0, __optique_core_message.value)(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
400
396
  };
401
397
  }
402
398
  }
403
399
  }, async function* suggestRef(dir, prefix) {
404
400
  try {
405
- const branches = await isomorphic_git.listBranches({
401
+ const [branches, tags] = await Promise.all([isomorphic_git.listBranches({
406
402
  fs: gitFs,
407
403
  dir
408
- });
409
- const tags = await isomorphic_git.listTags({
404
+ }), isomorphic_git.listTags({
410
405
  fs: gitFs,
411
406
  dir
412
- });
407
+ })]);
413
408
  for (const branch of branches) if (branch.startsWith(prefix)) yield {
414
409
  kind: "literal",
415
410
  text: branch
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { message, text } from "@optique/core/message";
1
+ import { message, value } 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,6 +24,14 @@ const METAVAR_REMOTE = "REMOTE";
24
24
  function getRepoDir(dirOption) {
25
25
  return dirOption ?? (typeof process !== "undefined" ? process.cwd() : ".");
26
26
  }
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
+ }
27
35
  function formatChoiceList(choices) {
28
36
  let result = [];
29
37
  for (let i = 0; i < choices.length; i++) {
@@ -33,16 +41,16 @@ function formatChoiceList(choices) {
33
41
  return result;
34
42
  }
35
43
  function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
44
+ ensureNonEmptyString(metavar);
36
45
  return {
37
46
  $mode: "async",
38
47
  metavar,
39
48
  parse(input) {
40
49
  const dir = getRepoDir(options?.dir);
41
- ensureNonEmptyString(metavar);
42
50
  return parseFn(dir, input, options?.errors);
43
51
  },
44
- format(value) {
45
- return value;
52
+ format(value$1) {
53
+ return value$1;
46
54
  },
47
55
  async *suggest(prefix) {
48
56
  const dir = getRepoDir(options?.dir);
@@ -86,16 +94,13 @@ function gitBranch(options) {
86
94
  };
87
95
  return {
88
96
  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)
97
+ error: message`Branch ${value(input)} does not exist. Available branches: ${formatChoiceList(branches)}`
95
98
  };
99
+ } catch (error) {
100
+ const fallback = message`Failed to list branches. Ensure ${value(dir)} is a valid git repository.`;
96
101
  return {
97
102
  success: false,
98
- error: message`Failed to list branches. Ensure ${text(dir)} is a valid git repository.`
103
+ error: listFailureMessage(error, dir, errors, fallback)
99
104
  };
100
105
  }
101
106
  }, async function* suggestBranch(dir, prefix) {
@@ -149,16 +154,13 @@ function gitRemoteBranch(remote, options) {
149
154
  };
150
155
  return {
151
156
  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)
157
+ error: message`Remote branch ${value(input)} does not exist on remote ${value(remote)}. Available branches: ${formatChoiceList(branches)}`
158
158
  };
159
+ } catch (error) {
160
+ const fallback = message`Failed to list remote branches. Ensure remote ${value(remote)} exists.`;
159
161
  return {
160
162
  success: false,
161
- error: message`Failed to list remote branches. Ensure remote ${text(remote)} exists.`
163
+ error: listFailureMessage(error, dir, errors, fallback)
162
164
  };
163
165
  }
164
166
  }, async function* suggestRemoteBranch(dir, prefix) {
@@ -200,16 +202,13 @@ function gitTag(options) {
200
202
  };
201
203
  return {
202
204
  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)
205
+ error: message`Tag ${value(input)} does not exist. Available tags: ${formatChoiceList(tags)}`
209
206
  };
207
+ } catch (error) {
208
+ const fallback = message`Failed to list tags. Ensure ${value(dir)} is a valid git repository.`;
210
209
  return {
211
210
  success: false,
212
- error: message`Failed to list tags. Ensure ${text(dir)} is a valid git repository.`
211
+ error: listFailureMessage(error, dir, errors, fallback)
213
212
  };
214
213
  }
215
214
  }, async function* suggestTag(dir, prefix) {
@@ -251,16 +250,13 @@ function gitRemote(options) {
251
250
  };
252
251
  return {
253
252
  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)
253
+ error: message`Remote ${value(input)} does not exist. Available remotes: ${formatChoiceList(names)}`
260
254
  };
255
+ } catch (error) {
256
+ const fallback = message`Failed to list remotes. Ensure ${value(dir)} is a valid git repository.`;
261
257
  return {
262
258
  success: false,
263
- error: message`Failed to list remotes. Ensure ${text(dir)} is a valid git repository.`
259
+ error: listFailureMessage(error, dir, errors, fallback)
264
260
  };
265
261
  }
266
262
  }, async function* suggestRemote(dir, prefix) {
@@ -298,7 +294,7 @@ function gitCommit(options) {
298
294
  };
299
295
  return {
300
296
  success: false,
301
- error: message`Invalid commit SHA: ${text(input)}`
297
+ error: message`Invalid commit SHA: ${value(input)}`
302
298
  };
303
299
  }
304
300
  if (input.length < 4 || input.length > 40) {
@@ -308,7 +304,7 @@ function gitCommit(options) {
308
304
  };
309
305
  return {
310
306
  success: false,
311
- error: message`Commit ${text(input)} must be between 4 and 40 characters.`
307
+ error: message`Commit ${value(input)} must be between 4 and 40 characters.`
312
308
  };
313
309
  }
314
310
  try {
@@ -328,7 +324,7 @@ function gitCommit(options) {
328
324
  };
329
325
  return {
330
326
  success: false,
331
- error: message`Commit ${text(input)} does not exist. Provide a valid commit SHA.`
327
+ error: message`Commit ${value(input)} does not exist. Provide a valid commit SHA.`
332
328
  };
333
329
  }
334
330
  });
@@ -374,20 +370,19 @@ function gitRef(options) {
374
370
  };
375
371
  return {
376
372
  success: false,
377
- error: message`Reference ${text(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
373
+ error: message`Reference ${value(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
378
374
  };
379
375
  }
380
376
  }
381
377
  }, async function* suggestRef(dir, prefix) {
382
378
  try {
383
- const branches = await git.listBranches({
379
+ const [branches, tags] = await Promise.all([git.listBranches({
384
380
  fs: gitFs,
385
381
  dir
386
- });
387
- const tags = await git.listTags({
382
+ }), git.listTags({
388
383
  fs: gitFs,
389
384
  dir
390
- });
385
+ })]);
391
386
  for (const branch of branches) if (branch.startsWith(prefix)) yield {
392
387
  kind: "literal",
393
388
  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.254+390a74da",
4
4
  "description": "Git value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",