-
Notifications
You must be signed in to change notification settings - Fork 62
/
nodeModuleCollector.go
386 lines (326 loc) · 8.76 KB
/
nodeModuleCollector.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
package node_modules
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/develar/app-builder/pkg/fs"
"github.com/develar/app-builder/pkg/log"
"github.com/develar/errors"
jsoniter "github.com/json-iterator/go"
"go.uber.org/zap"
)
type DependencyBinary struct {
NapiVersions []uint `json:"napi_versions"`
}
type Dependency struct {
Name string `json:"name"`
Version string `json:"version"`
Dependencies map[string]string `json:"dependencies"`
OptionalDependencies map[string]string `json:"optionalDependencies"`
Binary *DependencyBinary `json:"binary"`
parent *Dependency
conflictDependency map[string]*Dependency
dir string
isOptional int
alias string
}
type Collector struct {
rootDependency *Dependency
unresolvedDependencies map[string]bool
excludedDependencies map[string]bool
allDependencies []*Dependency
NodeModuleDirToDependencyMap map[string]*map[string]*Dependency `json:"nodeModuleDirToDependencyMap"`
HoiestedDependencyMap map[string]*Dependency `json:"hoiestedDependencyMap"`
}
func (t *Collector) readDependencyTree(dependency *Dependency) error {
if t.rootDependency == nil {
t.rootDependency = dependency
} else {
t.allDependencies = append(t.allDependencies, dependency)
}
maxQueueSize := len(dependency.Dependencies) + len(dependency.OptionalDependencies)
if maxQueueSize == 0 {
return nil
}
nodeModuleDir, err := findNearestNodeModuleDir(dependency.dir)
if err != nil {
return err
}
if len(nodeModuleDir) == 0 {
for name := range dependency.Dependencies {
t.unresolvedDependencies[name] = true
}
return nil
}
// process direct children first
queue := make([]*Dependency, maxQueueSize)
queueIndex := 0
queueIndex, err = t.processDependencies(&dependency.Dependencies, nodeModuleDir, false, &queue, queueIndex)
if err != nil {
return err
}
queueIndex, err = t.processDependencies(&dependency.OptionalDependencies, nodeModuleDir, true, &queue, queueIndex)
if err != nil {
return err
}
if queueIndex == 0 {
return nil
}
// do not sort - final result will be sorted
for i := 0; i < queueIndex; i++ {
err = t.readDependencyTree(queue[i])
if err != nil {
return err
}
queue[i].parent = dependency
}
return nil
}
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
}
}
last = p
p = p.parent
}
if last.conflictDependency == nil {
last.conflictDependency = make(map[string]*Dependency)
}
last.conflictDependency[d.Name] = 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
}
}
}
func (t *Collector) processDependencies(list *map[string]string, nodeModuleDir string, isOptional bool, queue *[]*Dependency, queueIndex int) (int, error) {
unresolved := make([]string, 0)
names := make([]string, 0, len(*list))
for k := range *list {
names = append(names, k)
}
sort.Strings(names)
for _, name := range names {
if strings.HasPrefix(name, "@types/") {
continue
}
if t.excludedDependencies != nil {
_, isExcluded := t.excludedDependencies[name]
if isExcluded {
continue
}
}
childDependency, err := t.resolveDependency(nodeModuleDir, name)
if err != nil {
return queueIndex, err
}
if childDependency == nil {
unresolved = append(unresolved, name)
} else {
(*queue)[queueIndex] = childDependency
correctOptionalState(isOptional, childDependency)
queueIndex++
}
}
var err error
guardCount := 0
for len(unresolved) > 0 {
nodeModuleDir, err = findNearestNodeModuleDir(getParentDir(getParentDir(nodeModuleDir)))
if err != nil {
return queueIndex, err
}
if len(nodeModuleDir) == 0 {
if !isOptional {
for _, name := range unresolved {
if len(name) != 0 {
t.unresolvedDependencies[name] = true
}
}
}
return queueIndex, nil
}
if log.IsDebugEnabled() {
log.Debug("unresolved deps", zap.Strings("unresolved", unresolved), zap.String("nodeModuleDir", nodeModuleDir), zap.Int("round", guardCount))
}
hasUnresolved := false
for index, name := range unresolved {
if len(name) == 0 {
continue
}
childDependency, err := t.resolveDependency(nodeModuleDir, name)
if err != nil {
return queueIndex, err
}
if childDependency == nil {
hasUnresolved = true
} else {
(*queue)[queueIndex] = childDependency
correctOptionalState(isOptional, childDependency)
queueIndex++
unresolved[index] = ""
}
}
if !hasUnresolved {
break
}
guardCount++
if guardCount > 999 {
return queueIndex, errors.New("Infinite loop: " + nodeModuleDir)
}
}
return queueIndex, nil
}
func correctOptionalState(isOptional bool, childDependency *Dependency) {
if isOptional {
if childDependency.isOptional == 0 {
childDependency.isOptional = 1
}
} else {
childDependency.isOptional = 2
}
}
// 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
}
}
realParentNodeModuleDir := fs.FindParentWithFile(parentNodeModuleDir, name)
if realParentNodeModuleDir == "" {
return nil, nil
}
// XXX consumers expect the node_modules shape or the dependency tree ?
// hoist the depends to the actual parent
if realParentNodeModuleDir != parentNodeModuleDir {
dependencyNameToDependency = t.NodeModuleDirToDependencyMap[realParentNodeModuleDir]
if dependencyNameToDependency != nil {
dependency := (*dependencyNameToDependency)[name]
if dependency != nil {
return nil, nil
}
}
}
dependencyDir := filepath.Join(realParentNodeModuleDir, name)
info, err := os.Stat(dependencyDir)
if err == nil && !info.IsDir() {
return nil, nil
}
dependency, err := readPackageJson(dependencyDir)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
} else {
return nil, errors.WithStack(err)
}
}
if name == "libui-node" {
// remove because production app doesn't need to download libui
//noinspection SpellCheckingInspection
delete(dependency.Dependencies, "libui-download")
}
if dependencyNameToDependency == nil {
m := make(map[string]*Dependency)
t.NodeModuleDirToDependencyMap[parentNodeModuleDir] = &m
dependencyNameToDependency = &m
}
(*dependencyNameToDependency)[name] = dependency
dependency.alias = name
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
}
realDir, err := filepath.EvalSymlinks(dir)
if err != nil {
return "", errors.WithStack(err)
}
dir = realDir
guardCount := 0
for {
nodeModuleDir := filepath.Join(dir, "node_modules")
fileInfo, err := os.Stat(nodeModuleDir)
if err != nil {
if !os.IsNotExist(err) {
return "", errors.WithStack(err)
}
} else if fileInfo.IsDir() {
return nodeModuleDir, nil
}
dir = getParentDir(dir)
if len(dir) == 0 {
return "", nil
}
guardCount++
if guardCount > 999 {
return "", errors.New("infinite loop: " + dir)
}
}
}
func getParentDir(file string) string {
if len(file) == 0 {
return file
}
dir := filepath.Dir(file)
// https://github.com/develar/app-builder/pull/3
if len(dir) > 1 /* . or / or empty */ && dir != file {
return dir
} else {
return ""
}
}
func readPackageJson(dir string) (*Dependency, error) {
packageFile := filepath.Join(dir, "package.json")
data, err := os.ReadFile(packageFile)
if err != nil {
return nil, err
}
var dependency Dependency
err = jsoniter.Unmarshal(data, &dependency)
if err != nil {
return nil, errors.WithMessage(err, "Error reading package.json: "+packageFile)
}
return &dependency, nil
}