Skip to content

Commit

Permalink
Merge pull request #23 from codacy/add-registry-to-install-command
Browse files Browse the repository at this point in the history
add registry to install command
  • Loading branch information
heliocodacy authored Nov 5, 2024
2 parents 24e7df9 + 1589c20 commit 7215e99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
16 changes: 10 additions & 6 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@ package cmd

import (
cfg "codacy/cli-v2/config"
"github.com/spf13/cobra"
"fmt"
"log"

"github.com/spf13/cobra"
)

var registry string

func init() {
installCmd.Flags().StringVarP(&registry, "registry", "r", "", "Registry to use for installing tools")
rootCmd.AddCommand(installCmd)
}

var installCmd = &cobra.Command{
Use: "install",
Use: "install",
Short: "Installs the tools specified in the project's config-file.",
Long: "Installs all runtimes and tools specified in the project's config-file file.",
Long: "Installs all runtimes and tools specified in the project's config-file file.",
Run: func(cmd *cobra.Command, args []string) {
// install runtimes
fetchRuntimes(&cfg.Config)
Expand Down Expand Up @@ -42,14 +47,13 @@ func fetchTools(config *cfg.ConfigType) {
case "eslint":
// eslint needs node runtime
nodeRuntime := config.Runtimes()["node"]
err := cfg.InstallEslint(nodeRuntime, tool)
err := cfg.InstallEslint(nodeRuntime, tool, registry)
if err != nil {
fmt.Println(err.Error())
log.Fatal(err)
}
default:
log.Fatal("Unknown tool:", tool.Name())
}
}
}


18 changes: 16 additions & 2 deletions config/eslint-utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,36 @@ func genInfoEslint(r *Runtime) map[string]string {

return map[string]string{
"installDir": installDir,
"eslint": path.Join(installDir, "node_modules", ".bin", "eslint"),
"eslint": path.Join(installDir, "node_modules", ".bin", "eslint"),
}
}

/*
* This installs eslint using node's npm alongside its sarif extension
*/
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime) error {
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime, registry string) error {
log.Println("Installing ESLint")

eslintInstallArg := fmt.Sprintf("%s@%s", eslint.Name(), eslint.Version())
if registry != "" {
fmt.Println("Using registry:", registry)
configCmd := exec.Command(nodeRuntime.Info()["npm"], "config", "set", "registry", registry)
if configOut, err := configCmd.Output(); err != nil {
fmt.Println("Error setting npm registry:", err)
fmt.Println(string(configOut))
return err
}
}
cmd := exec.Command(nodeRuntime.Info()["npm"], "install", "--prefix", eslint.Info()["installDir"],
eslintInstallArg, "@microsoft/eslint-formatter-sarif")
fmt.Println(cmd.String())
// to use the chdir command we needed to create the folder before, we can change this after
// cmd.Dir = eslintInstallationFolder
stdout, err := cmd.Output()
if err != nil {
fmt.Println("Error installing ESLint:", err)
fmt.Println(string(stdout))
}
// Print the output
fmt.Println(string(stdout))
return err
Expand Down

0 comments on commit 7215e99

Please sign in to comment.