Skip to content

Commit

Permalink
Add workspaces support to reify/rebuild commands
Browse files Browse the repository at this point in the history
This adds `--workspace` support to:

- audit (including audit fix)
- ci
- dedupe
- find-dupes
- install
- install-ci-test
- install-test
- prune
- rebuild
- uninstall
- update
  • Loading branch information
isaacs committed May 19, 2021
1 parent ec256a1 commit 2fc7c5d
Show file tree
Hide file tree
Showing 17 changed files with 221 additions and 23 deletions.
32 changes: 32 additions & 0 deletions docs/content/commands/npm-audit.md
Expand Up @@ -271,6 +271,38 @@ it will be included.
If the resulting omit list includes `'dev'`, then the `NODE_ENV` environment
variable will be set to `'production'` for all lifecycle scripts.

#### `workspace`

* Default:
* Type: String (can be set multiple times)

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.

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
nested workspaces)

When set for the `npm init` command, this may be set to the folder of a
workspace which does not yet exist, to create the folder and set it up as a
brand new workspace within the project.

This value is not exported to the environment for child processes.

#### `workspaces`

* Default: false
* Type: Boolean

Enable running a command in the context of **all** the configured
workspaces.

This value is not exported to the environment for child processes.

<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->

### See Also
Expand Down
32 changes: 32 additions & 0 deletions docs/content/commands/npm-install.md
Expand Up @@ -571,6 +571,38 @@ commands that modify your local installation, eg, `install`, `update`,
Note: This is NOT honored by other network related commands, eg `dist-tags`,
`owner`, etc.
#### `workspace`
* Default:
* Type: String (can be set multiple times)
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.
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
nested workspaces)
When set for the `npm init` command, this may be set to the folder of a
workspace which does not yet exist, to create the folder and set it up as a
brand new workspace within the project.
This value is not exported to the environment for child processes.
#### `workspaces`
* Default: false
* Type: Boolean
Enable running a command in the context of **all** the configured
workspaces.
This value is not exported to the environment for child processes.
<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->
### Algorithm
Expand Down
32 changes: 32 additions & 0 deletions docs/content/commands/npm-prune.md
Expand Up @@ -77,6 +77,38 @@ Whether or not to output JSON data, rather than the normal output.

Not supported by all npm commands.

#### `workspace`

* Default:
* Type: String (can be set multiple times)

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.

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
nested workspaces)

When set for the `npm init` command, this may be set to the folder of a
workspace which does not yet exist, to create the folder and set it up as a
brand new workspace within the project.

This value is not exported to the environment for child processes.

#### `workspaces`

* Default: false
* Type: Boolean

Enable running a command in the context of **all** the configured
workspaces.

This value is not exported to the environment for child processes.

<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->

### See Also
Expand Down
32 changes: 32 additions & 0 deletions docs/content/commands/npm-uninstall.md
Expand Up @@ -68,6 +68,38 @@ Save installed packages to a package.json file as dependencies.
When used with the `npm rm` command, removes the dependency from
package.json.

#### `workspace`

* Default:
* Type: String (can be set multiple times)

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.

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
nested workspaces)

When set for the `npm init` command, this may be set to the folder of a
workspace which does not yet exist, to create the folder and set it up as a
brand new workspace within the project.

This value is not exported to the environment for child processes.

#### `workspaces`

* Default: false
* Type: Boolean

Enable running a command in the context of **all** the configured
workspaces.

This value is not exported to the environment for child processes.

<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->

### See Also
Expand Down
32 changes: 32 additions & 0 deletions docs/content/commands/npm-update.md
Expand Up @@ -258,6 +258,38 @@ commands that modify your local installation, eg, `install`, `update`,
Note: This is NOT honored by other network related commands, eg `dist-tags`,
`owner`, etc.

#### `workspace`

* Default:
* Type: String (can be set multiple times)

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.

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
nested workspaces)

When set for the `npm init` command, this may be set to the folder of a
workspace which does not yet exist, to create the folder and set it up as a
brand new workspace within the project.

This value is not exported to the environment for child processes.

#### `workspaces`

* Default: false
* Type: Boolean

Enable running a command in the context of **all** the configured
workspaces.

This value is not exported to the environment for child processes.

<!-- AUTOGENERATED CONFIG DESCRIPTIONS END -->

### See Also
Expand Down
7 changes: 5 additions & 2 deletions lib/audit.js
Expand Up @@ -2,9 +2,9 @@ const Arborist = require('@npmcli/arborist')
const auditReport = require('npm-audit-report')
const reifyFinish = require('./utils/reify-finish.js')
const auditError = require('./utils/audit-error.js')
const BaseCommand = require('./base-command.js')
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')

