Skip to content

Commit 8cec797

Browse files
committed
chore: reorganize
1 parent 64b3027 commit 8cec797

File tree

3 files changed

+55
-54
lines changed

3 files changed

+55
-54
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import stripAnsi from 'strip-ansi';
44
import {
55
argMetadata,
66
CliOptionsSchema,
7-
coerceIfBoolean,
8-
coerceIfFalse,
97
generateYargsOptionsFromSchema,
108
getLocale,
119
parseArgs,
@@ -14,6 +12,7 @@ import {
1412
UnsupportedCliArgumentError,
1513
} from './arg-parser';
1614
import { z } from 'zod/v4';
15+
import { coerceIfBoolean, coerceIfFalse } from './utils';
1716

1817
describe('arg-parser', function () {
1918
describe('.getLocale', function () {

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

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,7 @@ import {
1515
UnknownCliArgumentError,
1616
UnsupportedCliArgumentError,
1717
} from './arg-metadata';
18-
19-
function unwrapType(type: unknown): z.ZodType {
20-
assertZodType(type);
21-
let unwrappedType = z.clone(type);
22-
while (
23-
unwrappedType instanceof z.ZodOptional ||
24-
unwrappedType instanceof z.ZodDefault
25-
) {
26-
const nextWrap = unwrappedType.unwrap();
27-
assertZodType(nextWrap);
28-
unwrappedType = nextWrap;
29-
}
30-
31-
return type;
32-
}
33-
34-
function assertZodType(type: unknown): asserts type is z.ZodType {
35-
if (!(type instanceof z.ZodType)) {
36-
throw new Error(
37-
`Unknown schema field type: ${
38-
type && typeof type === 'object' ? type.constructor.name : typeof type
39-
}`
40-
);
41-
}
42-
}
18+
import { coerceIfBoolean, coerceIfFalse, unwrapType } from './utils';
4319

4420
export const defaultParserOptions: Partial<YargsOptions> = {
4521
configuration: {
@@ -280,33 +256,6 @@ export function parseArgsWithCliOptions<
280256
};
281257
}
282258

283-
export function coerceIfBoolean(value: unknown): unknown {
284-
if (typeof value === 'string') {
285-
if (value === 'true') {
286-
return true;
287-
}
288-
if (value === 'false') {
289-
return false;
290-
}
291-
return value;
292-
}
293-
return value;
294-
}
295-
296-
export function coerceIfFalse(value: unknown): unknown {
297-
if (value === undefined || value === '') {
298-
return null;
299-
}
300-
301-
if (typeof value === 'string') {
302-
if (value === 'false') {
303-
return false;
304-
}
305-
return value;
306-
}
307-
return value;
308-
}
309-
310259
export { argMetadata, UnknownCliArgumentError, UnsupportedCliArgumentError };
311260
export { type ArgumentMetadata } from './arg-metadata';
312261
export { type CliOptions, CliOptionsSchema } from './cli-options';

packages/arg-parser/src/utils.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import z from 'zod/v4';
2+
3+
export function coerceIfBoolean(value: unknown): unknown {
4+
if (typeof value === 'string') {
5+
if (value === 'true') {
6+
return true;
7+
}
8+
if (value === 'false') {
9+
return false;
10+
}
11+
return value;
12+
}
13+
return value;
14+
}
15+
16+
export function coerceIfFalse(value: unknown): unknown {
17+
if (value === undefined || value === '') {
18+
return null;
19+
}
20+
21+
if (typeof value === 'string') {
22+
if (value === 'false') {
23+
return false;
24+
}
25+
return value;
26+
}
27+
return value;
28+
}
29+
30+
export function unwrapType(type: unknown): z.ZodType {
31+
assertZodType(type);
32+
let unwrappedType = z.clone(type);
33+
while (
34+
unwrappedType instanceof z.ZodOptional ||
35+
unwrappedType instanceof z.ZodDefault
36+
) {
37+
const nextWrap = unwrappedType.unwrap();
38+
assertZodType(nextWrap);
39+
unwrappedType = nextWrap;
40+
}
41+
42+
return unwrappedType;
43+
}
44+
45+
function assertZodType(type: unknown): asserts type is z.ZodType {
46+
if (!(type instanceof z.ZodType)) {
47+
throw new Error(
48+
`Unknown schema field type: ${
49+
type && typeof type === 'object' ? type.constructor.name : typeof type
50+
}`
51+
);
52+
}
53+
}

0 commit comments

Comments
 (0)