@@ -47,6 +47,8 @@ type GeneratorOptions = Protobuf.IParseOptions & Protobuf.IConversionOptions & {
47
47
outputTemplate : string ;
48
48
inputBranded : boolean ;
49
49
outputBranded : boolean ;
50
+ targetFileExtension ?: string ;
51
+ importFileExtension ?: string ;
50
52
}
51
53
52
54
class TextFormatter {
@@ -105,8 +107,8 @@ function getImportPath(to: Protobuf.Type | Protobuf.Enum | Protobuf.Service): st
105
107
return stripLeadingPeriod ( to . fullName ) . replace ( / \. / g, '/' ) ;
106
108
}
107
109
108
- function getPath ( to : Protobuf . Type | Protobuf . Enum | Protobuf . Service ) {
109
- return stripLeadingPeriod ( to . fullName ) . replace ( / \. / g, '/' ) + '.ts' ;
110
+ function getPath ( to : Protobuf . Type | Protobuf . Enum | Protobuf . Service , extension : string = '.ts' ) {
111
+ return stripLeadingPeriod ( to . fullName ) . replace ( / \. / g, '/' ) + extension ;
110
112
}
111
113
112
114
function getPathToRoot ( from : Protobuf . NamespaceBase ) {
@@ -153,7 +155,7 @@ function getImportLine(dependency: Protobuf.Type | Protobuf.Enum | Protobuf.Serv
153
155
throw new Error ( 'Invalid object passed to getImportLine' ) ;
154
156
}
155
157
}
156
- return `import type { ${ importedTypes } } from '${ filePath } ';`
158
+ return `import type { ${ importedTypes } } from '${ filePath } ${ options . importFileExtension ?? '' } ';`
157
159
}
158
160
159
161
function getChildMessagesAndEnums ( namespace : Protobuf . NamespaceBase ) : ( Protobuf . Type | Protobuf . Enum ) [ ] {
@@ -787,21 +789,21 @@ function generateFilesForNamespace(namespace: Protobuf.NamespaceBase, options: G
787
789
if ( nested instanceof Protobuf . Type ) {
788
790
generateMessageInterfaces ( fileFormatter , nested , options ) ;
789
791
if ( options . verbose ) {
790
- console . log ( `Writing ${ options . outDir } /${ getPath ( nested ) } from file ${ nested . filename } ` ) ;
792
+ console . log ( `Writing ${ options . outDir } /${ getPath ( nested , options . targetFileExtension ) } from file ${ nested . filename } ` ) ;
791
793
}
792
- filePromises . push ( writeFile ( `${ options . outDir } /${ getPath ( nested ) } ` , fileFormatter . getFullText ( ) ) ) ;
794
+ filePromises . push ( writeFile ( `${ options . outDir } /${ getPath ( nested , options . targetFileExtension ) } ` , fileFormatter . getFullText ( ) ) ) ;
793
795
} else if ( nested instanceof Protobuf . Enum ) {
794
796
generateEnumInterface ( fileFormatter , nested , options ) ;
795
797
if ( options . verbose ) {
796
- console . log ( `Writing ${ options . outDir } /${ getPath ( nested ) } from file ${ nested . filename } ` ) ;
798
+ console . log ( `Writing ${ options . outDir } /${ getPath ( nested , options . targetFileExtension ) } from file ${ nested . filename } ` ) ;
797
799
}
798
- filePromises . push ( writeFile ( `${ options . outDir } /${ getPath ( nested ) } ` , fileFormatter . getFullText ( ) ) ) ;
800
+ filePromises . push ( writeFile ( `${ options . outDir } /${ getPath ( nested , options . targetFileExtension ) } ` , fileFormatter . getFullText ( ) ) ) ;
799
801
} else if ( nested instanceof Protobuf . Service ) {
800
802
generateServiceInterfaces ( fileFormatter , nested , options ) ;
801
803
if ( options . verbose ) {
802
- console . log ( `Writing ${ options . outDir } /${ getPath ( nested ) } from file ${ nested . filename } ` ) ;
804
+ console . log ( `Writing ${ options . outDir } /${ getPath ( nested , options . targetFileExtension ) } from file ${ nested . filename } ` ) ;
803
805
}
804
- filePromises . push ( writeFile ( `${ options . outDir } /${ getPath ( nested ) } ` , fileFormatter . getFullText ( ) ) ) ;
806
+ filePromises . push ( writeFile ( `${ options . outDir } /${ getPath ( nested , options . targetFileExtension ) } ` , fileFormatter . getFullText ( ) ) ) ;
805
807
} else if ( isNamespaceBase ( nested ) ) {
806
808
filePromises . push ( ...generateFilesForNamespace ( nested , options ) ) ;
807
809
}
@@ -877,6 +879,8 @@ async function runScript() {
877
879
. option ( 'outputTemplate' , { string : true , default : `${ templateStr } __Output` } )
878
880
. option ( 'inputBranded' , boolDefaultFalseOption )
879
881
. option ( 'outputBranded' , boolDefaultFalseOption )
882
+ . option ( 'targetFileExtension' , { string : true , default : '.ts' } )
883
+ . option ( 'importFileExtension' , { string : true } )
880
884
. coerce ( 'longs' , value => {
881
885
switch ( value ) {
882
886
case 'String' : return String ;
@@ -916,6 +920,8 @@ async function runScript() {
916
920
outputTemplate : 'Template for mapping output or "restricted" type names' ,
917
921
inputBranded : 'Output property for branded type for "permissive" types with fullName of the Message as its value' ,
918
922
outputBranded : 'Output property for branded type for "restricted" types with fullName of the Message as its value' ,
923
+ targetFileExtension : 'File extension for generated files. Defaults to .ts' ,
924
+ importFileExtension : 'File extension for import specifiers in generated code. Defaults to none (omitted)'
919
925
} ) . demandOption ( [ 'outDir' ] )
920
926
. demand ( 1 )
921
927
. usage ( '$0 [options] filenames...' )
0 commit comments