@npmcli/config 6.1.0 → 6.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +33 -21
  2. package/package.json +4 -4
package/lib/index.js CHANGED
@@ -17,6 +17,14 @@ const {
17
17
  mkdir,
18
18
  } = require('fs/promises')
19
19
 
20
+ const fileExists = (...p) => stat(resolve(...p))
21
+ .then((st) => st.isFile())
22
+ .catch(() => false)
23
+
24
+ const dirExists = (...p) => stat(resolve(...p))
25
+ .then((st) => st.isDirectory())
26
+ .catch(() => false)
27
+
20
28
  const hasOwnProperty = (obj, key) =>
21
29
  Object.prototype.hasOwnProperty.call(obj, key)
22
30
 
@@ -90,6 +98,7 @@ class Config {
90
98
  platform = process.platform,
91
99
  execPath = process.execPath,
92
100
  cwd = process.cwd(),
101
+ excludeNpmCwd = false,
93
102
  }) {
94
103
  // turn the definitions into nopt's weirdo syntax
95
104
  this.definitions = definitions
@@ -117,10 +126,12 @@ class Config {
117
126
  this.execPath = execPath
118
127
  this.platform = platform
119
128
  this.cwd = cwd
129
+ this.excludeNpmCwd = excludeNpmCwd
120
130
 
121
131
  // set when we load configs
122
132
  this.globalPrefix = null
123
133
  this.localPrefix = null
134
+ this.localPackage = null
124
135
 
125
136
  // defaults to env.HOME, but will always be *something*
126
137
  this.home = null
@@ -311,15 +322,11 @@ class Config {
311
322
  // default the globalconfig file to that location, instead of the default
312
323
  // global prefix. It's weird that `npm get globalconfig --prefix=/foo`
313
324
  // returns `/foo/etc/npmrc`, but better to not change it at this point.
314
- settableGetter(data, 'globalconfig', () =>
315
- resolve(this[_get]('prefix'), 'etc/npmrc'))
325
+ settableGetter(data, 'globalconfig', () => resolve(this[_get]('prefix'), 'etc/npmrc'))
316
326
  }
317
327
 
318
328
  loadHome () {
319
- if (this.env.HOME) {
320
- return this.home = this.env.HOME
321
- }
322
- this.home = homedir()
329
+ this.home = this.env.HOME || homedir()
323
330
  }
324
331
 
325
332
  loadGlobalPrefix () {
@@ -330,7 +337,7 @@ class Config {
330
337
  if (this.env.PREFIX) {
331
338
  this.globalPrefix = this.env.PREFIX
332
339
  } else if (this.platform === 'win32') {
333
- // c:\node\node.exe --> prefix=c:\node\
340
+ // c:\node\node.exe --> prefix=c:\node\
334
341
  this.globalPrefix = dirname(this.execPath)
335
342
  } else {
336
343
  // /usr/local/bin/node --> prefix=/usr/local
@@ -475,8 +482,9 @@ class Config {
475
482
  if (problem.action === 'delete') {
476
483
  this.delete(problem.key, problem.where)
477
484
  } else if (problem.action === 'rename') {
478
- const old = this.get(problem.from, problem.where)
479
- this.set(problem.to, old, problem.where)
485
+ const raw = this.data.get(problem.where).raw?.[problem.from]
486
+ const calculated = this.get(problem.from, problem.where)
487
+ this.set(problem.to, raw || calculated, problem.where)
480
488
  this.delete(problem.from, problem.where)
481
489
  }
482
490
  }
@@ -599,6 +607,12 @@ class Config {
599
607
  // we return to make sure localPrefix is set
600
608
  await this.loadLocalPrefix()
601
609
 
610
+ // if we have not detected a local package json yet, try now that we
611
+ // have a local prefix
612
+ if (this.localPackage == null) {
613
+ this.localPackage = await fileExists(this.localPrefix, 'package.json')
614
+ }
615
+
602
616
  if (this[_get]('global') === true || this[_get]('location') === 'global') {
603
617
  this.data.get('project').source = '(global mode enabled, ignored)'
604
618
  this.sources.set(this.data.get('project').source, 'project')
@@ -630,16 +644,17 @@ class Config {
630
644
  const isGlobal = this[_get]('global') || this[_get]('location') === 'global'
631
645
 
632
646
  for (const p of walkUp(this.cwd)) {
633
- const hasNodeModules = await stat(resolve(p, 'node_modules'))
634
- .then((st) => st.isDirectory())
635
- .catch(() => false)
647
+ // HACK: this is an option set in tests to stop the local prefix from being set
648
+ // on tests that are created inside the npm repo
649
+ if (this.excludeNpmCwd && p === this.npmPath) {
650
+ break
651
+ }
636
652
 
637
- const hasPackageJson = await stat(resolve(p, 'package.json'))
638
- .then((st) => st.isFile())
639
- .catch(() => false)
653
+ const hasPackageJson = await fileExists(p, 'package.json')
640
654
 
641
- if (!this.localPrefix && (hasNodeModules || hasPackageJson)) {
655
+ if (!this.localPrefix && (hasPackageJson || await dirExists(p, 'node_modules'))) {
642
656
  this.localPrefix = p
657
+ this.localPackage = hasPackageJson
643
658
 
644
659
  // if workspaces are disabled, or we're in global mode, return now
645
660
  if (cliWorkspaces === false || isGlobal) {
@@ -663,11 +678,7 @@ class Config {
663
678
  for (const w of workspaces.values()) {
664
679
  if (w === this.localPrefix) {
665
680
  // see if there's a .npmrc file in the workspace, if so log a warning
666
- const hasNpmrc = await stat(resolve(this.localPrefix, '.npmrc'))
667
- .then((st) => st.isFile())
668
- .catch(() => false)
669
-
670
- if (hasNpmrc) {
681
+ if (await fileExists(this.localPrefix, '.npmrc')) {
671
682
  log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`)
672
683
  }
673
684
 
@@ -675,6 +686,7 @@ class Config {
675
686
  const { data } = this.data.get('default')
676
687
  data.workspace = [this.localPrefix]
677
688
  this.localPrefix = p
689
+ this.localPackage = hasPackageJson
678
690
  log.info(`found workspace root at ${this.localPrefix}`)
679
691
  // we found a root, so we return now
680
692
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/config",
3
- "version": "6.1.0",
3
+ "version": "6.1.2",
4
4
  "files": [
5
5
  "bin/",
6
6
  "lib/"
@@ -33,8 +33,8 @@
33
33
  },
34
34
  "devDependencies": {
35
35
  "@npmcli/eslint-config": "^4.0.0",
36
- "@npmcli/template-oss": "4.8.0",
37
- "tap": "^16.0.1"
36
+ "@npmcli/template-oss": "4.11.1",
37
+ "tap": "^16.3.2"
38
38
  },
39
39
  "dependencies": {
40
40
  "@npmcli/map-workspaces": "^3.0.0",
@@ -50,6 +50,6 @@
50
50
  },
51
51
  "templateOSS": {
52
52
  "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
53
- "version": "4.8.0"
53
+ "version": "4.11.1"
54
54
  }
55
55
  }