2020-04-24 18:29:30 +02:00
// Code generated by piper's step-generator. DO NOT EDIT.
package cmd
import (
"fmt"
"os"
"time"
"github.com/SAP/jenkins-library/pkg/config"
"github.com/SAP/jenkins-library/pkg/log"
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"
"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" `
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-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
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
}
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 )
}
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 ( )
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 )
}
2020-04-24 18:29:30 +02:00
npmExecuteScripts ( stepConfig , & telemetryData )
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." )
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
} ,
2020-04-24 18:29:30 +02:00
} ,
} ,
2020-11-20 09:13:59 +02:00
Containers : [ ] config . Container {
2021-06-17 16:19:39 +02:00
{ Name : "node" , Image : "node:12-buster" } ,
2020-11-20 09:13:59 +02:00
} ,
2020-04-24 18:29:30 +02:00
} ,
}
return theMetaData
}