Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/node-gyp.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class Gyp extends EventEmitter {
parseArgv (argv) {
this.opts = nopt(this.configDefs, this.shorthands, argv)
this.argv = this.opts.argv.remain.slice()
// the environment is read below and would overwrite this, but an explicit
// --loglevel on the command line should win over npm's inherited loglevel
const cliLoglevel = this.opts.loglevel

const commands = this.todo = []

Expand Down Expand Up @@ -158,8 +161,9 @@ class Gyp extends EventEmitter {
}
}

if (this.opts.loglevel) {
log.logger.level = this.opts.loglevel
const loglevel = cliLoglevel || this.opts.loglevel
if (loglevel) {
log.logger.level = loglevel
delete this.opts.loglevel
}
log.resume()
Expand Down
26 changes: 26 additions & 0 deletions test/test-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ describe('options', function () {
// loglevel does not get added to opts but will change the logger's level.
process.env.npm_config_loglevel = 'silly'

// the logger is shared, so reset whatever level earlier tests left behind
// (e.g. from an npm_config_loglevel set by the caller of `npm test`)
log.logger.level = 'info'

const g = gyp()

assert.strictEqual(log.logger.level.id, 'info')
Expand Down Expand Up @@ -99,4 +103,26 @@ describe('options', function () {

assert.strictEqual(g.opts['msvs-version'], '2022')
})

it('--loglevel on the command line wins over npm_config_loglevel', () => {
process.env.npm_config_loglevel = 'error'
log.logger.level = 'info'

try {
const g = gyp()
g.parseArgv(['node', 'node-gyp', 'rebuild', '--loglevel=verbose'])

assert.strictEqual(log.logger.level.id, 'verbose')
assert.strictEqual(g.opts.loglevel, undefined)

gyp().parseArgv(['node', 'node-gyp', 'rebuild', '--silly'])
assert.strictEqual(log.logger.level.id, 'silly')

gyp().parseArgv(['node', 'node-gyp', 'rebuild'])
assert.strictEqual(log.logger.level.id, 'error')
} finally {
delete process.env.npm_config_loglevel
log.logger.level = 'info'
}
})
})
Loading