Skip to content

Commit 2057c77

Browse files
committed
chore: improve and cleanup types
1 parent b59f469 commit 2057c77

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

packages/arg-parser/src/arg-metadata.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function getArgumentMetadata(
2626
schema: z.ZodObject,
2727
fieldName: string
2828
): ArgumentMetadata | undefined {
29-
const fieldSchema = schema.shape[fieldName as keyof typeof schema.shape];
29+
const fieldSchema = schema.shape[fieldName];
3030
if (!fieldSchema) {
3131
return undefined;
3232
}
@@ -36,14 +36,14 @@ export function getArgumentMetadata(
3636
/**
3737
* Maps deprecated arguments to their new counterparts, derived from schema metadata.
3838
*/
39-
export function getDeprecatedArgsWithReplacement<T>(
39+
export function getDeprecatedArgsWithReplacement(
4040
schema: z.ZodObject
41-
): Record<keyof z.infer<typeof schema>, T> {
42-
const deprecated: Record<string, T> = {};
41+
): Record<string, keyof z.infer<typeof schema>> {
42+
const deprecated: Record<string, keyof z.infer<typeof schema>> = {};
4343
for (const fieldName of Object.keys(schema.shape)) {
4444
const meta = getArgumentMetadata(schema, fieldName);
4545
if (meta?.deprecationReplacement) {
46-
deprecated[fieldName] = meta.deprecationReplacement as T;
46+
deprecated[fieldName] = meta.deprecationReplacement;
4747
}
4848
}
4949
return deprecated;

packages/arg-parser/src/arg-parser.ts

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import parser from 'yargs-parser';
22
import { z, ZodError } from 'zod/v4';
33
import type { Options as YargsOptions } from 'yargs-parser';
44
import {
5-
type CliOptions,
65
CliOptionsSchema,
76
processPositionalCliOptions,
87
validateCliOptions,
@@ -36,19 +35,19 @@ export const defaultParserOptions: Partial<YargsOptions> = {
3635

3736
export type ParserOptions = Partial<YargsOptions>;
3837

39-
export function parseArgs<T>({
38+
export function parseArgs<T extends z.ZodObject>({
4039
args,
4140
schema,
4241
parserOptions,
4342
}: {
4443
args: string[];
45-
schema: z.ZodObject;
44+
schema: T;
4645
parserOptions?: YargsOptions;
4746
}): {
4847
/** Parsed options from the schema, including replaced deprecated arguments. */
49-
parsed: z.infer<typeof schema> & Omit<parser.Arguments, '_'>;
48+
parsed: z.infer<T> & Omit<parser.Arguments, '_'>;
5049
/** Record of used deprecated arguments which have been replaced. */
51-
deprecated: Record<keyof z.infer<typeof schema>, T>;
50+
deprecated: Record<string, keyof z.infer<T>>;
5251
/** Positional arguments which were not parsed as options. */
5352
positional: parser.Arguments['_'];
5453
} {
@@ -69,8 +68,8 @@ export function parseArgs<T>({
6968
throw error;
7069
}
7170

72-
const allDeprecatedArgs = getDeprecatedArgsWithReplacement<T>(schema);
73-
const usedDeprecatedArgs = {} as Record<keyof z.infer<typeof schema>, T>;
71+
const allDeprecatedArgs = getDeprecatedArgsWithReplacement(schema);
72+
const usedDeprecatedArgs = {} as Record<string, keyof z.infer<typeof schema>>;
7473

7574
for (const deprecated of Object.keys(allDeprecatedArgs)) {
7675
if (deprecated in parsedArgs) {
@@ -100,30 +99,21 @@ export function parseArgs<T>({
10099
}
101100

102101
return {
103-
parsed: parsedArgs as T & Omit<parser.Arguments, '_'>,
102+
parsed: parsedArgs as z.infer<T> & Omit<parser.Arguments, '_'>,
104103
deprecated: usedDeprecatedArgs,
105104
positional,
106105
};
107106
}
108107

109-
type ParsedCliOptions = CliOptions & {
110-
smokeTests: boolean;
111-
perfTests: boolean;
112-
buildInfo: boolean;
113-
file?: string[];
114-
};
115-
116108
/** Parses the arguments with special handling of mongosh CLI options fields. */
117-
export function parseArgsWithCliOptions<
118-
T extends CliOptions = ParsedCliOptions
119-
>({
109+
export function parseArgsWithCliOptions<T extends z.ZodObject>({
120110
args,
121111
schema: schemaToExtend,
122112
parserOptions,
123113
}: {
124114
args: string[];
125115
/** Schema to extend the CLI options schema with. */
126-
schema?: z.ZodObject;
116+
schema?: T;
127117
parserOptions?: Partial<YargsOptions>;
128118
}): ReturnType<typeof parseArgs<T>> {
129119
const schema =
@@ -133,7 +123,7 @@ export function parseArgsWithCliOptions<
133123
...schemaToExtend.shape,
134124
})
135125
: CliOptionsSchema;
136-
const { parsed, positional, deprecated } = parseArgs<T>({
126+
const { parsed, positional, deprecated } = parseArgs({
137127
args,
138128
schema,
139129
parserOptions,
@@ -147,7 +137,7 @@ export function parseArgsWithCliOptions<
147137
validateCliOptions(processed);
148138

149139
return {
150-
parsed: processed,
140+
parsed: processed as z.infer<T> & Omit<parser.Arguments, '_'>,
151141
positional,
152142
deprecated,
153143
};

packages/arg-parser/src/cli-options.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,17 @@ export const CurrentCliOptionsSchema = z.object({
7373
alias: ['oidcIDTokenAsAccessToken'],
7474
}),
7575
oidcNoNonce: z.boolean().optional(),
76-
perfTests: z.boolean().optional(),
7776
quiet: z.boolean().optional(),
7877
retryWrites: z.boolean().optional(),
7978
shell: z.boolean().optional(),
80-
smokeTests: z.boolean().optional(),
8179
skipStartupWarnings: z.boolean().optional(),
8280
verbose: z.boolean().optional(),
8381
version: z.boolean().optional(),
8482

83+
// Tests
84+
smokeTests: z.boolean().optional(),
85+
perfTests: z.boolean().optional(),
86+
8587
// TLS boolean options
8688
tls: z.boolean().optional(),
8789
tlsAllowInvalidCertificates: z.boolean().optional(),

0 commit comments

Comments
 (0)