# npmRun

This step runs `npm ci` and then `npm run [scriptName]` in a node prepared docker container.
The step also ensures access to our private Github npm registry by providing the value of the `GITHUB_NPM_TOKEN` to the container.

## Parameters

| Name        | Description                   | Type     | Default | Required                          |
| ----------- | ----------------------------- | -------- | ------- | --------------------------------- |
| scriptNames | The scripts to run            | `List`   | n/a     | yes                               |
| nodeVersion | The node version to use.      | `String` | n/a     | yes, if no .nvmrc file in project |
| envVars     | Map of environment variables. | `Map`    | null    | no                                |

## Usage

### Using node version specified in .nvmrc file

This step runs the [`nodeSh`](./nodeSh.md) step internally. The `nodeSh` step automatically picks up the node version from your .nvmrc file.

```groovy
stage('Unit Tests and Style Checks') {
    steps {
        npmRun(scriptNames: ['test'])
    }
}
```

### With specified node version

If you do not have a .nvmrc file or you want to override it, you can specify the version using the "nodeVersion" param.

In the example below the script is run using the [docker-parent-images:node16](https://github.com/theorchard/docker-parent-images/tree/master/node16) image and the `NODE_VERSION` environment variable is set to "16.17.0".

```groovy
stage('Unit Tests and Style Checks') {
    steps {
        npmRun(scriptNames: ['test'], nodeVersion: '16.17.0')
    }
}
```

### Providing environment variables

Your npm script might depend on environment variables. To provide them to the docker container, use the `envVars` parameter.

```groovy
stage('Unit Tests and Style Checks') {
    steps {
        npmRun(
            scriptNames: ['build'],
            envVars: [
                AUTH0_CLIENT_ID: '1234'
            ]
        )
    }
}
```

### With multiple scripts

Run multiple scripts after each other by providing them in order.
In the example below, `npm run lint` will be run before `npm run test:unit`.
If the `lint` script fails, the execution is stopped and the step fails.

```groovy
stage('Unit Tests and Style Checks') {
    steps {
        npmRun(scriptNames: ['lint', 'test:unit'])
    }
}
```
