Skip to content

Commit

Permalink
fix(workspaces): explicitly error in global mode
Browse files Browse the repository at this point in the history
  • Loading branch information
wraithgar committed Jun 15, 2021
1 parent de445ad commit 1ea0c19
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class BaseCommand {
}

async setWorkspaces (filters) {
// TODO npm guards workspaces/global mode so we should use this.npm.prefix?
const ws = await getWorkspaces(filters, { path: this.npm.localPrefix })
this.workspaces = ws
this.workspaceNames = [...ws.keys()]
Expand Down
1 change: 0 additions & 1 deletion lib/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class Diff extends BaseCommand {
async packageName (path) {
let name
try {
// TODO this won't work as expected in global mode
name = await readPackageName(this.prefix)
} catch (e) {
npmlog.verbose('diff', 'could not read project dir package.json')
Expand Down
3 changes: 3 additions & 0 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ const npm = module.exports = new class extends EventEmitter {
this.output(impl.usage)
cb()
} else if (filterByWorkspaces) {
if (this.config.get('global'))
return cb(new Error('Workspaces not supported for global packages'))

impl.execWorkspaces(args, this.config.get('workspace'), er => {
process.emit('timeEnd', `command:${cmd}`)
cb(er)
Expand Down
56 changes: 55 additions & 1 deletion test/lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ t.test('npm.load', t => {
await new Promise((res) => setTimeout(res))
})

t.test('workpaces-aware configs and commands', async t => {
t.test('workspace-aware configs and commands', async t => {
const dir = t.testdir({
packages: {
a: {
Expand Down Expand Up @@ -438,6 +438,60 @@ t.test('npm.load', t => {
})
})

t.test('workspaces in global mode', async t => {
const dir = t.testdir({
packages: {
a: {
'package.json': JSON.stringify({
name: 'a',
version: '1.0.0',
scripts: { test: 'echo test a' },
}),
},
b: {
'package.json': JSON.stringify({
name: 'b',
version: '1.0.0',
scripts: { test: 'echo test b' },
}),
},
},
'package.json': JSON.stringify({
name: 'root',
version: '1.0.0',
workspaces: ['./packages/*'],
}),
})
const { execPath } = process
freshConfig({
argv: [
execPath,
process.argv[1],
'--userconfig',
resolve(dir, '.npmrc'),
'--color',
'false',
'--workspaces',
'--global',
'true',
],
})
await npm.load(er => {
if (er)
throw er
})
npm.localPrefix = dir
await new Promise((res, rej) => {
// verify that calling the command with a short name still sets
// the npm.command property to the full canonical name of the cmd.
npm.command = null
npm.commands.run([], er => {
t.match(er, /Workspaces not supported for global packages/)
res()
})
})
})

t.end()
})

Expand Down

0 comments on commit 1ea0c19

Please sign in to comment.