2020-04-24 18:29:30 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
2021-11-16 16:02:57 +02:00
"path/filepath"
2020-04-24 18:29:30 +02:00
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
2021-11-16 16:02:57 +02:00
"github.com/SAP/jenkins-library/pkg/piperenv"
2021-05-17 12:14:04 +02:00
"github.com/SAP/jenkins-library/pkg/splunk"
2020-04-24 18:29:30 +02:00
"github.com/SAP/jenkins-library/pkg/telemetry"
2021-10-01 12:49:05 +02:00
"github.com/SAP/jenkins-library/pkg/validation"
2020-04-24 18:29:30 +02:00
"github.com/spf13/cobra"
)
type npmExecuteScriptsOptions struct {
2020-07-16 17:16:55 +02:00
Install bool ` json:"install,omitempty" `
RunScripts [ ] string ` json:"runScripts,omitempty" `
DefaultNpmRegistry string ` json:"defaultNpmRegistry,omitempty" `
VirtualFrameBuffer bool ` json:"virtualFrameBuffer,omitempty" `
ScriptOptions [ ] string ` json:"scriptOptions,omitempty" `
BuildDescriptorExcludeList [ ] string ` json:"buildDescriptorExcludeList,omitempty" `
2020-11-04 17:20:26 +02:00
BuildDescriptorList [ ] string ` json:"buildDescriptorList,omitempty" `
2021-03-04 11:16:59 +02:00
CreateBOM bool ` json:"createBOM,omitempty" `
2021-07-15 14:46:04 +02:00
Publish bool ` json:"publish,omitempty" `
RepositoryURL string ` json:"repositoryUrl,omitempty" `
RepositoryPassword string ` json:"repositoryPassword,omitempty" `
RepositoryUsername string ` json:"repositoryUsername,omitempty" `
2021-11-16 16:02:57 +02:00
BuildSettingsInfo string ` json:"buildSettingsInfo,omitempty" `
}
type npmExecuteScriptsCommonPipelineEnvironment struct {
custom struct {
buildSettingsInfo string
}
}
func ( p * npmExecuteScriptsCommonPipelineEnvironment ) persist ( path , resourceName string ) {
content := [ ] struct {
category string
name string
value interface { }
} {
{ category : "custom" , name : "buildSettingsInfo" , value : p . custom . buildSettingsInfo } ,
}
errCount := 0
for _ , param := range content {
err := piperenv . SetResourceParameter ( path , resourceName , filepath . Join ( param . category , param . name ) , param . value )
if err != nil {
log . Entry ( ) . WithError ( err ) . Error ( "Error persisting piper environment." )
errCount ++
}
}
if errCount > 0 {
log . Entry ( ) . Fatal ( "failed to persist Piper environment" )
}
2020-04-24 18:29:30 +02:00
}
// NpmExecuteScriptsCommand Execute npm run scripts on all npm packages in a project
func NpmExecuteScriptsCommand ( ) * cobra . Command {
2020-04-28 07:42:02 +02:00
const STEP_NAME = "npmExecuteScripts"
2020-04-24 18:29:30 +02:00
metadata := npmExecuteScriptsMetadata ( )
var stepConfig npmExecuteScriptsOptions
var startTime time . Time
2021-11-16 16:02:57 +02:00
var commonPipelineEnvironment npmExecuteScriptsCommonPipelineEnvironment
2021-05-17 12:14:04 +02:00
var logCollector * log . CollectorHook
2020-04-24 18:29:30 +02:00
var createNpmExecuteScriptsCmd = & cobra . Command {
2020-04-28 07:42:02 +02:00
Use : STEP_NAME ,
2020-04-24 18:29:30 +02:00
Short : "Execute npm run scripts on all npm packages in a project" ,
Long : ` Execute npm run scripts in all package json files, if they implement the scripts. ` ,
2020-06-23 18:05:21 +02:00
PreRunE : func ( cmd * cobra . Command , _ [ ] string ) error {
2020-04-24 18:29:30 +02:00
startTime = time . Now ( )
2020-04-28 07:42:02 +02:00
log . SetStepName ( STEP_NAME )
2020-04-24 18:29:30 +02:00
log . SetVerbose ( GeneralConfig . Verbose )
2020-04-28 07:42:02 +02:00
2021-07-08 15:26:07 +02:00
GeneralConfig . GitHubAccessTokens = ResolveAccessTokens ( GeneralConfig . GitHubTokens )
2020-04-28 07:42:02 +02:00
path , _ := os . Getwd ( )
fatalHook := & log . FatalHook { CorrelationID : GeneralConfig . CorrelationID , Path : path }
log . RegisterHook ( fatalHook )
err := PrepareConfig ( cmd , & metadata , STEP_NAME , & stepConfig , config . OpenPiperFile )
2020-04-24 18:29:30 +02:00
if err != nil {
2020-06-24 10:04:05 +02:00
log . SetErrorCategory ( log . ErrorConfiguration )
2020-04-24 18:29:30 +02:00
return err
}
2021-07-15 14:46:04 +02:00
log . RegisterSecret ( stepConfig . RepositoryPassword )
log . RegisterSecret ( stepConfig . RepositoryUsername )
2020-05-05 08:36:24 +02:00
if len ( GeneralConfig . HookConfig . SentryConfig . Dsn ) > 0 {
sentryHook := log . NewSentryHook ( GeneralConfig . HookConfig . SentryConfig . Dsn , GeneralConfig . CorrelationID )
log . RegisterHook ( & sentryHook )
}
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
logCollector = & log . CollectorHook { CorrelationID : GeneralConfig . CorrelationID }
log . RegisterHook ( logCollector )
}
2021-10-01 12:49:05 +02:00
validation , err := validation . New ( validation . WithJSONNamesForStructFields ( ) , validation . WithPredefinedErrorMessages ( ) )
if err != nil {
return err
}
if err = validation . ValidateStruct ( stepConfig ) ; err != nil {
log . SetErrorCategory ( log . ErrorConfiguration )
return err
}
2020-04-24 18:29:30 +02:00
return nil
} ,
2020-06-23 18:05:21 +02:00
Run : func ( _ * cobra . Command , _ [ ] string ) {
2020-04-24 18:29:30 +02:00
telemetryData := telemetry . CustomData { }
telemetryData . ErrorCode = "1"
handler := func ( ) {
2020-10-26 15:20:04 +02:00
config . RemoveVaultSecretFiles ( )
2021-11-16 16:02:57 +02:00
commonPipelineEnvironment . persist ( GeneralConfig . EnvRootPath , "commonPipelineEnvironment" )
2020-04-24 18:29:30 +02:00
telemetryData . Duration = fmt . Sprintf ( "%v" , time . Since ( startTime ) . Milliseconds ( ) )
2020-09-29 13:49:40 +02:00
telemetryData . ErrorCategory = log . GetErrorCategory ( ) . String ( )
2020-04-24 18:29:30 +02:00
telemetry . Send ( & telemetryData )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Send ( & telemetryData , logCollector )
}
2020-04-24 18:29:30 +02:00
}
log . DeferExitHandler ( handler )
defer handler ( )
2020-04-28 07:42:02 +02:00
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
2021-05-17 12:14:04 +02:00
if len ( GeneralConfig . HookConfig . SplunkConfig . Dsn ) > 0 {
splunk . Initialize ( GeneralConfig . CorrelationID ,
GeneralConfig . HookConfig . SplunkConfig . Dsn ,
GeneralConfig . HookConfig . SplunkConfig . Token ,
GeneralConfig . HookConfig . SplunkConfig . Index ,
GeneralConfig . HookConfig . SplunkConfig . SendLogs )
}
2021-11-16 16:02:57 +02:00
npmExecuteScripts ( stepConfig , & telemetryData , & commonPipelineEnvironment )
2020-04-24 18:29:30 +02:00
telemetryData . ErrorCode = "0"
2020-05-18 21:52:54 +02:00
log . Entry ( ) . Info ( "SUCCESS" )
2020-04-24 18:29:30 +02:00
} ,
}
addNpmExecuteScriptsFlags ( createNpmExecuteScriptsCmd , & stepConfig )
return createNpmExecuteScriptsCmd
}
func addNpmExecuteScriptsFlags ( cmd * cobra . Command , stepConfig * npmExecuteScriptsOptions ) {
2020-11-13 14:01:02 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . Install , "install" , true , "Run npm install or similar commands depending on the project structure." )
2020-04-24 18:29:30 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . RunScripts , "runScripts" , [ ] string { } , "List of additional run scripts to execute from package.json." )
cmd . Flags ( ) . StringVar ( & stepConfig . DefaultNpmRegistry , "defaultNpmRegistry" , os . Getenv ( "PIPER_defaultNpmRegistry" ) , "URL of the npm registry to use. Defaults to https://registry.npmjs.org/" )
2020-06-16 11:42:51 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . VirtualFrameBuffer , "virtualFrameBuffer" , false , "(Linux only) Start a virtual frame buffer in the background. This allows you to run a web browser without the need for an X server. Note that xvfb needs to be installed in the execution environment." )
2020-07-09 14:57:41 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . ScriptOptions , "scriptOptions" , [ ] string { } , "Options are passed to all runScripts calls separated by a '--'. './piper npmExecuteScripts --runScripts ci-e2e --scriptOptions '--tag1' will correspond to 'npm run ci-e2e -- --tag1'" )
2020-07-16 17:16:55 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . BuildDescriptorExcludeList , "buildDescriptorExcludeList" , [ ] string { ` deployment/** ` } , "List of build descriptors and therefore modules to exclude from execution of the npm scripts. The elements can either be a path to the build descriptor or a pattern." )
2020-11-04 17:20:26 +02:00
cmd . Flags ( ) . StringSliceVar ( & stepConfig . BuildDescriptorList , "buildDescriptorList" , [ ] string { } , "List of build descriptors and therefore modules for execution of the npm scripts. The elements have to be paths to the build descriptors. **If set, buildDescriptorExcludeList will be ignored.**" )
2021-03-04 11:16:59 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . CreateBOM , "createBOM" , false , "Create a BOM xml using CycloneDX." )
2021-07-15 14:46:04 +02:00
cmd . Flags ( ) . BoolVar ( & stepConfig . Publish , "publish" , false , "Configures npm to publish the artifact to a repository." )
cmd . Flags ( ) . StringVar ( & stepConfig . RepositoryURL , "repositoryUrl" , os . Getenv ( "PIPER_repositoryUrl" ) , "Url to the repository to which the project artifacts should be published." )
cmd . Flags ( ) . StringVar ( & stepConfig . RepositoryPassword , "repositoryPassword" , os . Getenv ( "PIPER_repositoryPassword" ) , "Password for the repository to which the project artifacts should be published." )
cmd . Flags ( ) . StringVar ( & stepConfig . RepositoryUsername , "repositoryUsername" , os . Getenv ( "PIPER_repositoryUsername" ) , "Username for the repository to which the project artifacts should be published." )
2021-11-16 16:02:57 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . BuildSettingsInfo , "buildSettingsInfo" , os . Getenv ( "PIPER_buildSettingsInfo" ) , "build settings info is typically filled by the step automatically to create information about the build settings that were used during the npm build . This information is typically used for compliance related processes." )
2020-04-24 18:29:30 +02:00
}
// retrieve step metadata
func npmExecuteScriptsMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
2020-11-20 09:13:59 +02:00
Name : "npmExecuteScripts" ,
Aliases : [ ] config . Alias { { Name : "executeNpm" , Deprecated : false } } ,
Description : "Execute npm run scripts on all npm packages in a project" ,
2020-04-24 18:29:30 +02:00
} ,
Spec : config . StepSpec {
Inputs : config . StepInputs {
2021-03-01 14:03:42 +02:00
Resources : [ ] config . StepResources {
{ Name : "source" , Type : "stash" } ,
} ,
2020-04-24 18:29:30 +02:00
Parameters : [ ] config . StepParameters {
{
Name : "install" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : true ,
2020-04-24 18:29:30 +02:00
} ,
{
Name : "runScripts" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { } ,
2020-04-24 18:29:30 +02:00
} ,
{
Name : "defaultNpmRegistry" ,
ResourceRef : [ ] config . ResourceReference { } ,
2020-06-18 17:30:17 +02:00
Scope : [ ] string { "PARAMETERS" , "GENERAL" , "STAGES" , "STEPS" } ,
2020-04-24 18:29:30 +02:00
Type : "string" ,
Mandatory : false ,
2020-06-18 17:30:17 +02:00
Aliases : [ ] config . Alias { { Name : "npm/defaultNpmRegistry" } } ,
2021-06-16 08:43:30 +02:00
Default : os . Getenv ( "PIPER_defaultNpmRegistry" ) ,
2020-04-24 18:29:30 +02:00
} ,
2020-06-16 11:42:51 +02:00
{
Name : "virtualFrameBuffer" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : false ,
2020-06-16 11:42:51 +02:00
} ,
2020-07-09 14:57:41 +02:00
{
Name : "scriptOptions" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { } ,
2020-07-09 14:57:41 +02:00
} ,
2020-07-16 17:16:55 +02:00
{
Name : "buildDescriptorExcludeList" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { ` deployment/** ` } ,
2020-07-16 17:16:55 +02:00
} ,
2020-11-04 17:20:26 +02:00
{
Name : "buildDescriptorList" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : [ ] string { } ,
2020-11-04 17:20:26 +02:00
} ,
2021-03-04 11:16:59 +02:00
{
Name : "createBOM" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "GENERAL" , "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
2021-06-16 08:43:30 +02:00
Default : false ,
2021-03-04 11:16:59 +02:00
} ,
2021-07-15 14:46:04 +02:00
{
Name : "publish" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : false ,
} ,
{
Name : "repositoryUrl" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "custom/repositoryUrl" ,
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : os . Getenv ( "PIPER_repositoryUrl" ) ,
} ,
{
Name : "repositoryPassword" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "custom/repositoryPassword" ,
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : os . Getenv ( "PIPER_repositoryPassword" ) ,
} ,
{
Name : "repositoryUsername" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "custom/repositoryUsername" ,
} ,
} ,
Scope : [ ] string { "GENERAL" , "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : os . Getenv ( "PIPER_repositoryUsername" ) ,
} ,
2021-11-16 16:02:57 +02:00
{
Name : "buildSettingsInfo" ,
ResourceRef : [ ] config . ResourceReference {
{
Name : "commonPipelineEnvironment" ,
Param : "custom/buildSettingsInfo" ,
} ,
} ,
Scope : [ ] string { "STEPS" , "STAGES" , "PARAMETERS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
Default : os . Getenv ( "PIPER_buildSettingsInfo" ) ,
} ,
2020-04-24 18:29:30 +02:00
} ,
} ,
2020-11-20 09:13:59 +02:00
Containers : [ ] config . Container {
2021-07-05 12:07:22 +02:00
{ Name : "node" , Image : "node:lts-stretch" } ,
2020-11-20 09:13:59 +02:00
} ,
2021-11-16 16:02:57 +02:00
Outputs : config . StepOutputs {
Resources : [ ] config . StepResources {
{
Name : "commonPipelineEnvironment" ,
Type : "piperEnvironment" ,
Parameters : [ ] map [ string ] interface { } {
{ "Name" : "custom/buildSettingsInfo" } ,
} ,
} ,
} ,
} ,
2020-04-24 18:29:30 +02:00
} ,
}
return theMetaData
}