Skip to content

Commit 2fc7c5d

Browse files
committed
Add workspaces support to reify/rebuild commands
This adds `--workspace` support to: - audit (including audit fix) - ci - dedupe - find-dupes - install - install-ci-test - install-test - prune - rebuild - uninstall - update
1 parent ec256a1 commit 2fc7c5d

17 files changed

+221
-23
lines changed

docs/content/commands/npm-audit.md

+32
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,38 @@ it will be included.
271271
If the resulting omit list includes `'dev'`, then the `NODE_ENV` environment
272272
variable will be set to `'production'` for all lifecycle scripts.
273273

274+
#### `workspace`
275+
276+
* Default:
277+
* Type: String (can be set multiple times)
278+
279+
Enable running a command in the context of the configured workspaces of the
280+
current project while filtering by running only the workspaces defined by
281+
this configuration option.
282+
283+
Valid values for the `workspace` config are either:
284+
285+
* Workspace names
286+
* Path to a workspace directory
287+
* Path to a parent workspace directory (will result to selecting all of the
288+
nested workspaces)
289+
290+
When set for the `npm init` command, this may be set to the folder of a
291+
workspace which does not yet exist, to create the folder and set it up as a
292+
brand new workspace within the project.
293+
294+
This value is not exported to the environment for child processes.
295+
296+
#### `workspaces`
297+
298+
* Default: false
299+
* Type: Boolean
300+
301+
Enable running a command in the context of **all** the configured
302+
workspaces.
303+
304+
This value is not exported to the environment for child processes.
305+
274306
<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
275307

276308
### See Also

docs/content/commands/npm-install.md

+32
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,38 @@ commands that modify your local installation, eg, `install`, `update`,
571571
Note: This is NOT honored by other network related commands, eg `dist-tags`,
572572
`owner`, etc.
573573
574+
#### `workspace`
575+
576+
* Default:
577+
* Type: String (can be set multiple times)
578+
579+
Enable running a command in the context of the configured workspaces of the
580+
current project while filtering by running only the workspaces defined by
581+
this configuration option.
582+
583+
Valid values for the `workspace` config are either:
584+
585+
* Workspace names
586+
* Path to a workspace directory
587+
* Path to a parent workspace directory (will result to selecting all of the
588+
nested workspaces)
589+
590+
When set for the `npm init` command, this may be set to the folder of a
591+
workspace which does not yet exist, to create the folder and set it up as a
592+
brand new workspace within the project.
593+
594+
This value is not exported to the environment for child processes.
595+
596+
#### `workspaces`
597+
598+
* Default: false
599+
* Type: Boolean
600+
601+
Enable running a command in the context of **all** the configured
602+
workspaces.
603+
604+
This value is not exported to the environment for child processes.
605+
574606
<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
575607
576608
### Algorithm

docs/content/commands/npm-prune.md

+32
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,38 @@ Whether or not to output JSON data, rather than the normal output.
7777

7878
Not supported by all npm commands.
7979

80+
#### `workspace`
81+
82+
* Default:
83+
* Type: String (can be set multiple times)
84+
85+
Enable running a command in the context of the configured workspaces of the
86+
current project while filtering by running only the workspaces defined by
87+
this configuration option.
88+
89+
Valid values for the `workspace` config are either:
90+
91+
* Workspace names
92+
* Path to a workspace directory
93+
* Path to a parent workspace directory (will result to selecting all of the
94+
nested workspaces)
95+
96+
When set for the `npm init` command, this may be set to the folder of a
97+
workspace which does not yet exist, to create the folder and set it up as a
98+
brand new workspace within the project.
99+
100+
This value is not exported to the environment for child processes.
101+
102+
#### `workspaces`
103+
104+
* Default: false
105+
* Type: Boolean
106+
107+
Enable running a command in the context of **all** the configured
108+
workspaces.
109+
110+
This value is not exported to the environment for child processes.
111+
80112
<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
81113

82114
### See Also

docs/content/commands/npm-uninstall.md

+32
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ Save installed packages to a package.json file as dependencies.
6868
When used with the `npm rm` command, removes the dependency from
6969
package.json.
7070

