Skip to content

Commit bbf5128

Browse files
authored
feat: remove log option (npm#40)
Previously this library defaulted to using `proc-log` if no `log` option was passed in. Now it will only use `proc-log` to emit log events. BREAKING CHANGE: drop support for the `log` option
1 parent 9af098f commit bbf5128

File tree

5 files changed

+20
-59
lines changed

5 files changed

+20
-59
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ const conf = new Config({
7878
platform: process.platform,
7979
// optional, defaults to process.cwd()
8080
cwd: process.cwd(),
81-
// optional, defaults to emitting 'log' events on process object
82-
// only silly, verbose, warn, and error are logged by this module
83-
log: require('proc-log')
81+
})
82+
83+
// emits log events on the process object
84+
// see `proc-log` for more info
85+
process.on('log', (level, ...args) => {
86+
console.log(level, ...args)
8487
})
8588

8689
// returns a promise that fails if config loading fails, and
@@ -124,8 +127,6 @@ Options:
124127
Windows.
125128
- `execPath` Optional, defaults to `process.execPath`. Used to infer the
126129
`globalPrefix`.
127-
- `log` Optional, the object used to log debug messages, warnings, and
128-
errors. Defaults to emitting on the `process` object.
129130
- `env` Optional, defaults to `process.env`. Source of the environment
130131
variables for configuration.
131132
- `argv` Optional, defaults to `process.argv`. Source of the CLI options
@@ -161,7 +162,6 @@ Fields:
161162
- `argv` The `argv` param
162163
- `execPath` The `execPath` param
163164
- `platform` The `platform` param
164-
- `log` The `log` param
165165
- `defaults` The `defaults` param
166166
- `shorthands` The `shorthands` param
167167
- `types` The `types` param

lib/index.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nopt = require('nopt')
55
const mkdirp = require('mkdirp-infer-owner')
66
const mapWorkspaces = require('@npmcli/map-workspaces')
77
const rpj = require('read-package-json-fast')
8+
const log = require('proc-log')
89

910
/* istanbul ignore next */
1011
const myUid = process.getuid && process.getuid()
@@ -88,7 +89,6 @@ class Config {
8889
// options just to override in tests, mostly
8990
env = process.env,
9091
argv = process.argv,
91-
log = require('proc-log'),
9292
platform = process.platform,
9393
execPath = process.execPath,
9494
cwd = process.cwd(),
@@ -114,7 +114,6 @@ class Config {
114114
this.defaults = defaults
115115

116116
this.npmPath = npmPath
117-
this.log = log
118117
this.argv = argv
119118
this.env = env
120119
this.execPath = execPath
@@ -436,7 +435,7 @@ class Config {
436435
}
437436

438437
invalidHandler (k, val, type, source, where) {
439-
this.log.warn(
438+
log.warn(
440439
'invalid config',
441440
k + '=' + JSON.stringify(val),
442441
`set in ${source}`
@@ -469,7 +468,7 @@ class Config {
469468
: mustBe.filter(m => m !== Array)
470469
.map(n => typeof n === 'string' ? n : JSON.stringify(n))
471470
.join(', ')
472-
this.log.warn('invalid config', msg, desc)
471+
log.warn('invalid config', msg, desc)
473472
}
474473

475474
[_loadObject] (obj, where, source, er = null) {
@@ -491,7 +490,7 @@ class Config {
491490
if (er) {
492491
conf.loadError = er
493492
if (er.code !== 'ENOENT') {
494-
this.log.verbose('config', `error loading ${where} config`, er)
493+
log.verbose('config', `error loading ${where} config`, er)
495494
}
496495
} else {
497496
conf.raw = obj
@@ -510,7 +509,7 @@ class Config {
510509
// XXX a future npm version will make this a warning.
511510
// An even more future npm version will make this an error.
512511
if (this.deprecated[key]) {
513-
this.log.verbose('config', key, this.deprecated[key])
512+
log.verbose('config', key, this.deprecated[key])
514513
}
515514
}
516515

@@ -607,14 +606,14 @@ class Config {
607606
.catch(() => false)
608607

609608
if (hasNpmrc) {
610-
this.log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`)
609+
log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`)
611610
}
612611

613612
// set the workspace in the default layer, which allows it to be overridden easily
614613
const { data } = this.data.get('default')
615614
data.workspace = [this.localPrefix]
616615
this.localPrefix = p
617-
this.log.info(`found workspace root at ${this.localPrefix}`)
616+
log.info(`found workspace root at ${this.localPrefix}`)
618617
// we found a root, so we return now
619618
return
620619
}

test/index.js

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,9 @@ loglevel = yolo
140140
})
141141

142142
const logs = []
143-
const log = {
144-
info: (...msg) => logs.push(['info', ...msg]),
145-
warn: (...msg) => logs.push(['warn', ...msg]),
146-
verbose: (...msg) => logs.push(['verbose', ...msg]),
147-
}
143+
const logHandler = (...args) => logs.push(args)
144+
process.on('log', logHandler)
145+
t.teardown(() => process.off('log', logHandler))
148146

149147
const argv = [
150148
process.execPath,
@@ -217,7 +215,6 @@ loglevel = yolo
217215
env: {},
218216
argv: [process.execPath, __filename, '--userconfig', `${path}/WEIRD-ERROR`],
219217
cwd: path,
220-
log,
221218
shorthands,
222219
definitions,
223220
})
@@ -239,7 +236,6 @@ loglevel = yolo
239236
npmPath: `${path}/npm`,
240237
env,
241238
argv,
242-
log,
243239
cwd: `${path}/project`,
244240

245241
shorthands,
@@ -437,6 +433,7 @@ loglevel = yolo
437433
message: 'invalid config location param: yolo',
438434
})
439435
t.equal(config.valid, false, 'config should not be valid')
436+
logs.length = 0
440437
})
441438

442439
t.test('load configs from files, cli, and env, no builtin or project', async t => {
@@ -450,7 +447,6 @@ loglevel = yolo
450447
npmPath: path,
451448
env,
452449
argv,
453-
log,
454450
cwd: `${path}/project-no-config`,
455451

456452
// should prepend DESTDIR to /global
@@ -1032,11 +1028,9 @@ t.test('workspaces', async (t) => {
10321028
}))
10331029

10341030
const logs = []
1035-
const log = {
1036-
info: (...msg) => logs.push(['info', ...msg]),
1037-
warn: (...msg) => logs.push(['warn', ...msg]),
1038-
verbose: (...msg) => logs.push(['verbose', ...msg]),
1039-
}
1031+
const logHandler = (...args) => logs.push(args)
1032+
process.on('log', logHandler)
1033+
t.teardown(() => process.off('log', logHandler))
10401034
t.afterEach(() => logs.length = 0)
10411035

10421036
t.test('finds own parent', async (t) => {
@@ -1051,7 +1045,6 @@ t.test('workspaces', async (t) => {
10511045
cwd: `${path}/workspaces/one`,
10521046
shorthands,
10531047
definitions,
1054-
log,
10551048
})
10561049

10571050
await config.load()
@@ -1073,7 +1066,6 @@ t.test('workspaces', async (t) => {
10731066
cwd: `${path}/workspaces/one`,
10741067
shorthands,
10751068
definitions,
1076-
log,
10771069
})
10781070

10791071
await config.load()
@@ -1095,7 +1087,6 @@ t.test('workspaces', async (t) => {
10951087
cwd: `${path}/workspaces/three`,
10961088
shorthands,
10971089
definitions,
1098-
log,
10991090
})
11001091

11011092
await config.load()
@@ -1118,7 +1109,6 @@ t.test('workspaces', async (t) => {
11181109
cwd: `${path}/workspaces/one`,
11191110
shorthands,
11201111
definitions,
1121-
log,
11221112
})
11231113

11241114
await config.load()
@@ -1139,7 +1129,6 @@ t.test('workspaces', async (t) => {
11391129
cwd: `${path}/workspaces/one`,
11401130
shorthands,
11411131
definitions,
1142-
log,
11431132
})
11441133

11451134
await config.load()
@@ -1166,7 +1155,6 @@ t.test('workspaces', async (t) => {
11661155
cwd: `${path}/workspaces/one`,
11671156
shorthands,
11681157
definitions,
1169-
log,
11701158
})
11711159

11721160
await config.load()

test/parse-field.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ t.strictSame(parseField({ a: 1 }, 'a'), { a: 1 })
77
const opts = {
88
platform: 'posix',
99
types: require('./fixtures/types.js'),
10-
log: require('proc-log'),
1110
home: '/home/user',
1211
env: { foo: 'bar' },
1312
}

test/proc-log.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)