Skip to content

Commit

Permalink
support pnpm
Browse files Browse the repository at this point in the history
  • Loading branch information
beyondkmp committed Sep 20, 2024
1 parent 4bbc955 commit ce2bcbe
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 32 deletions.
70 changes: 43 additions & 27 deletions pkg/node-modules/nodeModuleCollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Collector struct {

excludedDependencies map[string]bool
allDependencies []*Dependency
allDependenciesMap map[string]*Dependency

NodeModuleDirToDependencyMap map[string]*map[string]*Dependency `json:"nodeModuleDirToDependencyMap"`

Expand All @@ -45,7 +46,13 @@ type Collector struct {
func (t *Collector) readDependencyTree(dependency *Dependency) error {
if t.rootDependency == nil {
t.rootDependency = dependency
t.allDependenciesMap = make(map[string]*Dependency)
} else {
key := dependency.alias + dependency.Version + dependency.dir
if _, ok := t.allDependenciesMap[key]; ok {
return nil
}
t.allDependenciesMap[key] = dependency
t.allDependencies = append(t.allDependencies, dependency)
}

Expand Down Expand Up @@ -97,43 +104,52 @@ func (t *Collector) readDependencyTree(dependency *Dependency) error {
}

func (t *Collector) writeToParentConflicDependency(d *Dependency) {
p := d.parent
last := d
for p != t.rootDependency {
if p.conflictDependency != nil {
if c, ok := p.conflictDependency[d.Name]; ok {
if c.Version == d.Version {
return
}
break
for p := d.parent; p != t.rootDependency; p = p.parent {
if strings.HasPrefix(d.dir, p.dir) {
if p.conflictDependency == nil {
p.conflictDependency = make(map[string]*Dependency)
}
p.conflictDependency[d.alias] = d
return
}
last = p
p = p.parent
}

if last.conflictDependency == nil {
last.conflictDependency = make(map[string]*Dependency)
if h, ok := t.HoiestedDependencyMap[d.alias]; ok {
if h.Version == d.Version {
return
}

// for pnpm
p := d.parent
last := d
for p != t.rootDependency {
if p.conflictDependency != nil {
if c, ok := p.conflictDependency[d.alias]; ok {
if c.Version == d.Version {
return
}
break
}
}
last = p
p = p.parent
}

if last.conflictDependency == nil {
last.conflictDependency = make(map[string]*Dependency)
}
last.conflictDependency[d.alias] = d
return
}
last.conflictDependency[d.Name] = d

t.HoiestedDependencyMap[d.alias] = d

}

func (t *Collector) processHoistDependencyMap() {
t.HoiestedDependencyMap = make(map[string]*Dependency)
for _, d := range t.allDependencies {
if e, ok := t.HoiestedDependencyMap[d.Name]; ok {
if e.Version != d.Version {
if d.parent == t.rootDependency {
t.HoiestedDependencyMap[d.Name] = d
t.writeToParentConflicDependency(e)
} else {
t.writeToParentConflicDependency(d)
}
}
} else {
t.HoiestedDependencyMap[d.Name] = d
}

t.writeToParentConflicDependency(d)
}
}

Expand Down
9 changes: 4 additions & 5 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 @@ -99,9 +99,8 @@ func TestReadDependencyTreeForTar(t *testing.T) {
g.Expect(len(r)).To(Equal(46))

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["tar"].conflictDependency["minipass"].Version).To(Equal("7.1.2"))
g.Expect(collector.HoiestedDependencyMap["tar"].conflictDependency["minizlib"].Version).To(Equal("3.0.1"))
}

func TestReadDependencyTreeForYarn(t *testing.T) {
Expand Down

0 comments on commit ce2bcbe

Please sign in to comment.