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 {
Install bool ` json:"install,omitempty" `
RunScripts [ ] string ` json:"runScripts,omitempty" `
DefaultNpmRegistry string ` json:"defaultNpmRegistry,omitempty" `
SapNpmRegistry string ` json:"sapNpmRegistry,omitempty" `
2020-06-16 11:42:51 +02:00
VirtualFrameBuffer bool ` json:"virtualFrameBuffer,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 {
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 ( ) )
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-05-25 19:48:59 +02:00
cmd . Flags ( ) . StringVar ( & stepConfig . SapNpmRegistry , "sapNpmRegistry" , ` https://npm.sap.com ` , "The default npm registry URL to be used as the remote mirror for the SAP npm packages." )
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-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
} ,
{
Name : "sapNpmRegistry" ,
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/sapNpmRegistry" } } ,
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-04-24 18:29:30 +02:00
} ,
} ,
} ,
}
return theMetaData
}