Skip to content

Commit 706b3d3

Browse files
fritzywraithgar
authored andcommitted
feat: set --no-audit when installing outside of a project (like --global)
1 parent 6df246f commit 706b3d3

File tree

11 files changed

+148
-3
lines changed

11 files changed

+148
-3
lines changed

lib/arborist-cmd.js

+14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
const log = require('./utils/log-shim.js')
2+
13
// This is the base for all commands whose execWorkspaces just gets
24
// a list of workspace names and passes it on to new Arborist() to
35
// be able to run a filtered Arborist.reify() at some point.
@@ -17,6 +19,18 @@ class ArboristCmd extends BaseCommand {
1719

1820
static ignoreImplicitWorkspace = false
1921

22+
constructor (npm) {
23+
super(npm)
24+
if (this.npm.config.isDefault('audit')
25+
&& (this.npm.global || this.npm.config.get('location') !== 'project')
26+
) {
27+
this.npm.config.set('audit', false)
28+
} else if (this.npm.global && this.npm.config.get('audit')) {
29+
log.warn('config',
30+
'includes both --global and --audit, which is currently unsupported.')
31+
}
32+
}
33+
2034
async execWorkspaces (args, filters) {
2135
await this.setWorkspaces(filters)
2236
return this.exec(args)

lib/npm.js

+15
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,21 @@ class Npm extends EventEmitter {
8888
// would be needed to change this
8989
async cmd (cmd) {
9090
await this.load()
91+
92+
// when location isn't set and global isn't true
93+
// check for a package.json at the localPrefix
94+
// and set the location to project if found
95+
// TODO: this logic can move to the config module loadLocalPrefix to
96+
// avoid double stat calls and consolidate logic
97+
if (this.config.isDefault('location') && !this.config.get('global')) {
98+
const hasPackageJson = await fs.stat(resolve(this.config.localPrefix, 'package.json'))
99+
.then((st) => st.isFile())
100+
.catch(() => false)
101+
if (hasPackageJson) {
102+
this.config.set('location', 'project')
103+
}
104+
}
105+
91106
const command = this.deref(cmd)
92107
if (!command) {
93108
throw Object.assign(new Error(`Unknown command ${cmd}`), {

tap-snapshots/test/lib/commands/config.js.test.cjs

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ exports[`test/lib/commands/config.js TAP config list with publishConfig > output
354354
; "cli" config from command line options
355355
356356
cache = "{NPMDIR}/test/lib/commands/tap-testdir-config-config-list-with-publishConfig-sandbox/cache"
357+
location = "project"
357358
prefix = "{LOCALPREFIX}"
358359
userconfig = "{HOME}/.npmrc"
359360

test/index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,17 @@ t.test('loading as main module will load the cli', t => {
1111
const cwd = t.testdir()
1212
const { spawn } = require('child_process')
1313
const LS = require('../lib/commands/ls.js')
14-
const ls = new LS({ config: { validate: () => {} } })
14+
const ls = new LS({
15+
config: {
16+
validate: () => {},
17+
get: (key) => {
18+
if (key === 'location') {
19+
return 'project'
20+
}
21+
},
22+
isDefault: () => {},
23+
},
24+
})
1525
const p = spawn(process.execPath, [index, 'ls', '-h', '--cache', cwd])
1626
const out = []
1727
p.stdout.on('data', c => out.push(c))

test/lib/arborist-cmd.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ const { resolve } = require('path')
22
const t = require('tap')
33
const ArboristCmd = require('../../lib/arborist-cmd.js')
44

5+
const configMock = {
6+
validate: () => {},
7+
get: (key) => {
8+
if (key === 'location') {
9+
return 'project'
10+
}
11+
},
12+
isDefault: () => {},
13+
}
14+
515
t.test('arborist-cmd', async t => {
616
const path = t.testdir({
717
'package.json': JSON.stringify({
@@ -44,7 +54,7 @@ t.test('arborist-cmd', async t => {
4454

4555
class TestCmd extends ArboristCmd {}
4656

47-
const cmd = new TestCmd({ localPrefix: path, config: { validate: () => {} } })
57+
const cmd = new TestCmd({ localPrefix: path, config: configMock })
4858

4959
// check filtering for a single workspace name
5060
cmd.exec = async function (args) {
@@ -96,7 +106,7 @@ t.test('handle getWorkspaces raising an error', async t => {
96106
},
97107
})
98108
class TestCmd extends ArboristCmd {}
99-
const cmd = new TestCmd({ localPrefix: t.testdir(), config: { validate: () => {} } })
109+
const cmd = new TestCmd({ localPrefix: t.testdir(), config: configMock })
100110

101111
await t.rejects(
102112
cmd.execWorkspaces(['foo'], ['a']),

test/lib/commands/explain.js

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ const npm = {
88
},
99
config: {
1010
validate: () => {},
11+
get: (key) => {
12+
if (key === 'location') {
13+
return 'project'
14+
}
15+
},
16+
isDefault: () => {},
1117
},
1218
}
1319
const { resolve } = require('path')

test/lib/commands/install-ci-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const installCITest = new InstallCITest({
2525
},
2626
config: {
2727
validate: () => {},
28+
get: (key) => {
29+
if (key === 'location') {
30+
return 'project'
31+
}
32+
},
33+
isDefault: () => {},
2834
},
2935
})
3036

test/lib/commands/install-test.js

+6
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ const installTest = new InstallTest({
2525
},
2626
config: {
2727
validate: () => {},
28+
get: (key) => {
29+
if (key === 'location') {
30+
return 'project'
31+
}
32+
},
33+
isDefault: () => {},
2834
},
2935
})
3036

test/lib/commands/install.js

+75
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ t.test('exec commands', async t => {
130130
)
131131
t.equal(REIFY_CALLED, true, 'called reify')
132132
t.strictSame(SCRIPTS, [], 'no scripts when installing globally')
133+
t.equal(npm.config.get('audit', 'cli'), false)
133134
})
134135

135136
await t.test('should not install invalid global package name', async t => {
@@ -327,3 +328,77 @@ t.test('completion', async t => {
327328
t.strictSame(res, [])
328329
})
329330
})
331+
332+
t.test('location detection and audit', async () => {
333+
t.test('audit false without package.json', async t => {
334+
const { npm } = await _loadMockNpm(t, {
335+
prefixDir: {
336+
// no package.json
337+
'readme.txt': 'just a file',
338+
other: {},
339+
},
340+
})
341+
const install = await npm.cmd('install')
342+
t.equal(install.npm.config.get('location'), 'user')
343+
t.equal(install.npm.config.get('audit'), false)
344+
})
345+
t.test('audit true with package.json', async t => {
346+
const { npm } = await _loadMockNpm(t, {
347+
prefixDir: {
348+
'package.json': '{ "name": "testpkg", "version": "1.0.0" }',
349+
'readme.txt': 'just a file',
350+
},
351+
})
352+
const install = await npm.cmd('install')
353+
t.equal(install.npm.config.get('location'), 'project')
354+
t.equal(install.npm.config.get('audit'), true)
355+
})
356+
t.test('audit true without package.json when set', async t => {
357+
const { npm } = await _loadMockNpm(t, {
358+
prefixDir: {
359+
// no package.json
360+
'readme.txt': 'just a file',
361+
other: {},
362+
},
363+
config: {
364+
audit: { value: true, where: 'cli' },
365+
},
366+
})
367+
const install = await npm.cmd('install')
368+
t.equal(install.npm.config.get('location'), 'user')
369+
t.equal(install.npm.config.get('audit'), true)
370+
})
371+
t.test('audit true in root config without package.json', async t => {
372+
const { npm } = await _loadMockNpm(t, {
373+
prefixDir: {
374+
// no package.json
375+
'readme.txt': 'just a file',
376+
other: {},
377+
},
378+
config: {
379+
audit: { value: true, where: 'builtin' },
380+
},
381+
})
382+
const install = await npm.cmd('install')
383+
t.equal(install.npm.config.get('location'), 'user')
384+
t.equal(install.npm.config.get('audit'), true)
385+
})
386+
t.test('test for warning when --global & --audit', async t => {
387+
const { npm, logs } = await _loadMockNpm(t, {
388+
prefixDir: {
389+
// no package.json
390+
'readme.txt': 'just a file',
391+
other: {},
392+
},
393+
config: {
394+
audit: { value: true, where: 'cli' },
395+
global: { value: true, where: 'cli' },
396+
},
397+
})
398+
const install = await npm.cmd('install')
399+
t.equal(install.npm.config.get('location'), 'user')
400+
t.equal(install.npm.config.get('audit'), true)
401+
t.equal(logs.warn[0][0], 'config')
402+
t.equal(logs.warn[0][1], 'includes both --global and --audit, which is currently unsupported.')
403+
})
404+
})

test/lib/commands/ls.js

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ const config = {
103103
global: false,
104104
json: false,
105105
link: false,
106+
location: 'project',
106107
omit: [],
107108
parseable: false,
108109
'package-lock-only': false,

test/lib/commands/set.js

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const npm = {
4242
},
4343
config: {
4444
validate: () => {},
45+
isDefault: () => {},
4546
},
4647
}
4748

0 commit comments

Comments
 (0)