Skip to content

Commit 5cf92e8

Browse files
committed
chore: separate mongosh logic, process and validate args separately
1 parent dac9f72 commit 5cf92e8

File tree

13 files changed

+947
-798
lines changed

13 files changed

+947
-798
lines changed

package-lock.json

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/arg-parser/.depcheckrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,7 @@ ignores:
1010
- eslint-config-mongodb-js
1111
# needed as a peer dependency of @mongodb-js/devtools-connect
1212
- mongodb
13+
# only used in arg-parser export; should be removed once switched to knip
14+
- yargs-parser
1315
ignore-patterns:
1416
- .eslintrc.js
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import z from 'zod/v4';
2+
3+
/**
4+
* Registry for argument options metadata
5+
*/
6+
export const argMetadata = z.registry<ArgumentMetadata>();
7+
8+
/**
9+
* Metadata that can be used to define field's parsing behavior
10+
*/
11+
export type ArgumentMetadata = {
12+
/** If set, sets this field as deprecated and replaces this field with the set field. */
13+
deprecationReplacement?: string;
14+
/** If set, gets replaced with a differet field name (without deprecation) */
15+
replacement?: string;
16+
/** Whether this argument is unsupported. Always throws an error if set to true. */
17+
unsupported?: boolean;
18+
/** Aliases for this argument. */
19+
alias?: string[];
20+
};
21+
22+
/**
23+
* Extract metadata for a field using the custom registry
24+
*/
25+
export function getArgumentMetadata(
26+
schema: z.ZodObject,
27+
fieldName: string
28+
): ArgumentMetadata | undefined {
29+
const fieldSchema = schema.shape[fieldName as keyof typeof schema.shape];
30+
if (!fieldSchema) {
31+
return undefined;
32+
}
33+
return argMetadata.get(fieldSchema);
34+
}
35+
36+
/**
37+
* Maps deprecated arguments to their new counterparts, derived from schema metadata.
38+
*/
39+
export function getDeprecatedArgsWithReplacement<T>(
40+
schema: z.ZodObject
41+
): Record<keyof z.infer<typeof schema>, T> {
42+
const deprecated: Record<string, T> = {};
43+
for (const fieldName of Object.keys(schema.shape)) {
44+
const meta = getArgumentMetadata(schema, fieldName);
45+
if (meta?.deprecationReplacement) {
46+
deprecated[fieldName] = meta.deprecationReplacement as T;
47+
}
48+
}
49+
return deprecated;
50+
}
51+
52+
/**
53+
* Get list of unsupported arguments, derived from schema metadata.
54+
*/
55+
export function getUnsupportedArgs(schema: z.ZodObject): string[] {
56+
const unsupported: string[] = [];
57+
for (const fieldName of Object.keys(schema.shape)) {
58+
const meta = getArgumentMetadata(schema, fieldName);
59+
if (meta?.unsupported) {
60+
unsupported.push(fieldName);
61+
}
62+
}
63+
return unsupported;
64+
}
65+
66+
export class UnknownCliArgumentError extends Error {
67+
/** The argument that was not parsed. */
68+
readonly argument: string;
69+
constructor(argument: string) {
70+
super(`Unknown argument: ${argument}`);
71+
this.name = 'UnknownCliArgumentError';
72+
this.argument = argument;
73+
}
74+
}
75+
76+
export class UnsupportedCliArgumentError extends Error {
77+
/** The argument that was not supported. */
78+
readonly argument: string;
79+
constructor(argument: string) {
80+
super(`Unsupported argument: ${argument}`);
81+
this.name = 'UnsupportedCliArgumentError';
82+
this.argument = argument;
83+
}
84+
}

0 commit comments

Comments
 (0)