32 lines
796 B
Go
32 lines
796 B
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// ProbeTimeout bounds `node --version`.
|
|
const ProbeTimeout = 15 * time.Second
|
|
|
|
// ProbeNode reads the installed Node version: what `node --version` prints ("v26.10.0"), as RecipeNode is written.
|
|
func ProbeNode(ctx context.Context) (string, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, ProbeTimeout)
|
|
defer cancel()
|
|
out, err := exec.CommandContext(ctx, "node", "--version").Output()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return parseNodeVersion(string(out))
|
|
}
|
|
|
|
func parseNodeVersion(out string) (string, error) {
|
|
v := strings.TrimSpace(out)
|
|
if len(v) < 2 || v[0] != 'v' || v[1] < '0' || v[1] > '9' || strings.ContainsAny(v, " \n\t") {
|
|
return "", fmt.Errorf("`node --version` answered %q", v)
|
|
}
|
|
return v, nil
|
|
}
|