forked from mcoops/deplist
/
nodejs.go
173 lines (138 loc) · 3.75 KB
/
nodejs.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
package scan
import (
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"strings"
"github.com/RedHatProductSecurity/deplist/internal/utils"
log "github.com/sirupsen/logrus"
)
type yarnDependencies []yarnDependency
type yarnDependency struct {
Name string `json:"name"`
Children yarnDependencies `json:"children"`
}
type yarnOutput struct {
Type string `json:"type"`
Data struct {
Deps yarnDependencies `json:"trees"`
}
}
type npmDependency struct {
Version string `json:"version"`
Dependencies map[string]npmDependency `json:"dependencies"`
}
type npmListOutput struct {
Dependencies map[string]npmDependency `json:"dependencies"`
}
// NodeJSGather dependencies found, name and version
type NodeJSGather struct {
Name string
Version string
}
// TODO: remove this global
var gatheredNode map[string]NodeJSGather
func recordPackage(packageName, version string) {
// opposite now, we don't care if its specifying version ranges like 5.x.x,
// or 5.* etc. Just get the versions.
if len(version) > 0 {
// skip if it's specifying the major
// i.e. d3-path@2 only care about d3-path@2.0.0
if len(version) == 1 {
return
}
// if not digit, then range
if !utils.CharIsDigit(version) {
return
}
if version[len(version)-1] == 'x' {
return
}
// still a version range
if strings.Contains(version, " - ") || strings.Contains(version, "||") {
return
}
}
if _, ok := gatheredNode[packageName+version]; !ok {
gatheredNode[packageName+version] = NodeJSGather{
Name: packageName,
Version: version,
}
}
}
func gatherYarnNode(dep yarnDependency) {
// incase package starts with @
splitIdx := strings.LastIndex(dep.Name, "@")
var name string
var version string
if splitIdx != -1 {
name = dep.Name[:splitIdx]
version = dep.Name[splitIdx+1:]
} else {
name = dep.Name
version = ""
}
recordPackage(name, version)
if len(dep.Children) > 0 {
for _, child := range dep.Children {
gatherYarnNode(child)
}
}
}
func gatherNPMNode(name string, dependency npmDependency) {
recordPackage(name, dependency.Version)
for childName, childDep := range dependency.Dependencies {
gatherNPMNode(childName, childDep)
}
}
// GetNodeJSDeps scans the path for either `yarn.lock` or `package-lock.json`,
// then use the appropriate pkg managers to produce depencies lists of type
// `NodeJSGather`
func GetNodeJSDeps(path string) (map[string]NodeJSGather, error) {
log.Debugf("GetNodeJSDeps %s", path)
switch filepath.Base(path) {
case "yarn.lock":
return getYarnDeps(path)
case "package-lock.json":
return getNPMDeps(path)
}
return nil, fmt.Errorf("unknown NodeJS dependency file %q", path)
}
func getYarnDeps(path string) (map[string]NodeJSGather, error) {
var yarnOutput yarnOutput
gatheredNode = make(map[string]NodeJSGather)
dirPath := filepath.Dir(path)
data, err := exec.Command("yarn", "--cwd", dirPath, "list", "--prod", "--json", "--no-progress").Output()
if err != nil {
return nil, err
}
err = json.Unmarshal(data, &yarnOutput)
if err != nil {
return nil, err
}
for _, deps := range yarnOutput.Data.Deps {
gatherYarnNode(deps)
}
return gatheredNode, nil
}
func getNPMDeps(path string) (map[string]NodeJSGather, error) {
var npmOutput npmListOutput
gatheredNode = make(map[string]NodeJSGather)
cmd := exec.Command("npm", "list", "--prod", "--json", "--depth=99")
cmd.Dir = filepath.Dir(path)
data, err := cmd.Output()
// npm has a nasty habbit of not returning cleanly so if there is data
// just attempt to unmarshal
if data == nil && err != nil {
return nil, err
}
err = json.Unmarshal(data, &npmOutput)
if err != nil {
return nil, err
}
for depName, dep := range npmOutput.Dependencies {
gatherNPMNode(depName, dep)
}
return gatheredNode, nil
}