diff --git a/gbm-cli/pkg/release/gb.go b/gbm-cli/pkg/release/gb.go index 7ada822..4adcbbf 100644 --- a/gbm-cli/pkg/release/gb.go +++ b/gbm-cli/pkg/release/gb.go @@ -9,7 +9,6 @@ import ( "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/render" "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/repo" "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/shell" - "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/utils" ) func CreateGbPR(build Build) (gh.PullRequest, error) { @@ -152,10 +151,6 @@ func CreateGbPR(build Build) (gh.PullRequest, error) { console.Info("Setting up Gutenberg node environment") - if err := utils.SetupNode(dir); err != nil { - return pr, fmt.Errorf("error setting up the node environment: %v", err) - } - if err := npm.Install(); err != nil { return pr, fmt.Errorf("error running npm install: %v", err) } diff --git a/gbm-cli/pkg/release/gbm.go b/gbm-cli/pkg/release/gbm.go index 8df0975..7dca53e 100644 --- a/gbm-cli/pkg/release/gbm.go +++ b/gbm-cli/pkg/release/gbm.go @@ -11,7 +11,6 @@ import ( "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/gbm" "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/gh" "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/shell" - "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/utils" "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/render" "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/repo" @@ -69,9 +68,6 @@ func CreateGbmPR(build Build) (gh.PullRequest, error) { // Set up Gutenberg Mobile node environment console.Info("Setting up Node environment") npm := shell.NewNpmCmd(sp) - if err := utils.SetupNode(dir); err != nil { - return pr, fmt.Errorf("error setting up Node environment: %v", err) - } // Run npm ci and npm run bundle if err := npm.Ci(); err != nil { diff --git a/gbm-cli/pkg/shell/cmds.go b/gbm-cli/pkg/shell/cmds.go index 3e673a9..d1650c6 100644 --- a/gbm-cli/pkg/shell/cmds.go +++ b/gbm-cli/pkg/shell/cmds.go @@ -3,7 +3,6 @@ package shell import ( "os" "os/exec" - "strings" ) type CmdProps struct { @@ -29,22 +28,12 @@ func execute(cmd *exec.Cmd, dir string, verbose bool) error { func NewNpmCmd(cp CmdProps) NpmCmds { return &client{ cmd: func(cmds ...string) error { - var cmd *exec.Cmd - - // If we are running on a CI and NVM is available we run `nvm use` before each npm command - // to make sure we are using the correct node version - ci := os.Getenv("CI") - if ci == "true" && os.Getenv("NVM_DIR") != "" { - strCmds := strings.Join(cmds, " ") - cmd = exec.Command("bash", "-l", "-c", ". $NVM_DIR/nvm.sh && nvm use && npm "+strCmds) - } else { - cmd = exec.Command("npm", cmds...) - } - + cmd := switchNodeCmd(cmds...) + cmd.Env = os.Environ() return execute(cmd, cp.Dir, cp.Verbose) }, cmdInPath: func(path string, cmds ...string) error { - cmd := exec.Command("npm", cmds...) + cmd := switchNodeCmd(cmds...) return execute(cmd, path, cp.Verbose) }, dir: cp.Dir, diff --git a/gbm-cli/pkg/shell/npm.go b/gbm-cli/pkg/shell/npm.go index 993c8c2..ae7be99 100644 --- a/gbm-cli/pkg/shell/npm.go +++ b/gbm-cli/pkg/shell/npm.go @@ -1,5 +1,11 @@ package shell +import ( + "os" + "os/exec" + "strings" +) + type NpmCmds interface { Install(...string) error Ci() error @@ -9,6 +15,27 @@ type NpmCmds interface { VersionIn(string, string) error } +// Check to see if a node manager is available and set up the command accordingly +func switchNodeCmd(cmds ...string) *exec.Cmd { + + // Check if nvm is installed + if os.Getenv("NVM_DIR") != "" { + nvmCmd := "nvm use && npm " + strings.Join(cmds, " ") + nvmCheck := exec.Command("bash", "-l", "-c", "nvm") + if err := nvmCheck.Run(); err != nil { + // Load nvm before running npm + return exec.Command("bash", "-l", "-c", ". $NVM_DIR/nvm.sh && "+nvmCmd) + } else { + return exec.Command("bash", "-l", "-c", nvmCmd) + } + } + + // Other node managers can be added here... + + // Use system node + return exec.Command("npm", cmds...) +} + func (c *client) Ci() error { return c.cmd("ci") } diff --git a/gbm-cli/pkg/utils/utils.go b/gbm-cli/pkg/utils/utils.go deleted file mode 100644 index 57f0e88..0000000 --- a/gbm-cli/pkg/utils/utils.go +++ /dev/null @@ -1,29 +0,0 @@ -package utils - -import ( - "os" - "os/exec" - - "github.com/wordpress-mobile/release-toolkit-gutenberg-mobile/gbm-cli/pkg/console" -) - -func SetupNode(dir string) error { - var cmd *exec.Cmd - - // Check for nvm - if os.Getenv("NVM_DIR") != "" { - cmd = exec.Command("bash", "-l", "-c", ". $NVM_DIR/nvm.sh && nvm use") - cmd.Path = "/bin/bash" - } - - // @TODO check for asdf and set up the command accordingly - - if cmd == nil { - console.Warn("No node version manager found. Using system node.") - return nil - } - cmd.Dir = dir - cmd.Stdout = os.Stdout - cmd.Stderr = os.Stderr - return cmd.Run() -}