diff --git a/internal/generator/runner.go b/internal/generator/runner.go index ea990d3..99c04dd 100644 --- a/internal/generator/runner.go +++ b/internal/generator/runner.go @@ -75,5 +75,9 @@ func (r *Runner) Run(plugin *protogen.Plugin) error { r.generateTypescriptFile(file, generatedFileStream) } + // Generate file with shared protobuf Root instance + rootFileStream := plugin.NewGeneratedFile("root.ts", "") + r.generateRoot(rootFileStream) + return nil } diff --git a/internal/generator/runner_generate.go b/internal/generator/runner_generate.go index dbbbb03..51bb30c 100644 --- a/internal/generator/runner_generate.go +++ b/internal/generator/runner_generate.go @@ -8,6 +8,7 @@ import ( "fmt" "sort" "strconv" + "strings" "github.com/iancoleman/strcase" "github.com/join-com/protoc-gen-ts/internal/join_proto" @@ -26,6 +27,7 @@ func (r *Runner) generateTypescriptFile(protoFile *protogen.File, generatedFileS ) r.generateTypescriptImports(protoFile, generatedFileStream) + r.generateRootChange(protoFile, generatedFileStream) r.generateTypescriptNamespace(generatedFileStream, protoFile) } @@ -63,13 +65,23 @@ func (r *Runner) generateTypescriptImports(protoFile *protogen.File, generatedFi for _, importLine := range importLines { generatedFileStream.P(importLine) } - generatedFileStream.P("") if len(protoFile.Proto.GetService()) > 0 { - generatedFileStream.P("import { grpc } from '@join-com/grpc'\n") + generatedFileStream.P("import { grpc } from '@join-com/grpc'") + } + + numStepsBack := len(strings.Split(currentSourcePath, "/")) - 1 + if numStepsBack == 0 { + generatedFileStream.P("import { root } from './root'\n") + } else { + generatedFileStream.P("import { root } from '" + strings.Repeat("../", numStepsBack) + "root'\n") } } +func (r *Runner) generateRootChange(protoFile *protogen.File, generatedFileStream *protogen.GeneratedFile) { + generatedFileStream.P("protobufjs.roots['decorated'] = root\n") +} + func (r *Runner) fileHasFlavors(generatedFileStream *protogen.GeneratedFile, protoFile *protogen.File) bool { for _, messageSpec := range protoFile.Proto.GetMessageType() { for _, fieldSpec := range messageSpec.GetField() { diff --git a/internal/generator/runner_generate_root.go b/internal/generator/runner_generate_root.go new file mode 100644 index 0000000..416d1d4 --- /dev/null +++ b/internal/generator/runner_generate_root.go @@ -0,0 +1,12 @@ +package generator + +import ( + "google.golang.org/protobuf/compiler/protogen" +) + +func (r *Runner) generateRoot(generatedFileStream *protogen.GeneratedFile) { + generatedFileStream.P( + "import { Root } from 'protobufjs'\n\n", + "export const root = new Root()", + ) +} diff --git a/tests/__tests__/generated/Flavors.ts b/tests/__tests__/generated/Flavors.ts index 2918a44..3caef81 100644 --- a/tests/__tests__/generated/Flavors.ts +++ b/tests/__tests__/generated/Flavors.ts @@ -1,13 +1,15 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' import * as protobufjs from 'protobufjs/light' import { WithFlavor } from '@coderspirit/nominal' - import { grpc } from '@join-com/grpc' +import { root } from './root' + +protobufjs.roots['decorated'] = root // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Flavors { diff --git a/tests/__tests__/generated/Regressions.ts b/tests/__tests__/generated/Regressions.ts index 5f6deb0..f833dc2 100644 --- a/tests/__tests__/generated/Regressions.ts +++ b/tests/__tests__/generated/Regressions.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from './root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Regressions { interface ConvertibleTo { diff --git a/tests/__tests__/generated/Test.ts b/tests/__tests__/generated/Test.ts index 4345a4f..5f2886e 100644 --- a/tests/__tests__/generated/Test.ts +++ b/tests/__tests__/generated/Test.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' @@ -9,8 +9,10 @@ import { Common as Common_Common } from './common/Common' import { Common as Common_Extra } from './common/Extra' import { GoogleProtobuf as GoogleProtobuf_Empty } from './google/protobuf/Empty' import { GoogleProtobuf as GoogleProtobuf_Timestamp } from './google/protobuf/Timestamp' - import { grpc } from '@join-com/grpc' +import { root } from './root' + +protobufjs.roots['decorated'] = root // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Foo { diff --git a/tests/__tests__/generated/common/Common.ts b/tests/__tests__/generated/common/Common.ts index 71736f7..7c84dae 100644 --- a/tests/__tests__/generated/common/Common.ts +++ b/tests/__tests__/generated/common/Common.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from '../root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { interface ConvertibleTo { diff --git a/tests/__tests__/generated/common/Extra.ts b/tests/__tests__/generated/common/Extra.ts index dc0fb1e..339a7a9 100644 --- a/tests/__tests__/generated/common/Extra.ts +++ b/tests/__tests__/generated/common/Extra.ts @@ -1,10 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' import { GoogleProtobuf } from '../google/protobuf/Timestamp' +import { root } from '../root' + +protobufjs.roots['decorated'] = root // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { diff --git a/tests/__tests__/generated/google/protobuf/Empty.ts b/tests/__tests__/generated/google/protobuf/Empty.ts index a01644a..7550cf5 100644 --- a/tests/__tests__/generated/google/protobuf/Empty.ts +++ b/tests/__tests__/generated/google/protobuf/Empty.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from '../../root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { interface ConvertibleTo { diff --git a/tests/__tests__/generated/google/protobuf/Timestamp.ts b/tests/__tests__/generated/google/protobuf/Timestamp.ts index ebf8172..fd186a7 100644 --- a/tests/__tests__/generated/google/protobuf/Timestamp.ts +++ b/tests/__tests__/generated/google/protobuf/Timestamp.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from '../../root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { interface ConvertibleTo { diff --git a/tests/__tests__/generated/root.ts b/tests/__tests__/generated/root.ts new file mode 100644 index 0000000..99cca6f --- /dev/null +++ b/tests/__tests__/generated/root.ts @@ -0,0 +1,3 @@ +import { Root } from 'protobufjs' + +export const root = new Root() diff --git a/tests/__tests__/generatedRedundant/Flavors.ts b/tests/__tests__/generatedRedundant/Flavors.ts index 2918a44..3caef81 100644 --- a/tests/__tests__/generatedRedundant/Flavors.ts +++ b/tests/__tests__/generatedRedundant/Flavors.ts @@ -1,13 +1,15 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' import * as protobufjs from 'protobufjs/light' import { WithFlavor } from '@coderspirit/nominal' - import { grpc } from '@join-com/grpc' +import { root } from './root' + +protobufjs.roots['decorated'] = root // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Flavors { diff --git a/tests/__tests__/generatedRedundant/Regressions.ts b/tests/__tests__/generatedRedundant/Regressions.ts index 5f6deb0..f833dc2 100644 --- a/tests/__tests__/generatedRedundant/Regressions.ts +++ b/tests/__tests__/generatedRedundant/Regressions.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from './root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Regressions { interface ConvertibleTo { diff --git a/tests/__tests__/generatedRedundant/Test.ts b/tests/__tests__/generatedRedundant/Test.ts index 4345a4f..5f2886e 100644 --- a/tests/__tests__/generatedRedundant/Test.ts +++ b/tests/__tests__/generatedRedundant/Test.ts @@ -1,5 +1,5 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as joinGRPC from '@join-com/grpc' @@ -9,8 +9,10 @@ import { Common as Common_Common } from './common/Common' import { Common as Common_Extra } from './common/Extra' import { GoogleProtobuf as GoogleProtobuf_Empty } from './google/protobuf/Empty' import { GoogleProtobuf as GoogleProtobuf_Timestamp } from './google/protobuf/Timestamp' - import { grpc } from '@join-com/grpc' +import { root } from './root' + +protobufjs.roots['decorated'] = root // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Foo { diff --git a/tests/__tests__/generatedRedundant/common/Common.ts b/tests/__tests__/generatedRedundant/common/Common.ts index 71736f7..7c84dae 100644 --- a/tests/__tests__/generatedRedundant/common/Common.ts +++ b/tests/__tests__/generatedRedundant/common/Common.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from '../root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { interface ConvertibleTo { diff --git a/tests/__tests__/generatedRedundant/common/Extra.ts b/tests/__tests__/generatedRedundant/common/Extra.ts index dc0fb1e..339a7a9 100644 --- a/tests/__tests__/generatedRedundant/common/Extra.ts +++ b/tests/__tests__/generatedRedundant/common/Extra.ts @@ -1,10 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' import { GoogleProtobuf } from '../google/protobuf/Timestamp' +import { root } from '../root' + +protobufjs.roots['decorated'] = root // eslint-disable-next-line @typescript-eslint/no-namespace export namespace Common { diff --git a/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts b/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts index a01644a..7550cf5 100644 --- a/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts +++ b/tests/__tests__/generatedRedundant/google/protobuf/Empty.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from '../../root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { interface ConvertibleTo { diff --git a/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts b/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts index ebf8172..fd186a7 100644 --- a/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts +++ b/tests/__tests__/generatedRedundant/google/protobuf/Timestamp.ts @@ -1,9 +1,13 @@ // GENERATED CODE -- DO NOT EDIT! -// GENERATOR VERSION: 2.1.0.9e5a89f.1645085209 +// GENERATOR VERSION: 2.1.0.e9910b5.1646051017 /* eslint-disable @typescript-eslint/no-non-null-assertion */ import * as protobufjs from 'protobufjs/light' +import { root } from '../../root' + +protobufjs.roots['decorated'] = root + // eslint-disable-next-line @typescript-eslint/no-namespace export namespace GoogleProtobuf { interface ConvertibleTo { diff --git a/tests/__tests__/generatedRedundant/root.ts b/tests/__tests__/generatedRedundant/root.ts new file mode 100644 index 0000000..99cca6f --- /dev/null +++ b/tests/__tests__/generatedRedundant/root.ts @@ -0,0 +1,3 @@ +import { Root } from 'protobufjs' + +export const root = new Root()