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" `
}
// 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. ` ,
PreRunE : func ( cmd * cobra . Command , args [ ] string ) error {
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
} ,
Run : func ( cmd * cobra . Command , args [ ] string ) {
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"
} ,
}
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/" )
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." )
}
// 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 { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
} ,
{
Name : "sapNpmRegistry" ,
ResourceRef : [ ] config . ResourceReference { } ,
Scope : [ ] string { "PARAMETERS" , "STAGES" , "STEPS" } ,
Type : "string" ,
Mandatory : false ,
Aliases : [ ] config . Alias { } ,
} ,
} ,
} ,
} ,
}
return theMetaData
}