-
Notifications
You must be signed in to change notification settings - Fork 9
/
parse-npm-lock.go
174 lines (135 loc) · 4.25 KB
/
parse-npm-lock.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
package lockfile
import (
"encoding/json"
"fmt"
"os"
"path"
"strings"
)
type NpmLockDependency struct {
Version string `json:"version"`
Dependencies map[string]NpmLockDependency `json:"dependencies,omitempty"`
}
type NpmLockPackage struct {
Name string `json:"name"`
Version string `json:"version"`
Resolved string `json:"resolved"`
Dependencies map[string]string `json:"dependencies"`
}
type NpmLockfile struct {
Version int `json:"lockfileVersion"`
// npm v1- lockfiles use "dependencies"
Dependencies map[string]NpmLockDependency `json:"dependencies"`
// npm v2+ lockfiles use "packages"
Packages map[string]NpmLockPackage `json:"packages,omitempty"`
}
const NpmEcosystem Ecosystem = "npm"
func pkgDetailsMapToSlice(m map[string]PackageDetails) []PackageDetails {
details := make([]PackageDetails, 0, len(m))
for _, detail := range m {
details = append(details, detail)
}
return details
}
func mergePkgDetailsMap(m1 map[string]PackageDetails, m2 map[string]PackageDetails) map[string]PackageDetails {
details := map[string]PackageDetails{}
for name, detail := range m1 {
details[name] = detail
}
for name, detail := range m2 {
details[name] = detail
}
return details
}
func parseNpmLockDependencies(dependencies map[string]NpmLockDependency) map[string]PackageDetails {
details := map[string]PackageDetails{}
for name, detail := range dependencies {
if detail.Dependencies != nil {
details = mergePkgDetailsMap(details, parseNpmLockDependencies(detail.Dependencies))
}
version := detail.Version
finalVersion := version
commit := ""
// we can't resolve a version from a "file:" dependency
if strings.HasPrefix(detail.Version, "file:") {
finalVersion = ""
} else {
// use the name of the underlying package rather than the alias
if strings.HasPrefix(detail.Version, "npm:") {
i := strings.LastIndex(detail.Version, "@")
name = detail.Version[4:i]
finalVersion = detail.Version[i+1:]
}
commit = tryExtractCommit(detail.Version)
// if there is a commit, we want to deduplicate based on that rather than
// the version (the versions must match anyway for the commits to match)
//
// we also don't actually know what the "version" is, so blank it
if commit != "" {
finalVersion = ""
version = commit
}
}
details[name+"@"+version] = PackageDetails{
Name: name,
Version: finalVersion,
Ecosystem: NpmEcosystem,
CompareAs: NpmEcosystem,
Commit: commit,
}
}
return details
}
func extractNpmPackageName(name string) string {
maybeScope := path.Base(path.Dir(name))
pkgName := path.Base(name)
if strings.HasPrefix(maybeScope, "@") {
pkgName = maybeScope + "/" + pkgName
}
return pkgName
}
func parseNpmLockPackages(packages map[string]NpmLockPackage) map[string]PackageDetails {
details := map[string]PackageDetails{}
for namePath, detail := range packages {
if namePath == "" {
continue
}
finalName := detail.Name
if finalName == "" {
finalName = extractNpmPackageName(namePath)
}
finalVersion := detail.Version
commit := tryExtractCommit(detail.Resolved)
// if there is a commit, we want to deduplicate based on that rather than
// the version (the versions must match anyway for the commits to match)
if commit != "" {
finalVersion = commit
}
details[finalName+"@"+finalVersion] = PackageDetails{
Name: finalName,
Version: detail.Version,
Ecosystem: NpmEcosystem,
CompareAs: NpmEcosystem,
Commit: commit,
}
}
return details
}
func parseNpmLock(lockfile NpmLockfile) map[string]PackageDetails {
if lockfile.Packages != nil {
return parseNpmLockPackages(lockfile.Packages)
}
return parseNpmLockDependencies(lockfile.Dependencies)
}
func ParseNpmLock(pathToLockfile string) ([]PackageDetails, error) {
var parsedLockfile *NpmLockfile
lockfileContents, err := os.ReadFile(pathToLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not read %s: %w", pathToLockfile, err)
}
err = json.Unmarshal(lockfileContents, &parsedLockfile)
if err != nil {
return []PackageDetails{}, fmt.Errorf("could not parse %s: %w", pathToLockfile, err)
}
return pkgDetailsMapToSlice(parseNpmLock(*parsedLockfile)), nil
}