Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Fix nvm setup #241

Merged
merged 3 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions gbm-cli/pkg/release/gb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 0 additions & 4 deletions gbm-cli/pkg/release/gbm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 3 additions & 14 deletions gbm-cli/pkg/shell/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package shell
import (
"os"
"os/exec"
"strings"
)

type CmdProps struct {
Expand All @@ -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,
Expand Down
27 changes: 27 additions & 0 deletions gbm-cli/pkg/shell/npm.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package shell

import (
"os"
"os/exec"
"strings"
)

type NpmCmds interface {
Install(...string) error
Ci() error
Expand All @@ -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")
}
Expand Down
29 changes: 0 additions & 29 deletions gbm-cli/pkg/utils/utils.go

This file was deleted.

Loading