Skip to content

Commit 7215e99

Browse files
authored
Merge pull request #23 from codacy/add-registry-to-install-command
add registry to install command
2 parents 24e7df9 + 1589c20 commit 7215e99

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

cmd/install.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@ package cmd
22

33
import (
44
cfg "codacy/cli-v2/config"
5-
"github.com/spf13/cobra"
5+
"fmt"
66
"log"
7+
8+
"github.com/spf13/cobra"
79
)
810

11+
var registry string
12+
913
func init() {
14+
installCmd.Flags().StringVarP(&registry, "registry", "r", "", "Registry to use for installing tools")
1015
rootCmd.AddCommand(installCmd)
1116
}
1217

1318
var installCmd = &cobra.Command{
14-
Use: "install",
19+
Use: "install",
1520
Short: "Installs the tools specified in the project's config-file.",
16-
Long: "Installs all runtimes and tools specified in the project's config-file file.",
21+
Long: "Installs all runtimes and tools specified in the project's config-file file.",
1722
Run: func(cmd *cobra.Command, args []string) {
1823
// install runtimes
1924
fetchRuntimes(&cfg.Config)
@@ -42,14 +47,13 @@ func fetchTools(config *cfg.ConfigType) {
4247
case "eslint":
4348
// eslint needs node runtime
4449
nodeRuntime := config.Runtimes()["node"]
45-
err := cfg.InstallEslint(nodeRuntime, tool)
50+
err := cfg.InstallEslint(nodeRuntime, tool, registry)
4651
if err != nil {
52+
fmt.Println(err.Error())
4753
log.Fatal(err)
4854
}
4955
default:
5056
log.Fatal("Unknown tool:", tool.Name())
5157
}
5258
}
5359
}
54-
55-

config/eslint-utils.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,36 @@ func genInfoEslint(r *Runtime) map[string]string {
1313

1414
return map[string]string{
1515
"installDir": installDir,
16-
"eslint": path.Join(installDir, "node_modules", ".bin", "eslint"),
16+
"eslint": path.Join(installDir, "node_modules", ".bin", "eslint"),
1717
}
1818
}
1919

2020
/*
2121
* This installs eslint using node's npm alongside its sarif extension
2222
*/
23-
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime) error {
23+
func InstallEslint(nodeRuntime *Runtime, eslint *Runtime, registry string) error {
2424
log.Println("Installing ESLint")
2525

2626
eslintInstallArg := fmt.Sprintf("%s@%s", eslint.Name(), eslint.Version())
27+
if registry != "" {
28+
fmt.Println("Using registry:", registry)
29+
configCmd := exec.Command(nodeRuntime.Info()["npm"], "config", "set", "registry", registry)
30+
if configOut, err := configCmd.Output(); err != nil {
31+
fmt.Println("Error setting npm registry:", err)
32+
fmt.Println(string(configOut))
33+
return err
34+
}
35+
}
2736
cmd := exec.Command(nodeRuntime.Info()["npm"], "install", "--prefix", eslint.Info()["installDir"],
2837
eslintInstallArg, "@microsoft/eslint-formatter-sarif")
38+
fmt.Println(cmd.String())
2939
// to use the chdir command we needed to create the folder before, we can change this after
3040
// cmd.Dir = eslintInstallationFolder
3141
stdout, err := cmd.Output()
42+
if err != nil {
43+
fmt.Println("Error installing ESLint:", err)
44+
fmt.Println(string(stdout))
45+
}
3246
// Print the output
3347
fmt.Println(string(stdout))
3448
return err

0 commit comments

Comments
 (0)