-
Notifications
You must be signed in to change notification settings - Fork 55
/
yarn.go
368 lines (322 loc) · 10.7 KB
/
yarn.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
package yarn
import (
"bufio"
"encoding/json"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/jfrog/build-info-go/build"
buildUtils "github.com/jfrog/jfrog-cli-core/v2/common/build"
commandUtils "github.com/jfrog/jfrog-cli-core/v2/artifactory/commands/utils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils"
"github.com/jfrog/jfrog-cli-core/v2/artifactory/utils/yarn"
"github.com/jfrog/jfrog-cli-core/v2/common/project"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli-core/v2/utils/ioutils"
"github.com/jfrog/jfrog-client-go/auth"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/jfrog/jfrog-client-go/utils/log"
)
const (
YarnrcFileName = ".yarnrc.yml"
YarnrcBackupFileName = "jfrog.yarnrc.backup"
NpmScopesConfigName = "npmScopes"
YarnLockFileName = "yarn.lock"
//#nosec G101
yarnNpmRegistryServerEnv = "YARN_NPM_REGISTRY_SERVER"
yarnNpmAuthIndent = "YARN_NPM_AUTH_IDENT"
yarnNpmAlwaysAuth = "YARN_NPM_ALWAYS_AUTH"
)
type YarnCommand struct {
executablePath string
workingDirectory string
registry string
npmAuthIdent string
repo string
collectBuildInfo bool
configFilePath string
yarnArgs []string
threads int
serverDetails *config.ServerDetails
buildConfiguration *buildUtils.BuildConfiguration
buildInfoModule *build.YarnModule
}
func NewYarnCommand() *YarnCommand {
return &YarnCommand{}
}
func (yc *YarnCommand) SetConfigFilePath(configFilePath string) *YarnCommand {
yc.configFilePath = configFilePath
return yc
}
func (yc *YarnCommand) SetArgs(args []string) *YarnCommand {
yc.yarnArgs = args
return yc
}
func (yc *YarnCommand) Run() (err error) {
log.Info("Running Yarn...")
if err = yc.validateSupportedCommand(); err != nil {
return
}
if err = yc.readConfigFile(); err != nil {
return
}
var filteredYarnArgs []string
yc.threads, _, _, _, filteredYarnArgs, yc.buildConfiguration, err = commandUtils.ExtractYarnOptionsFromArgs(yc.yarnArgs)
if err != nil {
return
}
if err = yc.preparePrerequisites(); err != nil {
return
}
var missingDepsChan chan string
var missingDependencies []string
if yc.collectBuildInfo {
missingDepsChan, err = yc.prepareBuildInfo()
if err != nil {
return
}
go func() {
for depId := range missingDepsChan {
missingDependencies = append(missingDependencies, depId)
}
}()
}
restoreYarnrcFunc, err := ioutils.BackupFile(filepath.Join(yc.workingDirectory, YarnrcFileName), YarnrcBackupFileName)
if err != nil {
return errors.Join(err, restoreYarnrcFunc())
}
backupEnvMap, err := ModifyYarnConfigurations(yc.executablePath, yc.registry, yc.npmAuthIdent)
if err != nil {
return errors.Join(err, restoreYarnrcFunc())
}
yc.buildInfoModule.SetArgs(filteredYarnArgs)
if err = yc.buildInfoModule.Build(); err != nil {
return errors.Join(err, restoreYarnrcFunc())
}
if yc.collectBuildInfo {
close(missingDepsChan)
commandUtils.PrintMissingDependencies(missingDependencies)
}
if err = RestoreConfigurationsFromBackup(backupEnvMap, restoreYarnrcFunc); err != nil {
return
}
log.Info("Yarn finished successfully.")
return
}
func (yc *YarnCommand) ServerDetails() (*config.ServerDetails, error) {
return yc.serverDetails, nil
}
func (yc *YarnCommand) CommandName() string {
return "rt_yarn"
}
func (yc *YarnCommand) validateSupportedCommand() error {
for index, arg := range yc.yarnArgs {
if arg == "npm" && len(yc.yarnArgs) > index {
npmCommand := yc.yarnArgs[index+1]
// The command 'yarn npm publish' is not supported
if npmCommand == "publish" {
return errorutils.CheckErrorf("The command 'jfrog rt yarn npm publish' is not supported. Use 'jfrog rt upload' instead.")
}
// 'yarn npm *' commands other than 'info' and 'whoami' are not supported
if npmCommand != "info" && npmCommand != "whoami" {
return errorutils.CheckErrorf("The command 'jfrog rt yarn npm %s' is not supported.", npmCommand)
}
}
}
return nil
}
func (yc *YarnCommand) readConfigFile() error {
log.Debug("Preparing to read the config file", yc.configFilePath)
vConfig, err := project.ReadConfigFile(yc.configFilePath, project.YAML)
if err != nil {
return err
}
// Extract resolution params
resolverParams, err := project.GetRepoConfigByPrefix(yc.configFilePath, project.ProjectConfigResolverPrefix, vConfig)
if err != nil {
return err
}
yc.repo = resolverParams.TargetRepo()
yc.serverDetails, err = resolverParams.ServerDetails()
return err
}
func (yc *YarnCommand) preparePrerequisites() error {
log.Debug("Preparing prerequisites.")
var err error
if err = yc.setYarnExecutable(); err != nil {
return err
}
yc.workingDirectory, err = coreutils.GetWorkingDirectory()
if err != nil {
return err
}
log.Debug("Working directory set to:", yc.workingDirectory)
yc.collectBuildInfo, err = yc.buildConfiguration.IsCollectBuildInfo()
if err != nil {
return err
}
buildName, err := yc.buildConfiguration.GetBuildName()
if err != nil {
return err
}
buildNumber, err := yc.buildConfiguration.GetBuildNumber()
if err != nil {
return err
}
buildInfoService := buildUtils.CreateBuildInfoService()
npmBuild, err := buildInfoService.GetOrCreateBuildWithProject(buildName, buildNumber, yc.buildConfiguration.GetProject())
if err != nil {
return errorutils.CheckError(err)
}
yc.buildInfoModule, err = npmBuild.AddYarnModule(yc.workingDirectory)
if err != nil {
return errorutils.CheckError(err)
}
if yc.buildConfiguration.GetModule() != "" {
yc.buildInfoModule.SetName(yc.buildConfiguration.GetModule())
}
yc.registry, yc.npmAuthIdent, err = GetYarnAuthDetails(yc.serverDetails, yc.repo)
return err
}
func (yc *YarnCommand) prepareBuildInfo() (missingDepsChan chan string, err error) {
log.Info("Preparing for dependencies information collection... For the first run of the build, the dependencies collection may take a few minutes. Subsequent runs should be faster.")
servicesManager, err := utils.CreateServiceManager(yc.serverDetails, -1, 0, false)
if err != nil {
return
}
// Collect checksums from last build to decrease requests to Artifactory
buildName, err := yc.buildConfiguration.GetBuildName()
if err != nil {
return
}
previousBuildDependencies, err := commandUtils.GetDependenciesFromLatestBuild(servicesManager, buildName)
if err != nil {
return
}
missingDepsChan = make(chan string)
collectChecksumsFunc := commandUtils.CreateCollectChecksumsFunc(previousBuildDependencies, servicesManager, missingDepsChan)
yc.buildInfoModule.SetTraverseDependenciesFunc(collectChecksumsFunc)
yc.buildInfoModule.SetThreads(yc.threads)
return
}
func (yc *YarnCommand) setYarnExecutable() error {
yarnExecPath, err := exec.LookPath("yarn")
if err != nil {
return errorutils.CheckError(err)
}
yc.executablePath = yarnExecPath
log.Debug("Found Yarn executable at:", yc.executablePath)
return nil
}
func GetYarnAuthDetails(server *config.ServerDetails, repo string) (string, string, error) {
authRtDetails, err := setArtifactoryAuth(server)
if err != nil {
return "", "", err
}
var npmAuthOutput string
npmAuthOutput, registry, err := commandUtils.GetArtifactoryNpmRepoDetails(repo, &authRtDetails)
if err != nil {
return "", "", err
}
npmAuthIdent, err := extractAuthIdentFromNpmAuth(npmAuthOutput)
if err != nil {
return "", "", err
}
return registry, npmAuthIdent, nil
}
func setArtifactoryAuth(server *config.ServerDetails) (auth.ServiceDetails, error) {
authArtDetails, err := server.CreateArtAuthConfig()
if err != nil {
return nil, err
}
if authArtDetails.GetSshAuthHeaders() != nil {
return nil, errorutils.CheckErrorf("SSH authentication is not supported in this command")
}
return authArtDetails, nil
}
func RestoreConfigurationsFromBackup(envVarsBackup map[string]*string, restoreYarnrcFunc func() error) error {
if err := restoreEnvironmentVariables(envVarsBackup); err != nil {
return err
}
return restoreYarnrcFunc()
}
func restoreEnvironmentVariables(envVarsBackup map[string]*string) error {
for key, value := range envVarsBackup {
if value == nil || *value == "" {
if err := os.Unsetenv(key); err != nil {
return err
}
continue
}
if err := os.Setenv(key, *value); err != nil {
return err
}
}
return nil
}
func ModifyYarnConfigurations(execPath, registry, npmAuthIdent string) (map[string]*string, error) {
envVarsUpdated := map[string]string{
yarnNpmRegistryServerEnv: registry,
yarnNpmAuthIndent: npmAuthIdent,
yarnNpmAlwaysAuth: "true",
}
envVarsBackup := make(map[string]*string)
for key, value := range envVarsUpdated {
oldVal, err := backupAndSetEnvironmentVariable(key, value)
if err != nil {
return nil, err
}
envVarsBackup[key] = &oldVal
}
// Update scoped registries (these cannot be set in environment variables)
return envVarsBackup, errorutils.CheckError(updateScopeRegistries(execPath, registry, npmAuthIdent))
}
func updateScopeRegistries(execPath, registry, npmAuthIdent string) error {
npmScopesStr, err := yarn.ConfigGet(NpmScopesConfigName, execPath, true)
if err != nil {
return err
}
npmScopesMap := make(map[string]yarnNpmScope)
err = json.Unmarshal([]byte(npmScopesStr), &npmScopesMap)
if err != nil {
return errorutils.CheckError(err)
}
artifactoryScope := yarnNpmScope{NpmAlwaysAuth: true, NpmAuthIdent: npmAuthIdent, NpmRegistryServer: registry}
for scopeName := range npmScopesMap {
npmScopesMap[scopeName] = artifactoryScope
}
updatedNpmScopesStr, err := json.Marshal(npmScopesMap)
if err != nil {
return errorutils.CheckError(err)
}
return yarn.ConfigSet(NpmScopesConfigName, string(updatedNpmScopesStr), execPath, true)
}
type yarnNpmScope struct {
NpmAlwaysAuth bool `json:"npmAlwaysAuth,omitempty"`
NpmAuthIdent string `json:"npmAuthIdent,omitempty"`
NpmRegistryServer string `json:"npmRegistryServer,omitempty"`
}
func backupAndSetEnvironmentVariable(key, value string) (string, error) {
oldVal, _ := os.LookupEnv(key)
return oldVal, errorutils.CheckError(os.Setenv(key, value))
}
// npmAuth we get back from Artifactory includes several fields, but we need only the field '_auth'
func extractAuthIdentFromNpmAuth(npmAuth string) (string, error) {
authIdentFieldName := "_auth"
scanner := bufio.NewScanner(strings.NewReader(npmAuth))
for scanner.Scan() {
currLine := scanner.Text()
if !strings.HasPrefix(currLine, authIdentFieldName) {
continue
}
lineParts := strings.SplitN(currLine, "=", 2)
if len(lineParts) < 2 {
return "", errorutils.CheckErrorf("failed while retrieving npm auth details from Artifactory")
}
return strings.TrimSpace(lineParts[1]), nil
}
return "", errorutils.CheckErrorf("failed while retrieving npm auth details from Artifactory")
}