71+
#### `workspace`
72+
73+
* Default:
74+
* Type: String (can be set multiple times)
75+
76+
Enable running a command in the context of the configured workspaces of the
77+
current project while filtering by running only the workspaces defined by
78+
this configuration option.
79+
80+
Valid values for the `workspace` config are either:
81+
82+
* Workspace names
83+
* Path to a workspace directory
84+
* Path to a parent workspace directory (will result to selecting all of the
85+
nested workspaces)
86+
87+
When set for the `npm init` command, this may be set to the folder of a
88+
workspace which does not yet exist, to create the folder and set it up as a
89+
brand new workspace within the project.
90+
91+
This value is not exported to the environment for child processes.
92+
93+
#### `workspaces`
94+
95+
* Default: false
96+
* Type: Boolean
97+
98+
Enable running a command in the context of **all** the configured
99+
workspaces.
100+
101+
This value is not exported to the environment for child processes.
102+
71103
<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
72104

73105
### See Also

docs/content/commands/npm-update.md

+32
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,38 @@ commands that modify your local installation, eg, `install`, `update`,
258258
Note: This is NOT honored by other network related commands, eg `dist-tags`,
259259
`owner`, etc.
260260

261+
#### `workspace`
262+
263+
* Default:
264+
* Type: String (can be set multiple times)
265+
266+
Enable running a command in the context of the configured workspaces of the
267+
current project while filtering by running only the workspaces defined by
268+
this configuration option.
269+
270+
Valid values for the `workspace` config are either:
271+
272+
* Workspace names
273+
* Path to a workspace directory
274+
* Path to a parent workspace directory (will result to selecting all of the
275+
nested workspaces)
276+
277+
When set for the `npm init` command, this may be set to the folder of a
278+
workspace which does not yet exist, to create the folder and set it up as a
279+
brand new workspace within the project.
280+
281+
This value is not exported to the environment for child processes.
282+
283+
#### `workspaces`
284+
285+
* Default: false
286+
* Type: Boolean
287+
288+
Enable running a command in the context of **all** the configured
289+
workspaces.
290+
291+
This value is not exported to the environment for child processes.
292+
261293
<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
262294

263295
### See Also

lib/audit.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const Arborist = require('@npmcli/arborist')
22
const auditReport = require('npm-audit-report')
33
const reifyFinish = require('./utils/reify-finish.js')
44
const auditError = require('./utils/audit-error.js')
5-
const BaseCommand = require('./base-command.js')
5+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
66

