# packageManagerRun

This step automatically detects the package manager from your `package.json` file and runs the appropriate package manager step ([`yarnRun`](./yarnRun.md), [`npmRun`](./npmRun.md), or [`pnpmRun`](./pnpmRun.md)).
The detection is based on the `packageManager` field in `package.json`.

## 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                                |

## How it works

The step reads your `package.json` file and looks for the `packageManager` field:

```json
{
  "name": "my-app",
  "packageManager": "pnpm@8.15.0"
}
```

Based on the value, it delegates to the appropriate step:
- `npm@*` → calls [`npmRun`](./npmRun.md)
- `pnpm@*` → calls [`pnpmRun`](./pnpmRun.md)
- `yarn@*` → calls [`yarnRun`](./yarnRun.md)
- No `packageManager` field → defaults to [`yarnRun`](./yarnRun.md)
- Unsupported package manager → defaults to [`yarnRun`](./yarnRun.md)

## Usage

### Basic usage with auto-detection

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

### With specified node version

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

### Providing environment variables

```groovy
stage('Build') {
    steps {
        packageManagerRun(
            scriptNames: ['build'],
            envVars: [
                AUTH0_CLIENT_ID: '1234'
            ]
        )
    }
}
```

### With multiple scripts

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

## Setting the packageManager field in package.json

To ensure the correct package manager is used, add the `packageManager` field to your `package.json`:

```json
{
  "name": "my-app",
  "version": "1.0.0",
  "packageManager": "npm@10.0.0",
  "scripts": {
    "test": "jest",
    "build": "webpack"
  }
}
```
