Skip to content

Commit

Permalink
[CLI] call nvm use before each npm call on CI (#220)
Browse files Browse the repository at this point in the history
* use the current directory for the workspace on CI

* Clean up CI check

* run nvm use before npm commands if on CI

* Update prefixing nvm command
  • Loading branch information
jhnstn authored Nov 8, 2023
1 parent 98d48a7 commit 0001131
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion gbm-cli/cmd/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewWorkspace() (Workspace, error) {
w.disabled = true
}

if _, ci := os.LookupEnv("CI"); ci {
if os.Getenv("CI") == "true" {
console.Info("CI environment detected, not creating a workspace directory")
w.disabled = true
w.dir = "."
Expand Down
14 changes: 13 additions & 1 deletion gbm-cli/pkg/shell/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package shell
import (
"os"
"os/exec"
"strings"
)

type CmdProps struct {
Expand All @@ -28,7 +29,18 @@ func execute(cmd *exec.Cmd, dir string, verbose bool) error {
func NewNpmCmd(cp CmdProps) NpmCmds {
return &client{
cmd: func(cmds ...string) error {
cmd := exec.Command("npm", cmds...)
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...)
}

return execute(cmd, cp.Dir, cp.Verbose)
},
cmdInPath: func(path string, cmds ...string) error {
Expand Down

0 comments on commit 0001131

Please sign in to comment.