@npmcli/config 6.0.1 → 6.1.1

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 +30 -19
  2. package/package.json +9 -8
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
@@ -599,6 +606,12 @@ class Config {
599
606
  // we return to make sure localPrefix is set
600
607
  await this.loadLocalPrefix()
601
608
 
609
+ // if we have not detected a local package json yet, try now that we
610
+ // have a local prefix
611
+ if (this.localPackage == null) {
612
+ this.localPackage = await fileExists(this.localPrefix, 'package.json')
613
+ }
614
+
602
615
  if (this[_get]('global') === true || this[_get]('location') === 'global') {
603
616
  this.data.get('project').source = '(global mode enabled, ignored)'
604
617
  this.sources.set(this.data.get('project').source, 'project')
@@ -630,16 +643,17 @@ class Config {
630
643
  const isGlobal = this[_get]('global') || this[_get]('location') === 'global'
631
644
 
632
645
  for (const p of walkUp(this.cwd)) {
633
- const hasNodeModules = await stat(resolve(p, 'node_modules'))
634
- .then((st) => st.isDirectory())
635
- .catch(() => false)
646
+ // HACK: this is an option set in tests to stop the local prefix from being set
647
+ // on tests that are created inside the npm repo
648
+ if (this.excludeNpmCwd && p === this.npmPath) {
649
+ break
650
+ }
636
651
 
637
- const hasPackageJson = await stat(resolve(p, 'package.json'))
638
- .then((st) => st.isFile())
639
- .catch(() => false)
652
+ const hasPackageJson = await fileExists(p, 'package.json')
640
653
 
641
- if (!this.localPrefix && (hasNodeModules || hasPackageJson)) {
654
+ if (!this.localPrefix && (hasPackageJson || await dirExists(p, 'node_modules'))) {
642
655
  this.localPrefix = p
656
+ this.localPackage = hasPackageJson
643
657
 
644
658
  // if workspaces are disabled, or we're in global mode, return now
645
659
  if (cliWorkspaces === false || isGlobal) {
@@ -663,11 +677,7 @@ class Config {
663
677
  for (const w of workspaces.values()) {
664
678
  if (w === this.localPrefix) {
665
679
  // 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) {
680
+ if (await fileExists(this.localPrefix, '.npmrc')) {
671
681
  log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`)
672
682
  }
673
683
 
@@ -675,6 +685,7 @@ class Config {
675
685
  const { data } = this.data.get('default')
676
686
  data.workspace = [this.localPrefix]
677
687
  this.localPrefix = p
688
+ this.localPackage = hasPackageJson
678
689
  log.info(`found workspace root at ${this.localPrefix}`)
679
690
  // we found a root, so we return now
680
691
  return
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@npmcli/config",
3
- "version": "6.0.1",
3
+ "version": "6.1.1",
4
4
  "files": [
5
5
  "bin/",
6
6
  "lib/"
@@ -9,7 +9,8 @@
9
9
  "description": "Configuration management for the npm cli",
10
10
  "repository": {
11
11
  "type": "git",
12
- "url": "https://github.com/npm/config.git"
12
+ "url": "https://github.com/npm/cli.git",
13
+ "directory": "workspaces/config"
13
14
  },
14
15
  "author": "GitHub Inc.",
15
16
  "license": "ISC",
@@ -18,8 +19,8 @@
18
19
  "snap": "tap",
19
20
  "lint": "eslint \"**/*.js\"",
20
21
  "postlint": "template-oss-check",
21
- "lintfix": "npm run lint -- --fix",
22
- "posttest": "npm run lint",
22
+ "lintfix": "node ../.. run lint -- --fix",
23
+ "posttest": "node ../.. run lint",
23
24
  "template-oss-apply": "template-oss-apply --force"
24
25
  },
25
26
  "tap": {
@@ -32,13 +33,13 @@
32
33
  },
33
34
  "devDependencies": {
34
35
  "@npmcli/eslint-config": "^4.0.0",
35
- "@npmcli/template-oss": "4.5.1",
36
- "tap": "^16.0.1"
36
+ "@npmcli/template-oss": "4.11.0",
37
+ "tap": "^16.3.2"
37
38
  },
38
39
  "dependencies": {
39
40
  "@npmcli/map-workspaces": "^3.0.0",
40
41
  "ini": "^3.0.0",
41
- "nopt": "^6.0.0",
42
+ "nopt": "^7.0.0",
42
43
  "proc-log": "^3.0.0",
43
44
  "read-package-json-fast": "^3.0.0",
44
45
  "semver": "^7.3.5",
@@ -49,6 +50,6 @@
49
50
  },
50
51
  "templateOSS": {
51
52
  "//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.",
52
- "version": "4.5.1"
53
+ "version": "4.11.0"
53
54
  }
54
55
  }