Skip to content

Commit

Permalink
fix: resolved term issues and cleaned up code (#1)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeroen Claassens <[email protected]>
  • Loading branch information
favna committed Jan 2, 2024
1 parent 7875365 commit 33d5f9d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 57 deletions.
29 changes: 5 additions & 24 deletions src/nodejs/yarn/yarn.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package yarn

import (
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"

"github.com/cloudfoundry/libbuildpack"
Expand Down Expand Up @@ -67,45 +65,30 @@ func (y *Yarn) executeCommandAndGetStdout(buildDir string, args ...string) ([]by
return cmd.Output()
}

func (y *Yarn) parseStdoutResponse(stdoutResponse []byte) string {
result := strings.TrimSpace(string(stdoutResponse))
re := regexp.MustCompile(`(?i)(?:\\x9B|\\x1B\[)[0-?]*[ -/]*[@-~]`)
return re.ReplaceAllString(result, "")
}

func (y *Yarn) getYarnVersion(buildDir string) (string, error) {
output, err := y.executeCommandAndGetStdout(buildDir, "--version")

if err != nil {
return "", err
}

yarnVersion := y.parseStdoutResponse(output)
yarnVersion := strings.TrimSpace(string(output))
return yarnVersion, nil
}

func (y *Yarn) isYarnGlobalCacheEnabled(buildDir string) (bool, error) {
output, err := y.executeCommandAndGetStdout(buildDir, "config", "get", "enableGlobalCache")
if err != nil {
println("In the err != nil branch")
return true, err
}

result := y.parseStdoutResponse(output)

println(fmt.Sprintf("resolved result: %v", result))
result := strings.TrimSpace(string(output))

if result == "true" {
println("In the result == true branch")
if strings.Contains(result, "true") {
return true, nil
} else if result == "false" {
println("In the result == false branch")
return false, nil
}

println("In the result == neither branch, an error should get formatted below")

return true, fmt.Errorf("unexpected output from yarn config get enableGlobalCache: %s", result)
return false, nil
}

func (y *Yarn) getYarnCacheFolder(buildDir string) string {
Expand All @@ -114,7 +97,7 @@ func (y *Yarn) getYarnCacheFolder(buildDir string) string {
return ".yarn/cache"
}

result := y.parseStdoutResponse(output)
result := strings.TrimSpace(string(output))
return result
}

Expand Down Expand Up @@ -182,8 +165,6 @@ func (y *Yarn) doBuildBerry(buildDir string) error {

installArgs := []string{"install", "--immutable"}

println(fmt.Sprintf("resolved useGlobalCache: %v", useGlobalCache))

if useGlobalCache {
y.Log.Info("Yarn is using global cache, cache is allowed to be mutable")
y.Log.Info("To run yarn with local cache, see: https://yarnpkg.com/configuration/yarnrc#enableGlobalCache")
Expand Down
43 changes: 10 additions & 33 deletions src/nodejs/yarn/yarn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package yarn_test

import (
"bytes"
"log"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -158,24 +157,14 @@ var _ = Describe("Yarn", func() {
})

Context("local cache enabled", func() {
// Disables global cache
JustBeforeEach(func() {
yarnRcFilePath := buildDir + "/.yarnrc.yml"
// Disabled global cache
cmd := exec.Command("yarn", "config", "set", "enableGlobalCache", "false")
cmd.Dir = buildDir

file, err := os.OpenFile(yarnRcFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
err := cmd.Run()
if err != nil {
log.Fatalf("Failed to open "+yarnRcFilePath+": %s", err)
}

defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalf("Failed to close "+yarnRcFilePath+": %s", err)
}
}(file)

if _, err := file.WriteString("\nenableGlobalCache: false\n"); err != nil {
log.Fatalf("Failed to write to "+yarnRcFilePath+": %s", err)
return
}
})

Expand All @@ -197,26 +186,14 @@ var _ = Describe("Yarn", func() {

Context("NO local cache enabled", func() {
JustBeforeEach(func() {
// Disables global cache
yarnRcFilePath := buildDir + "/.yarnrc.yml"
// Enables global cache
cmd := exec.Command("yarn", "config", "set", "enableGlobalCache", "true")
cmd.Dir = buildDir

file, err := os.OpenFile(yarnRcFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
err := cmd.Run()
if err != nil {
log.Fatalf("Failed to open "+yarnRcFilePath+": %s", err)
return
}
defer func(file *os.File) {
err := file.Close()
if err != nil {
log.Fatalf("Failed to close "+yarnRcFilePath+": %s", err)
}
}(file)

if _, err := file.WriteString("\nenableGlobalCache: true\n"); err != nil {
log.Fatalf("Failed to write to "+yarnRcFilePath+": %s", err)
}

content, err := os.ReadFile(yarnRcFilePath)
println("yarnrc file content: " + string(content))
})

It("tells the user it is running with global cache", func() {
Expand Down

0 comments on commit 33d5f9d

Please sign in to comment.