diff --git a/grpc_gen_types.sh b/grpc_gen_types.sh index 6bfd1ea8..200ae36e 100755 --- a/grpc_gen_types.sh +++ b/grpc_gen_types.sh @@ -15,16 +15,23 @@ rm -rf src/grpc/types # https://github.com/protobufjs/protobuf.js/issues/1799 sed -Ei '/^ +\/\/.*/d' proto/*.proto +cd proto +dirs=$(find -type d) +cd .. + # generate types to tmp dir -npx proto-loader-gen-types \ - --keepCase \ - --longs=String \ - --enums=String \ - --defaults \ - --oneofs \ - --grpcLib=@grpc/grpc-js \ - --outDir=src/grpc/types \ - proto/*.proto || exit 1 +for d in $dirs +do + npx proto-loader-gen-types \ + --keepCase \ + --longs=String \ + --enums=String \ + --defaults \ + --oneofs \ + --grpcLib=@grpc/grpc-js \ + --outDir=src/grpc/types/"$d" \ + proto/"$d"/*.proto || exit 1 +done # undo `sed` on the proto dir git restore proto @@ -46,10 +53,12 @@ done # replace any with unknown in some generated type sed -zi 's/{\n}/{ [k: string]: never }/g;s/Constructor extends new (...args: any) => any/Constructor extends new (...args: unknown[]) => unknown/g' **/*.ts -# array of names of the .ts files without their extension -protos=$(ls *.ts | sed 's/\..*$//') +cd ../../../proto -cd .. # src/grpc +# array of names of the .proto files without their extension +protos=$(ls **/*.proto | sed 's/\..*$//') + +cd ../src/grpc # header of generated file, notice one redirect symbol (>), this creates a fresh file with this content echo -e "// Generated file. Do not edit. Edit the template proto.ts.template instead.\n" > proto.ts @@ -57,11 +66,12 @@ echo -e "// Generated file. Do not edit. Edit the template proto.ts.template ins # loop through lines of the template file and echo lines to the generated file. >> is used here to append it cat proto.ts.template | while read line do - if echo "$line" | grep -i '{{name}}' > /dev/null 2>&1; then - # this line contains {{Name}} or {{name}} + if echo "$line" | grep -E '{{name}}|{{path}}' > /dev/null 2>&1; then + # this line contains {{name}} or {{path}} for p in $protos do - echo "$line" | sed "s/{{name}}/$p/g;s/{{Name}}/${p^}/g" + n=$(basename "$p") + echo "$line" | sed "s|{{name}}|${n^}|g;s|{{path}}|$p|g" done else # just write, nothing special diff --git a/proto/node.proto b/proto/cln/node.proto similarity index 100% rename from proto/node.proto rename to proto/cln/node.proto diff --git a/proto/primitives.proto b/proto/cln/primitives.proto similarity index 100% rename from proto/primitives.proto rename to proto/cln/primitives.proto diff --git a/src/grpc/proto.ts.template b/src/grpc/proto.ts.template index b597215b..e142c6c4 100644 --- a/src/grpc/proto.ts.template +++ b/src/grpc/proto.ts.template @@ -1,15 +1,15 @@ import * as grpc from '@grpc/grpc-js' import { loadSync, Options } from '@grpc/proto-loader' -import type { ProtoGrpcType as {{Name}}ProtoGrpcType } from './types/{{name}}' +import type { ProtoGrpcType as {{name}}ProtoGrpcType } from './types/{{path}}' process.env.GRPC_SSL_CIPHER_SUITES = 'HIGH+ECDSA' type ProtoName = - | '{{name}}' + | '{{path}}' type ProtoGrpcType = - | {{Name}}ProtoGrpcType + | {{name}}ProtoGrpcType const opts: Options = { keepCase: true, @@ -19,7 +19,7 @@ const opts: Options = { oneofs: true, } -export function loadProto(name: '{{name}}'): {{Name}}ProtoGrpcType +export function loadProto(name: '{{path}}'): {{name}}ProtoGrpcType export function loadProto(name: ProtoName): ProtoGrpcType export function loadProto(name: ProtoName): ProtoGrpcType { return grpc.loadPackageDefinition(