Skip to content

Commit

Permalink
fixup! feat: add run-script workspaces
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Mar 15, 2021
1 parent ec9f675 commit 30e8766
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
85 changes: 85 additions & 0 deletions docs/content/commands/npm-run-script.md
Expand Up @@ -8,6 +8,8 @@ description: Run arbitrary package scripts

```bash
npm run-script <command> [--if-present] [--silent] [-- <args>]
npm run-script <command> [--workspace=<workspace-name>]
npm run-script <command> [--workspaces]

aliases: run, rum, urn
```
Expand Down Expand Up @@ -78,6 +80,65 @@ If you try to run a script without having a `node_modules` directory and it
fails, you will be given a warning to run `npm install`, just in case you've
forgotten.

### Workspaces support

You may use the `workspace` or `workspaces` configs in order to run an
arbitrary command from a package's `"scripts"` object in the context of the
specified workspaces. If no `"command"` is provided, it will list the available
scripts for each of these configured workspaces.

Given a project with configured workspaces, e.g:

```
.
+-- package.json
`-- packages
+-- a
| `-- package.json
+-- b
| `-- package.json
`-- c
`-- package.json
```

Assuming the workspace configuration is properly set up at the root level
`package.json` file. e.g:

```
{
"workspaces": [ "./packages/*" ]
}
```

And that each of the configured workspaces has a configured `test` script,
we can run tests in all of them using the `workspaces` config:

```
npm test --workspaces
```

#### Filtering workspaces

It's also possible to run a script in a single workspace using the `workspace`
config along with a name or directory path:

```
npm test --workspace=a
```

The `workspace` config can also be specified multiple times in order to run a
specific script in the context of multiple workspaces. When defining values for
the `workspace` config in the command line, it also possible to use `-w` as a
shorthand, e.g:

```
npm test -w a -w b
```

This last command will run `test` in both `./packages/a` and `./packages/b`
packages.


### Configuration

#### if-present
Expand Down Expand Up @@ -111,6 +172,30 @@ to `/bin/sh` on Unix, defaults to `env.comspec` or `cmd.exe` on Windows.

You can use the `--silent` flag to prevent showing `npm ERR!` output on error.

#### workspace

* Alias: `-w`
* Type: Array
* Default: `[]`

Enable running scripts in the context of workspaces while also filtering by
the provided names or paths provided.

Valid values for the `workspace` config are either:
- Workspace names
- Path to a workspace directory
- Path to a parent workspace directory (will result to selecting all of the
children workspaces)

#### workspaces

* Alias: `-ws`
* Type: Boolean
* Default: `false`

Run scripts in the context of all configured workspaces for the current
project.

### See Also

* [npm scripts](/using-npm/scripts)
Expand Down
72 changes: 72 additions & 0 deletions docs/content/using-npm/config.md
Expand Up @@ -1356,6 +1356,78 @@ The program to use to view help content.

Set to `"browser"` to view html help content in the default web browser.

#### workspace

* Alias: `-w`
* Default: `[]`
* Type: Array

Enable running a command in the context of the configured workspaces of the
current project while filtering by running only the workspaces defined by
this configuration option.

For a project containing multiple workspaces, e.g:

```
.
+-- package.json
`-- packages
+-- a
| `-- package.json
`-- b
`-- package.json
```

By running a command using the `workspace` option, it's possible to run the
given command in the context of that workspace. e.g:

```
npm run test --workspace=a
```

Will run the `test` script defined within the `./packages/a/package.json` file.

Please note that you can also specify this argument multiple times in the
command-line in order to target multiple workspaces, e.g:

```
npm run test --workspace=a --workspace=b
```

Valid values for the `workspace` config are either:
- Workspace names
- Path to a workspace directory
- Path to a parent workspace directory (will result to selecting all of the
children workspaces)

#### workspaces

* Alias: `-ws`
* Default: `false`
* Type: boolean

Enable running a command in the context of **all** the configured workspaces.
For a project containing multiple workspaces, e.g:

```
.
+-- package.json
`-- packages
+-- a
| `-- package.json
`-- b
`-- package.json
```

Running a command using the `workspaces` option is going to execute that
command in the context of each configured workspace. e.g:

```
npm run test --workspaces
```

Will run `test` script in both `./packages/a` and `./packages/b`.

### See also

* [npm config](/commands/npm-config)
Expand Down

0 comments on commit 30e8766

Please sign in to comment.