Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change node module symlink to real path #120

Merged
merged 6 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-bugs-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"app-builder-bin": patch
---

change node module symlink to real path
29 changes: 19 additions & 10 deletions pkg/node-modules/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ type NodeTreeItem struct {
Deps []NodeTreeDepItem `json:"deps"`
}

type NodePathItem struct {
Name string `json:"name"`
Version string `json:"version"`
Dir string `json:"dir"`
}

func nodeDepPath(t *testing.T, dir string) {
g := NewGomegaWithT(t)
rootPath := fs.FindParentWithFile(Dirname(), "go.mod")
Expand All @@ -31,15 +37,18 @@ func nodeDepPath(t *testing.T, dir string) {
fmt.Println("err", err)
}
g.Expect(err).NotTo(HaveOccurred())
var j []NodeTreeItem
var j []NodePathItem
json.Unmarshal(output, &j)
r := lo.FlatMap(j, func(it NodeTreeItem, i int) []string {
return lo.Map(it.Deps, func(it NodeTreeDepItem, i int) string {
return it.Name
})
})
g.Expect(r).To(ConsistOf([]string{
"react", "js-tokens", "loose-envify",
dependencies := make([]NodePathItem, 4)
names := make([]string, 4)
index := 0
for _, d := range j {
dependencies[index] = d
names[index] = d.Name
index++
}
g.Expect(names).To(Equal([]string{
"js-tokens", "loose-envify", "react", "remote",
}))
}

Expand Down Expand Up @@ -70,6 +79,6 @@ func TestNodeDepTreeCmd(t *testing.T) {
}

func TestNodeDepPathCmd(t *testing.T) {
nodeDepTree(t, path.Join(Dirname(), "npm-demo"))
nodeDepTree(t, path.Join(Dirname(), "pnpm-demo"))
nodeDepPath(t, path.Join(Dirname(), "npm-demo"))
nodeDepPath(t, path.Join(Dirname(), "pnpm-demo"))
}
25 changes: 22 additions & 3 deletions pkg/node-modules/nodeModuleCollector.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package node_modules

import (
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -239,10 +238,30 @@ func (t *Collector) resolveDependency(parentNodeModuleDir string, name string) (

(*dependencyNameToDependency)[name] = dependency
dependency.alias = name
dependency.dir = dependencyDir
dependency.dir = resolvePath(dependencyDir)
return dependency, nil
}

func resolvePath(dir string) string {
// Check if the path is a symlink
info, err := os.Lstat(dir)
if err != nil {
return dir
}

if info.Mode()&os.ModeSymlink != 0 {
// It's a symlink, resolve the real path
realPath, err := filepath.EvalSymlinks(dir)
if err != nil {
return dir
}
return realPath
}

// Not a symlink, return the original path
return dir
}

func findNearestNodeModuleDir(dir string) (string, error) {
if len(dir) == 0 {
return "", nil
Expand Down Expand Up @@ -294,7 +313,7 @@ func getParentDir(file string) string {

func readPackageJson(dir string) (*Dependency, error) {
packageFile := filepath.Join(dir, "package.json")
data, err := ioutil.ReadFile(packageFile)
data, err := os.ReadFile(packageFile)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/node-modules/nodeModuleCollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package node_modules

import (
"path"
"path/filepath"
"testing"

. "github.com/onsi/gomega"
Expand Down Expand Up @@ -31,6 +32,9 @@ func TestReadDependencyTreeByNpm(t *testing.T) {
g.Expect(r).To(ConsistOf([]string{
"js-tokens", "react", "remote", "loose-envify",
}))
remoteModule := collector.DependencyMap["@electron/remote"]
g.Expect(remoteModule.alias).To(Equal("remote"))
g.Expect(remoteModule.Name).To(Equal("@electron/remote"))
}

func TestReadDependencyTreeByPnpm(t *testing.T) {
Expand All @@ -56,4 +60,12 @@ func TestReadDependencyTreeByPnpm(t *testing.T) {
g.Expect(r).To(ConsistOf([]string{
"js-tokens", "react", "remote", "loose-envify",
}))

remoteModule := collector.DependencyMap["@electron/remote"]
g.Expect(remoteModule.Name).To(Equal("@electron/remote"))
g.Expect(remoteModule.alias).To(Equal("remote"))
g.Expect(remoteModule.dir).To(Equal(filepath.Join(dir, "node_modules/.pnpm/@[email protected][email protected]/node_modules/@electron/remote")))

reactModule := collector.DependencyMap["react"]
g.Expect(reactModule.dir).To(Equal(filepath.Join(dir, "node_modules/.pnpm/[email protected]/node_modules/react")))
}
8 changes: 3 additions & 5 deletions pkg/node-modules/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,9 @@ func writeFlattenResult(jsonWriter *jsoniter.Stream, dependencyMap map[string]*D
}

if len(dependencies) > 1 {
if len(dependencies) > 1 {
sort.Slice(dependencies, func(i, j int) bool {
return dependencies[i].alias < dependencies[j].alias
})
}
sort.Slice(dependencies, func(i, j int) bool {
return dependencies[i].alias < dependencies[j].alias
})
}

jsonWriter.WriteArrayStart()
Expand Down
Loading