-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathparseArgs.ts
214 lines (204 loc) · 7.1 KB
/
parseArgs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import {basename} from 'path';
import {sync as globSync} from 'glob';
import {
getDefaultArgs,
Args as TypeScriptJsonSchemaArgs,
} from 'typescript-json-schema';
import Ajv from 'ajv';
export interface Options {
schema: Pick<
TypeScriptJsonSchemaArgs,
Exclude<keyof TypeScriptJsonSchemaArgs, 'out'>
>;
ajv: Ajv.Options;
useNamedExport: boolean;
customKeywordFnName: string | undefined;
customKeywordPath: string | undefined;
}
export interface File {
fileName: string;
typeName?: string;
}
export interface ParsedArgs {
files: File[];
options: Options;
}
export function parseArgs(args?: string[]): ParsedArgs {
var helpText =
'Usage: typescript-json-schema <path-to-typescript-file> <type>';
const defaultArgs = getDefaultArgs();
const parsedArgs = require('yargs')
.usage(helpText)
.demand(1)
// typescript-json-schema options
.boolean('refs')
.default('refs', defaultArgs.ref)
.describe('refs', 'Create shared ref definitions.')
.boolean('aliasRefs')
.default('aliasRefs', defaultArgs.aliasRef)
.describe(
'aliasRefs',
'Create shared ref definitions for the type aliases.',
)
.boolean('topRef')
.default('topRef', defaultArgs.topRef)
.describe('topRef', 'Create a top-level ref definition.')
.boolean('titles')
.default('titles', defaultArgs.titles)
.describe('titles', 'Creates titles in the output schema.')
.boolean('defaultProps')
// default to enabling default props
.default('defaultProps', true)
.describe('defaultProps', 'Create default properties definitions.')
.boolean('noExtraProps')
.default('noExtraProps', defaultArgs.noExtraProps)
.describe(
'noExtraProps',
'Disable additional properties in objects by default.',
)
.boolean('propOrder')
.default('propOrder', defaultArgs.propOrder)
.describe('propOrder', 'Create property order definitions.')
.boolean('typeOfKeyword')
.default('typeOfKeyword', defaultArgs.typeOfKeyword)
.describe(
'typeOfKeyword',
'Use typeOf keyword (https://goo.gl/DC6sni) for functions.',
)
.boolean('required')
// default to requiring non-optional props
.default('required', true)
.describe('required', 'Create required array for non-optional properties.')
.boolean('strictNullChecks')
// default to strict null checks
.default('strictNullChecks', true)
.describe('strictNullChecks', 'Make values non-nullable by default.')
.boolean('ignoreErrors')
.default('ignoreErrors', defaultArgs.ignoreErrors)
.describe('ignoreErrors', 'Generate even if the program has errors.')
.array('validationKeywords')
.default('validationKeywords', defaultArgs.validationKeywords)
.describe(
'validationKeywords',
'Provide additional validation keywords to include.',
)
.boolean('excludePrivate')
.default('excludePrivate', defaultArgs.excludePrivate)
.describe('excludePrivate', 'Exclude private members from the schema.')
.boolean('uniqueNames')
.default('uniqueNames', defaultArgs.uniqueNames)
.describe('uniqueNames', 'Use unique names for type symbols.')
.array('include')
.default('*', defaultArgs.include)
.describe(
'include',
'Further limit tsconfig to include only matching files.',
)
.boolean('rejectDateType')
.default('rejectDateType', defaultArgs.rejectDateType)
.describe('rejectDateType', 'Rejects Date fields in type definitions.')
.string('id')
.default('id', defaultArgs.id)
.describe('id', 'ID of schema.')
// ajv options
.boolean('uniqueItems')
.default('uniqueItems', true)
.describe('uniqueItems', 'Validate `uniqueItems` keyword')
.boolean('unicode')
.default('unicode', true)
.describe(
'unicode',
'calculate correct length of strings with unicode pairs (true by default). Pass false to use .length of strings that is faster, but gives "incorrect" lengths of strings with unicode pairs - each unicode pair is counted as two characters.',
)
.boolean('nullable')
.default('nullable', true)
.describe(
'nullable',
'support keyword "nullable" from Open API 3 specification.',
)
.choices('format', ['fast', 'full'])
.default('format', 'fast')
.describe(
'format',
"formats validation mode ('fast' by default). Pass 'full' for more correct and slow validation or false not to validate formats at all. E.g., 25:00:00 and 2015/14/33 will be invalid time and date in 'full' mode but it will be valid in 'fast' mode.",
)
.boolean('coerceTypes')
.default('coerceTypes', false)
.describe(
'coerceTypes',
'Change data type of data to match type keyword. e.g. parse numbers in strings',
)
// specific to typescript-json-validator
.boolean('collection')
.default('collection', false)
.describe(
'collection',
'Process the file as a collection of types, instead of one single type.',
)
.boolean('useNamedExport')
.default('useNamedExport', false)
.describe(
'useNamedExport',
'Type name is a named export, rather than the default export of the file',
)
.string('customKeywordFnName')
.default('customKeywordFnName', undefined)
.describe(
'customKeywordFnName',
'Name of function which configures AJV custom keywords',
)
.string('customKeywordPath')
.default('customKeywordPath', undefined)
.describe(
'customKeywordPath',
'Path to file containing customKeywordFnName',
)
.parse(args);
const isCollection: boolean = parsedArgs.collection;
const files: File[] = [];
globSync(parsedArgs._[0])
.filter(filename => !/\.validator\.tsx?$/.test(filename))
.forEach(fileName => {
if (isCollection) {
files.push({fileName});
} else {
const typeName = parsedArgs._[1] || basename(fileName, '.ts');
files.push({fileName, typeName});
}
});
return {
files,
options: {
schema: {
ref: parsedArgs.refs,
aliasRef: parsedArgs.aliasRefs,
topRef: parsedArgs.topRef,
titles: parsedArgs.titles,
defaultProps: parsedArgs.defaultProps,
noExtraProps: parsedArgs.noExtraProps,
propOrder: parsedArgs.propOrder,
typeOfKeyword: parsedArgs.useTypeOfKeyword,
required: parsedArgs.required,
strictNullChecks: parsedArgs.strictNullChecks,
ignoreErrors: parsedArgs.ignoreErrors,
validationKeywords: parsedArgs.validationKeywords,
include: parsedArgs.include,
excludePrivate: parsedArgs.excludePrivate,
uniqueNames: parsedArgs.uniqueNames,
rejectDateType: parsedArgs.rejectDateType,
id: parsedArgs.id,
},
ajv: {
coerceTypes: parsedArgs.coerceTypes,
format: parsedArgs.format,
nullable: parsedArgs.nullable,
unicode: parsedArgs.unicode,
uniqueItems: parsedArgs.uniqueItems,
useDefaults: parsedArgs.defaultProps,
},
useNamedExport: parsedArgs.useNamedExport,
customKeywordFnName: parsedArgs.customKeywordFnName,
customKeywordPath: parsedArgs.customKeywordPath,
},
};
}