@@ -39,7 +39,7 @@ const useNameFmter = ({outputTemplate, inputTemplate}: GeneratorOptions) => {
39
39
40
40
type GeneratorOptions = Protobuf . IParseOptions & Protobuf . IConversionOptions & {
41
41
includeDirs ?: string [ ] ;
42
- grpcLib : string ;
42
+ grpcLib ? : string ;
43
43
outDir : string ;
44
44
verbose ?: boolean ;
45
45
includeComments ?: boolean ;
@@ -522,12 +522,12 @@ function generateEnumInterface(formatter: TextFormatter, enumType: Protobuf.Enum
522
522
* We always generate two service client methods per service method: one camel
523
523
* cased, and one with the original casing. So we will still generate one
524
524
* service client method for any conflicting name.
525
- *
525
+ *
526
526
* Technically, at runtime conflicting name in the service client method
527
527
* actually shadows the original method, but TypeScript does not have a good
528
528
* way to represent that. So this change is not 100% accurate, but it gets the
529
529
* generated code to compile.
530
- *
530
+ *
531
531
* This is just a list of the methods in the Client class definitions in
532
532
533
533
*/
@@ -640,7 +640,11 @@ function generateServiceHandlerInterface(formatter: TextFormatter, serviceType:
640
640
641
641
function generateServiceDefinitionInterface ( formatter : TextFormatter , serviceType : Protobuf . Service , options : GeneratorOptions ) {
642
642
const { inputName, outputName} = useNameFmter ( options ) ;
643
- formatter . writeLine ( `export interface ${ serviceType . name } Definition extends grpc.ServiceDefinition {` ) ;
643
+ if ( options . grpcLib ) {
644
+ formatter . writeLine ( `export interface ${ serviceType . name } Definition extends grpc.ServiceDefinition {` ) ;
645
+ } else {
646
+ formatter . writeLine ( `export interface ${ serviceType . name } Definition {` ) ;
647
+ }
644
648
formatter . indent ( ) ;
645
649
for ( const methodName of Object . keys ( serviceType . methods ) . sort ( ) ) {
646
650
const method = serviceType . methods [ methodName ] ;
@@ -655,8 +659,10 @@ function generateServiceDefinitionInterface(formatter: TextFormatter, serviceTyp
655
659
function generateServiceInterfaces ( formatter : TextFormatter , serviceType : Protobuf . Service , options : GeneratorOptions ) {
656
660
formatter . writeLine ( `// Original file: ${ ( serviceType . filename ?? 'null' ) ?. replace ( / \\ / g, '/' ) } ` ) ;
657
661
formatter . writeLine ( '' ) ;
658
- const grpcImportPath = options . grpcLib . startsWith ( '.' ) ? getPathToRoot ( serviceType ) + options . grpcLib : options . grpcLib ;
659
- formatter . writeLine ( `import type * as grpc from '${ grpcImportPath } '` ) ;
662
+ if ( options . grpcLib ) {
663
+ const grpcImportPath = options . grpcLib . startsWith ( '.' ) ? getPathToRoot ( serviceType ) + options . grpcLib : options . grpcLib ;
664
+ formatter . writeLine ( `import type * as grpc from '${ grpcImportPath } '` ) ;
665
+ }
660
666
formatter . writeLine ( `import type { MethodDefinition } from '@grpc/proto-loader'` )
661
667
const dependencies : Set < Protobuf . Type > = new Set< Protobuf . Type > ();
662
668
for (const method of serviceType.methodsArray) {
@@ -668,11 +674,13 @@ function generateServiceInterfaces(formatter: TextFormatter, serviceType: Protob
668
674
}
669
675
formatter.writeLine('');
670
676
671
- generateServiceClientInterface(formatter, serviceType, options);
672
- formatter.writeLine('');
677
+ if (options.grpcLib) {
678
+ generateServiceClientInterface ( formatter , serviceType , options ) ;
679
+ formatter . writeLine ( '' ) ;
673
680
674
- generateServiceHandlerInterface(formatter, serviceType, options);
675
- formatter.writeLine('');
681
+ generateServiceHandlerInterface ( formatter , serviceType , options ) ;
682
+ formatter . writeLine ( '' ) ;
683
+ }
676
684
677
685
generateServiceDefinitionInterface(formatter, serviceType, options);
678
686
}
@@ -742,6 +750,9 @@ function generateLoadedDefinitionTypes(formatter: TextFormatter, namespace: Prot
742
750
}
743
751
744
752
function generateRootFile ( formatter : TextFormatter , root : Protobuf . Root , options : GeneratorOptions ) {
753
+ if ( ! options . grpcLib ) {
754
+ return ;
755
+ }
745
756
formatter.writeLine(`import type * as grpc from '${ options . grpcLib } ';`);
746
757
generateDefinitionImports(formatter, root, options);
747
758
formatter.writeLine('');
@@ -802,11 +813,13 @@ function writeFilesForRoot(root: Protobuf.Root, masterFileName: string, options:
802
813
const filePromises : Promise < void > [ ] = [ ] ;
803
814
804
815
const masterFileFormatter = new TextFormatter ( ) ;
805
- generateRootFile ( masterFileFormatter , root , options ) ;
806
- if ( options . verbose ) {
807
- console . log ( `Writing ${ options . outDir } /${ masterFileName } ` ) ;
816
+ if ( options . grpcLib ) {
817
+ generateRootFile ( masterFileFormatter , root , options ) ;
818
+ if ( options . verbose ) {
819
+ console . log ( `Writing ${ options . outDir } /${ masterFileName } ` ) ;
820
+ }
821
+ filePromises . push ( writeFile ( `${ options . outDir } /${ masterFileName } ` , masterFileFormatter . getFullText ( ) ) ) ;
808
822
}
809
- filePromises.push(writeFile(`${ options . outDir } /${ masterFileName } `, masterFileFormatter.getFullText()));
810
823
811
824
filePromises . push ( ...generateFilesForNamespace ( root , options ) ) ;
812
825
@@ -898,12 +911,12 @@ async function runScript() {
898
911
includeComments : 'Generate doc comments from comments in the original files' ,
899
912
includeDirs : 'Directories to search for included files' ,
900
913
outDir : 'Directory in which to output files' ,
901
- grpcLib : 'The gRPC implementation library that these types will be used with' ,
914
+ grpcLib : 'The gRPC implementation library that these types will be used with. If not provided, some types will not be generated ' ,
902
915
inputTemplate : 'Template for mapping input or "permissive" type names' ,
903
916
outputTemplate : 'Template for mapping output or "restricted" type names' ,
904
917
inputBranded : 'Output property for branded type for "permissive" types with fullName of the Message as its value' ,
905
918
outputBranded : 'Output property for branded type for "restricted" types with fullName of the Message as its value' ,
906
- } ) . demandOption ( [ 'outDir' , 'grpcLib' ] )
919
+ } ) . demandOption ( [ 'outDir' ] )
907
920
. demand ( 1 )
908
921
. usage ( '$0 [options] filenames...' )
909
922
. epilogue ( 'WARNING: This tool is in alpha. The CLI and generated code are subject to change' )
0 commit comments