2
2
// Thanks for his awesome work!
3
3
import { camelCase } from "@clerc/utils" ;
4
4
5
+ import type { TranslateFn } from "./types" ;
6
+
5
7
const { stringify } = JSON ;
6
8
7
9
interface ParsedParameter {
@@ -10,15 +12,15 @@ interface ParsedParameter {
10
12
spread : boolean
11
13
}
12
14
13
- export function parseParameters ( parameters : string [ ] ) {
15
+ export function parseParameters ( parameters : string [ ] , t : TranslateFn ) {
14
16
const parsedParameters : ParsedParameter [ ] = [ ] ;
15
17
16
18
let hasOptional : string | undefined ;
17
19
let hasSpread : string | undefined ;
18
20
19
21
for ( const parameter of parameters ) {
20
22
if ( hasSpread ) {
21
- throw new Error ( `Invalid parameter: Spread parameter ${ stringify ( hasSpread ) } must be last` ) ;
23
+ throw new Error ( t ( "core.spreadParameterMustBeLast" , stringify ( hasSpread ) ) ) ;
22
24
}
23
25
24
26
const firstCharacter = parameter [ 0 ] ;
@@ -29,7 +31,7 @@ export function parseParameters(parameters: string[]) {
29
31
required = true ;
30
32
31
33
if ( hasOptional ) {
32
- throw new Error ( `Invalid parameter: Required parameter ${ stringify ( parameter ) } cannot come after optional parameter ${ stringify ( hasOptional ) } ` ) ;
34
+ throw new Error ( t ( "core.requiredParameterMustBeBeforeOptional" , stringify ( parameter ) , stringify ( hasOptional ) ) ) ;
33
35
}
34
36
}
35
37
@@ -39,7 +41,7 @@ export function parseParameters(parameters: string[]) {
39
41
}
40
42
41
43
if ( required === undefined ) {
42
- throw new Error ( `Invalid parameter: ${ stringify ( parameter ) } . Must be wrapped in <> (required parameter) or [] (optional parameter)` ) ;
44
+ throw new Error ( t ( "core.parameterMustBeWrappedInBrackets" , stringify ( parameter ) ) ) ;
43
45
}
44
46
45
47
let name = parameter . slice ( 1 , - 1 ) ;
@@ -65,12 +67,13 @@ export function mapParametersToArguments(
65
67
mapping : Record < string , string | string [ ] > ,
66
68
parameters : ParsedParameter [ ] ,
67
69
cliArguments : string [ ] ,
70
+ t : TranslateFn ,
68
71
) {
69
72
for ( let i = 0 ; i < parameters . length ; i += 1 ) {
70
73
const { name, required, spread } = parameters [ i ] ;
71
74
const camelCaseName = camelCase ( name ) ;
72
75
if ( camelCaseName in mapping ) {
73
- throw new Error ( `Invalid parameter: ${ stringify ( name ) } is used more than once.` ) ;
76
+ throw new Error ( t ( "core.parameterIsUsedMoreThanOnce" , stringify ( name ) ) ) ;
74
77
}
75
78
76
79
const value = spread ? cliArguments . slice ( i ) : cliArguments [ i ] ;
@@ -83,7 +86,7 @@ export function mapParametersToArguments(
83
86
required
84
87
&& ( ! value || ( spread && value . length === 0 ) )
85
88
) {
86
- throw new Error ( `Missing required parameter ${ stringify ( name ) } ` ) ;
89
+ throw new Error ( t ( "core.missingRequiredParameter" , stringify ( name ) ) ) ;
87
90
}
88
91
89
92
mapping [ camelCaseName ] = value ;
0 commit comments