sap-business-technology-platformsap-project-piper

How to pass arguments / parameters to the npm-script using npmExecuteScripts?


I'm trying to run the npm-script from the Jenkins pipeline via the SAP Project Piper's npmExecuteScripts:

npmExecuteScripts:
    runScripts: ["testScript"]

That works! Now, I want to pass some arguments to my script.
According to the Project Piper documentation, there is a property scriptOptions, which cares about passing arguments to the called script:

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

Unfortunately, I can't figure out what is the proper syntax for that command.
I've tried several combinations of using scriptOptions, e.g.:

scriptOptions: ["myArg"]
scriptOptions: ["myArg=myVal"]

and many others, but still no desired outcome!

How can I call an npm-script and pass arguments / parameters to the script using the Project Piper's npmExecuteScripts?


Solution

  • To solve the issue, it's important to bear in mind that in contrast to the regular argument-value mapping via the npm_config_-prefix, the SAP Project Piper scriptOptions doesn't perform a mapping and passes an array of argument-value pairs «as is» instead, and then this array can be picked up via process.argv.

    The Jenkins pipeline configuration:

    npmExecuteScripts:
        runScripts: ["testScript"]
        scriptOptions: ["arg1=Val1", "arg2=Val2"]
    

    package.json:

    "scripts": {
        "testScript": "node ./testScript.mjs"
    }
    

    The server-side script:

    /**
     * @param {Array.<String>} args - array of console input arguments to be parsed
     */
    const testScript = function testScript(args) {…}
    
    testScript(process.argv.slice(2));
    

    P.S. Just to compare, the regular way to pass an argument's value to the npm-script looks like:

    npm run testScript --arg=Val
    

    and the server-side script:

    "testScript": "echo \"*** My argument's value: ${npm_config_arg} ***\""
    

    The output:

    *** My argument's value: Val ***

    The npm-script engine performs an argument-value mapping under the hood by using the npm_config_-prefix.