class Audit extends BaseCommand {
class Audit extends ArboristWorkspaceCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Run a security audit'
Expand All @@ -24,6 +24,7 @@ class Audit extends BaseCommand {
'json',
'package-lock-only',
'omit',
...super.params,
]
}

Expand Down Expand Up @@ -57,7 +58,9 @@ class Audit extends BaseCommand {
audit: true,
path: this.npm.prefix,
reporter,
workspaces: this.workspaces,
}

const arb = new Arborist(opts)
const fix = args[0] === 'fix'
await arb.audit({ fix })
Expand Down
1 change: 1 addition & 0 deletions lib/base-command.js
Expand Up @@ -6,6 +6,7 @@ class BaseCommand {
constructor (npm) {
this.wrapWidth = 80
this.npm = npm
this.workspaces = null
}

get name () {
Expand Down
5 changes: 3 additions & 2 deletions lib/ci.js
Expand Up @@ -17,9 +17,9 @@ const removeNodeModules = async where => {
await Promise.all(entries.map(f => rimraf(`${path}/${f}`, rimrafOpts)))
process.emit('timeEnd', 'npm-ci:rm')
}
const BaseCommand = require('./base-command.js')
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')

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

const arb = new Arborist(opts)
Expand Down
5 changes: 3 additions & 2 deletions lib/dedupe.js
Expand Up @@ -2,9 +2,9 @@
const Arborist = require('@npmcli/arborist')
const reifyFinish = require('./utils/reify-finish.js')

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

class Dedupe extends BaseCommand {
class Dedupe extends ArboristWorkspaceCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Reduce duplication in the package tree'
Expand Down Expand Up @@ -49,6 +49,7 @@ class Dedupe extends BaseCommand {
log: this.npm.log,
path: where,
dryRun,
workspaces: this.workspaces,
}
const arb = new Arborist(opts)
await arb.dedupe(opts)
Expand Down
6 changes: 4 additions & 2 deletions lib/install.js
Expand Up @@ -9,8 +9,8 @@ const { resolve, join } = require('path')
const Arborist = require('@npmcli/arborist')
const runScript = require('@npmcli/run-script')

const BaseCommand = require('./base-command.js')
class Install extends BaseCommand {
const ArboristWorkspaceCmd = require('./workspaces/arborist-cmd.js')
class Install extends ArboristWorkspaceCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Install a package'
Expand All @@ -37,6 +37,7 @@ class Install extends BaseCommand {
'bin-links',
'fund',
'dry-run',
...super.params,
]
}

Expand Down Expand Up @@ -143,6 +144,7 @@ class Install extends BaseCommand {
auditLevel: null,
path: where,
add: args,
workspaces: this.workspaces,
}
const arb = new Arborist(opts)
await arb.reify(opts)
Expand Down
7 changes: 4 additions & 3 deletions lib/prune.js
Expand Up @@ -2,8 +2,8 @@
const Arborist = require('@npmcli/arborist')
const reifyFinish = require('./utils/reify-finish.js')

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

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

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand All @@ -34,6 +34,7 @@ class Prune extends BaseCommand {
...this.npm.flatOptions,
path: where,
log: this.npm.log,
workspaces: this.workspaces,
}
const arb = new Arborist(opts)
await arb.prune(opts)
Expand Down
9 changes: 7 additions & 2 deletions lib/rebuild.js
Expand Up @@ -4,8 +4,10 @@ const npa = require('npm-package-arg')
const semver = require('semver')
const completion = require('./utils/completion/installed-deep.js')

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

Expand All @@ -45,6 +48,8 @@ class Rebuild extends BaseCommand {
const arb = new Arborist({
...this.npm.flatOptions,
path: where,
// TODO when extending ReifyCmd
// workspaces: this.workspaces,
})

if (args.length) {
Expand Down
8 changes: 4 additions & 4 deletions lib/uninstall.js
Expand Up @@ -5,8 +5,8 @@ const rpj = require('read-package-json-fast')
const reifyFinish = require('./utils/reify-finish.js')
const completion = require('./utils/completion/installed-shallow.js')

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

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

/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -66,7 +66,7 @@ class Uninstall extends BaseCommand {
path,
log: this.npm.log,
rm: args,

workspaces: this.workspaces,
}
const arb = new Arborist(opts)
await arb.reify(opts)
Expand Down

0 comments on commit 2fc7c5d

Please sign in to comment.