-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathtree.go
178 lines (148 loc) · 3.66 KB
/
tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package node_modules
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/alecthomas/kingpin"
"github.com/json-iterator/go"
)
func ConfigureCommand(app *kingpin.Application) {
command := app.Command("node-dep-tree", "")
dir := command.Flag("dir", "").Required().String()
excludedDependencies := command.Flag("exclude-dep", "").Strings()
command.Action(func(context *kingpin.ParseContext) error {
var excluded map[string]bool
if excludedDependencies == nil || len(*excludedDependencies) == 0 {
excluded = nil
} else {
excluded = make(map[string]bool, len(*excludedDependencies))
for _, name := range *excludedDependencies {
excluded[name] = true
}
}
collector := &Collector{
unresolvedDependencies: make(map[string]bool),
excludedDependencies: excluded,
NodeModuleDirToDependencyMap: make(map[string]*map[string]*Dependency),
}
dependency, err := readPackageJson(*dir)
if err != nil {
return err
}
dependency.dir = *dir
err = collector.readDependencyTree(dependency)
if err != nil {
return err
}
jsonWriter := jsoniter.NewStream(jsoniter.ConfigFastest, os.Stdout, 32*1024)
writeResult(jsonWriter, collector)
err = jsonWriter.Flush()
if err != nil {
return err
}
return nil
})
}
func writeResult(jsonWriter *jsoniter.Stream, collector *Collector) {
moduleDirs := make([]string, len(collector.NodeModuleDirToDependencyMap))
index := 0
for k := range collector.NodeModuleDirToDependencyMap {
moduleDirs[index] = k
index++
}
if len(moduleDirs) > 1 {
sort.Slice(moduleDirs, func(i, j int) bool {
return pathSorter(strings.Split(moduleDirs[i], string(filepath.Separator)), strings.Split(moduleDirs[j], string(filepath.Separator)))
})
}
jsonWriter.WriteArrayStart()
isFirst := true
for _, nodeModulesDir := range moduleDirs {
if isFirst {
isFirst = false
} else {
jsonWriter.WriteMore()
}
jsonWriter.WriteObjectStart()
jsonWriter.WriteObjectField("dir")
jsonWriter.WriteString(nodeModulesDir)
jsonWriter.WriteMore()
jsonWriter.WriteObjectField("deps")
writeDependencyList(jsonWriter, collector.NodeModuleDirToDependencyMap[nodeModulesDir])
jsonWriter.WriteObjectEnd()
}
jsonWriter.WriteArrayEnd()
}
func writeDependencyList(jsonWriter *jsoniter.Stream, dependencyMap *map[string]*Dependency) {
jsonWriter.WriteArrayStart()
isFirst := true
// names must be sorted for consistent result
names := make([]string, len(*dependencyMap))
index := 0
for name := range *dependencyMap {
names[index] = name
index++
}
if len(names) > 1 {
sort.Strings(names)
}
for _, name := range names {
info := (*dependencyMap)[name]
if isFirst {
isFirst = false
} else {
jsonWriter.WriteMore()
}
jsonWriter.WriteObjectStart()
jsonWriter.WriteObjectField("name")
jsonWriter.WriteString(name)
jsonWriter.WriteMore()
jsonWriter.WriteObjectField("version")
jsonWriter.WriteString(info.Version)
if info.isOptional == 1 {
jsonWriter.WriteMore()
jsonWriter.WriteObjectField("optional")
jsonWriter.WriteBool(true)
}
for name := range info.Dependencies {
if name == "prebuild-install" {
jsonWriter.WriteMore()
jsonWriter.WriteObjectField("hasPrebuildInstall")
jsonWriter.WriteBool(true)
break
}
}
jsonWriter.WriteObjectEnd()
}
jsonWriter.WriteArrayEnd()
}
func pathSorter(a []string, b []string) bool {
aL := len(a)
l := aL
bL := len(b)
if bL > l {
l = bL
}
for i := 0; i < l; i++ {
if i == aL {
return true
}
if i == bL {
return false
}
if a[i] > b[i] {
return false
}
if a[i] < b[i] {
return true
}
if aL < bL {
return true
}
if aL > bL {
return false
}
}
return false
}