shell/cmd/node.go

39 lines
703 B
Go
Raw Permalink Normal View History

2024-07-05 17:28:33 +02:00
package cmd
import (
"errors"
"fmt"
"os/exec"
"strings"
)
var NodeNotFound = errors.New("nodejs not found")
2024-07-10 00:53:22 +02:00
func Node(options BasicOptions, args ...string) (output string, err error) {
2024-07-05 17:28:33 +02:00
2024-07-10 00:53:22 +02:00
if _, err := Which("node", BasicOptions{
Env: options.Env,
Sources: options.Sources,
Cwd: options.Cwd,
}); err != nil {
2024-07-05 17:28:33 +02:00
if errors.Is(err, ErrNotFound) {
return "", NodeNotFound
} else {
return "", err
}
}
command := exec.Command("node", args...)
outputBytes, err := command.Output()
if err != nil {
var exitErr *exec.ExitError
if errors.As(err, &exitErr) {
return "", fmt.Errorf("command error: %w", err)
2024-07-05 17:28:33 +02:00
}
}
return strings.Trim(string(outputBytes), "\n"), nil
}