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

feat: move most used modues to highest level #137

Closed
wants to merge 2 commits into from
Closed
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
75 changes: 57 additions & 18 deletions pkg/node-modules/nodeModuleCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,13 @@ func (t *Collector) writeToParentConflicDependency(d *Dependency) {
p := d.parent
last := d
for p != t.rootDependency {
if h, ok := t.HoiestedDependencyMap[p.alias]; ok && p.Version == h.Version {
last = p
break
}

if p.conflictDependency != nil {
if c, ok := p.conflictDependency[d.Name]; ok {
if c, ok := p.conflictDependency[d.alias]; ok {
if c.Version == d.Version {
return
}
Expand All @@ -115,23 +120,64 @@ func (t *Collector) writeToParentConflicDependency(d *Dependency) {
if last.conflictDependency == nil {
last.conflictDependency = make(map[string]*Dependency)
}
last.conflictDependency[d.Name] = d
last.conflictDependency[d.alias] = d
}

func getMostUsedDeps(deps []*Dependency) []*Dependency {
groups := make(map[string][]*Dependency)
for _, item := range deps {
groups[item.alias] = append(groups[item.alias], item)
}

for name, group := range groups {
versionCounts := make(map[string]int)
for _, item := range group {
versionCounts[item.Version]++
}

sort.Slice(group, func(i, j int) bool {
vi, vj := group[i].Version, group[j].Version
ci, cj := versionCounts[vi], versionCounts[vj]

if (ci > 1) != (cj > 1) {
return ci == 1
}
if ci != cj {
return ci < cj
}
return vi < vj
})

groups[name] = group
}

var sortedGroups [][]*Dependency
for _, group := range groups {
sortedGroups = append(sortedGroups, group)
}
sort.Slice(sortedGroups, func(i, j int) bool {
return len(sortedGroups[i]) < len(sortedGroups[j])
})

var result []*Dependency
for _, group := range sortedGroups {
result = append(result, group[len(group)-1])
}

return result
}

func (t *Collector) processHoistDependencyMap() {
t.HoiestedDependencyMap = make(map[string]*Dependency)
for _, h := range getMostUsedDeps(t.allDependencies) {
t.HoiestedDependencyMap[h.alias] = h
}

for _, d := range t.allDependencies {
if e, ok := t.HoiestedDependencyMap[d.Name]; ok {
if e, ok := t.HoiestedDependencyMap[d.alias]; ok {
if e.Version != d.Version {
if d.parent == t.rootDependency {
t.HoiestedDependencyMap[d.Name] = d
t.writeToParentConflicDependency(e)
} else {
t.writeToParentConflicDependency(d)
}
t.writeToParentConflicDependency(d)
}
} else {
t.HoiestedDependencyMap[d.Name] = d
}

}
Expand Down Expand Up @@ -242,13 +288,6 @@ func correctOptionalState(isOptional bool, childDependency *Dependency) {
// nil if already handled
func (t *Collector) resolveDependency(parentNodeModuleDir string, name string) (*Dependency, error) {
dependencyNameToDependency := t.NodeModuleDirToDependencyMap[parentNodeModuleDir]
if dependencyNameToDependency != nil {
dependency := (*dependencyNameToDependency)[name]
if dependency != nil {
return dependency, nil
}
}

dependencyDir := filepath.Join(parentNodeModuleDir, name)
info, err := os.Stat(dependencyDir)
if err == nil && !info.IsDir() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/node-modules/nodeModuleCollector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestReadDependencyTreeByNpm(t *testing.T) {
g.Expect(r).To(ConsistOf([]string{
"js-tokens", "react", "remote", "loose-envify",
}))
remoteModule := collector.HoiestedDependencyMap["@electron/remote"]
remoteModule := collector.HoiestedDependencyMap["remote"]
g.Expect(remoteModule.alias).To(Equal("remote"))
g.Expect(remoteModule.Name).To(Equal("@electron/remote"))
}
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestReadDependencyTreeByPnpm(t *testing.T) {
"js-tokens", "react", "remote", "loose-envify",
}))

remoteModule := collector.HoiestedDependencyMap["@electron/remote"]
remoteModule := collector.HoiestedDependencyMap["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")))
Expand Down Expand Up @@ -101,7 +101,7 @@ func TestReadDependencyTreeForTar(t *testing.T) {
g.Expect(collector.HoiestedDependencyMap["tar"].dir).To(Equal(filepath.Join(dir, "node_modules/tar")))
g.Expect(collector.HoiestedDependencyMap["minipass"].Version).To(Equal("7.1.2"))
g.Expect(collector.HoiestedDependencyMap["minizlib"].Version).To(Equal("3.0.1"))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["ansi-regex"].Version).To(Equal("5.0.1"))
g.Expect(collector.HoiestedDependencyMap["string-width-cjs"].conflictDependency["ansi-regex"].Version).To(Equal("5.0.1"))
}

func TestReadDependencyTreeForYarn(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/node-modules/tar-demo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading