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"
"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-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
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 )
}
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 ( ) {
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 )
}
log . DeferExitHandler ( handler )
defer handler ( )
2020-04-28 07:42:02 +02:00
telemetry . Initialize ( GeneralConfig . NoTelemetry , STEP_NAME )
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 ) {
cmd . Flags ( ) . BoolVar ( & stepConfig . Install , "install" , false , "Run npm install or similar commands depending on the project structure." )
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-04-24 18:29:30 +02:00
}
// retrieve step metadata
func npmExecuteScriptsMetadata ( ) config . StepData {
var theMetaData = config . StepData {
Metadata : config . StepMetadata {
Name : "npmExecuteScripts" ,
Aliases : [ ] config . Alias { { Name : "executeNpm" , Deprecated : false } } ,
} ,
Spec : config . StepSpec {
Inputs : config . StepInputs {
Parameters : [ ] config . StepParameters {
{
Name : "install" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "bool" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
} ,
{
Name : "runScripts" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "[]string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
} ,
{
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" } } ,
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 { } ,
} ,
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 { } ,
} ,
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 { } ,
} ,
2020-04-24 18:29:30 +02:00
} ,
} ,
} ,
}
return theMetaData
}