github.com/vugu/vugu@v0.3.6-0.20240430171613-3f6f402e014b/distutil/wasm-exec-js.go (about)

     1  package distutil
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // WasmExecJsPath find wasm_exec.js in the local Go distribution and return it's path.
    12  // Return error if not found.
    13  func WasmExecJsPath() (string, error) {
    14  
    15  	b, err := exec.Command("go", "env", "GOROOT").CombinedOutput()
    16  	if err != nil {
    17  		return "", err
    18  	}
    19  	bstr := strings.TrimSpace(string(b))
    20  	if bstr == "" {
    21  		return "", fmt.Errorf("failed to find wasm_exec.js, empty path from `go env GOROOT`")
    22  	}
    23  
    24  	p := filepath.Join(bstr, "misc/wasm/wasm_exec.js")
    25  	_, err = os.Stat(p)
    26  	if err != nil {
    27  		return "", err
    28  	}
    29  
    30  	return p, nil
    31  }
    32  
    33  // MustWasmExecJsPath find wasm_exec.js in the local Go distribution and return it's path.
    34  // Panic if not found.
    35  func MustWasmExecJsPath() string {
    36  	s, err := WasmExecJsPath()
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  	return s
    41  }