Skip to content

Commit

Permalink
Add nvm version lock/set
Browse files Browse the repository at this point in the history
  • Loading branch information
AutomationD committed Aug 8, 2024
1 parent 078028c commit d676a58
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 15 deletions.
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ func InitConfig() {

// TODO: those static defaults should probably go to a separate package and/or function. Also would include image names and such.
viper.SetDefault("TERRAFORM_VERSION", "1.1.3")
viper.SetDefault("NVM_VERSION", "0.39.7")
viper.SetDefault("PREFER_RUNTIME", "native")
viper.SetDefault("CUSTOM_PROMPT", false)
viper.SetDefault("PLAIN_TEXT_OUTPUT", false)
Expand Down
1 change: 1 addition & 0 deletions internal/config/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Project struct {
EndpointUrl string `mapstructure:"endpoint_url,omitempty"`
LocalStack bool `mapstructure:"localstack,omitempty"`
SshPublicKey string `mapstructure:"ssh_public_key,omitempty"`
NvmVersion string `mapstructure:"nvm_version"`

Home string `mapstructure:"home,omitempty"`
RootDir string `mapstructure:"root_dir,omitempty"`
Expand Down
67 changes: 52 additions & 15 deletions internal/manager/serverless/native.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,13 @@ func (sls *Manager) runNpmInstall(w io.Writer) error {
}

func (sls *Manager) nvm(w io.Writer, command string) error {
nvmDir := os.Getenv("NVM_DIR")
if len(nvmDir) == 0 {
nvmDir = "$HOME/.nvm"
}

// Check if nvm.sh exists in the nvmDir
nvmShPath := filepath.Join(nvmDir, "nvm.sh")
if _, err := os.Stat(nvmShPath); os.IsNotExist(err) {
logrus.Debugf("nvm.sh does not exist in the directory:", nvmDir)
return err
} else if err != nil {
logrus.Debugf("Error checking nvm.sh:", err)
nvmDir, err := sls.installNvm()
if err != nil {
return err
}

// TODO: If nvm.sh doesn't exist in the nvmDir, we should install it
// curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash

err := sls.readNvmrc()
err = sls.readNvmrc()
if err != nil {
return err
}
Expand Down Expand Up @@ -391,3 +379,52 @@ func npmToYarn(cmd string) string {
cmd = strings.ReplaceAll(cmd, "npm", "yarn")
return strings.ReplaceAll(cmd, "npx", "yarn")
}

func (sls *Manager) installNvm() (string, error) {
var err error

nvmDir := os.Getenv("NVM_DIR")
if len(nvmDir) == 0 {
nvmDir = "$HOME/.nvm"
}

// Check if nvm.sh exists in the nvmDir
nvmShPath := filepath.Join(nvmDir, "nvm.sh")
_, err = os.Stat(nvmShPath)
if !os.IsNotExist(err) {
logrus.Debug("nvm.sh found in the directory:", nvmDir)

//check if version is what we expect
cmd := exec.Command("bash", "-c", fmt.Sprintf("source %s/nvm.sh && nvm --version", nvmDir))
cmd.Dir = sls.Project.RootDir

var out bytes.Buffer
cmd.Stdout = &out
err = cmd.Run()
if err != nil {
logrus.Debug("Error checking nvm version:", err)
return "", err
}

if strings.TrimSpace(out.String()) == sls.Project.NvmVersion {
logrus.Debugf("nvm version is correct. Expected: %s, Found: %s", sls.Project.NvmVersion, strings.TrimSpace(out.String()))
return nvmDir, nil
}

}
logrus.Debugf("No correct nvm version found is incorrect, (re)installing nvm")

// Install nvm.
cmd := exec.Command("bash", "-c", fmt.Sprintf("curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v%s/install.sh | bash", sls.Project.NvmVersion))
cmd.Dir = sls.Project.RootDir
err = cmd.Run()
if err != nil {
logrus.Debugf("Error installing nvm:", err)

Check failure on line 422 in internal/manager/serverless/native.go

View workflow job for this annotation

GitHub Actions / Unit Tests

github.com/sirupsen/logrus.Debugf call has arguments but no formatting directives

Check failure on line 422 in internal/manager/serverless/native.go

View workflow job for this annotation

GitHub Actions / Unit Tests

github.com/sirupsen/logrus.Debugf call has arguments but no formatting directives
return "", err
}

// TODO: If nvm.sh doesn't exist in the nvmDir, we should install it
// curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash

return nvmDir, nil
}
4 changes: 4 additions & 0 deletions internal/schema/ize-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
"type": "string",
"description": "(optional) Terraform version can be set here. 1.1.3 by default"
},
"nvm_version": {
"type": "string",
"description": "(optional) Nvm version can be set here. 0.39.7 by default"
},
"endpoint_url": {
"type": "string",
"description": "(optional) AWS Endpoint url (can be used with Localstack)"
Expand Down

0 comments on commit d676a58

Please sign in to comment.