Skip to content

Commit 33c4189

Browse files
committed
feat: add run-script workspaces
- Add workspaces-related configs: - workspace: list of workspaces names/dir to filter for - workspaces: boolean value to enable/disable workspaces awareness - adds the proposed note in the docs of each of the commands that are not affected by these configs. - Add workspaces support to `npm run-script` - add ability to serially run lifecycle scripts in workspaces - add ability to list scripts for all workspaces - add colors to `npm run` (no args) output Relates to: npm/rfcs#117 Fixes: npm/statusboard#276 Fixes: npm/statusboard#283 Fixes: npm/statusboard#284 Fixes: npm/statusboard#285 Fixes: npm/statusboard#286 PR-URL: #2864 Credit: @ruyadorno Close: #2864 Reviewed-by: @wraithgar
1 parent 8a38afe commit 33c4189

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+964
-36
lines changed

docs/content/commands/npm-adduser.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ npm adduser [--registry=url] [--scope=@orgname] [--always-auth] [--auth-type=leg
1212
aliases: login, add-user
1313
```
1414

15+
Note: This command is unaware of workspaces.
16+
1517
### Description
1618

1719
Create or verify a user named `<username>` in the specified registry, and

docs/content/commands/npm-bin.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Display npm bin folder
1010
npm bin [-g|--global]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Print the folder where npm will install executables.

docs/content/commands/npm-cache.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ aliases: npm cache clear, npm cache rm
1818
npm cache verify
1919
```
2020

21+
Note: This command is unaware of workspaces.
22+
2123
### Description
2224

2325
Used to add, list, or clean the npm cache folder.

docs/content/commands/npm-completion.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Tab Completion for npm
1010
source <(npm completion)
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Enables tab-completion in all npm commands.

docs/content/commands/npm-config.md

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ npm get [<key> [<key> ...]]
1818
alias: c
1919
```
2020
21+
Note: This command is unaware of workspaces.
22+
2123
### Description
2224
2325
npm gets its config settings from the command line, environment

docs/content/commands/npm-deprecate.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Deprecate a version of a package
1010
npm deprecate <pkg>[@<version range>] <message>
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
This command will update the npm registry entry for a package, providing a

docs/content/commands/npm-doctor.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Check your npm environment
1010
npm doctor
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
`npm doctor` runs a set of checks to ensure that your npm installation has

docs/content/commands/npm-edit.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Edit an installed package
1010
npm edit <pkg>
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Selects a dependency in the current project and opens the package folder in

docs/content/commands/npm-explore.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Browse an installed package
1010
npm explore <pkg> [ -- <command>]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Spawn a subshell in the directory of the installed package specified.

docs/content/commands/npm-help-search.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Search npm help documentation
1010
npm help-search <text>
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
This command will search the npm markdown documentation files for the terms

docs/content/commands/npm-help.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Get help on npm
1010
npm help <term> [<terms..>]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
If supplied a topic, then show the appropriate documentation page.

docs/content/commands/npm-hook.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ npm hook update <id> <url> [secret]
1313
npm hook rm <id>
1414
```
1515

16+
Note: This command is unaware of workspaces.
17+
1618
### Description
1719

1820
Allows you to manage [npm

docs/content/commands/npm-logout.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Log out of the registry
1010
npm logout [--registry=<url>] [--scope=<@scope>]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
When logged into a registry that supports token-based authentication, tell

docs/content/commands/npm-org.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ npm org rm <orgname> <username>
1212
npm org ls <orgname> [<username>]
1313
```
1414

15+
Note: This command is unaware of workspaces.
16+
1517
### Example
1618

1719
Add a new developer to an org:

docs/content/commands/npm-owner.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ npm owner ls [<@scope>/]<pkg>
1414
aliases: author
1515
```
1616

17+
Note: This command is unaware of workspaces.
18+
1719
### Description
1820

1921
Manage ownership of published packages.

docs/content/commands/npm-ping.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Ping npm registry
1010
npm ping [--registry <registry>]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Ping the configured or given npm registry and verify authentication.

docs/content/commands/npm-prefix.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Display prefix
1010
npm prefix [-g]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Print the local prefix to standard output. This is the closest parent directory

docs/content/commands/npm-profile.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ npm profile enable-2fa [auth-and-writes|auth-only]
1414
npm profile disable-2fa
1515
```
1616

17+
Note: This command is unaware of workspaces.
18+
1719
### Description
1820

1921
Change your profile information on the registry. Note that this command

docs/content/commands/npm-run-script.md

+85
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ description: Run arbitrary package scripts
88

99
```bash
1010
npm run-script <command> [--if-present] [--silent] [-- <args>]
11+
npm run-script <command> [--workspace=<workspace-name>]
12+
npm run-script <command> [--workspaces]
1113

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

83+
### Workspaces support
84+
85+
You may use the `workspace` or `workspaces` configs in order to run an
86+
arbitrary command from a package's `"scripts"` object in the context of the
87+
specified workspaces. If no `"command"` is provided, it will list the available
88+
scripts for each of these configured workspaces.
89+
90+
Given a project with configured workspaces, e.g:
91+
92+
```
93+
.
94+
+-- package.json
95+
`-- packages
96+
+-- a
97+
| `-- package.json
98+
+-- b
99+
| `-- package.json
100+
`-- c
101+
`-- package.json
102+
```
103+
104+
Assuming the workspace configuration is properly set up at the root level
105+
`package.json` file. e.g:
106+
107+
```
108+
{
109+
"workspaces": [ "./packages/*" ]
110+
}
111+
```
112+
113+
And that each of the configured workspaces has a configured `test` script,
114+
we can run tests in all of them using the `workspaces` config:
115+
116+
```
117+
npm test --workspaces
118+
```
119+
120+
#### Filtering workspaces
121+
122+
It's also possible to run a script in a single workspace using the `workspace`
123+
config along with a name or directory path:
124+
125+
```
126+
npm test --workspace=a
127+
```
128+
129+
The `workspace` config can also be specified multiple times in order to run a
130+
specific script in the context of multiple workspaces. When defining values for
131+
the `workspace` config in the command line, it also possible to use `-w` as a
132+
shorthand, e.g:
133+
134+
```
135+
npm test -w a -w b
136+
```
137+
138+
This last command will run `test` in both `./packages/a` and `./packages/b`
139+
packages.
140+
141+
81142
### Configuration
82143

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

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

175+
#### workspace
176+
177+
* Alias: `-w`
178+
* Type: Array
179+
* Default: `[]`
180+
181+
Enable running scripts in the context of workspaces while also filtering by
182+
the provided names or paths provided.
183+
184+
Valid values for the `workspace` config are either:
185+
- Workspace names
186+
- Path to a workspace directory
187+
- Path to a parent workspace directory (will result to selecting all of the
188+
children workspaces)
189+
190+
#### workspaces
191+
192+
* Alias: `-ws`
193+
* Type: Boolean
194+
* Default: `false`
195+
196+
Run scripts in the context of all configured workspaces for the current
197+
project.
198+
114199
### See Also
115200

116201
* [npm scripts](/using-npm/scripts)

docs/content/commands/npm-search.md

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ npm search [-l|--long] [--json] [--parseable] [--no-description] [search terms .
1212
aliases: s, se, find
1313
```
1414

15+
Note: This command is unaware of workspaces.
16+
1517
### Description
1618

1719
Search the registry for packages matching the search terms. `npm search`

docs/content/commands/npm-shrinkwrap.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Lock down dependency versions for publication
1010
npm shrinkwrap
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
This command repurposes `package-lock.json` into a publishable

docs/content/commands/npm-star.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Mark your favorite packages
1010
npm star [<pkg>...]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
"Starring" a package means that you have some interest in it. It's

docs/content/commands/npm-stars.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ description: View packages marked as favorites
99
npm stars [<user>]
1010
```
1111

12+
Note: This command is unaware of workspaces.
13+
1214
### Description
1315

1416
If you have starred a lot of neat things and want to find them again

docs/content/commands/npm-team.md

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ npm team rm <scope:team> <user>
1616
npm team ls <scope>|<scope:team>
1717
```
1818

19+
Note: This command is unaware of workspaces.
20+
1921
### Description
2022

2123
Used to manage teams in organizations, and change team memberships. Does not

docs/content/commands/npm-token.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ description: Manage your authentication tokens
99
npm token list [--json|--parseable]
1010
npm token create [--read-only] [--cidr=1.1.1.1/24,2.2.2.2/16]
1111
npm token revoke <id|token>
12-
```
12+
```
13+
14+
Note: This command is unaware of workspaces.
1315

1416
### Description
1517

docs/content/commands/npm-unstar.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Remove an item from your favorite packages
1010
npm unstar [<pkg>...]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
"Unstarring" a package is the opposite of [`npm star`](/commands/npm-star),

docs/content/commands/npm-whoami.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ description: Display npm username
1010
npm whoami [--registry <registry>]
1111
```
1212

13+
Note: This command is unaware of workspaces.
14+
1315
### Description
1416

1517
Print the `username` config to standard output.

docs/content/using-npm/workspaces.md

+46
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,54 @@ This demonstrates how the nature of `node_modules` resolution allows for
8888
in such a way that is also easy to [publish](/commands/npm-publish) these
8989
nested workspaces to be consumed elsewhere.
9090

91+
### Running commands in the context of workspaces
92+
93+
You man use the `workspace` configuration option to run commands in the context
94+
of a configured workspace.
95+
96+
Following is a quick example on how to use the `npm run` command in the context
97+
of nested workspaces. For a project containing multiple workspaces, e.g:
98+
99+
```
100+
.
101+
+-- package.json
102+
`-- packages
103+
+-- a
104+
| `-- package.json
105+
`-- b
106+
`-- package.json
107+
```
108+
109+
By running a command using the `workspace` option, it's possible to run the
110+
given command in the context of that specific workspace. e.g:
111+
112+
```
113+
npm run test --workspace=a
114+
```
115+
116+
This will run the `test` script defined within the
117+
`./packages/a/package.json` file.
118+
119+
Please note that you can also specify this argument multiple times in the
120+
command-line in order to target multiple workspaces, e.g:
121+
122+
```
123+
npm run test --workspace=a --workspace=b
124+
```
125+
126+
It's also possible to use the `workspaces` (plural) configuration option to
127+
enable the same behavior but running that command in the context of **all**
128+
configured workspaces. e.g:
129+
130+
```
131+
npm run test --workspaces
132+
```
133+
134+
Will run the `test` script in both `./packages/a` and `./packages/b`.
135+
91136
### See also
92137

93138
* [npm install](/commands/npm-install)
94139
* [npm publish](/commands/npm-publish)
140+
* [npm run-script](/commands/npm-run-script)
95141

0 commit comments

Comments
 (0)