@@ -141,8 +141,7 @@ export namespace ${messageDef.name} {
141
141
export const codec = () => {
142
142
return enumeration<typeof ${ messageDef . name } >(${ messageDef . name } )
143
143
}
144
- }
145
- `
144
+ }` . trim ( )
146
145
}
147
146
148
147
let nested = ''
@@ -151,11 +150,10 @@ export namespace ${messageDef.name} {
151
150
nested = '\n'
152
151
nested += Object . values ( messageDef . nested )
153
152
. map ( def => compileMessage ( def , moduleDef ) . trim ( ) )
154
- . join ( '\n' )
153
+ . join ( '\n\n ' )
155
154
. split ( '\n' )
156
155
. map ( line => line . trim ( ) === '' ? '' : ` ${ line } ` )
157
156
. join ( '\n' )
158
- nested += '\n'
159
157
}
160
158
161
159
const fields = messageDef . fields ?? { }
@@ -164,18 +162,26 @@ export namespace ${messageDef.name} {
164
162
moduleDef . imports . add ( 'encodeMessage' )
165
163
moduleDef . imports . add ( 'decodeMessage' )
166
164
moduleDef . imports . add ( 'message' )
165
+ moduleDef . importedTypes . add ( 'Codec' )
167
166
168
- return `
167
+ const interfaceFields = defineFields ( fields , messageDef , moduleDef )
168
+ . join ( '\n ' )
169
+ . trim ( )
170
+
171
+ let interfaceDef = ''
172
+ let interfaceCodecDef = ''
173
+
174
+ if ( interfaceFields !== '' ) {
175
+ interfaceDef = `
169
176
export interface ${ messageDef . name } {
170
177
${
171
178
defineFields ( fields , messageDef , moduleDef )
172
179
. join ( '\n ' )
173
180
. trim ( )
174
181
}
175
- }
176
-
177
- export namespace ${ messageDef . name } {${ nested }
178
- export const codec = () => {
182
+ }`
183
+ interfaceCodecDef = `
184
+ export const codec = (): Codec<${ messageDef . name } > => {
179
185
return message<${ messageDef . name } >({
180
186
${ Object . entries ( fields )
181
187
. map ( ( [ name , fieldDef ] ) => {
@@ -207,13 +213,21 @@ export namespace ${messageDef.name} {${nested}
207
213
208
214
export const decode = (buf: Uint8Array): ${ messageDef . name } => {
209
215
return decodeMessage(buf, ${ messageDef . name } .codec())
216
+ }`
210
217
}
218
+
219
+ return `
220
+ ${ interfaceDef }
221
+
222
+ export namespace ${ messageDef . name } {
223
+ ${ `${ nested } ${ nested !== '' && interfaceCodecDef !== '' ? '\n' : '' } ${ interfaceCodecDef } ` . trim ( ) }
211
224
}
212
- `
225
+ ` . trimStart ( )
213
226
}
214
227
215
228
interface ModuleDef {
216
229
imports : Set < string >
230
+ importedTypes : Set < string >
217
231
types : Set < string >
218
232
compiled : string [ ]
219
233
globals : Record < string , ClassDef >
@@ -222,6 +236,7 @@ interface ModuleDef {
222
236
function defineModule ( def : ClassDef ) : ModuleDef {
223
237
const moduleDef : ModuleDef = {
224
238
imports : new Set ( ) ,
239
+ importedTypes : new Set ( ) ,
225
240
types : new Set ( ) ,
226
241
compiled : [ ] ,
227
242
globals : { }
@@ -273,14 +288,27 @@ export async function generate (source: string, flags: any) {
273
288
const def = JSON . parse ( json )
274
289
const moduleDef = defineModule ( def )
275
290
276
- const content = `
277
- /* eslint-disable import/export */
278
- /* eslint-disable @typescript-eslint/no-namespace */
291
+ let lines = [
292
+ '/* eslint-disable import/export */' ,
293
+ '/* eslint-disable @typescript-eslint/no-namespace */' ,
294
+ ''
295
+ ]
296
+
297
+ if ( moduleDef . imports . size > 0 ) {
298
+ lines . push ( `import { ${ Array . from ( moduleDef . imports ) . join ( ', ' ) } } from 'protons-runtime'` )
299
+ }
300
+
301
+ if ( moduleDef . importedTypes . size > 0 ) {
302
+ lines . push ( `import type { ${ Array . from ( moduleDef . importedTypes ) . join ( ', ' ) } } from 'protons-runtime'` )
303
+ }
279
304
280
- import { ${ Array . from ( moduleDef . imports ) . join ( ', ' ) } } from 'protons-runtime'
305
+ lines = [
306
+ ...lines ,
307
+ '' ,
308
+ ...moduleDef . compiled
309
+ ]
281
310
282
- ${ moduleDef . compiled . map ( str => str . trim ( ) ) . join ( '\n\n' ) . trim ( ) }
283
- ` . trim ( )
311
+ const content = lines . join ( '\n' ) . trim ( )
284
312
285
313
await fs . writeFile ( pathWithExtension ( source , '.ts' ) , content + '\n' )
286
314
}
0 commit comments