2022-08-24 01:05:23 +00:00
|
|
|
// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-25 01:05:42 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2022-08-24 01:05:23 +00:00
|
|
|
"log"
|
2022-08-25 01:05:42 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2022-08-24 01:05:23 +00:00
|
|
|
|
|
|
|
esbuild "github.com/evanw/esbuild/pkg/api"
|
2022-08-25 01:05:42 +00:00
|
|
|
"github.com/tailscale/hujson"
|
|
|
|
"tailscale.com/version"
|
2022-08-24 01:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func runBuildPkg() {
|
|
|
|
buildOptions, err := commonSetup(prodMode)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Cannot setup: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Linting...\n")
|
|
|
|
if err := runYarn("lint"); err != nil {
|
|
|
|
log.Fatalf("Linting failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cleanDir(*pkgDir, "package.json"); err != nil {
|
|
|
|
log.Fatalf("Cannot clean %s: %v", *pkgDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildOptions.EntryPoints = []string{"src/pkg/pkg.ts", "src/pkg/pkg.css"}
|
|
|
|
buildOptions.Outdir = *pkgDir
|
|
|
|
buildOptions.Format = esbuild.FormatESModule
|
|
|
|
buildOptions.AssetNames = "[name]"
|
|
|
|
buildOptions.Write = true
|
|
|
|
buildOptions.MinifyWhitespace = true
|
|
|
|
buildOptions.MinifyIdentifiers = true
|
|
|
|
buildOptions.MinifySyntax = true
|
|
|
|
|
|
|
|
runEsbuild(*buildOptions)
|
|
|
|
|
|
|
|
log.Printf("Generating types...\n")
|
|
|
|
if err := runYarn("pkg-types"); err != nil {
|
|
|
|
log.Fatalf("Type generation failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:05:42 +00:00
|
|
|
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)
|
2022-08-24 01:05:23 +00:00
|
|
|
}
|