Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ignore implict workspace for some commands #4479

Merged
merged 4 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/base-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ class BaseCommand {
return this.constructor.description
}

get ignoreImplicitWorkspace () {
return !!this.constructor.ignoreImplicitWorkspace
}
fritzy marked this conversation as resolved.
Show resolved Hide resolved

get usage () {
let usage = `npm ${this.constructor.name}\n\n`
if (this.constructor.description) {
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/access.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class Access extends BaseCommand {
'otp',
]

static ignoreImplicitWorkspace = true

static usage = [
'public [<package>]',
'restricted [<package>]',
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/adduser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class AddUser extends BaseCommand {
'scope',
]

static ignoreImplicitWorkspace = true

async exec (args) {
const { scope } = this.npm.flatOptions
const registry = this.getRegistry(this.npm.flatOptions)
Expand Down
1 change: 1 addition & 0 deletions lib/commands/bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Bin extends BaseCommand {
static description = 'Display npm bin folder'
static name = 'bin'
static params = ['global']
static ignoreImplicitWorkspace = true

async exec (args) {
const b = this.npm.bin
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/birthday.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ const BaseCommand = require('../base-command.js')

class Birthday extends BaseCommand {
static name = 'birthday'
static ignoreImplicitWorkspace = true

async exec () {
this.npm.config.set('yes', true)
return this.npm.exec('exec', ['@npmcli/npm-birthday'])
Expand Down
1 change: 1 addition & 0 deletions lib/commands/bugs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Bugs extends BaseCommand {
static name = 'bugs'
static usage = ['[<pkgname>]']
static params = ['browser', 'registry']
static ignoreImplicitWorkspace = true

async exec (args) {
if (!args || !args.length) {
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Cache extends BaseCommand {
'verify',
]

static ignoreImplicitWorkspace = true

async completion (opts) {
const argv = opts.conf.argv.remain
if (argv.length === 2) {
Expand Down
1 change: 1 addition & 0 deletions lib/commands/help-search.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class HelpSearch extends BaseCommand {
static name = 'help-search'
static usage = ['<text>']
static params = ['long']
static ignoreImplicitWorkspace = true

async exec (args) {
if (!args.length) {
Expand Down
1 change: 1 addition & 0 deletions lib/commands/help.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Help extends BaseCommand {
static name = 'help'
static usage = ['<term> [<terms..>]']
static params = ['viewer']
static ignoreImplicitWorkspace = true

async completion (opts) {
if (opts.conf.argv.remain.length > 2) {
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Hook extends BaseCommand {
'update <id> <url> <secret>',
]

static ignoreImplicitWorkspace = true

async exec (args) {
return otplease({
...this.npm.flatOptions,
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/logout.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Logout extends BaseCommand {
'scope',
]

static ignoreImplicitWorkspace = true

async exec (args) {
const registry = this.npm.config.get('registry')
const scope = this.npm.config.get('scope')
Expand Down
1 change: 1 addition & 0 deletions lib/commands/org.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Org extends BaseCommand {
]

static params = ['registry', 'otp', 'json', 'parseable']
static ignoreImplicitWorkspace = true

async completion (opts) {
const argv = opts.conf.argv.remain
Expand Down
1 change: 1 addition & 0 deletions lib/commands/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Ping extends BaseCommand {
static description = 'Ping npm registry'
static params = ['registry']
static name = 'ping'
static ignoreImplicitWorkspace = true

async exec (args) {
log.notice('PING', this.npm.config.get('registry'))
Expand Down
1 change: 1 addition & 0 deletions lib/commands/prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Prefix extends BaseCommand {
static name = 'prefix'
static params = ['global']
static usage = ['[-g]']
static ignoreImplicitWorkspace = true

async exec (args) {
return this.npm.output(this.npm.prefix)
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class Profile extends BaseCommand {
'otp',
]

static ignoreImplicitWorkspace = true

async completion (opts) {
var argv = opts.conf.argv.remain

Expand Down
1 change: 1 addition & 0 deletions lib/commands/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Root extends BaseCommand {
static description = 'Display npm root'
static name = 'root'
static params = ['global']
static ignoreImplicitWorkspace = true

async exec () {
this.npm.output(this.npm.dir)
Expand Down
1 change: 1 addition & 0 deletions lib/commands/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Search extends BaseCommand {
]

static usage = ['[search terms ...]']
static ignoreImplicitWorkspace = true

async exec (args) {
const opts = {
Expand Down
24 changes: 1 addition & 23 deletions lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,6 @@ const _tmpFolder = Symbol('_tmpFolder')
const _title = Symbol('_title')
const pkg = require('../package.json')

const ignoreWorkspacesCommands = new Set([
'adduser',
'access',
'login',
'bin',
'birthday',
'bugs',
'cache',
'help-search',
'help',
'hook',
'logout',
'org',
// 'owner', // TODO, this may need some workspace support
'ping',
'prefix',
'profile',
// 'restart', // TODO add workspace support
'root',
'search',
])

class Npm extends EventEmitter {
static get version () {
return pkg.version
Expand Down Expand Up @@ -146,7 +124,7 @@ class Npm extends EventEmitter {
// or when it is implicit and not in our ignore list
const filterByWorkspaces =
(workspacesEnabled || workspacesFilters.length > 0)
&& (!implicitWorkspace || !ignoreWorkspacesCommands.has(cmd))
&& (!implicitWorkspace || !command.ignoreImplicitWorkspace)
// normally this would go in the constructor, but our tests don't
// actually use a real npm object so this.npm.config isn't always
// populated. this is the compromise until we can make that a reality
Expand Down
1 change: 0 additions & 1 deletion test/lib/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,6 @@ t.test('implicit workspace rejection', async t => {
})
const cwd = join(mock.npm.config.localPrefix, 'packages', 'a')
mock.npm.config.set('workspace', [cwd], 'default')
console.log(mock.npm.config.get('workspace', 'default'))
mockGlobals(t, {
'process.argv': [
process.execPath,
Expand Down