@optique/git 0.9.0-dev.217 → 0.9.0-dev.248

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 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;
56
+ }
49
57
  function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
50
58
  return {
51
59
  $mode: "async",
@@ -53,7 +61,7 @@ function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
53
61
  parse(input) {
54
62
  const dir = getRepoDir(options?.dir);
55
63
  (0, __optique_core_nonempty.ensureNonEmptyString)(metavar);
56
- return parseFn(dir, input);
64
+ return parseFn(dir, input, options?.errors);
57
65
  },
58
66
  format(value) {
59
67
  return value;
@@ -84,7 +92,7 @@ function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
84
92
  */
85
93
  function gitBranch(options) {
86
94
  const metavar = options?.metavar ?? METAVAR_BRANCH;
87
- return createAsyncValueParser(options, metavar, async (dir, input) => {
95
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
88
96
  try {
89
97
  const branches = await isomorphic_git.listBranches({
90
98
  fs: gitFs,
@@ -94,11 +102,19 @@ function gitBranch(options) {
94
102
  success: true,
95
103
  value: input
96
104
  };
105
+ if (errors?.notFound) return {
106
+ success: false,
107
+ error: errors.notFound(input, branches)
108
+ };
97
109
  return {
98
110
  success: false,
99
- error: __optique_core_message.message`Branch ${(0, __optique_core_message.text)(input)} does not exist. Available branches: ${branches.join(", ")}`
111
+ error: __optique_core_message.message`Branch ${(0, __optique_core_message.text)(input)} does not exist. Available branches: ${formatChoiceList(branches)}`
100
112
  };
101
113
  } catch {
114
+ if (errors?.listFailed) return {
115
+ success: false,
116
+ error: errors.listFailed(dir)
117
+ };
102
118
  return {
103
119
  success: false,
104
120
  error: __optique_core_message.message`Failed to list branches. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
@@ -138,7 +154,7 @@ function gitBranch(options) {
138
154
  */
139
155
  function gitRemoteBranch(remote, options) {
140
156
  const metavar = options?.metavar ?? METAVAR_BRANCH;
141
- return createAsyncValueParser(options, metavar, async (dir, input) => {
157
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
142
158
  try {
143
159
  const branches = await isomorphic_git.listBranches({
144
160
  fs: gitFs,
@@ -149,11 +165,19 @@ function gitRemoteBranch(remote, options) {
149
165
  success: true,
150
166
  value: input
151
167
  };
168
+ if (errors?.notFound) return {
169
+ success: false,
170
+ error: errors.notFound(input, branches)
171
+ };
152
172
  return {
153
173
  success: false,
154
- 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: ${branches.join(", ")}`
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)}`
155
175
  };
156
176
  } catch {
177
+ if (errors?.listFailed) return {
178
+ success: false,
179
+ error: errors.listFailed(dir)
180
+ };
157
181
  return {
158
182
  success: false,
159
183
  error: __optique_core_message.message`Failed to list remote branches. Ensure remote ${(0, __optique_core_message.text)(remote)} exists.`
@@ -182,7 +206,7 @@ function gitRemoteBranch(remote, options) {
182
206
  */
183
207
  function gitTag(options) {
184
208
  const metavar = options?.metavar ?? METAVAR_TAG;
185
- return createAsyncValueParser(options, metavar, async (dir, input) => {
209
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
186
210
  try {
187
211
  const tags = await isomorphic_git.listTags({
188
212
  fs: gitFs,
@@ -192,11 +216,19 @@ function gitTag(options) {
192
216
  success: true,
193
217
  value: input
194
218
  };
219
+ if (errors?.notFound) return {
220
+ success: false,
221
+ error: errors.notFound(input, tags)
222
+ };
195
223
  return {
196
224
  success: false,
197
- error: __optique_core_message.message`Tag ${(0, __optique_core_message.text)(input)} does not exist. Available tags: ${tags.join(", ")}`
225
+ error: __optique_core_message.message`Tag ${(0, __optique_core_message.text)(input)} does not exist. Available tags: ${formatChoiceList(tags)}`
198
226
  };
199
227
  } catch {
228
+ if (errors?.listFailed) return {
229
+ success: false,
230
+ error: errors.listFailed(dir)
231
+ };
200
232
  return {
201
233
  success: false,
202
234
  error: __optique_core_message.message`Failed to list tags. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
@@ -224,7 +256,7 @@ function gitTag(options) {
224
256
  */
225
257
  function gitRemote(options) {
226
258
  const metavar = options?.metavar ?? METAVAR_REMOTE;
227
- return createAsyncValueParser(options, metavar, async (dir, input) => {
259
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
228
260
  try {
229
261
  const remotes = await isomorphic_git.listRemotes({
230
262
  fs: gitFs,
@@ -235,11 +267,19 @@ function gitRemote(options) {
235
267
  success: true,
236
268
  value: input
237
269
  };
270
+ if (errors?.notFound) return {
271
+ success: false,
272
+ error: errors.notFound(input, names)
273
+ };
238
274
  return {
239
275
  success: false,
240
- error: __optique_core_message.message`Remote ${(0, __optique_core_message.text)(input)} does not exist. Available remotes: ${names.join(", ")}`
276
+ error: __optique_core_message.message`Remote ${(0, __optique_core_message.text)(input)} does not exist. Available remotes: ${formatChoiceList(names)}`
241
277
  };
242
278
  } catch {
279
+ if (errors?.listFailed) return {
280
+ success: false,
281
+ error: errors.listFailed(dir)
282
+ };
243
283
  return {
244
284
  success: false,
245
285
  error: __optique_core_message.message`Failed to list remotes. Ensure ${(0, __optique_core_message.text)(dir)} is a valid git repository.`
@@ -270,19 +310,29 @@ function gitRemote(options) {
270
310
  */
271
311
  function gitCommit(options) {
272
312
  const metavar = options?.metavar ?? "COMMIT";
273
- return createAsyncValueParser(options, metavar, async (dir, input) => {
313
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
274
314
  try {
275
315
  (0, __optique_core_nonempty.ensureNonEmptyString)(input);
276
316
  } catch {
317
+ if (errors?.invalidFormat) return {
318
+ success: false,
319
+ error: errors.invalidFormat(input)
320
+ };
277
321
  return {
278
322
  success: false,
279
323
  error: __optique_core_message.message`Invalid commit SHA: ${(0, __optique_core_message.text)(input)}`
280
324
  };
281
325
  }
282
- if (input.length < 4 || input.length > 40) return {
283
- success: false,
284
- error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} must be between 4 and 40 characters.`
285
- };
326
+ if (input.length < 4 || input.length > 40) {
327
+ if (errors?.invalidFormat) return {
328
+ success: false,
329
+ error: errors.invalidFormat(input)
330
+ };
331
+ return {
332
+ success: false,
333
+ error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} must be between 4 and 40 characters.`
334
+ };
335
+ }
286
336
  try {
287
337
  const oid = await isomorphic_git.expandOid({
288
338
  fs: gitFs,
@@ -294,6 +344,10 @@ function gitCommit(options) {
294
344
  value: oid
295
345
  };
296
346
  } catch {
347
+ if (errors?.notFound) return {
348
+ success: false,
349
+ error: errors.notFound(input)
350
+ };
297
351
  return {
298
352
  success: false,
299
353
  error: __optique_core_message.message`Commit ${(0, __optique_core_message.text)(input)} does not exist. Provide a valid commit SHA.`
@@ -313,7 +367,7 @@ function gitCommit(options) {
313
367
  */
314
368
  function gitRef(options) {
315
369
  const metavar = options?.metavar ?? "REF";
316
- return createAsyncValueParser(options, metavar, async (dir, input) => {
370
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
317
371
  try {
318
372
  const resolved = await isomorphic_git.resolveRef({
319
373
  fs: gitFs,
@@ -336,6 +390,10 @@ function gitRef(options) {
336
390
  value: oid
337
391
  };
338
392
  } catch {
393
+ if (errors?.notFound) return {
394
+ success: false,
395
+ error: errors.notFound(input)
396
+ };
339
397
  return {
340
398
  success: false,
341
399
  error: __optique_core_message.message`Reference ${(0, __optique_core_message.text)(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
package/dist/index.d.cts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { ValueParser } from "@optique/core/valueparser";
2
+ import { Message } from "@optique/core/message";
2
3
  import { NonEmptyString } from "@optique/core/nonempty";
3
4
  import { expandOid, listBranches, listRemotes, listTags, readObject, resolveRef } from "isomorphic-git";
4
5
 
@@ -20,6 +21,43 @@ interface GitParserOptions {
20
21
  * Used in help messages to indicate what kind of value this parser expects.
21
22
  */
22
23
  metavar?: NonEmptyString;
24
+ /**
25
+ * Custom error messages for validation failures.
26
+ *
27
+ * @since 0.9.0
28
+ */
29
+ errors?: GitParserErrors;
30
+ }
31
+ /**
32
+ * Custom error messages for git value parsers.
33
+ *
34
+ * @since 0.9.0
35
+ */
36
+ interface GitParserErrors {
37
+ /**
38
+ * Error message when the git reference (branch, tag, remote, commit) is not found.
39
+ *
40
+ * @param input The user-provided input that was not found.
41
+ * @param available List of available references (if applicable).
42
+ * @returns A custom error message.
43
+ */
44
+ notFound?: (input: string, available?: readonly string[]) => Message;
45
+ /**
46
+ * Error message when listing git references fails.
47
+ * This typically occurs when the directory is not a valid git repository.
48
+ *
49
+ * @param dir The directory that was being accessed.
50
+ * @returns A custom error message.
51
+ */
52
+ listFailed?: (dir: string) => Message;
53
+ /**
54
+ * Error message when the input format is invalid.
55
+ * Applies to parsers that validate format (e.g., commit SHA).
56
+ *
57
+ * @param input The user-provided input that has invalid format.
58
+ * @returns A custom error message.
59
+ */
60
+ invalidFormat?: (input: string) => Message;
23
61
  }
24
62
  /**
25
63
  * Git parsers factory interface.
@@ -152,4 +190,4 @@ declare function gitRef(options?: GitParserOptions): ValueParser<"async", string
152
190
  */
153
191
  declare function createGitParsers(options?: GitParserOptions): GitParsers;
154
192
  //#endregion
155
- export { GitParserOptions, GitParsers, createGitParsers, expandOid, gitBranch, gitCommit, gitRef, gitRemote, gitRemoteBranch, gitTag, listBranches, listRemotes, listTags, readObject, resolveRef };
193
+ export { GitParserErrors, GitParserOptions, GitParsers, createGitParsers, expandOid, gitBranch, gitCommit, gitRef, gitRemote, gitRemoteBranch, gitTag, listBranches, listRemotes, listTags, readObject, resolveRef };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Message } from "@optique/core/message";
1
2
  import { NonEmptyString } from "@optique/core/nonempty";
2
3
  import { expandOid, listBranches, listRemotes, listTags, readObject, resolveRef } from "isomorphic-git";
3
4
  import { ValueParser } from "@optique/core/valueparser";
@@ -20,6 +21,43 @@ interface GitParserOptions {
20
21
  * Used in help messages to indicate what kind of value this parser expects.
21
22
  */
22
23
  metavar?: NonEmptyString;
24
+ /**
25
+ * Custom error messages for validation failures.
26
+ *
27
+ * @since 0.9.0
28
+ */
29
+ errors?: GitParserErrors;
30
+ }
31
+ /**
32
+ * Custom error messages for git value parsers.
33
+ *
34
+ * @since 0.9.0
35
+ */
36
+ interface GitParserErrors {
37
+ /**
38
+ * Error message when the git reference (branch, tag, remote, commit) is not found.
39
+ *
40
+ * @param input The user-provided input that was not found.
41
+ * @param available List of available references (if applicable).
42
+ * @returns A custom error message.
43
+ */
44
+ notFound?: (input: string, available?: readonly string[]) => Message;
45
+ /**
46
+ * Error message when listing git references fails.
47
+ * This typically occurs when the directory is not a valid git repository.
48
+ *
49
+ * @param dir The directory that was being accessed.
50
+ * @returns A custom error message.
51
+ */
52
+ listFailed?: (dir: string) => Message;
53
+ /**
54
+ * Error message when the input format is invalid.
55
+ * Applies to parsers that validate format (e.g., commit SHA).
56
+ *
57
+ * @param input The user-provided input that has invalid format.
58
+ * @returns A custom error message.
59
+ */
60
+ invalidFormat?: (input: string) => Message;
23
61
  }
24
62
  /**
25
63
  * Git parsers factory interface.
@@ -152,4 +190,4 @@ declare function gitRef(options?: GitParserOptions): ValueParser<"async", string
152
190
  */
153
191
  declare function createGitParsers(options?: GitParserOptions): GitParsers;
154
192
  //#endregion
155
- export { GitParserOptions, GitParsers, createGitParsers, expandOid, gitBranch, gitCommit, gitRef, gitRemote, gitRemoteBranch, gitTag, listBranches, listRemotes, listTags, readObject, resolveRef };
193
+ export { GitParserErrors, GitParserOptions, GitParsers, createGitParsers, expandOid, gitBranch, gitCommit, gitRef, gitRemote, gitRemoteBranch, gitTag, listBranches, listRemotes, listTags, readObject, resolveRef };
package/dist/index.js CHANGED
@@ -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 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;
34
+ }
27
35
  function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
28
36
  return {
29
37
  $mode: "async",
@@ -31,7 +39,7 @@ function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
31
39
  parse(input) {
32
40
  const dir = getRepoDir(options?.dir);
33
41
  ensureNonEmptyString(metavar);
34
- return parseFn(dir, input);
42
+ return parseFn(dir, input, options?.errors);
35
43
  },
36
44
  format(value) {
37
45
  return value;
@@ -62,7 +70,7 @@ function createAsyncValueParser(options, metavar, parseFn, suggestFn) {
62
70
  */
63
71
  function gitBranch(options) {
64
72
  const metavar = options?.metavar ?? METAVAR_BRANCH;
65
- return createAsyncValueParser(options, metavar, async (dir, input) => {
73
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
66
74
  try {
67
75
  const branches = await git.listBranches({
68
76
  fs: gitFs,
@@ -72,11 +80,19 @@ function gitBranch(options) {
72
80
  success: true,
73
81
  value: input
74
82
  };
83
+ if (errors?.notFound) return {
84
+ success: false,
85
+ error: errors.notFound(input, branches)
86
+ };
75
87
  return {
76
88
  success: false,
77
- error: message`Branch ${text(input)} does not exist. Available branches: ${branches.join(", ")}`
89
+ error: message`Branch ${text(input)} does not exist. Available branches: ${formatChoiceList(branches)}`
78
90
  };
79
91
  } catch {
92
+ if (errors?.listFailed) return {
93
+ success: false,
94
+ error: errors.listFailed(dir)
95
+ };
80
96
  return {
81
97
  success: false,
82
98
  error: message`Failed to list branches. Ensure ${text(dir)} is a valid git repository.`
@@ -116,7 +132,7 @@ function gitBranch(options) {
116
132
  */
117
133
  function gitRemoteBranch(remote, options) {
118
134
  const metavar = options?.metavar ?? METAVAR_BRANCH;
119
- return createAsyncValueParser(options, metavar, async (dir, input) => {
135
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
120
136
  try {
121
137
  const branches = await git.listBranches({
122
138
  fs: gitFs,
@@ -127,11 +143,19 @@ function gitRemoteBranch(remote, options) {
127
143
  success: true,
128
144
  value: input
129
145
  };
146
+ if (errors?.notFound) return {
147
+ success: false,
148
+ error: errors.notFound(input, branches)
149
+ };
130
150
  return {
131
151
  success: false,
132
- error: message`Remote branch ${text(input)} does not exist on remote ${text(remote)}. Available branches: ${branches.join(", ")}`
152
+ error: message`Remote branch ${text(input)} does not exist on remote ${text(remote)}. Available branches: ${formatChoiceList(branches)}`
133
153
  };
134
154
  } catch {
155
+ if (errors?.listFailed) return {
156
+ success: false,
157
+ error: errors.listFailed(dir)
158
+ };
135
159
  return {
136
160
  success: false,
137
161
  error: message`Failed to list remote branches. Ensure remote ${text(remote)} exists.`
@@ -160,7 +184,7 @@ function gitRemoteBranch(remote, options) {
160
184
  */
161
185
  function gitTag(options) {
162
186
  const metavar = options?.metavar ?? METAVAR_TAG;
163
- return createAsyncValueParser(options, metavar, async (dir, input) => {
187
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
164
188
  try {
165
189
  const tags = await git.listTags({
166
190
  fs: gitFs,
@@ -170,11 +194,19 @@ function gitTag(options) {
170
194
  success: true,
171
195
  value: input
172
196
  };
197
+ if (errors?.notFound) return {
198
+ success: false,
199
+ error: errors.notFound(input, tags)
200
+ };
173
201
  return {
174
202
  success: false,
175
- error: message`Tag ${text(input)} does not exist. Available tags: ${tags.join(", ")}`
203
+ error: message`Tag ${text(input)} does not exist. Available tags: ${formatChoiceList(tags)}`
176
204
  };
177
205
  } catch {
206
+ if (errors?.listFailed) return {
207
+ success: false,
208
+ error: errors.listFailed(dir)
209
+ };
178
210
  return {
179
211
  success: false,
180
212
  error: message`Failed to list tags. Ensure ${text(dir)} is a valid git repository.`
@@ -202,7 +234,7 @@ function gitTag(options) {
202
234
  */
203
235
  function gitRemote(options) {
204
236
  const metavar = options?.metavar ?? METAVAR_REMOTE;
205
- return createAsyncValueParser(options, metavar, async (dir, input) => {
237
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
206
238
  try {
207
239
  const remotes = await git.listRemotes({
208
240
  fs: gitFs,
@@ -213,11 +245,19 @@ function gitRemote(options) {
213
245
  success: true,
214
246
  value: input
215
247
  };
248
+ if (errors?.notFound) return {
249
+ success: false,
250
+ error: errors.notFound(input, names)
251
+ };
216
252
  return {
217
253
  success: false,
218
- error: message`Remote ${text(input)} does not exist. Available remotes: ${names.join(", ")}`
254
+ error: message`Remote ${text(input)} does not exist. Available remotes: ${formatChoiceList(names)}`
219
255
  };
220
256
  } catch {
257
+ if (errors?.listFailed) return {
258
+ success: false,
259
+ error: errors.listFailed(dir)
260
+ };
221
261
  return {
222
262
  success: false,
223
263
  error: message`Failed to list remotes. Ensure ${text(dir)} is a valid git repository.`
@@ -248,19 +288,29 @@ function gitRemote(options) {
248
288
  */
249
289
  function gitCommit(options) {
250
290
  const metavar = options?.metavar ?? "COMMIT";
251
- return createAsyncValueParser(options, metavar, async (dir, input) => {
291
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
252
292
  try {
253
293
  ensureNonEmptyString(input);
254
294
  } catch {
295
+ if (errors?.invalidFormat) return {
296
+ success: false,
297
+ error: errors.invalidFormat(input)
298
+ };
255
299
  return {
256
300
  success: false,
257
301
  error: message`Invalid commit SHA: ${text(input)}`
258
302
  };
259
303
  }
260
- if (input.length < 4 || input.length > 40) return {
261
- success: false,
262
- error: message`Commit ${text(input)} must be between 4 and 40 characters.`
263
- };
304
+ if (input.length < 4 || input.length > 40) {
305
+ if (errors?.invalidFormat) return {
306
+ success: false,
307
+ error: errors.invalidFormat(input)
308
+ };
309
+ return {
310
+ success: false,
311
+ error: message`Commit ${text(input)} must be between 4 and 40 characters.`
312
+ };
313
+ }
264
314
  try {
265
315
  const oid = await git.expandOid({
266
316
  fs: gitFs,
@@ -272,6 +322,10 @@ function gitCommit(options) {
272
322
  value: oid
273
323
  };
274
324
  } catch {
325
+ if (errors?.notFound) return {
326
+ success: false,
327
+ error: errors.notFound(input)
328
+ };
275
329
  return {
276
330
  success: false,
277
331
  error: message`Commit ${text(input)} does not exist. Provide a valid commit SHA.`
@@ -291,7 +345,7 @@ function gitCommit(options) {
291
345
  */
292
346
  function gitRef(options) {
293
347
  const metavar = options?.metavar ?? "REF";
294
- return createAsyncValueParser(options, metavar, async (dir, input) => {
348
+ return createAsyncValueParser(options, metavar, async (dir, input, errors) => {
295
349
  try {
296
350
  const resolved = await git.resolveRef({
297
351
  fs: gitFs,
@@ -314,6 +368,10 @@ function gitRef(options) {
314
368
  value: oid
315
369
  };
316
370
  } catch {
371
+ if (errors?.notFound) return {
372
+ success: false,
373
+ error: errors.notFound(input)
374
+ };
317
375
  return {
318
376
  success: false,
319
377
  error: message`Reference ${text(input)} does not exist. Provide a valid branch, tag, or commit SHA.`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/git",
3
- "version": "0.9.0-dev.217+7fc9df46",
3
+ "version": "0.9.0-dev.248+a552207e",
4
4
  "description": "Git value parsers for Optique",
5
5
  "keywords": [
6
6
  "CLI",