Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

refactor(runtime): split npm args and stage activation out of installNPMCLI (#487)

fx-kurumi0602a18d4bab
diff --git a/internal/runtime/cli_ondemand.go b/internal/runtime/cli_ondemand.go
index 185ba7dd..b6b18fcb 100644
--- a/internal/runtime/cli_ondemand.go
+++ b/internal/runtime/cli_ondemand.go
@@ -153,21 +153,31 @@ func installNPMCLI(ctx context.Context, spec onDemandCLI) (string, error) {
if err := os.Chmod(stage, 0o755); err != nil {
return "", err
}
+ cmd := exec.CommandContext(ctx, "npm", npmCLIInstallArgs(stage, spec)...)
+ cmd.Dir = stage
+ if out, err := cmd.CombinedOutput(); err != nil {
+ return "", errors.New(installCommandError(ctx, err, out))
+ }
+ return activateNPMStage(parent, stage, spec.Binary)
+}
+
+// npmCLIInstallArgs builds the npm install argv for installing spec into stage.
+func npmCLIInstallArgs(stage string, spec onDemandCLI) []string {
args := []string{"install", "--prefix", stage, "--no-audit", "--no-fund", "--omit=dev", "--save-exact"}
if spec.RelaxEngines {
args = append(args, "--engine-strict=false")
} else {
args = append(args, "--engine-strict")
}
- args = append(args, spec.Package)
- cmd := exec.CommandContext(ctx, "npm", args...)
- cmd.Dir = stage
- if out, err := cmd.CombinedOutput(); err != nil {
- return "", errors.New(installCommandError(ctx, err, out))
- }
- staged := filepath.Join(stage, "node_modules", ".bin", spec.Binary)
+ return append(args, spec.Package)
+}
+
+// activateNPMStage verifies the staged binary, replaces parent/current with
+// stage and returns the binary path inside the activated directory.
+func activateNPMStage(parent, stage, binary string) (string, error) {
+ staged := filepath.Join(stage, "node_modules", ".bin", binary)
if !fileIsExecutable(staged) {
- return "", fmt.Errorf("安装后没有找到可执行文件 %s", spec.Binary)
+ return "", fmt.Errorf("安装后没有找到可执行文件 %s", binary)
}
target := filepath.Join(parent, "current")
if err := os.RemoveAll(target); err != nil {
@@ -176,7 +186,7 @@ func installNPMCLI(ctx context.Context, spec onDemandCLI) (string, error) {
if err := os.Rename(stage, target); err != nil {
return "", fmt.Errorf("启用安装结果失败:%w", err)
}
- return filepath.Join(target, "node_modules", ".bin", spec.Binary), nil
+ return filepath.Join(target, "node_modules", ".bin", binary), nil
}
func installScriptCLI(ctx context.Context, spec onDemandCLI) (string, error) {
diff --git a/internal/runtime/cli_ondemand_npm_test.go b/internal/runtime/cli_ondemand_npm_test.go
new file mode 100644
index 00000000..c0c8799b
--- /dev/null
+++ b/internal/runtime/cli_ondemand_npm_test.go
@@ -0,0 +1,68 @@
+package runtime
+
+import (
+ "os"
+ "path/filepath"
+ "reflect"
+ "strings"
+ "testing"
+)
+
+func TestNPMCLIInstallArgs(t *testing.T) {
+ base := []string{"install", "--prefix", "/stage", "--no-audit", "--no-fund", "--omit=dev", "--save-exact"}
+ strict := npmCLIInstallArgs("/stage", onDemandCLI{Package: "[email protected]"})
+ if want := append(append([]string{}, base...), "--engine-strict", "[email protected]"); !reflect.DeepEqual(strict, want) {
+ t.Fatalf("strict args = %v, want %v", strict, want)
+ }
+ relaxed := npmCLIInstallArgs("/stage", onDemandCLI{Package: "dsh@2", RelaxEngines: true})
+ if want := append(append([]string{}, base...), "--engine-strict=false", "dsh@2"); !reflect.DeepEqual(relaxed, want) {
+ t.Fatalf("relaxed args = %v, want %v", relaxed, want)
+ }
+}
+
+func TestActivateNPMStageReplacesCurrent(t *testing.T) {
+ parent := t.TempDir()
+ old := filepath.Join(parent, "current", "stale.txt")
+ if err := os.MkdirAll(filepath.Dir(old), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(old, []byte("old"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ stage := filepath.Join(parent, ".install-x")
+ bin := filepath.Join(stage, "node_modules", ".bin")
+ if err := os.MkdirAll(bin, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(bin, "tool"), []byte("#!/bin/sh\n"), 0o755); err != nil {
+ t.Fatal(err)
+ }
+ path, err := activateNPMStage(parent, stage, "tool")
+ if err != nil {
+ t.Fatal(err)
+ }
+ if want := filepath.Join(parent, "current", "node_modules", ".bin", "tool"); path != want || !fileIsExecutable(path) {
+ t.Fatalf("path = %q, want executable %q", path, want)
+ }
+ if _, err := os.Stat(old); !os.IsNotExist(err) {
+ t.Fatalf("stale current content should be removed, stat err = %v", err)
+ }
+ if _, err := os.Stat(stage); !os.IsNotExist(err) {
+ t.Fatalf("stage should be renamed away, stat err = %v", err)
+ }
+}
+
+func TestActivateNPMStageMissingBinary(t *testing.T) {
+ parent := t.TempDir()
+ stage := filepath.Join(parent, ".install-y")
+ if err := os.MkdirAll(stage, 0o755); err != nil {
+ t.Fatal(err)
+ }
+ _, err := activateNPMStage(parent, stage, "tool")
+ if err == nil || !strings.Contains(err.Error(), "安装后没有找到可执行文件 tool") {
+ t.Fatalf("err = %v", err)
+ }
+ if _, err := os.Stat(filepath.Join(parent, "current")); !os.IsNotExist(err) {
+ t.Fatalf("current must not be created on failure, stat err = %v", err)
+ }
+}