From 189519a8292f939d9e5d3b47c6407444fee70334 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Mon, 17 Jun 2024 07:14:25 +0800 Subject: [PATCH] fix: change node module symlink to real path (#120) --- .changeset/lovely-bugs-prove.md | 5 ++++ pkg/node-modules/cli_test.go | 29 +++++++++++++------- pkg/node-modules/nodeModuleCollector.go | 25 +++++++++++++++-- pkg/node-modules/nodeModuleCollector_test.go | 12 ++++++++ pkg/node-modules/tree.go | 8 ++---- 5 files changed, 61 insertions(+), 18 deletions(-) create mode 100644 .changeset/lovely-bugs-prove.md diff --git a/.changeset/lovely-bugs-prove.md b/.changeset/lovely-bugs-prove.md new file mode 100644 index 0000000..546229a --- /dev/null +++ b/.changeset/lovely-bugs-prove.md @@ -0,0 +1,5 @@ +--- +"app-builder-bin": patch +--- + +change node module symlink to real path diff --git a/pkg/node-modules/cli_test.go b/pkg/node-modules/cli_test.go index d7b6fe0..f7addc4 100644 --- a/pkg/node-modules/cli_test.go +++ b/pkg/node-modules/cli_test.go @@ -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") @@ -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", })) } @@ -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")) } diff --git a/pkg/node-modules/nodeModuleCollector.go b/pkg/node-modules/nodeModuleCollector.go index a7cd526..b3a4ecc 100644 --- a/pkg/node-modules/nodeModuleCollector.go +++ b/pkg/node-modules/nodeModuleCollector.go @@ -1,7 +1,6 @@ package node_modules import ( - "io/ioutil" "os" "path/filepath" "strings" @@ -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 @@ -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 } diff --git a/pkg/node-modules/nodeModuleCollector_test.go b/pkg/node-modules/nodeModuleCollector_test.go index 93313e4..50df36c 100644 --- a/pkg/node-modules/nodeModuleCollector_test.go +++ b/pkg/node-modules/nodeModuleCollector_test.go @@ -2,6 +2,7 @@ package node_modules import ( "path" + "path/filepath" "testing" . "github.com/onsi/gomega" @@ -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) { @@ -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/@electron+remote@2.1.2_electron@31.0.0/node_modules/@electron/remote"))) + + reactModule := collector.DependencyMap["react"] + g.Expect(reactModule.dir).To(Equal(filepath.Join(dir, "node_modules/.pnpm/react@18.2.0/node_modules/react"))) } diff --git a/pkg/node-modules/tree.go b/pkg/node-modules/tree.go index 33539fa..fcc1873 100644 --- a/pkg/node-modules/tree.go +++ b/pkg/node-modules/tree.go @@ -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()