From 032f7f53de2f0f8ba49bd741e38ddb47fcb62521 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Thu, 13 Jun 2024 13:06:28 +0800 Subject: [PATCH 1/5] change all symlink to real path --- pkg/node-modules/nodeModuleCollector.go | 25 +++++++++++-- pkg/node-modules/pnpm-demo/package.json | 3 +- pkg/node-modules/pnpm-demo/pnpm-lock.yaml | 45 ++++++++++++++++++++++- 3 files changed, 68 insertions(+), 5 deletions(-) diff --git a/pkg/node-modules/nodeModuleCollector.go b/pkg/node-modules/nodeModuleCollector.go index 2d74695..3d26b2f 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" @@ -237,10 +236,30 @@ func (t *Collector) resolveDependency(parentNodeModuleDir string, name string) ( } (*dependencyNameToDependency)[name] = dependency - 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 @@ -292,7 +311,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/pnpm-demo/package.json b/pkg/node-modules/pnpm-demo/package.json index ca1a446..5a7ea4f 100644 --- a/pkg/node-modules/pnpm-demo/package.json +++ b/pkg/node-modules/pnpm-demo/package.json @@ -10,6 +10,7 @@ "author": "", "license": "ISC", "dependencies": { - "react": "^18.2.0" + "react": "^18.2.0", + "auto-launch": "5.0.6" } } diff --git a/pkg/node-modules/pnpm-demo/pnpm-lock.yaml b/pkg/node-modules/pnpm-demo/pnpm-lock.yaml index 56b57ae..0c20585 100644 --- a/pkg/node-modules/pnpm-demo/pnpm-lock.yaml +++ b/pkg/node-modules/pnpm-demo/pnpm-lock.yaml @@ -1,16 +1,34 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true excludeLinksFromLockfile: false dependencies: + auto-launch: + specifier: 5.0.6 + version: 5.0.6 react: specifier: ^18.2.0 version: 18.2.0 packages: + /applescript@1.0.0: + resolution: {integrity: sha512-yvtNHdWvtbYEiIazXAdp/NY+BBb65/DAseqlNiJQjOx9DynuzOYDbVLBJvuc0ve0VL9x6B3OHF6eH52y9hCBtQ==} + dev: false + + /auto-launch@5.0.6: + resolution: {integrity: sha512-OgxiAm4q9EBf9EeXdPBiVNENaWE3jUZofwrhAkWjHDYGezu1k3FRZHU8V2FBxGuSJOHzKmTJEd0G7L7/0xDGFA==} + engines: {node: '>=4.0.0'} + dependencies: + applescript: 1.0.0 + mkdirp: 0.5.6 + path-is-absolute: 1.0.1 + untildify: 3.0.3 + winreg: 1.2.4 + dev: false + /js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} dev: false @@ -22,9 +40,34 @@ packages: js-tokens: 4.0.0 dev: false + /minimist@1.2.8: + resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} + dev: false + + /mkdirp@0.5.6: + resolution: {integrity: sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==} + hasBin: true + dependencies: + minimist: 1.2.8 + dev: false + + /path-is-absolute@1.0.1: + resolution: {integrity: sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==} + engines: {node: '>=0.10.0'} + dev: false + /react@18.2.0: resolution: {integrity: sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==} engines: {node: '>=0.10.0'} dependencies: loose-envify: 1.4.0 dev: false + + /untildify@3.0.3: + resolution: {integrity: sha512-iSk/J8efr8uPT/Z4eSUywnqyrQU7DSdMfdqK4iWEaUVVmcP5JcnpRqmVMwcwcnmI1ATFNgC5V90u09tBynNFKA==} + engines: {node: '>=4'} + dev: false + + /winreg@1.2.4: + resolution: {integrity: sha512-IHpzORub7kYlb8A43Iig3reOvlcBJGX9gZ0WycHhghHtA65X0LYnMRuJs+aH1abVnMJztQkvQNlltnbPi5aGIA==} + dev: false From 712f8f167c491f93bc7382f0c8fb5b118396d9da Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Thu, 13 Jun 2024 13:48:42 +0800 Subject: [PATCH 2/5] delete auto-launch --- pkg/node-modules/pnpm-demo/package.json | 3 +- pkg/node-modules/pnpm-demo/pnpm-lock.yaml | 43 ----------------------- 2 files changed, 1 insertion(+), 45 deletions(-) diff --git a/pkg/node-modules/pnpm-demo/package.json b/pkg/node-modules/pnpm-demo/package.json index 5a7ea4f..ca1a446 100644 --- a/pkg/node-modules/pnpm-demo/package.json +++ b/pkg/node-modules/pnpm-demo/package.json @@ -10,7 +10,6 @@ "author": "", "license": "ISC", "dependencies": { - "react": "^18.2.0", - "auto-launch": "5.0.6" + "react": "^18.2.0" } } diff --git a/pkg/node-modules/pnpm-demo/pnpm-lock.yaml b/pkg/node-modules/pnpm-demo/pnpm-lock.yaml index 0c20585..f6338e0 100644 --- a/pkg/node-modules/pnpm-demo/pnpm-lock.yaml +++ b/pkg/node-modules/pnpm-demo/pnpm-lock.yaml @@ -5,30 +5,12 @@ settings: excludeLinksFromLockfile: false dependencies: - auto-launch: - specifier: 5.0.6 - version: 5.0.6 react: specifier: ^18.2.0 version: 18.2.0 packages: - /applescript@1.0.0: - resolution: {integrity: sha512-yvtNHdWvtbYEiIazXAdp/NY+BBb65/DAseqlNiJQjOx9DynuzOYDbVLBJvuc0ve0VL9x6B3OHF6eH52y9hCBtQ==} - dev: false - - /auto-launch@5.0.6: - resolution: {integrity: sha512-OgxiAm4q9EBf9EeXdPBiVNENaWE3jUZofwrhAkWjHDYGezu1k3FRZHU8V2FBxGuSJOHzKmTJEd0G7L7/0xDGFA==} - engines: {node: '>=4.0.0'} - dependencies: - applescript: 1.0.0 - mkdirp: 0.5.6 - path-is-absolute: 1.0.1 - untildify: 3.0.3 - winreg: 1.2.4 - dev: false - /js-tokens@4.0.0: resolution: {integrity: sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==} dev: false @@ -40,34 +22,9 @@ packages: js-tokens: 4.0.0 dev: false - /minimist@1.2.8: - resolution: {integrity: sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==} - dev: false - - /mkdirp@0.5.6: - resolution: {integrity: sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==} - hasBin: true - dependencies: - minimist: 1.2.8 - dev: false - - /path-is-absolute@1.0.1: - resolution: {integrity: sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==} - engines: {node: '>=0.10.0'} - dev: false - /react@18.2.0: resolution: {integrity: sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==} engines: {node: '>=0.10.0'} dependencies: loose-envify: 1.4.0 dev: false - - /untildify@3.0.3: - resolution: {integrity: sha512-iSk/J8efr8uPT/Z4eSUywnqyrQU7DSdMfdqK4iWEaUVVmcP5JcnpRqmVMwcwcnmI1ATFNgC5V90u09tBynNFKA==} - engines: {node: '>=4'} - dev: false - - /winreg@1.2.4: - resolution: {integrity: sha512-IHpzORub7kYlb8A43Iig3reOvlcBJGX9gZ0WycHhghHtA65X0LYnMRuJs+aH1abVnMJztQkvQNlltnbPi5aGIA==} - dev: false From 13492d0f96820fdb8232667968f4fcab22875b7f Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 14 Jun 2024 06:22:29 +0800 Subject: [PATCH 3/5] delete .vscode --- .vscode/symbols.json | 1 - pkg/node-modules/pnpm-demo/pnpm-lock.yaml | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 .vscode/symbols.json diff --git a/.vscode/symbols.json b/.vscode/symbols.json deleted file mode 100644 index bd81e24..0000000 --- a/.vscode/symbols.json +++ /dev/null @@ -1 +0,0 @@ -{"symbols":{},"files":{"index.d.ts":"2024-06-09T08:51:10.639Z"}} \ No newline at end of file diff --git a/pkg/node-modules/pnpm-demo/pnpm-lock.yaml b/pkg/node-modules/pnpm-demo/pnpm-lock.yaml index 1490866..facc98d 100644 --- a/pkg/node-modules/pnpm-demo/pnpm-lock.yaml +++ b/pkg/node-modules/pnpm-demo/pnpm-lock.yaml @@ -1,4 +1,4 @@ -lockfileVersion: '6.1' +lockfileVersion: '6.0' settings: autoInstallPeers: true From 84e07b667f5cbfbd0b591efe6f67fc74019fd086 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 14 Jun 2024 08:41:56 +0800 Subject: [PATCH 4/5] add ut --- pkg/node-modules/cli_test.go | 29 +++++++++++++------- pkg/node-modules/nodeModuleCollector_test.go | 12 ++++++++ pkg/node-modules/tree.go | 8 ++---- 3 files changed, 34 insertions(+), 15 deletions(-) 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_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() From 47e4a40cb2dfdbd25f6bc7005ebbeb4e90e459a4 Mon Sep 17 00:00:00 2001 From: beyondkmp Date: Fri, 14 Jun 2024 08:53:49 +0800 Subject: [PATCH 5/5] add changeset --- .changeset/lovely-bugs-prove.md | 5 +++++ 1 file changed, 5 insertions(+) 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