-
Notifications
You must be signed in to change notification settings - Fork 594
/
Copy pathnpmExecuteScripts.go
126 lines (107 loc) · 3.94 KB
/
npmExecuteScripts.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
package cmd
import (
"encoding/json"
"os"
"github.com/SAP/jenkins-library/pkg/build"
"github.com/SAP/jenkins-library/pkg/buildsettings"
"github.com/SAP/jenkins-library/pkg/log"
"github.com/SAP/jenkins-library/pkg/npm"
"github.com/SAP/jenkins-library/pkg/telemetry"
"github.com/SAP/jenkins-library/pkg/versioning"
)
func npmExecuteScripts(config npmExecuteScriptsOptions, telemetryData *telemetry.CustomData, commonPipelineEnvironment *npmExecuteScriptsCommonPipelineEnvironment) {
npmExecutorOptions := npm.ExecutorOptions{DefaultNpmRegistry: config.DefaultNpmRegistry}
npmExecutor := npm.NewExecutor(npmExecutorOptions)
err := runNpmExecuteScripts(npmExecutor, &config, commonPipelineEnvironment)
if err != nil {
log.SetErrorCategory(log.ErrorBuild)
log.Entry().WithError(err).Fatal("step execution failed")
}
}
func runNpmExecuteScripts(npmExecutor npm.Executor, config *npmExecuteScriptsOptions, commonPipelineEnvironment *npmExecuteScriptsCommonPipelineEnvironment) error {
// setting env. variable to omit installation of dev. dependencies
if config.Production {
os.Setenv("NODE_ENV", "production")
}
if config.Install {
if len(config.BuildDescriptorList) > 0 {
if err := npmExecutor.InstallAllDependencies(config.BuildDescriptorList); err != nil {
return err
}
} else {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
if err := npmExecutor.InstallAllDependencies(packageJSONFiles); err != nil {
return err
}
}
}
if config.CreateBOM {
if len(config.BuildDescriptorList) > 0 {
if err := npmExecutor.CreateBOM(config.BuildDescriptorList); err != nil {
return err
}
} else {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
if err := npmExecutor.CreateBOM(packageJSONFiles); err != nil {
return err
}
}
}
err := npmExecutor.RunScriptsInAllPackages(config.RunScripts, nil, config.ScriptOptions, config.VirtualFrameBuffer, config.BuildDescriptorExcludeList, config.BuildDescriptorList)
if err != nil {
return err
}
log.Entry().Debugf("creating build settings information...")
stepName := "npmExecuteScripts"
dockerImage, err := GetDockerImageValue(stepName)
if err != nil {
return err
}
npmConfig := buildsettings.BuildOptions{
Publish: config.Publish,
CreateBOM: config.CreateBOM,
DefaultNpmRegistry: config.DefaultNpmRegistry,
BuildSettingsInfo: config.BuildSettingsInfo,
DockerImage: dockerImage,
}
buildSettingsInfo, err := buildsettings.CreateBuildSettingsInfo(&npmConfig, stepName)
if err != nil {
log.Entry().Warnf("failed to create build settings info: %v", err)
}
commonPipelineEnvironment.custom.buildSettingsInfo = buildSettingsInfo
buildCoordinates := []versioning.Coordinates{}
if config.Publish {
if len(config.BuildDescriptorList) > 0 {
err = npmExecutor.PublishAllPackages(config.BuildDescriptorList, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish, &buildCoordinates)
if err != nil {
return err
}
} else {
packageJSONFiles, err := npmExecutor.FindPackageJSONFilesWithExcludes(config.BuildDescriptorExcludeList)
if err != nil {
return err
}
err = npmExecutor.PublishAllPackages(packageJSONFiles, config.RepositoryURL, config.RepositoryUsername, config.RepositoryPassword, config.PackBeforePublish, &buildCoordinates)
if err != nil {
return err
}
}
}
if config.CreateBuildArtifactsMetadata {
if len(buildCoordinates) == 0 {
log.Entry().Warnf("unable to identify artifact coordinates for the npm packages published")
return nil
}
var buildArtifacts build.BuildArtifacts
buildArtifacts.Coordinates = buildCoordinates
jsonResult, _ := json.Marshal(buildArtifacts)
commonPipelineEnvironment.custom.npmBuildArtifacts = string(jsonResult)
}
return nil
}