@@ -3,7 +3,6 @@ import * as path from 'path'
3
3
import { parseFromSource , parseThriftFile } from './parser'
4
4
5
5
import {
6
- CompileTarget ,
7
6
IFileExports ,
8
7
IGeneratedFile ,
9
8
IMakeOptions ,
@@ -32,9 +31,19 @@ import { print } from './printer'
32
31
import { readThriftFile } from './reader'
33
32
import { rendererForTarget } from './render'
34
33
import { resolveFile } from './resolver'
35
- import { exportsForFile } from './resolver/utils '
34
+ import { exportsForFile } from './resolver'
36
35
import { validateFile } from './validator'
37
36
37
+ import * as Parser from './parser'
38
+ import * as Resolver from './resolver'
39
+ import * as Validator from './validator'
40
+ import * as Utils from './utils'
41
+
42
+ export { Resolver }
43
+ export { Parser }
44
+ export { Validator }
45
+ export { Utils }
46
+
38
47
/**
39
48
* This function is mostly for testing purposes. It does not support includes.
40
49
* Given a string of Thrift IDL it will return a string of TypeScript. If the
@@ -45,29 +54,19 @@ import { validateFile } from './validator'
45
54
*/
46
55
export function make (
47
56
source : string ,
48
- target : CompileTarget = 'thrift-server' ,
49
- strictUnions : boolean = false ,
57
+ options : Partial < IMakeOptions > = { } ,
50
58
) : string {
51
- const options : IMakeOptions = mergeWithDefaults ( {
52
- target,
53
- strictUnions,
54
- } )
55
- const parsedFile : IParsedFile = parseFromSource ( source , options )
56
- const resolvedFile : IResolvedFile = resolveFile ( parsedFile , { } , '' , options )
57
- const validatedFile : IResolvedFile = validateFile ( resolvedFile , { } , '' )
58
-
59
- if ( validatedFile . errors . length > 0 ) {
60
- throw new Error ( `Shit broke` )
61
- }
59
+ const mergedOptions : IMakeOptions = mergeWithDefaults ( options )
60
+ const validatedFile : IResolvedFile = parseThriftSource ( source , options )
62
61
63
- const fileExports : IFileExports = exportsForFile ( resolvedFile . body )
62
+ const fileExports : IFileExports = exportsForFile ( validatedFile . body )
64
63
const state : IRenderState = {
65
- options,
64
+ options : mergedOptions ,
66
65
currentNamespace : {
67
66
type : 'Namespace' ,
68
67
namespace : emptyNamespace ( ) ,
69
68
files : {
70
- [ resolvedFile . sourceFile . fullPath ] : resolvedFile ,
69
+ [ validatedFile . sourceFile . fullPath ] : validatedFile ,
71
70
} ,
72
71
exports : fileExports ,
73
72
includedNamespaces : { } ,
@@ -85,41 +84,53 @@ export function make(
85
84
sourceDir : '' ,
86
85
outDir : '' ,
87
86
namespaces : { } ,
88
- options,
87
+ options : mergedOptions ,
89
88
} ,
90
89
}
91
90
92
91
return print (
93
- processStatements ( resolvedFile . body , state , rendererForTarget ( target ) ) ,
92
+ processStatements (
93
+ validatedFile . body ,
94
+ state ,
95
+ rendererForTarget ( mergedOptions . target ) ,
96
+ ) ,
94
97
)
95
98
}
96
99
97
- export async function generate ( options : Partial < IMakeOptions > ) : Promise < void > {
100
+ export function parseThriftSource (
101
+ source : string ,
102
+ options : Partial < IMakeOptions > = { } ,
103
+ ) : IResolvedFile {
98
104
const mergedOptions : IMakeOptions = mergeWithDefaults ( options )
99
-
100
- // Root at which we operate relative to
101
- const rootDir : string = path . resolve ( process . cwd ( ) , mergedOptions . rootDir )
102
-
103
- // Where do we save generated files
104
- const outDir : string = path . resolve ( rootDir , mergedOptions . outDir )
105
-
106
- // Where do we read source files
107
- const sourceDir : string = path . resolve ( rootDir , mergedOptions . sourceDir )
108
-
109
- const fileNames : Array < string > = collectSourceFiles (
110
- sourceDir ,
111
- mergedOptions ,
105
+ const parsedFile : IParsedFile = parseFromSource (
106
+ source ,
107
+ mergedOptions . fallbackNamespace ,
112
108
)
113
-
114
- const thriftFiles : Array < ISourceFile > = await Promise . all (
115
- fileNames . map ( ( next : string ) => {
116
- return readThriftFile ( next , [ sourceDir ] )
117
- } ) ,
109
+ const resolvedFile : IResolvedFile = resolveFile (
110
+ parsedFile ,
111
+ { } ,
112
+ '' ,
113
+ mergedOptions . fallbackNamespace ,
118
114
)
115
+ const validatedFile : IResolvedFile = validateFile ( resolvedFile , { } , '' )
119
116
117
+ if ( validatedFile . errors . length > 0 ) {
118
+ throw new Error ( `Unable to validate thrift source` )
119
+ }
120
+
121
+ return validatedFile
122
+ }
123
+
124
+ export async function parseThriftFiles (
125
+ thriftFiles : Array < ISourceFile > ,
126
+ options : {
127
+ sourceDir : string
128
+ fallbackNamespace : string
129
+ } ,
130
+ ) : Promise < Array < IResolvedFile > > {
120
131
const parsedFiles : Array < IParsedFile > = thriftFiles . map (
121
132
( next : ISourceFile ) => {
122
- const parsed = parseThriftFile ( next , mergedOptions )
133
+ const parsed = parseThriftFile ( next , options . fallbackNamespace )
123
134
return parsed
124
135
} ,
125
136
)
@@ -134,7 +145,12 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {
134
145
135
146
const resolvedFiles : Array < IResolvedFile > = parsedFiles . map (
136
147
( next : IParsedFile ) => {
137
- return resolveFile ( next , parsedFileMap , sourceDir , mergedOptions )
148
+ return resolveFile (
149
+ next ,
150
+ parsedFileMap ,
151
+ options . sourceDir ,
152
+ options . fallbackNamespace ,
153
+ )
138
154
} ,
139
155
)
140
156
@@ -144,7 +160,7 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {
144
160
145
161
if ( resolvedInvalidFiles . length > 0 ) {
146
162
printErrors ( resolvedInvalidFiles )
147
- process . exitCode = 1
163
+ throw new Error ( `Unable to parse Thrift files` )
148
164
} else {
149
165
const resolvedFileMap : ResolvedFileMap = resolvedFiles . reduce (
150
166
( acc : ResolvedFileMap , next : IResolvedFile ) => {
@@ -155,7 +171,7 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {
155
171
)
156
172
const validatedFiles : Array < IResolvedFile > = resolvedFiles . map (
157
173
( next : IResolvedFile ) => {
158
- return validateFile ( next , resolvedFileMap , sourceDir )
174
+ return validateFile ( next , resolvedFileMap , options . sourceDir )
159
175
} ,
160
176
)
161
177
@@ -165,24 +181,76 @@ export async function generate(options: Partial<IMakeOptions>): Promise<void> {
165
181
166
182
if ( validatedInvalidFiles . length > 0 ) {
167
183
printErrors ( validatedInvalidFiles )
168
- process . exitCode = 1
184
+ throw new Error ( `Unable to parse Thrift files` )
169
185
} else {
170
- const namespaces : INamespaceMap = organizeByNamespace ( resolvedFiles )
171
-
172
- const thriftProject : IThriftProject = {
173
- type : 'ThriftProject' ,
174
- rootDir,
175
- outDir,
176
- sourceDir,
177
- namespaces,
178
- options : mergedOptions ,
179
- }
180
-
181
- const generatedFiles : Array < IGeneratedFile > = generateProject (
182
- thriftProject ,
183
- )
184
-
185
- saveFiles ( generatedFiles , outDir )
186
+ return validatedFiles
186
187
}
187
188
}
188
189
}
190
+
191
+ export async function readThriftFiles ( options : {
192
+ rootDir : string
193
+ sourceDir : string
194
+ files ?: Array < string >
195
+ } ) : Promise < Array < ISourceFile > > {
196
+ // Root at which we operate relative to
197
+ const rootDir : string = path . resolve ( process . cwd ( ) , options . rootDir )
198
+
199
+ // Where do we read source files
200
+ const sourceDir : string = path . resolve ( rootDir , options . sourceDir )
201
+
202
+ const fileNames : Array < string > = collectSourceFiles (
203
+ sourceDir ,
204
+ options . files ,
205
+ )
206
+
207
+ const thriftFiles : Array < ISourceFile > = await Promise . all (
208
+ fileNames . map ( ( next : string ) => {
209
+ return readThriftFile ( next , [ sourceDir ] )
210
+ } ) ,
211
+ )
212
+
213
+ return thriftFiles
214
+ }
215
+
216
+ export async function generate ( options : Partial < IMakeOptions > ) : Promise < void > {
217
+ const mergedOptions : IMakeOptions = mergeWithDefaults ( options )
218
+
219
+ // Root at which we operate relative to
220
+ const rootDir : string = path . resolve ( process . cwd ( ) , mergedOptions . rootDir )
221
+
222
+ // Where do we save generated files
223
+ const outDir : string = path . resolve ( rootDir , mergedOptions . outDir )
224
+
225
+ // Where do we read source files
226
+ const sourceDir : string = path . resolve ( rootDir , mergedOptions . sourceDir )
227
+
228
+ const thriftFiles : Array < ISourceFile > = await readThriftFiles ( {
229
+ rootDir,
230
+ sourceDir,
231
+ files : mergedOptions . files ,
232
+ } )
233
+
234
+ const validatedFiles : Array < IResolvedFile > = await parseThriftFiles (
235
+ thriftFiles ,
236
+ {
237
+ sourceDir,
238
+ fallbackNamespace : mergedOptions . fallbackNamespace ,
239
+ } ,
240
+ )
241
+
242
+ const namespaces : INamespaceMap = organizeByNamespace ( validatedFiles )
243
+
244
+ const thriftProject : IThriftProject = {
245
+ type : 'ThriftProject' ,
246
+ rootDir,
247
+ outDir,
248
+ sourceDir,
249
+ namespaces,
250
+ options : mergedOptions ,
251
+ }
252
+
253
+ const generatedFiles : Array < IGeneratedFile > = generateProject ( thriftProject )
254
+
255
+ saveFiles ( generatedFiles , outDir )
256
+ }
0 commit comments