Skip to content

Commit 0f89e07

Browse files
authored
fix: add global getter to npm class (#4935)
1 parent 357b0af commit 0f89e07

21 files changed

+44
-32
lines changed

lib/commands/bin.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Bin extends BaseCommand {
1515
async exec (args) {
1616
const b = this.npm.bin
1717
this.npm.output(b)
18-
if (this.npm.config.get('global') && !path.split(delimiter).includes(b)) {
18+
if (this.npm.global && !path.split(delimiter).includes(b)) {
1919
log.error('bin', '(not in PATH env variable)')
2020
}
2121
}

lib/commands/ci.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CI extends ArboristWorkspaceCmd {
2121
]
2222

2323
async exec () {
24-
if (this.npm.config.get('global')) {
24+
if (this.npm.global) {
2525
throw Object.assign(new Error('`npm ci` does not work for global packages'), {
2626
code: 'ECIGLOBAL',
2727
})

lib/commands/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ ${defData}
276276
msg.push('')
277277
}
278278

279-
if (!this.npm.config.get('global')) {
279+
if (!this.npm.global) {
280280
const pkgPath = resolve(this.npm.prefix, 'package.json')
281281
const pkg = await rpj(pkgPath).catch(() => ({}))
282282

lib/commands/dedupe.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Dedupe extends ArboristWorkspaceCmd {
2222
]
2323

2424
async exec (args) {
25-
if (this.npm.config.get('global')) {
25+
if (this.npm.global) {
2626
const er = new Error('`npm dedupe` does not work in global mode.')
2727
er.code = 'EDEDUPEGLOBAL'
2828
throw er

lib/commands/diff.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Diff extends BaseCommand {
5050
// node_modules is sometimes under ./lib, and in global mode we're only ever
5151
// walking through node_modules (because we will have been given a package
5252
// name already)
53-
if (this.npm.config.get('global')) {
53+
if (this.npm.global) {
5454
this.top = resolve(this.npm.globalDir, '..')
5555
} else {
5656
this.top = this.prefix

lib/commands/dist-tag.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ class DistTag extends BaseCommand {
148148

149149
async list (spec, opts) {
150150
if (!spec) {
151-
if (this.npm.config.get('global')) {
151+
if (this.npm.global) {
152152
throw this.usageError()
153153
}
154154
const { name } = await readPackage(path.resolve(this.npm.prefix, 'package.json'))

lib/commands/fund.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class Fund extends ArboristWorkspaceCmd {
4545
throw err
4646
}
4747

48-
if (this.npm.config.get('global')) {
48+
if (this.npm.global) {
4949
const err = new Error('`npm fund` does not support global packages')
5050
err.code = 'EFUNDGLOBAL'
5151
throw err

lib/commands/install.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class Install extends ArboristWorkspaceCmd {
106106
// the /path/to/node_modules/..
107107
const globalTop = resolve(this.npm.globalDir, '..')
108108
const ignoreScripts = this.npm.config.get('ignore-scripts')
109-
const isGlobalInstall = this.npm.config.get('global')
109+
const isGlobalInstall = this.npm.global
110110
const where = isGlobalInstall ? globalTop : this.npm.prefix
111111
const forced = this.npm.config.get('force')
112112
const scriptShell = this.npm.config.get('script-shell') || undefined

lib/commands/link.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Link extends ArboristWorkspaceCmd {
4343
}
4444

4545
async exec (args) {
46-
if (this.npm.config.get('global')) {
46+
if (this.npm.global) {
4747
throw Object.assign(
4848
new Error(
4949
'link should never be --global.\n' +

lib/commands/ls.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class LS extends ArboristWorkspaceCmd {
5252
const all = this.npm.config.get('all')
5353
const color = this.npm.color
5454
const depth = this.npm.config.get('depth')
55-
const global = this.npm.config.get('global')
55+
const global = this.npm.global
5656
const json = this.npm.config.get('json')
5757
const link = this.npm.config.get('link')
5858
const long = this.npm.config.get('long')

lib/commands/outdated.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Outdated extends ArboristWorkspaceCmd {
2727

2828
async exec (args) {
2929
const global = path.resolve(this.npm.globalDir, '..')
30-
const where = this.npm.config.get('global')
30+
const where = this.npm.global
3131
? global
3232
: this.npm.prefix
3333

@@ -140,7 +140,7 @@ class Outdated extends ArboristWorkspaceCmd {
140140

141141
getEdgesOut (node) {
142142
// TODO: normalize usage of edges and avoid looping through nodes here
143-
if (this.npm.config.get('global')) {
143+
if (this.npm.global) {
144144
for (const child of node.children.values()) {
145145
this.trackEdge(child)
146146
}
@@ -166,7 +166,7 @@ class Outdated extends ArboristWorkspaceCmd {
166166
}
167167

168168
getWorkspacesEdges (node) {
169-
if (this.npm.config.get('global')) {
169+
if (this.npm.global) {
170170
return
171171
}
172172

lib/commands/owner.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Owner extends BaseCommand {
5050

5151
// reaches registry in order to autocomplete rm
5252
if (argv[2] === 'rm') {
53-
if (this.npm.config.get('global')) {
53+
if (this.npm.global) {
5454
return []
5555
}
5656
const { name } = await readJson(resolve(this.npm.prefix, 'package.json'))
@@ -126,7 +126,7 @@ class Owner extends BaseCommand {
126126

127127
async getPkg (prefix, pkg) {
128128
if (!pkg) {
129-
if (this.npm.config.get('global')) {
129+
if (this.npm.global) {
130130
throw this.usageError()
131131
}
132132
const { name } = await readJson(resolve(prefix, 'package.json'))

lib/commands/pkg.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Pkg extends BaseCommand {
2929
this.prefix = prefix
3030
}
3131

32-
if (this.npm.config.get('global')) {
32+
if (this.npm.global) {
3333
throw Object.assign(
3434
new Error(`There's no package.json file to manage on global mode`),
3535
{ code: 'EPKGGLOBAL' }

lib/commands/rebuild.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Rebuild extends ArboristWorkspaceCmd {
2626

2727
async exec (args) {
2828
const globalTop = resolve(this.npm.globalDir, '..')
29-
const where = this.npm.config.get('global') ? globalTop : this.npm.prefix
29+
const where = this.npm.global ? globalTop : this.npm.prefix
3030
const arb = new Arborist({
3131
...this.npm.flatOptions,
3232
path: where,

lib/commands/shrinkwrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Shrinkwrap extends BaseCommand {
1515
//
1616
// loadVirtual, fall back to loadActual
1717
// rename shrinkwrap file type, and tree.meta.save()
18-
if (this.npm.config.get('global')) {
18+
if (this.npm.global) {
1919
const er = new Error('`npm shrinkwrap` does not work for global packages')
2020
er.code = 'ESHRINKWRAPGLOBAL'
2121
throw er

lib/commands/uninstall.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,12 @@ class Uninstall extends ArboristWorkspaceCmd {
2121

2222
async exec (args) {
2323
// the /path/to/node_modules/..
24-
const global = this.npm.config.get('global')
25-
const path = global
24+
const path = this.npm.global
2625
? resolve(this.npm.globalDir, '..')
2726
: this.npm.localPrefix
2827

2928
if (!args.length) {
30-
if (!global) {
29+
if (!this.npm.global) {
3130
throw new Error('Must provide a package name to remove')
3231
} else {
3332
let pkg

lib/commands/update.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class Update extends ArboristWorkspaceCmd {
3939
async exec (args) {
4040
const update = args.length === 0 ? true : args
4141
const global = path.resolve(this.npm.globalDir, '..')
42-
const where = this.npm.config.get('global')
42+
const where = this.npm.global
4343
? global
4444
: this.npm.prefix
4545

lib/commands/view.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class View extends BaseCommand {
9191
const local = /^\.@/.test(pkg) || pkg === '.'
9292

9393
if (local) {
94-
if (this.npm.config.get('global')) {
94+
if (this.npm.global) {
9595
throw new Error('Cannot use view command in global mode.')
9696
}
9797
const dir = this.npm.prefix

lib/npm.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ class Npm extends EventEmitter {
129129
})
130130
}
131131

132-
const isGlobal = this.config.get('global')
133132
const workspacesEnabled = this.config.get('workspaces')
134133
// if cwd is a workspace, the default is set to [that workspace]
135134
const implicitWorkspace = this.config.get('workspace', 'default').length > 0
@@ -160,7 +159,7 @@ class Npm extends EventEmitter {
160159
execPromise = Promise.reject(
161160
new Error('Can not use --no-workspaces and --workspace at the same time'))
162161
} else if (filterByWorkspaces) {
163-
if (isGlobal) {
162+
if (this.global) {
164163
execPromise = Promise.reject(new Error('Workspaces not supported for global packages'))
165164
} else {
166165
execPromise = command.execWorkspaces(args, workspacesFilters)
@@ -333,6 +332,10 @@ class Npm extends EventEmitter {
333332
return this.#chalk
334333
}
335334

335+
get global () {
336+
return this.config.get('global') || this.config.get('location') === 'global'
337+
}
338+
336339
get logColor () {
337340
return this.flatOptions.logColor
338341
}
@@ -409,7 +412,7 @@ class Npm extends EventEmitter {
409412
}
410413

411414
get dir () {
412-
return this.config.get('global') ? this.globalDir : this.localDir
415+
return this.global ? this.globalDir : this.localDir
413416
}
414417

415418
get globalBin () {
@@ -422,15 +425,15 @@ class Npm extends EventEmitter {
422425
}
423426

424427
get bin () {
425-
return this.config.get('global') ? this.globalBin : this.localBin
428+
return this.global ? this.globalBin : this.localBin
426429
}
427430

428431
get prefix () {
429-
return this.config.get('global') ? this.globalPrefix : this.localPrefix
432+
return this.global ? this.globalPrefix : this.localPrefix
430433
}
431434

432435
set prefix (r) {
433-
const k = this.config.get('global') ? 'globalPrefix' : 'localPrefix'
436+
const k = this.global ? 'globalPrefix' : 'localPrefix'
434437
this[k] = r
435438
}
436439

test/fixtures/mock-npm.js

+4
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ class MockNpm {
224224
}
225225
}
226226

227+
get global () {
228+
return this.config.get('global') || this.config.get('location') === 'global'
229+
}
230+
227231
output (...msg) {
228232
if (this.base.output) {
229233
return this.base.output(msg)

test/lib/commands/outdated.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ t.test('should return if no outdated deps', async t => {
343343
})
344344

345345
await outdated(testDir, {
346-
global: false,
346+
config: {
347+
global: false,
348+
},
347349
}).exec([])
348350
t.equal(logs.length, 0, 'no logs')
349351
})
@@ -369,7 +371,9 @@ t.test('throws if error with a dep', async t => {
369371

370372
await t.rejects(
371373
outdated(testDir, {
372-
global: false,
374+
config: {
375+
global: false,
376+
},
373377
}).exec([]),
374378
'There is an error with this package.'
375379
)
@@ -388,7 +392,9 @@ t.test('should skip missing non-prod deps', async t => {
388392
})
389393

390394
await outdated(testDir, {
391-
global: false,
395+
config: {
396+
global: false,
397+
},
392398
}).exec([])
393399
t.equal(logs.length, 0, 'no logs')
394400
})

0 commit comments

Comments
 (0)