-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
232 lines (196 loc) · 5.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
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
package internal
import (
"fmt"
"github.com/CircleCI-Public/circleci-config/config"
"github.com/CircleCI-Public/circleci-config/labeling/labels"
)
const nodeOrb = "circleci/node@5"
func npmTaskDefined(ls labels.LabelSet, task string) bool {
return ls[labels.DepsNode].Tasks[task] != ""
}
func nodePackageManager(ls labels.LabelSet) string {
if ls[labels.PackageManagerYarn].Valid {
return "yarn"
}
return "npm"
}
func nodeRunCommand(ls labels.LabelSet, task string) string {
if task == "test" {
// don't fail for default test value for new node projects
if ls[labels.DepsNode].Tasks[task] == "echo \"Error: no test specified\" && exit 1" {
return "echo \\\"No test specified in package.json\\\""
}
return fmt.Sprintf("%s test --passWithNoTests", nodePackageManager(ls))
}
return fmt.Sprintf("%s run %s", nodePackageManager(ls), task)
}
func nodeInitialSteps(ls labels.LabelSet) []config.Step {
steps := []config.Step{
checkoutStep(ls[labels.DepsNode]),
}
installParams := config.OrbCommandParameters{
"pkg-manager": nodePackageManager(ls),
}
if !ls[labels.DepsNode].HasLockFile {
installParams = config.OrbCommandParameters{
"cache-path": "~/project/node_modules",
"override-ci-command": fmt.Sprintf("%s install", nodePackageManager(ls)),
}
}
steps = append(steps,
config.Step{
Type: config.OrbCommand,
Command: "node/install-packages",
Parameters: installParams,
},
)
return steps
}
func nodeTestSteps(ls labels.LabelSet) []config.Step {
hasJestLabel := ls[labels.TestJest].Valid
if npmTaskDefined(ls, "test:ci") {
return []config.Step{{
Name: "Run tests",
Type: config.Run,
Command: nodeRunCommand(ls, "test:ci"),
}}
}
if npmTaskDefined(ls, "test") {
if hasJestLabel {
return []config.Step{{
Name: "Run tests",
Type: config.Run,
Command: fmt.Sprintf(
"%s run test --ci --runInBand --reporters=default --reporters=jest-junit",
nodePackageManager(ls)),
}}
}
return []config.Step{{
Name: "Run tests",
Type: config.Run,
Command: nodeRunCommand(ls, "test"),
}}
}
if npmTaskDefined(ls, "test:unit") {
return []config.Step{{
Name: "Run tests",
Type: config.Run,
Command: nodeRunCommand(ls, "test:unit"),
}}
}
if hasJestLabel {
return []config.Step{{
Type: config.Run,
Name: "Run tests with Jest",
Command: "./node_modules/.bin/jest --ci --runInBand --reporters=default --reporters=jest-junit",
}}
}
return []config.Step{}
}
func nodeTestJob(ls labels.LabelSet) *Job {
hasJestLabel := ls[labels.TestJest].Valid
steps := nodeInitialSteps(ls)
if hasJestLabel && ls[labels.DepsNode].Dependencies["jest-junit"] == "" {
if nodePackageManager(ls) == "yarn" {
command := "yarn add jest-junit --ignore-workspace-root-check"
if ls[labels.PackageManagerYarn].Version == "berry" {
// yarn-berry doesn't support --ignore-workspace-root-check and it's not needed in this case
command = "yarn add jest-junit"
}
steps = append(steps, config.Step{
Type: config.Run,
Command: command,
})
} else {
steps = append(steps, config.Step{
Type: config.Run,
Command: "npm install jest-junit",
})
}
}
testSteps := nodeTestSteps(ls)
if len(testSteps) == 0 {
return nil
}
steps = append(steps, testSteps...)
if hasJestLabel {
steps = append(steps, config.Step{
Type: config.OrbCommand,
Command: "store_test_results",
Parameters: config.OrbCommandParameters{
"path": "./test-results/",
},
})
}
job := config.Job{
Name: "test-node",
Comment: "Install node dependencies and run tests",
Executor: "node/default",
WorkingDirectory: workingDirectory(ls[labels.DepsNode]),
Steps: steps}
if hasJestLabel {
job.Environment = map[string]string{
"JEST_JUNIT_OUTPUT_DIR": "./test-results/",
}
}
return &Job{
Job: job,
Type: TestJob,
Orbs: map[string]string{"node": nodeOrb},
}
}
func nodeBuildJob(ls labels.LabelSet) *Job {
// Possible build task names in order of preference
buildTasks := []string{
"build:ci",
"build:production",
"build:prod",
"build",
"build:development",
"build:dev",
}
steps := nodeInitialSteps(ls)
for _, task := range buildTasks {
if npmTaskDefined(ls, task) {
steps = append(steps, []config.Step{
{
Type: config.Run,
Command: nodeRunCommand(ls, task),
},
createArtifactsDirStep,
{
Type: config.Run,
Comment: "Copy output to artifacts dir",
Name: "Copy artifacts",
Command: "cp -R build dist public .output .next .docusaurus ~/artifacts 2>/dev/null || true",
},
storeArtifactsStep("node-build")}...)
return &Job{
Job: config.Job{
Name: "build-node",
Comment: "Build node project",
Executor: "node/default",
WorkingDirectory: workingDirectory(ls[labels.DepsNode]),
Steps: steps,
},
Type: ArtifactJob,
Orbs: map[string]string{"node": nodeOrb},
}
}
}
return nil
}
func GenerateNodeJobs(ls labels.LabelSet) (jobs []*Job) {
if !ls[labels.DepsNode].Valid {
return nil
}
testJob := nodeTestJob(ls)
if testJob != nil {
jobs = append(jobs, testJob)
}
buildJob := nodeBuildJob(ls)
if buildJob != nil {
jobs = append(jobs, buildJob)
}
return jobs
}