7-
class Audit extends BaseCommand {
7+
class Audit extends ArboristWorkspaceCmd {
88
/* istanbul ignore next - see test/lib/load-all-commands.js */
99
static get description () {
1010
return 'Run a security audit'
@@ -24,6 +24,7 @@ class Audit extends BaseCommand {
2424
'json',
2525
'package-lock-only',
2626
'omit',
27+
...super.params,
2728
]
2829
}
2930

@@ -57,7 +58,9 @@ class Audit extends BaseCommand {
5758
audit: true,
5859
path: this.npm.prefix,
5960
reporter,
61+
workspaces: this.workspaces,
6062
}
63+
6164
const arb = new Arborist(opts)
6265
const fix = args[0] === 'fix'
6366
await arb.audit({ fix })

lib/base-command.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class BaseCommand {
66
constructor (npm) {
77
this.wrapWidth = 80
88
this.npm = npm
9+
this.workspaces = null
910
}
1011

1112
get name () {

lib/ci.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ const removeNodeModules = async where => {
1717
await Promise.all(entries.map(f => rimraf(`${path}/${f}`, rimrafOpts)))
1818
process.emit('timeEnd', 'npm-ci:rm')
1919
}
20-
const BaseCommand = require('./base-command.js')
20+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
2121

22-
class CI extends BaseCommand {
22+
class CI extends ArboristWorkspaceCmd {
2323
/* istanbul ignore next - see test/lib/load-all-commands.js */
2424
static get description () {
2525
return 'Install a project with a clean slate'
@@ -55,6 +55,7 @@ class CI extends BaseCommand {
5555
path: where,
5656
log: this.npm.log,
5757
save: false, // npm ci should never modify the lockfile or package.json
58+
workspaces: this.workspaces,
5859
}
5960

6061
const arb = new Arborist(opts)

lib/dedupe.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
const Arborist = require('@npmcli/arborist')
33
const reifyFinish = require('./utils/reify-finish.js')
44

5-
const BaseCommand = require('./base-command.js')
5+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
66

7-
class Dedupe extends BaseCommand {
7+
class Dedupe extends ArboristWorkspaceCmd {
88
/* istanbul ignore next - see test/lib/load-all-commands.js */
99
static get description () {
1010
return 'Reduce duplication in the package tree'
@@ -49,6 +49,7 @@ class Dedupe extends BaseCommand {
4949
log: this.npm.log,
5050
path: where,
5151
dryRun,
52+
workspaces: this.workspaces,
5253
}
5354
const arb = new Arborist(opts)
5455
await arb.dedupe(opts)

lib/install.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const { resolve, join } = require('path')
99
const Arborist = require('@npmcli/arborist')
1010
const runScript = require('@npmcli/run-script')
1111

12-
const BaseCommand = require('./base-command.js')
13-
class Install extends BaseCommand {
12+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
13+
class Install extends ArboristWorkspaceCmd {
1414
/* istanbul ignore next - see test/lib/load-all-commands.js */
1515
static get description () {
1616
return 'Install a package'
@@ -37,6 +37,7 @@ class Install extends BaseCommand {
3737
'bin-links',
3838
'fund',
3939
'dry-run',
40+
...super.params,
4041
]
4142
}
4243

@@ -143,6 +144,7 @@ class Install extends BaseCommand {
143144
auditLevel: null,
144145
path: where,
145146
add: args,
147+
workspaces: this.workspaces,
146148
}
147149
const arb = new Arborist(opts)
148150
await arb.reify(opts)

lib/prune.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
const Arborist = require('@npmcli/arborist')
33
const reifyFinish = require('./utils/reify-finish.js')
44

5-
const BaseCommand = require('./base-command.js')
6-
class Prune extends BaseCommand {
5+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
6+
class Prune extends ArboristWorkspaceCmd {
77
/* istanbul ignore next - see test/lib/load-all-commands.js */
88
static get description () {
99
return 'Remove extraneous packages'
@@ -16,7 +16,7 @@ class Prune extends BaseCommand {
1616

1717
/* istanbul ignore next - see test/lib/load-all-commands.js */
1818
static get params () {
19-
return ['omit', 'dry-run', 'json']
19+
return ['omit', 'dry-run', 'json', ...super.params]
2020
}
2121

2222
/* istanbul ignore next - see test/lib/load-all-commands.js */
@@ -34,6 +34,7 @@ class Prune extends BaseCommand {
3434
...this.npm.flatOptions,
3535
path: where,
3636
log: this.npm.log,
37+
workspaces: this.workspaces,
3738
}
3839
const arb = new Arborist(opts)
3940
await arb.prune(opts)

lib/rebuild.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ const npa = require('npm-package-arg')
44
const semver = require('semver')
55
const completion = require('./utils/completion/installed-deep.js')
66

7-
const BaseCommand = require('./base-command.js')
8-
class Rebuild extends BaseCommand {
7+
// TODO: make Arborist.rebuild() understand the workspaces option
8+
// and then extend ArboristWorkspaceCmd instead
9+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
10+
class Rebuild extends ArboristWorkspaceCmd {
911
/* istanbul ignore next - see test/lib/load-all-commands.js */
1012
static get description () {
1113
return 'Rebuild a package'
@@ -22,6 +24,7 @@ class Rebuild extends BaseCommand {
2224
'global',
2325
'bin-links',
2426
'ignore-scripts',
27+
...super.params,
2528
]
2629
}
2730

@@ -45,6 +48,8 @@ class Rebuild extends BaseCommand {
4548
const arb = new Arborist({
4649
...this.npm.flatOptions,
4750
path: where,
51+
// TODO when extending ReifyCmd
52+
// workspaces: this.workspaces,
4853
})
4954

5055
if (args.length) {

lib/uninstall.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ const rpj = require('read-package-json-fast')
55
const reifyFinish = require('./utils/reify-finish.js')
66
const completion = require('./utils/completion/installed-shallow.js')
77

8-
const BaseCommand = require('./base-command.js')
9-
class Uninstall extends BaseCommand {
8+
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
9+
class Uninstall extends ArboristWorkspaceCmd {
1010
static get description () {
1111
return 'Remove a package'
1212
}
@@ -18,7 +18,7 @@ class Uninstall extends BaseCommand {
1818

1919
/* istanbul ignore next - see test/lib/load-all-commands.js */
2020
static get params () {
21-
return ['save']
21+
return ['save', ...super.params]
2222
}
2323

2424
/* istanbul ignore next - see test/lib/load-all-commands.js */
@@ -66,7 +66,7 @@ class Uninstall extends BaseCommand {
6666
path,
6767
log: this.npm.log,
6868
rm: args,
69-
69+
workspaces: this.workspaces,
7070
}
7171
const arb = new Arborist(opts)
7272
await arb.reify(opts)

0 commit comments

Comments
 (0)