Skip to content

Commit 0e74ee4

Browse files
authored
fix: clean up npm object (#7416)
Removed all the setters, they were dangerous and not the right way to set those values. Tweaked `this.#init` so it always returns an object so the coerce in `this.load` didn't need to happen.
1 parent c060e60 commit 0e74ee4

File tree

4 files changed

+17
-57
lines changed

4 files changed

+17
-57
lines changed

lib/npm.js

+11-29
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class Npm {
9494

9595
async load () {
9696
return time.start('npm:load', async () => {
97-
const { exec = true } = await this.#load().then(r => r ?? {})
97+
const { exec } = await this.#load()
9898
return {
9999
exec,
100100
command: this.argv.shift(),
@@ -130,11 +130,12 @@ class Npm {
130130
output.error('')
131131
}
132132

133-
const logsMax = this.config.get('logs-max')
134-
135133
if (this.logFiles.length) {
136-
log.error('', `A complete log of this run can be found in: ${this.logFiles}`)
137-
} else if (logsMax <= 0) {
134+
return log.error('', `A complete log of this run can be found in: ${this.logFiles}`)
135+
}
136+
137+
const logsMax = this.config.get('logs-max')
138+
if (logsMax <= 0) {
138139
// user specified no log file
139140
log.error('', `Log files were not written due to the config logs-max=${logsMax}`)
140141
} else {
@@ -151,11 +152,6 @@ class Npm {
151152
return this.#title
152153
}
153154

154-
set title (t) {
155-
process.title = t
156-
this.#title = t
157-
}
158-
159155
async #load () {
160156
await time.start('npm:load:whichnode', async () => {
161157
// TODO should we throw here?
@@ -210,8 +206,9 @@ class Npm {
210206
const { parsedArgv: { cooked, remain } } = this.config
211207
this.argv = remain
212208
// Secrets are mostly in configs, so title is set using only the positional args
213-
// to keep those from being leaked.
214-
this.title = ['npm'].concat(replaceInfo(remain)).join(' ').trim()
209+
// to keep those from being leaked. We still do a best effort replaceInfo.
210+
this.#title = ['npm'].concat(replaceInfo(remain)).join(' ').trim()
211+
process.title = this.#title
215212
// The cooked argv is also logged separately for debugging purposes. It is
216213
// cleaned as a best effort by replacing known secrets like basic auth
217214
// password and strings that look like npm tokens. XXX: for this to be
@@ -252,6 +249,8 @@ class Npm {
252249
this.argv = ['version']
253250
this.config.set('usage', false, 'cli')
254251
}
252+
253+
return { exec: true }
255254
}
256255

257256
get isShellout () {
@@ -331,26 +330,14 @@ class Npm {
331330
return this.config.get('cache')
332331
}
333332

334-
set cache (r) {
335-
this.config.set('cache', r)
336-
}
337-
338333
get globalPrefix () {
339334
return this.config.globalPrefix
340335
}
341336

342-
set globalPrefix (r) {
343-
this.config.globalPrefix = r
344-
}
345-
346337
get localPrefix () {
347338
return this.config.localPrefix
348339
}
349340

350-
set localPrefix (r) {
351-
this.config.localPrefix = r
352-
}
353-
354341
get localPackage () {
355342
return this.config.localPackage
356343
}
@@ -386,11 +373,6 @@ class Npm {
386373
return this.global ? this.globalPrefix : this.localPrefix
387374
}
388375

389-
set prefix (r) {
390-
const k = this.global ? 'globalPrefix' : 'localPrefix'
391-
this[k] = r
392-
}
393-
394376
get usage () {
395377
return usage(this)
396378
}

test/lib/arborist-cmd.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,8 @@ t.test('arborist-cmd', async t => {
117117
chdir: (dirs) => dirs.testdir,
118118
})
119119

120-
npm.localPrefix = prefix
120+
// TODO there has to be a better way to do this
121+
npm.config.localPrefix = prefix
121122
await cmd.execWorkspaces([])
122123

123124
t.same(cmd.workspaceNames, ['a', 'c'], 'should set array with single ws name')

test/lib/commands/exec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ t.test('--prefix', async t => {
7676
})
7777

7878
// This is what `--prefix` does
79-
npm.globalPrefix = npm.localPrefix
79+
npm.config.globalPrefix = npm.config.localPrefix
8080

8181
await registry.package({
8282
manifest,

test/lib/npm.js

+3-26
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ t.test('npm.load', async t => {
3636
})
3737

3838
await t.test('basic loading', async t => {
39-
const { npm, logs, prefix: dir, cache, other } = await loadMockNpm(t, {
39+
const { npm, logs, cache } = await loadMockNpm(t, {
4040
prefixDir: { node_modules: {} },
41-
otherDirs: {
42-
newCache: {},
43-
},
4441
config: {
4542
timing: true,
4643
},
@@ -61,25 +58,11 @@ t.test('npm.load', async t => {
6158

6259
mockGlobals(t, { process: { platform: 'posix' } })
6360
t.equal(resolve(npm.cache), resolve(cache), 'cache is cache')
64-
npm.cache = other.newCache
65-
t.equal(npm.config.get('cache'), other.newCache, 'cache setter sets config')
66-
t.equal(npm.cache, other.newCache, 'cache getter gets new config')
6761
t.equal(npm.lockfileVersion, 2, 'lockfileVersion getter')
6862
t.equal(npm.prefix, npm.localPrefix, 'prefix is local prefix')
6963
t.not(npm.prefix, npm.globalPrefix, 'prefix is not global prefix')
70-
npm.globalPrefix = npm.prefix
71-
t.equal(npm.prefix, npm.globalPrefix, 'globalPrefix setter')
72-
npm.localPrefix = dir + '/extra/prefix'
73-
t.equal(npm.prefix, npm.localPrefix, 'prefix is local prefix after localPrefix setter')
74-
t.not(npm.prefix, npm.globalPrefix, 'prefix is not global prefix after localPrefix setter')
75-
76-
npm.prefix = dir + '/some/prefix'
77-
t.equal(npm.prefix, npm.localPrefix, 'prefix is local prefix after prefix setter')
78-
t.not(npm.prefix, npm.globalPrefix, 'prefix is not global prefix after prefix setter')
79-
t.equal(npm.bin, npm.localBin, 'bin is local bin after prefix setter')
80-
t.not(npm.bin, npm.globalBin, 'bin is not global bin after prefix setter')
81-
t.equal(npm.dir, npm.localDir, 'dir is local dir after prefix setter')
82-
t.not(npm.dir, npm.globalDir, 'dir is not global dir after prefix setter')
64+
t.equal(npm.bin, npm.localBin, 'bin is local bin')
65+
t.not(npm.bin, npm.globalBin, 'bin is not global bin')
8366

8467
npm.config.set('global', true)
8568
t.equal(npm.prefix, npm.globalPrefix, 'prefix is global prefix after setting global')
@@ -89,12 +72,6 @@ t.test('npm.load', async t => {
8972
t.equal(npm.dir, npm.globalDir, 'dir is global dir after setting global')
9073
t.not(npm.dir, npm.localDir, 'dir is not local dir after setting global')
9174

92-
npm.prefix = dir + '/new/global/prefix'
93-
t.equal(npm.prefix, npm.globalPrefix, 'prefix is global prefix after prefix setter')
94-
t.not(npm.prefix, npm.localPrefix, 'prefix is not local prefix after prefix setter')
95-
t.equal(npm.bin, npm.globalBin, 'bin is global bin after prefix setter')
96-
t.not(npm.bin, npm.localBin, 'bin is not local bin after prefix setter')
97-
9875
mockGlobals(t, { process: { platform: 'win32' } })
9976
t.equal(npm.bin, npm.globalBin, 'bin is global bin in windows mode')
10077
t.equal(npm.dir, npm.globalDir, 'dir is global dir in windows mode')

0 commit comments

Comments
 (0)