diff options
author | nlf <quitlahok@gmail.com> | 2022-05-25 19:56:00 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-25 19:56:00 +0300 |
commit | 0f89e0750f2ac9b5b4794b5718d047b5286283c8 (patch) | |
tree | 3ca4a25f16c841498e350dfe437e5be7ee83f512 /lib | |
parent | 357b0af2af2b07a58d2d837043d1d77c9495d8b5 (diff) |
fix: add global getter to npm class (#4935)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/commands/bin.js | 2 | ||||
-rw-r--r-- | lib/commands/ci.js | 2 | ||||
-rw-r--r-- | lib/commands/config.js | 2 | ||||
-rw-r--r-- | lib/commands/dedupe.js | 2 | ||||
-rw-r--r-- | lib/commands/diff.js | 2 | ||||
-rw-r--r-- | lib/commands/dist-tag.js | 2 | ||||
-rw-r--r-- | lib/commands/fund.js | 2 | ||||
-rw-r--r-- | lib/commands/install.js | 2 | ||||
-rw-r--r-- | lib/commands/link.js | 2 | ||||
-rw-r--r-- | lib/commands/ls.js | 2 | ||||
-rw-r--r-- | lib/commands/outdated.js | 6 | ||||
-rw-r--r-- | lib/commands/owner.js | 4 | ||||
-rw-r--r-- | lib/commands/pkg.js | 2 | ||||
-rw-r--r-- | lib/commands/rebuild.js | 2 | ||||
-rw-r--r-- | lib/commands/shrinkwrap.js | 2 | ||||
-rw-r--r-- | lib/commands/uninstall.js | 5 | ||||
-rw-r--r-- | lib/commands/update.js | 2 | ||||
-rw-r--r-- | lib/commands/view.js | 2 | ||||
-rw-r--r-- | lib/npm.js | 15 |
19 files changed, 31 insertions, 29 deletions
diff --git a/lib/commands/bin.js b/lib/commands/bin.js index 4200d5b8c..9ba3cb400 100644 --- a/lib/commands/bin.js +++ b/lib/commands/bin.js @@ -15,7 +15,7 @@ class Bin extends BaseCommand { async exec (args) { const b = this.npm.bin this.npm.output(b) - if (this.npm.config.get('global') && !path.split(delimiter).includes(b)) { + if (this.npm.global && !path.split(delimiter).includes(b)) { log.error('bin', '(not in PATH env variable)') } } diff --git a/lib/commands/ci.js b/lib/commands/ci.js index 9d8946987..2a6125d56 100644 --- a/lib/commands/ci.js +++ b/lib/commands/ci.js @@ -21,7 +21,7 @@ class CI extends ArboristWorkspaceCmd { ] async exec () { - if (this.npm.config.get('global')) { + if (this.npm.global) { throw Object.assign(new Error('`npm ci` does not work for global packages'), { code: 'ECIGLOBAL', }) diff --git a/lib/commands/config.js b/lib/commands/config.js index 0432abac3..96dd4abca 100644 --- a/lib/commands/config.js +++ b/lib/commands/config.js @@ -276,7 +276,7 @@ ${defData} msg.push('') } - if (!this.npm.config.get('global')) { + if (!this.npm.global) { const pkgPath = resolve(this.npm.prefix, 'package.json') const pkg = await rpj(pkgPath).catch(() => ({})) diff --git a/lib/commands/dedupe.js b/lib/commands/dedupe.js index 96d1ac2ae..2cc44b2a9 100644 --- a/lib/commands/dedupe.js +++ b/lib/commands/dedupe.js @@ -22,7 +22,7 @@ class Dedupe extends ArboristWorkspaceCmd { ] async exec (args) { - if (this.npm.config.get('global')) { + if (this.npm.global) { const er = new Error('`npm dedupe` does not work in global mode.') er.code = 'EDEDUPEGLOBAL' throw er diff --git a/lib/commands/diff.js b/lib/commands/diff.js index 11ee78265..b8a64bd98 100644 --- a/lib/commands/diff.js +++ b/lib/commands/diff.js @@ -50,7 +50,7 @@ class Diff extends BaseCommand { // node_modules is sometimes under ./lib, and in global mode we're only ever // walking through node_modules (because we will have been given a package // name already) - if (this.npm.config.get('global')) { + if (this.npm.global) { this.top = resolve(this.npm.globalDir, '..') } else { this.top = this.prefix diff --git a/lib/commands/dist-tag.js b/lib/commands/dist-tag.js index 42cad80df..a207e422c 100644 --- a/lib/commands/dist-tag.js +++ b/lib/commands/dist-tag.js @@ -148,7 +148,7 @@ class DistTag extends BaseCommand { async list (spec, opts) { if (!spec) { - if (this.npm.config.get('global')) { + if (this.npm.global) { throw this.usageError() } const { name } = await readPackage(path.resolve(this.npm.prefix, 'package.json')) diff --git a/lib/commands/fund.js b/lib/commands/fund.js index 787a5193f..09ca81653 100644 --- a/lib/commands/fund.js +++ b/lib/commands/fund.js @@ -45,7 +45,7 @@ class Fund extends ArboristWorkspaceCmd { throw err } - if (this.npm.config.get('global')) { + if (this.npm.global) { const err = new Error('`npm fund` does not support global packages') err.code = 'EFUNDGLOBAL' throw err diff --git a/lib/commands/install.js b/lib/commands/install.js index d1f6d1481..4cda36448 100644 --- a/lib/commands/install.js +++ b/lib/commands/install.js @@ -106,7 +106,7 @@ class Install extends ArboristWorkspaceCmd { // the /path/to/node_modules/.. const globalTop = resolve(this.npm.globalDir, '..') const ignoreScripts = this.npm.config.get('ignore-scripts') - const isGlobalInstall = this.npm.config.get('global') + const isGlobalInstall = this.npm.global const where = isGlobalInstall ? globalTop : this.npm.prefix const forced = this.npm.config.get('force') const scriptShell = this.npm.config.get('script-shell') || undefined diff --git a/lib/commands/link.js b/lib/commands/link.js index d65679146..80a60d36e 100644 --- a/lib/commands/link.js +++ b/lib/commands/link.js @@ -43,7 +43,7 @@ class Link extends ArboristWorkspaceCmd { } async exec (args) { - if (this.npm.config.get('global')) { + if (this.npm.global) { throw Object.assign( new Error( 'link should never be --global.\n' + diff --git a/lib/commands/ls.js b/lib/commands/ls.js index 06268fe7e..cfd9cb5a5 100644 --- a/lib/commands/ls.js +++ b/lib/commands/ls.js @@ -52,7 +52,7 @@ class LS extends ArboristWorkspaceCmd { const all = this.npm.config.get('all') const color = this.npm.color const depth = this.npm.config.get('depth') - const global = this.npm.config.get('global') + const global = this.npm.global const json = this.npm.config.get('json') const link = this.npm.config.get('link') const long = this.npm.config.get('long') diff --git a/lib/commands/outdated.js b/lib/commands/outdated.js index 0953c17ca..081e75a2c 100644 --- a/lib/commands/outdated.js +++ b/lib/commands/outdated.js @@ -27,7 +27,7 @@ class Outdated extends ArboristWorkspaceCmd { async exec (args) { const global = path.resolve(this.npm.globalDir, '..') - const where = this.npm.config.get('global') + const where = this.npm.global ? global : this.npm.prefix @@ -140,7 +140,7 @@ class Outdated extends ArboristWorkspaceCmd { getEdgesOut (node) { // TODO: normalize usage of edges and avoid looping through nodes here - if (this.npm.config.get('global')) { + if (this.npm.global) { for (const child of node.children.values()) { this.trackEdge(child) } @@ -166,7 +166,7 @@ class Outdated extends ArboristWorkspaceCmd { } getWorkspacesEdges (node) { - if (this.npm.config.get('global')) { + if (this.npm.global) { return } diff --git a/lib/commands/owner.js b/lib/commands/owner.js index 9338b22a5..4797e9c7e 100644 --- a/lib/commands/owner.js +++ b/lib/commands/owner.js @@ -50,7 +50,7 @@ class Owner extends BaseCommand { // reaches registry in order to autocomplete rm if (argv[2] === 'rm') { - if (this.npm.config.get('global')) { + if (this.npm.global) { return [] } const { name } = await readJson(resolve(this.npm.prefix, 'package.json')) @@ -126,7 +126,7 @@ class Owner extends BaseCommand { async getPkg (prefix, pkg) { if (!pkg) { - if (this.npm.config.get('global')) { + if (this.npm.global) { throw this.usageError() } const { name } = await readJson(resolve(prefix, 'package.json')) diff --git a/lib/commands/pkg.js b/lib/commands/pkg.js index 3a8e01f65..5fac9bfb5 100644 --- a/lib/commands/pkg.js +++ b/lib/commands/pkg.js @@ -29,7 +29,7 @@ class Pkg extends BaseCommand { this.prefix = prefix } - if (this.npm.config.get('global')) { + if (this.npm.global) { throw Object.assign( new Error(`There's no package.json file to manage on global mode`), { code: 'EPKGGLOBAL' } diff --git a/lib/commands/rebuild.js b/lib/commands/rebuild.js index 0e8a1510b..3e6046d8d 100644 --- a/lib/commands/rebuild.js +++ b/lib/commands/rebuild.js @@ -26,7 +26,7 @@ class Rebuild extends ArboristWorkspaceCmd { async exec (args) { const globalTop = resolve(this.npm.globalDir, '..') - const where = this.npm.config.get('global') ? globalTop : this.npm.prefix + const where = this.npm.global ? globalTop : this.npm.prefix const arb = new Arborist({ ...this.npm.flatOptions, path: where, diff --git a/lib/commands/shrinkwrap.js b/lib/commands/shrinkwrap.js index 67cde1581..a240f0393 100644 --- a/lib/commands/shrinkwrap.js +++ b/lib/commands/shrinkwrap.js @@ -15,7 +15,7 @@ class Shrinkwrap extends BaseCommand { // // loadVirtual, fall back to loadActual // rename shrinkwrap file type, and tree.meta.save() - if (this.npm.config.get('global')) { + if (this.npm.global) { const er = new Error('`npm shrinkwrap` does not work for global packages') er.code = 'ESHRINKWRAPGLOBAL' throw er diff --git a/lib/commands/uninstall.js b/lib/commands/uninstall.js index 5911818b6..e4a193cc5 100644 --- a/lib/commands/uninstall.js +++ b/lib/commands/uninstall.js @@ -21,13 +21,12 @@ class Uninstall extends ArboristWorkspaceCmd { async exec (args) { // the /path/to/node_modules/.. - const global = this.npm.config.get('global') - const path = global + const path = this.npm.global ? resolve(this.npm.globalDir, '..') : this.npm.localPrefix if (!args.length) { - if (!global) { + if (!this.npm.global) { throw new Error('Must provide a package name to remove') } else { let pkg diff --git a/lib/commands/update.js b/lib/commands/update.js index c2c3502ee..ca80d6153 100644 --- a/lib/commands/update.js +++ b/lib/commands/update.js @@ -39,7 +39,7 @@ class Update extends ArboristWorkspaceCmd { async exec (args) { const update = args.length === 0 ? true : args const global = path.resolve(this.npm.globalDir, '..') - const where = this.npm.config.get('global') + const where = this.npm.global ? global : this.npm.prefix diff --git a/lib/commands/view.js b/lib/commands/view.js index d78ee77dc..efb298a03 100644 --- a/lib/commands/view.js +++ b/lib/commands/view.js @@ -91,7 +91,7 @@ class View extends BaseCommand { const local = /^\.@/.test(pkg) || pkg === '.' if (local) { - if (this.npm.config.get('global')) { + if (this.npm.global) { throw new Error('Cannot use view command in global mode.') } const dir = this.npm.prefix diff --git a/lib/npm.js b/lib/npm.js index 732362565..2197f11a5 100644 --- a/lib/npm.js +++ b/lib/npm.js @@ -129,7 +129,6 @@ class Npm extends EventEmitter { }) } - const isGlobal = this.config.get('global') const workspacesEnabled = this.config.get('workspaces') // if cwd is a workspace, the default is set to [that workspace] const implicitWorkspace = this.config.get('workspace', 'default').length > 0 @@ -160,7 +159,7 @@ class Npm extends EventEmitter { execPromise = Promise.reject( new Error('Can not use --no-workspaces and --workspace at the same time')) } else if (filterByWorkspaces) { - if (isGlobal) { + if (this.global) { execPromise = Promise.reject(new Error('Workspaces not supported for global packages')) } else { execPromise = command.execWorkspaces(args, workspacesFilters) @@ -333,6 +332,10 @@ class Npm extends EventEmitter { return this.#chalk } + get global () { + return this.config.get('global') || this.config.get('location') === 'global' + } + get logColor () { return this.flatOptions.logColor } @@ -409,7 +412,7 @@ class Npm extends EventEmitter { } get dir () { - return this.config.get('global') ? this.globalDir : this.localDir + return this.global ? this.globalDir : this.localDir } get globalBin () { @@ -422,15 +425,15 @@ class Npm extends EventEmitter { } get bin () { - return this.config.get('global') ? this.globalBin : this.localBin + return this.global ? this.globalBin : this.localBin } get prefix () { - return this.config.get('global') ? this.globalPrefix : this.localPrefix + return this.global ? this.globalPrefix : this.localPrefix } set prefix (r) { - const k = this.config.get('global') ? 'globalPrefix' : 'localPrefix' + const k = this.global ? 'globalPrefix' : 'localPrefix' this[k] = r } |