Skip to content

Commit

Permalink
perf(projects): Get node.js version from volta config if not in packa…
Browse files Browse the repository at this point in the history
…ge.json.
  • Loading branch information
jwalton committed Feb 1, 2022
1 parent 2a676c4 commit 7798486
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
55 changes: 54 additions & 1 deletion internal/kitsch/projects/nodejsGetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
Expand Down Expand Up @@ -58,7 +59,12 @@ func (getter nodejsGetter) getFromVolta(getterContext getters.GetterContext) (in
return result, nil
}

// If the version isn't in package.json, need to run volta.
result, err = getter.getFromVoltaConfig(getterContext)
if err == nil {
return result, nil
}

// If the version isn't in package.json, and we can't get it from the Volta config, then we need to run volta.
volta, err := fileutils.LookPathSafe("volta")
if err != nil {
return nil, fmt.Errorf("could not find volta: %w", err)
Expand Down Expand Up @@ -107,6 +113,53 @@ func (getter nodejsGetter) getFromVoltaPackageJSON(getterContext getters.GetterC
return result, nil
}

type voltaConfig struct {
Node struct {
Runtime string `json:"runtime"`
Npm string `json:"npm"`
} `json:"node"`
Yarn string `json:"yarn"`
}

// Try to get the nodejs version from ~/.volta/tools/user/platform.json
func (getter nodejsGetter) getFromVoltaConfig(getterContext getters.GetterContext) (string, error) {
voltaHome := getterContext.Getenv("VOLTA_HOME")
if voltaHome == "" {
voltaHome = filepath.Join(getterContext.Getenv("HOME") + ".volta")
}
configPath := filepath.Join(voltaHome, "tools", "user", "platform.json")
configContents, err := os.ReadFile(configPath)
if err != nil {
return "", fmt.Errorf("could not read %s: %w", configPath, err)
}

var config voltaConfig
err = json.Unmarshal(configContents, &config)
if err != nil {
return "", fmt.Errorf("could not parse %s: %w", configPath, err)
}

switch getter.executable {
case "node":
if config.Node.Runtime == "" {
return "", fmt.Errorf("No node version specified")
}
return config.Node.Runtime, nil
case "npm":
if config.Node.Npm == "" {
return "", fmt.Errorf("No npm version specified")
}
return config.Node.Npm, nil
case "yarn":
if config.Yarn == "" {
return "", fmt.Errorf("No yarn version specified")
}
return config.Yarn, nil
default:
return "", fmt.Errorf("Unknown executable: %s", getter.executable)
}
}

func (getter nodejsGetter) getFromExecutable(getterContext getters.GetterContext, resolvedExecutable string) (interface{}, error) {
// Delegate this to a custom getter to handle caching.
customGetter := getters.CustomGetter{
Expand Down
2 changes: 1 addition & 1 deletion internal/kitsch/projects/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func ResolveProjectType(

toolVersion, err := getStringValue(projectType.ToolVersion, getterContext)
if err != nil || toolVersion == "" {
// If we can't get a toolVesrion, skip this project type.
// If we can't get a toolVersion, skip this project type.
log.Info("Could not get tool version for project type", projectType.Name)
continue
}
Expand Down

0 comments on commit 7798486

Please sign in to comment.