Skip to content

Commit edb7fb9

Browse files
committed
refactors node/eslint installations
1 parent 45077af commit edb7fb9

File tree

2 files changed

+50
-37
lines changed

2 files changed

+50
-37
lines changed

.codacy/.gitignore

Lines changed: 0 additions & 2 deletions
This file was deleted.

cli-v2.go

Lines changed: 50 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func downloadFile(url string, destDir string) (string, error) {
8989
return destPath, nil
9090
}
9191

92-
func extract(t *os.File) {
92+
func extract(t *os.File, codacyDirectory string) {
9393

9494
format := archiver.CompressedArchive{
9595
Compression: archiver.Gz{},
@@ -111,7 +111,7 @@ func extract(t *os.File) {
111111

112112
fmt.Printf("Contents of %s:\n", f.NameInArchive)
113113

114-
path := ".codacy/runtimes/" + f.NameInArchive
114+
path := filepath.Join(codacyDirectory, "runtimes", f.NameInArchive)
115115

116116
switch f.IsDir() {
117117
case true:
@@ -162,73 +162,88 @@ func extract(t *os.File) {
162162

163163
}
164164

165-
func installESLint(npmExecutablePath string, ESLintversion string) {
165+
func installESLint(npmExecutablePath string, ESLintversion string, codacyPath string) {
166166

167167
fmt.Println("Installing ESLint")
168168

169-
cmd := exec.Command(npmExecutablePath, "install", "--prefix", "./.codacy/tools/"+ESLintversion, ESLintversion)
169+
eslintInstallationFolder := filepath.Join(codacyPath, "tools", ESLintversion)
170+
171+
cmd := exec.Command(npmExecutablePath, "install", "--prefix", eslintInstallationFolder, ESLintversion, "@microsoft/eslint-formatter-sarif")
172+
// to use the chdir command we needed to create the folder before, we can change this after
173+
// cmd.Dir = eslintInstallationFolder
170174
stdout, err := cmd.Output()
171175

172176
// Print the output
173177
fmt.Println(string(stdout))
174178

175179
if err != nil {
176-
fmt.Println(err.Error())
177-
return
180+
log.Fatal(err)
178181
}
182+
}
179183

180-
workingDirectory, _ := os.Getwd()
181-
182-
nodeVersion := "22.2.0-darwin-x64"
183-
184-
// TODO clean eslint version
185-
186-
scriptContent := fmt.Sprintf(`
187-
#!/bin/sh
188-
189-
export PATH="%s/.codacy/tools/%s/node_modules/.bin:%s/node-v%s/bin:${PATH}"
190-
export HOME="${HOME:-}"
191-
export NODE_PATH="%s/.codacy/tools/%s/node_modules"
184+
func main() {
185+
content, err := os.ReadFile(".codacy/codacy.yaml")
186+
if err != nil {
187+
log.Fatal(err)
188+
}
192189

190+
config := Config{}
191+
if err := yaml.Unmarshal(content, &config); err != nil {
192+
log.Fatalf("error: %v", err)
193+
}
193194

194-
exec eslint "$@"
195-
196-
`, workingDirectory, ESLintversion, workingDirectory, nodeVersion, workingDirectory, ESLintversion)
195+
homePath, err := os.UserHomeDir()
196+
if err != nil {
197+
log.Fatal(err)
198+
}
197199

198-
w, err := os.OpenFile(".codacy/tools/"+ESLintversion+"/eslint.sh", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0770)
199-
defer w.Close()
200+
codacyDirectory := filepath.Join(homePath, ".cache", "codacy")
201+
runtimesDirectory := filepath.Join(codacyDirectory, "runtimes")
202+
toolsDirectory := filepath.Join(codacyDirectory, "tools")
200203

204+
fmt.Println("creating: " + codacyDirectory)
205+
err = os.MkdirAll(codacyDirectory, 0777)
201206
if err != nil {
202207
log.Fatal(err)
203208
}
204209

205-
_, err = io.WriteString(w, scriptContent)
210+
fmt.Println("creating: " + runtimesDirectory)
211+
err = os.MkdirAll(runtimesDirectory, 0777)
206212
if err != nil {
207213
log.Fatal(err)
208214
}
209-
}
210215

211-
func main() {
212-
content, err := os.ReadFile(".codacy/codacy.yaml")
216+
fmt.Println("creating: " + toolsDirectory)
217+
err = os.MkdirAll(toolsDirectory, 0777)
213218
if err != nil {
214219
log.Fatal(err)
215220
}
216221

217-
config := Config{}
218-
if err := yaml.Unmarshal(content, &config); err != nil {
219-
log.Fatalf("error: %v", err)
220-
}
222+
fmt.Println(codacyDirectory)
221223

222224
fmt.Println(config)
223225
downloadNodeURL := getNodeDownloadURL("v22.2.0")
224-
nodeTar, _ := downloadFile(downloadNodeURL, ".codacy")
225226

226-
t, _ := os.Open(nodeTar)
227+
nodeTar, err := downloadFile(downloadNodeURL, codacyDirectory)
228+
if err != nil {
229+
log.Fatal(err)
230+
}
231+
232+
fmt.Println("Downloaded node: " + nodeTar)
233+
234+
t, err := os.Open(nodeTar)
227235
defer t.Close()
236+
if err != nil {
237+
log.Fatal(err)
238+
}
239+
240+
fmt.Println("About to extract node: " + t.Name())
241+
extract(t, codacyDirectory)
228242

229-
extract(t)
243+
npmPath := filepath.Join(codacyDirectory, "runtimes", "node-v22.2.0-darwin-x64", "bin", "npm")
230244

231-
installESLint(".codacy/runtimes/node-v22.2.0-darwin-x64/bin/npm", "[email protected]")
245+
fmt.Println("About to install eslint")
246+
installESLint(npmPath, "[email protected]", codacyDirectory)
232247

233248
cmd.Execute()
234249
}

0 commit comments

Comments
 (0)