cmd/tsconnect: add npm publish workflow

Adds an on-demand GitHub Action that publishes the package to the npm
registry (currently under tailscale-connect, will be moved to
@tailscale/connect once we get control of the npm org).

Makes the package.json for the NPM package be dynamically generated to
have the current Tailscale client version.

Updates #5415

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2022-08-24 18:05:42 -07:00
committed by Mihai Parparita
parent d6c3588ed3
commit 2c18517121
4 changed files with 82 additions and 10 deletions

View File

@ -5,9 +5,15 @@
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"path"
esbuild "github.com/evanw/esbuild/pkg/api"
"github.com/tailscale/hujson"
"tailscale.com/version"
)
func runBuildPkg() {
@ -41,4 +47,33 @@ func runBuildPkg() {
log.Fatalf("Type generation failed: %v", err)
}
if err := updateVersion(); err != nil {
log.Fatalf("Cannot update version: %v", err)
}
log.Printf("Built package version %s", version.Long)
}
func updateVersion() error {
packageJSONBytes, err := os.ReadFile("package.json.tmpl")
if err != nil {
return fmt.Errorf("Could not read package.json: %w", err)
}
var packageJSON map[string]any
packageJSONBytes, err = hujson.Standardize(packageJSONBytes)
if err != nil {
return fmt.Errorf("Could not standardize template package.json: %w", err)
}
if err := json.Unmarshal(packageJSONBytes, &packageJSON); err != nil {
return fmt.Errorf("Could not unmarshal package.json: %w", err)
}
packageJSON["version"] = version.Long
packageJSONBytes, err = json.MarshalIndent(packageJSON, "", " ")
if err != nil {
return fmt.Errorf("Could not marshal package.json: %w", err)
}
return os.WriteFile(path.Join(*pkgDir, "package.json"), packageJSONBytes, 0644)
}