2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-08-24 01:05:23 +00:00
|
|
|
|
2023-08-24 22:02:42 +00:00
|
|
|
//go:build !plan9
|
|
|
|
|
2022-08-24 01:05:23 +00:00
|
|
|
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
|
|
|
|
2022-08-25 01:05:42 +00:00
|
|
|
"github.com/tailscale/hujson"
|
2022-10-14 21:52:45 +00:00
|
|
|
"tailscale.com/util/precompress"
|
2022-08-25 01:05:42 +00:00
|
|
|
"tailscale.com/version"
|
2022-08-24 01:05:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func runBuildPkg() {
|
2022-08-31 01:05:49 +00:00
|
|
|
buildOptions, err := commonPkgSetup(prodMode)
|
2022-08-24 01:05:23 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-24 20:38:47 +00:00
|
|
|
if err := cleanDir(*pkgDir); err != nil {
|
2022-08-24 01:05:23 +00:00
|
|
|
log.Fatalf("Cannot clean %s: %v", *pkgDir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildOptions.Write = true
|
|
|
|
buildOptions.MinifyWhitespace = true
|
|
|
|
buildOptions.MinifyIdentifiers = true
|
|
|
|
buildOptions.MinifySyntax = true
|
|
|
|
|
|
|
|
runEsbuild(*buildOptions)
|
|
|
|
|
2022-10-14 21:52:45 +00:00
|
|
|
if err := precompressWasm(); err != nil {
|
|
|
|
log.Fatalf("Could not pre-recompress wasm: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-08-24 01:05:23 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-10-24 20:38:47 +00:00
|
|
|
if err := copyReadme(); err != nil {
|
|
|
|
log.Fatalf("Cannot copy readme: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-02-11 06:20:36 +00:00
|
|
|
log.Printf("Built package version %s", version.Long())
|
2022-08-25 01:05:42 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 21:52:45 +00:00
|
|
|
func precompressWasm() error {
|
|
|
|
log.Printf("Pre-compressing main.wasm...\n")
|
|
|
|
return precompress.Precompress(path.Join(*pkgDir, "main.wasm"), precompress.Options{
|
|
|
|
FastCompression: *fastCompression,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-25 01:05:42 +00:00
|
|
|
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)
|
|
|
|
}
|
2023-02-11 06:20:36 +00:00
|
|
|
packageJSON["version"] = version.Long()
|
2022-08-25 01:05:42 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
2022-10-24 20:38:47 +00:00
|
|
|
|
|
|
|
func copyReadme() error {
|
|
|
|
readmeBytes, err := os.ReadFile("README.pkg.md")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Could not read README.pkg.md: %w", err)
|
|
|
|
}
|
|
|
|
return os.WriteFile(path.Join(*pkgDir, "README.md"), readmeBytes, 0644)
|
|
|
|
}
|