Skip to content

Commit

Permalink
wip: Add workspaces functionality to reify commands
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed May 11, 2021
1 parent 5514837 commit 2504abe
Show file tree
Hide file tree
Showing 19 changed files with 80 additions and 18 deletions.
6 changes: 4 additions & 2 deletions lib/audit.js
Original file line number Diff line number Diff line change
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 ReifyCmd = require('./utils/reify-cmd.js')

class Audit extends BaseCommand {
class Audit extends ReifyCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Run a security audit'
Expand Down Expand Up @@ -57,7 +57,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 ReifyCmd = require('./utils/reify-cmd.js')

class CI extends BaseCommand {
class CI extends ReifyCmd {
/* 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 @@ -47,6 +47,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
Original file line number Diff line number Diff line change
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 ReifyCmd = require('./utils/reify-cmd.js')

class Dedupe extends BaseCommand {
class Dedupe extends ReifyCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Reduce duplication in the package tree'
Expand Down Expand Up @@ -33,6 +33,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
5 changes: 3 additions & 2 deletions lib/install.js
Original file line number Diff line number Diff line change
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 ReifyCmd = require('./utils/reify-cmd.js')
class Install extends ReifyCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Install a package'
Expand Down Expand Up @@ -132,6 +132,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
5 changes: 3 additions & 2 deletions lib/prune.js
Original file line number Diff line number Diff line change
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 ReifyCmd = require('./utils/reify-cmd.js')
class Prune extends ReifyCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Remove extraneous packages'
Expand Down Expand Up @@ -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
4 changes: 4 additions & 0 deletions lib/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const npa = require('npm-package-arg')
const semver = require('semver')
const completion = require('./utils/completion/installed-deep.js')

// TODO: make Arborist.rebuild() understand the workspaces option
// and then extend ReifyCmd instead
const BaseCommand = require('./base-command.js')
class Rebuild extends BaseCommand {
/* istanbul ignore next - see test/lib/load-all-commands.js */
Expand Down Expand Up @@ -36,6 +38,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
6 changes: 3 additions & 3 deletions lib/uninstall.js
Original file line number Diff line number Diff line change
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 ReifyCmd = require('./utils/reify-cmd.js')
class Uninstall extends ReifyCmd {
static get description () {
return 'Remove a package'
}
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
5 changes: 3 additions & 2 deletions lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const log = require('npmlog')
const reifyFinish = require('./utils/reify-finish.js')
const completion = require('./utils/completion/installed-deep.js')

const BaseCommand = require('./base-command.js')
class Update extends BaseCommand {
const ReifyCmd = require('./utils/reify-cmd.js')
class Update extends ReifyCmd {
/* istanbul ignore next - see test/lib/load-all-commands.js */
static get description () {
return 'Update packages'
Expand Down Expand Up @@ -53,6 +53,7 @@ class Update extends BaseCommand {
...this.npm.flatOptions,
log: this.npm.log,
path: where,
workspaces: this.workspaces,
})

await arb.reify({ update })
Expand Down
17 changes: 17 additions & 0 deletions lib/utils/reify-cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// This is the base for all commands whose execWorkspaces just gets
// a list of workspace names and passes it on to new Arborist() to
// be able to run a filtered Arborist.reify() at some point.

const BaseCommand = require('../base-command.js')
const getWorkspaces = require('./workspaces/get-workspaces.js')
class ReifyCmd extends BaseCommand {
execWorkspaces (args, filters, cb) {
getWorkspaces(filters, { path: this.npm.localPrefix })
.then(workspaces => {
this.workspaces = workspaces.map(([name]) => name)
this.exec(args, cb)
})
}
}

module.exports = ReifyCmd
2 changes: 2 additions & 0 deletions test/lib/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,3 +191,5 @@ t.test('completion', t => {

t.end()
})

t.test('workspaces')
2 changes: 2 additions & 0 deletions test/lib/ci.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,5 @@ t.test('should remove existing node_modules before installing', (t) => {
throw er
})
})

t.test('workspaces')
2 changes: 2 additions & 0 deletions test/lib/dedupe.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ t.test('should remove dupes using Arborist - no arguments', (t) => {
t.end()
})
})

t.test('workspaces')
2 changes: 2 additions & 0 deletions test/lib/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,3 +218,5 @@ t.test('completion', async t => {
t.notOk(res)
t.end()
})

t.test('workspaces')
2 changes: 2 additions & 0 deletions test/lib/prune.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ t.test('should prune using Arborist', (t) => {
t.end()
})
})

t.test('workspaces')
2 changes: 2 additions & 0 deletions test/lib/rebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,5 @@ t.test('global prefix', t => {
t.end()
})
})

t.test('workspaces')
2 changes: 2 additions & 0 deletions test/lib/uninstall.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,5 @@ t.test('unknown error reading from localPrefix package.json', t => {
t.end()
})
})

t.test('workspaces')
18 changes: 15 additions & 3 deletions test/lib/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ t.test('no args', t => {
constructor (args) {
t.same(
args,
{ ...npm.flatOptions, path: npm.prefix, log: noop },
{
...npm.flatOptions,
path: npm.prefix,
log: noop,
workspaces: null,
},
'should call arborist contructor with expected args'
)
}
Expand Down Expand Up @@ -71,7 +76,12 @@ t.test('with args', t => {
constructor (args) {
t.same(
args,
{ ...npm.flatOptions, path: npm.prefix, log: noop },
{
...npm.flatOptions,
path: npm.prefix,
log: noop,
workspaces: null,
},
'should call arborist contructor with expected args'
)
}
Expand Down Expand Up @@ -139,7 +149,7 @@ t.test('update --global', t => {
const { path, ...opts } = args
t.same(
opts,
{ ...npm.flatOptions, log: noop },
{ ...npm.flatOptions, log: noop, workspaces: undefined },
'should call arborist contructor with expected options'
)

Expand All @@ -164,3 +174,5 @@ t.test('update --global', t => {
throw err
})
})

t.test('workspaces')
7 changes: 7 additions & 0 deletions test/lib/utils/reify-cmd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const t = require('tap')
const ReifyCmd = require('../../../lib/utils/reify-cmd.js')
// Just a dummy thing here so eslint will stop yelling, but it'll still
// log a todo.
t.todo('ReifyCmd basic tests', { todo: true }, async t => {
console.log(ReifyCmd)
})

0 comments on commit 2504abe

Please sign in to comment.