-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
98 lines (79 loc) · 2.27 KB
/
node.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
package internal
import (
"encoding/json"
"path"
"github.com/CircleCI-Public/circleci-config/labeling/codebase"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
var lockFiles = []string{
"package-lock.json",
"yarn.lock",
}
var NodeRules = []labels.Rule{
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.DepsNode
packagePath := findPackageJSON(c)
label.Valid = packagePath != ""
if !label.Valid {
return label, err
}
err = readPackageJSON(c, packagePath, &label)
// Lock files
lockFilesPath, _ := c.FindFile(lockFiles...)
label.LabelData.HasLockFile = lockFilesPath != ""
return label, err
},
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.PackageManagerYarn
yarnLock, _ := c.FindFile("yarn.lock")
if yarnLock == "" {
return label, err
}
label.Valid = true
label.Version = "classic"
yarnrc, _ := c.FindFile(".yarnrc.yml", ".yarnrc.yaml")
if yarnrc != "" {
label.Version = "berry"
}
return label, err
},
func(c codebase.Codebase, ls labels.LabelSet) (label labels.Label, err error) {
label.Key = labels.TestJest
label.Valid = hasDependency(ls, "jest")
return label, err
},
}
// npmPackageJSON for unmarshalling npm package.json files
type npmPackageJSON struct {
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
Scripts map[string]string `json:"scripts"`
}
func findPackageJSON(c codebase.Codebase) string {
file, _ := c.FindFile("package.json")
return file
}
func readPackageJSON(c codebase.Codebase, filePath string, label *labels.Label) error {
var packageJSON npmPackageJSON
fileContents, err := c.ReadFile(filePath)
if err != nil {
return err
}
err = json.Unmarshal(fileContents, &packageJSON)
if err != nil {
return err
}
label.LabelData.BasePath = path.Dir(filePath)
label.Tasks = packageJSON.Scripts
label.Dependencies = make(map[string]string)
for k, v := range packageJSON.Dependencies {
label.Dependencies[k] = v
}
for k, v := range packageJSON.DevDependencies {
label.Dependencies[k] = v
}
return err
}
func hasDependency(ls labels.LabelSet, dep string) bool {
return ls[labels.DepsNode].Dependencies[dep] != ""
}