Skip to content

Commit 17966d2

Browse files
author
E. Cooper
committed
Handle all yargs validation errors in isYargsError
1 parent b3b82eb commit 17966d2

File tree

1 file changed

+83
-68
lines changed

1 file changed

+83
-68
lines changed

src/lib/command-helpers.mjs

+83-68
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,24 @@
33
import { container } from "../cli.mjs";
44
import { Format } from "./formatting/colorize.mjs";
55

6+
/*
7+
* These are the error message prefixes that yargs throws during
8+
* validation. To detect these errors, you can either parse the stack
9+
* or the message. We've decided to parse the messages.
10+
*/
11+
const YARGS_STATIC_PREFIXES = [
12+
"Unknown argument:",
13+
"Unknown arguments:",
14+
"Missing required argument:",
15+
"Missing required arguments:",
16+
"Unknown command:",
17+
"Unknown commands:",
18+
"Invalid values:",
19+
"Not enough non-option arguments:",
20+
"Too many non-option arguments:",
21+
"Implications failed:",
22+
];
23+
624
const COMMON_OPTIONS = {
725
// hidden
826
accountUrl: {
@@ -78,6 +96,69 @@ const COMMON_QUERY_OPTIONS = {
7896
},
7997
};
8098

99+
// used for queries customers can configure
100+
const COMMON_CONFIGURABLE_QUERY_OPTIONS = {
101+
...COMMON_QUERY_OPTIONS,
102+
apiVersion: {
103+
description: "FQL version to use.",
104+
type: "string",
105+
alias: "v",
106+
default: "10",
107+
choices: ["4", "10"],
108+
group: "API:",
109+
},
110+
// v10 specific options
111+
format: {
112+
type: "string",
113+
alias: "f",
114+
description:
115+
"Output format for the query. When present, --json takes precedence over --format. Only applies to v10 queries.",
116+
choices: [Format.FQL, Format.JSON],
117+
default: Format.FQL,
118+
group: "API:",
119+
},
120+
typecheck: {
121+
type: "boolean",
122+
description:
123+
"Enable typechecking. Defaults to the typechecking setting of the database.",
124+
default: undefined,
125+
group: "API:",
126+
},
127+
timeout: {
128+
type: "number",
129+
description:
130+
"Maximum runtime, in milliseconds, for Fauna Core HTTP API requests made by the command.",
131+
default: 5000,
132+
group: "API:",
133+
},
134+
summary: {
135+
type: "boolean",
136+
description:
137+
"Output the summary field of the API response. Only applies to v10 queries.",
138+
default: false,
139+
group: "API:",
140+
},
141+
performanceHints: {
142+
type: "boolean",
143+
description:
144+
"Output the performance hints for the current query. Only applies to v10 queries.",
145+
default: false,
146+
group: "API:",
147+
},
148+
};
149+
150+
export function yargsWithCommonQueryOptions(yargs) {
151+
return yargsWithCommonOptions(yargs, COMMON_QUERY_OPTIONS);
152+
}
153+
154+
export function yargsWithCommonConfigurableQueryOptions(yargs) {
155+
return yargsWithCommonOptions(yargs, COMMON_CONFIGURABLE_QUERY_OPTIONS);
156+
}
157+
158+
export function yargsWithCommonOptions(yargs, options) {
159+
return yargs.options({ ...options, ...COMMON_OPTIONS });
160+
}
161+
81162
/**
82163
* An error that is thrown by commands that is not a validation error, but
83164
* a known error state that should be communicated to the user.
@@ -126,13 +207,10 @@ function isYargsError(error) {
126207
return true;
127208
}
128209

129-
// Usage errors from yargs are thrown as plain old Error. The best
130-
// you can do is check for the message.
210+
// Does the message look
131211
if (
132212
error.message &&
133-
(error.message.startsWith("Unknown argument") ||
134-
error.message.startsWith("Missing required argument") ||
135-
error.message.startsWith("Unknown command"))
213+
YARGS_STATIC_PREFIXES.some((prefix) => error.message.startsWith(prefix))
136214
) {
137215
return true;
138216
}
@@ -189,66 +267,3 @@ export const validateDatabaseOrSecret = (argv) => {
189267
}
190268
return true;
191269
};
192-
193-
// used for queries customers can configure
194-
const COMMON_CONFIGURABLE_QUERY_OPTIONS = {
195-
...COMMON_QUERY_OPTIONS,
196-
apiVersion: {
197-
description: "FQL version to use.",
198-
type: "string",
199-
alias: "v",
200-
default: "10",
201-
choices: ["4", "10"],
202-
group: "API:",
203-
},
204-
// v10 specific options
205-
format: {
206-
type: "string",
207-
alias: "f",
208-
description:
209-
"Output format for the query. When present, --json takes precedence over --format. Only applies to v10 queries.",
210-
choices: [Format.FQL, Format.JSON],
211-
default: Format.FQL,
212-
group: "API:",
213-
},
214-
typecheck: {
215-
type: "boolean",
216-
description:
217-
"Enable typechecking. Defaults to the typechecking setting of the database.",
218-
default: undefined,
219-
group: "API:",
220-
},
221-
timeout: {
222-
type: "number",
223-
description:
224-
"Maximum runtime, in milliseconds, for Fauna Core HTTP API requests made by the command.",
225-
default: 5000,
226-
group: "API:",
227-
},
228-
summary: {
229-
type: "boolean",
230-
description:
231-
"Output the summary field of the API response. Only applies to v10 queries.",
232-
default: false,
233-
group: "API:",
234-
},
235-
performanceHints: {
236-
type: "boolean",
237-
description:
238-
"Output the performance hints for the current query. Only applies to v10 queries.",
239-
default: false,
240-
group: "API:",
241-
},
242-
};
243-
244-
export function yargsWithCommonQueryOptions(yargs) {
245-
return yargsWithCommonOptions(yargs, COMMON_QUERY_OPTIONS);
246-
}
247-
248-
export function yargsWithCommonConfigurableQueryOptions(yargs) {
249-
return yargsWithCommonOptions(yargs, COMMON_CONFIGURABLE_QUERY_OPTIONS);
250-
}
251-
252-
export function yargsWithCommonOptions(yargs, options) {
253-
return yargs.options({ ...options, ...COMMON_OPTIONS });
254-
}

0 commit comments

Comments
 (0)