-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Handle symlinks, support pnpm and monorepo, (#89)
* feat: Handle symlinks, support pnpm and monorepo, ref: #84 * chore: suggested changes
- Loading branch information
Showing
12 changed files
with
272 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package fs | ||
|
||
import ( | ||
"os" | ||
"path" | ||
"path/filepath" | ||
) | ||
|
||
func pathExists(path string) bool { | ||
_, err := os.Stat(path) | ||
return err == nil | ||
} | ||
|
||
func FindParentWithFile(cwd string, file string) string { | ||
if pathExists(path.Join(cwd, file)) { | ||
return cwd | ||
} | ||
parent := filepath.Dir(cwd) | ||
if parent == cwd { | ||
return "" | ||
} | ||
return FindParentWithFile(parent, file) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package node_modules | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os/exec" | ||
"path" | ||
"testing" | ||
|
||
"github.com/develar/app-builder/pkg/fs" | ||
. "github.com/onsi/gomega" | ||
"github.com/samber/lo" | ||
) | ||
|
||
type NodeTreeDepItem struct { | ||
Name string `json:"name"` | ||
Version string `json:"version"` | ||
} | ||
|
||
type NodeTreeItem struct { | ||
Dir string `json:"dir"` | ||
Deps []NodeTreeDepItem `json:"deps"` | ||
} | ||
|
||
func nodeDepTree(t *testing.T, dir string) { | ||
g := NewGomegaWithT(t) | ||
rootPath := fs.FindParentWithFile(Dirname(), "go.mod") | ||
cmd := exec.Command("go", "run", path.Join(rootPath, "main.go"), "node-dep-tree", "--dir", dir) | ||
output, err := cmd.Output() | ||
if err != nil { | ||
fmt.Println("err", err) | ||
} | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
var j []NodeTreeItem | ||
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", | ||
})) | ||
} | ||
|
||
func TestNodeDepTreeCmd(t *testing.T) { | ||
nodeDepTree(t, path.Join(Dirname(), "npm-demo")) | ||
nodeDepTree(t, path.Join(Dirname(), "pnpm-demo")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package node_modules | ||
|
||
import ( | ||
"path" | ||
"runtime" | ||
) | ||
|
||
func Dirname() string { | ||
_, filename, _, _ := runtime.Caller(1) | ||
return path.Dir(filename) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package node_modules | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
"github.com/samber/lo" | ||
) | ||
|
||
func TestReadDependencyTreeByNpm(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
collector := &Collector{ | ||
unresolvedDependencies: make(map[string]bool), | ||
excludedDependencies: make(map[string]bool), | ||
NodeModuleDirToDependencyMap: make(map[string]*map[string]*Dependency), | ||
} | ||
|
||
dir := path.Join(Dirname(), "npm-demo") | ||
|
||
dependency, err := readPackageJson(dir) | ||
dependency.dir = dir | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = collector.readDependencyTree(dependency) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
r := lo.FlatMap(lo.Values(collector.NodeModuleDirToDependencyMap), func(it *map[string]*Dependency, i int) []string { | ||
return lo.Keys(*it) | ||
}) | ||
g.Expect(r).To(ConsistOf([]string{ | ||
"js-tokens", "react", "loose-envify", | ||
})) | ||
} | ||
|
||
func TestReadDependencyTreeByPnpm(t *testing.T) { | ||
g := NewGomegaWithT(t) | ||
|
||
collector := &Collector{ | ||
unresolvedDependencies: make(map[string]bool), | ||
excludedDependencies: make(map[string]bool), | ||
NodeModuleDirToDependencyMap: make(map[string]*map[string]*Dependency), | ||
} | ||
|
||
dir := path.Join(Dirname(), "pnpm-demo") | ||
|
||
dependency, err := readPackageJson(dir) | ||
dependency.dir = dir | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
|
||
err = collector.readDependencyTree(dependency) | ||
g.Expect(err).NotTo(HaveOccurred()) | ||
r := lo.FlatMap(lo.Values(collector.NodeModuleDirToDependencyMap), func(it *map[string]*Dependency, i int) []string { | ||
return lo.Keys(*it) | ||
}) | ||
g.Expect(r).To(ConsistOf([]string{ | ||
"js-tokens", "react", "loose-envify", | ||
})) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "npm-demo", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"react": "^18.2.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"name": "pnpm-demo", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"react": "^18.2.0" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.