Skip to content

Commit af3218f

Browse files
author
Jon Skeet
authored
feat(nodejs): add a DefaultVersion field to NodeJSPackage (#6358)
Adds a DefaultVersion field to the Node config, which is then written to .repo-metadata.json, overriding the default of "the first API path variant". The combine-libraries part of the post-processor is also provided with the default version to create the appropriate index.ts. Like Python, our Node packages have the concept of a "default version", which is the API variant which is imported implicitly. See #6357 for further work we'll need to consider post-migration.
1 parent 2b6d7e4 commit af3218f

4 files changed

Lines changed: 103 additions & 4 deletions

File tree

doc/config-schema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ This document describes the schema for the librarian.yaml.
370370
| :--- | :--- | :--- |
371371
| `additional_protos` | list of string | Is a list of additional proto files to include in generation. This can be overridden at the API level. |
372372
| `bundle_config` | string | Is the path to a GAPIC bundle config file. |
373+
| `default_version` | string | Is the default version of the API to use. When omitted, the version in the first API path is used. |
373374
| `dependencies` | map[string]string | Maps npm package names to version constraints. |
374375
| `esm` | bool | Indicates that generation should produce ES Modules (ESM) outputs. |
375376
| `extra_protoc_parameters` | list of string | Is a list of extra parameters to pass to protoc. |

internal/config/language.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,10 @@ type NodejsPackage struct {
742742
// BundleConfig is the path to a GAPIC bundle config file.
743743
BundleConfig string `yaml:"bundle_config,omitempty"`
744744

745+
// DefaultVersion is the default version of the API to use. When omitted,
746+
// the version in the first API path is used.
747+
DefaultVersion string `yaml:"default_version,omitempty"`
748+
745749
// Dependencies maps npm package names to version constraints.
746750
Dependencies map[string]string `yaml:"dependencies,omitempty"`
747751

internal/librarian/nodejs/generate.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ func runPostProcessor(ctx context.Context, cfg *config.Config, library *config.L
286286
"combine-library",
287287
"--source-path", stagingDir,
288288
"--destination-path", outDir,
289+
"--default-version", resolveDefaultVersion(library),
289290
}
290291
if library.Nodejs != nil && library.Nodejs.ESM {
291292
combineArgs = append(combineArgs, "--is-esm")
@@ -498,8 +499,8 @@ func writeRepoMetadata(cfg *config.Config, library *config.Library, googleapisDi
498499
return err
499500
}
500501
metadata.DistributionName = DerivePackageName(library)
501-
metadata.DefaultVersion = filepath.Base(library.APIs[0].Path)
502502
metadata.LibraryType = repometadata.GAPICAutoLibraryType
503+
metadata.DefaultVersion = resolveDefaultVersion(library)
503504

504505
if strings.HasPrefix(metadata.DistributionName, "@google-cloud/") {
505506
pkgSuffix := strings.TrimPrefix(metadata.DistributionName, "@google-cloud/")
@@ -668,6 +669,20 @@ func DefaultOutput(name, defaultOutput string) string {
668669
return filepath.Join(defaultOutput, name)
669670
}
670671

672+
// resolveDefaultVersion returns the default API version (v1, v1beta etc) for
673+
// a library, using the Node-specific override if present, or the path of the
674+
// first API otherwise. If the library has no override and no APIs, an empty
675+
// string is returned.
676+
func resolveDefaultVersion(library *config.Library) string {
677+
if library.Nodejs != nil && library.Nodejs.DefaultVersion != "" {
678+
return library.Nodejs.DefaultVersion
679+
}
680+
if len(library.APIs) == 0 {
681+
return ""
682+
}
683+
return filepath.Base(library.APIs[0].Path)
684+
}
685+
671686
// TODO(https://github.com/googleapis/google-cloud-node/issues/8149):
672687
// This function is a temporary workaround to preserve v1small exports in the compute library.
673688
// It must be deleted once v1small is formally deprecated and removed.

internal/librarian/nodejs/generate_test.go

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,10 @@ func TestRunPostProcessor(t *testing.T) {
443443
testhelper.RequireCommand(t, "compileProtos")
444444

445445
repoRoot := t.TempDir()
446-
library := &config.Library{Name: "google-cloud-secretmanager"}
446+
library := &config.Library{
447+
Name: "google-cloud-secretmanager",
448+
APIs: []*config.API{{Path: "google/cloud/secretmanager/v1"}},
449+
}
447450
outDir := filepath.Join(repoRoot, "packages", library.Name)
448451
if err := os.MkdirAll(outDir, 0755); err != nil {
449452
t.Fatal(err)
@@ -473,7 +476,10 @@ func TestRunPostProcessor_RemovesOwlBotYaml(t *testing.T) {
473476
testhelper.RequireCommand(t, "compileProtos")
474477

475478
repoRoot := t.TempDir()
476-
library := &config.Library{Name: "google-cloud-test"}
479+
library := &config.Library{
480+
Name: "google-cloud-test",
481+
APIs: []*config.API{{Path: "google/cloud/test/v1"}},
482+
}
477483
outDir := filepath.Join(repoRoot, "packages", library.Name)
478484
if err := os.MkdirAll(outDir, 0755); err != nil {
479485
t.Fatal(err)
@@ -513,7 +519,10 @@ func TestRunPostProcessor_RemovesCloudCommonResourcesProto(t *testing.T) {
513519
testhelper.RequireCommand(t, "compileProtos")
514520

515521
repoRoot := t.TempDir()
516-
library := &config.Library{Name: "google-cloud-test"}
522+
library := &config.Library{
523+
Name: "google-cloud-test",
524+
APIs: []*config.API{{Path: "google/cloud/test/v1"}},
525+
}
517526
outDir := filepath.Join(repoRoot, "packages", library.Name)
518527
if err := os.MkdirAll(outDir, 0755); err != nil {
519528
t.Fatal(err)
@@ -557,6 +566,7 @@ func TestRunPostProcessor_CustomScripts(t *testing.T) {
557566
repoRoot := t.TempDir()
558567
library := &config.Library{
559568
Name: "google-cloud-secretmanager",
569+
APIs: []*config.API{{Path: "google/cloud/secretmanager/v1"}},
560570
Keep: []string{"librarian.js", ".readme-partials.yaml", "README.md"},
561571
}
562572
outDir := filepath.Join(repoRoot, "packages", library.Name)
@@ -640,6 +650,7 @@ func TestRunPostProcessor_PreservesFiles(t *testing.T) {
640650
repoRoot := t.TempDir()
641651
library := &config.Library{
642652
Name: "google-cloud-test",
653+
APIs: []*config.API{{Path: "google/cloud/test/v1"}},
643654
Keep: []string{"README.md", ".readme-partials.yaml", "system-test/.eslintrc.yml"},
644655
}
645656
outDir := filepath.Join(repoRoot, "packages", library.Name)
@@ -1042,6 +1053,29 @@ func TestWriteRepoMetadata(t *testing.T) {
10421053
return w
10431054
},
10441055
},
1056+
{
1057+
name: "default version override",
1058+
library: &config.Library{
1059+
Name: "google-cloud-secretmanager",
1060+
APIs: []*config.API{
1061+
{Path: "google/cloud/secretmanager/v1"},
1062+
{Path: "google/cloud/secretmanager/v1beta"},
1063+
},
1064+
Nodejs: &config.NodejsPackage{
1065+
DefaultVersion: "v1beta",
1066+
},
1067+
},
1068+
want: func() *repometadata.RepoMetadata {
1069+
w := sample.RepoMetadata()
1070+
w.DistributionName = "@google-cloud/secretmanager"
1071+
w.Language = cfg.Language
1072+
w.Repo = cfg.Repo
1073+
w.ClientDocumentation = "https://cloud.google.com/nodejs/docs/reference/secretmanager/latest"
1074+
w.ProductDocumentation = "https://cloud.google.com/secret-manager/docs"
1075+
w.DefaultVersion = "v1beta"
1076+
return w
1077+
},
1078+
},
10451079
} {
10461080
t.Run(test.name, func(t *testing.T) {
10471081
outDir := t.TempDir()
@@ -1076,6 +1110,7 @@ func TestRunPostProcessor_CustomScripts_RootRelativePath(t *testing.T) {
10761110
repoRoot := t.TempDir()
10771111
library := &config.Library{
10781112
Name: "google-cloud-secretmanager",
1113+
APIs: []*config.API{{Path: "google/cloud/secretmanager/v1"}},
10791114
Keep: []string{"librarian.js"},
10801115
}
10811116

@@ -1450,3 +1485,47 @@ func TestRemoveRedundantLinterFiles(t *testing.T) {
14501485
})
14511486
}
14521487
}
1488+
1489+
func TestResolveDefaultVersion(t *testing.T) {
1490+
for _, test := range []struct {
1491+
name string
1492+
lib *config.Library
1493+
want string
1494+
}{
1495+
{
1496+
name: "default to first API path",
1497+
lib: &config.Library{
1498+
APIs: []*config.API{
1499+
{Path: "google/cloud/test/v3"},
1500+
{Path: "google/cloud/test/v4"},
1501+
},
1502+
},
1503+
want: "v3",
1504+
},
1505+
{
1506+
name: "no APIs or override",
1507+
lib: &config.Library{},
1508+
want: "",
1509+
},
1510+
{
1511+
name: "override",
1512+
lib: &config.Library{
1513+
APIs: []*config.API{
1514+
{Path: "google/cloud/test/v3"},
1515+
{Path: "google/cloud/test/v4"},
1516+
},
1517+
Nodejs: &config.NodejsPackage{
1518+
DefaultVersion: "v4",
1519+
},
1520+
},
1521+
want: "v4",
1522+
},
1523+
} {
1524+
t.Run(test.name, func(t *testing.T) {
1525+
got := resolveDefaultVersion(test.lib)
1526+
if diff := cmp.Diff(test.want, got); diff != "" {
1527+
t.Errorf("mismatch (-want +got):\n%s", diff)
1528+
}
1529+
})
1530+
}
1531+
}

0 commit comments

Comments
 (0)