diff --git a/README.md b/README.md index 5ace207..95cbb39 100644 --- a/README.md +++ b/README.md @@ -78,9 +78,12 @@ const conf = new Config({ platform: process.platform, // optional, defaults to process.cwd() cwd: process.cwd(), - // optional, defaults to emitting 'log' events on process object - // only silly, verbose, warn, and error are logged by this module - log: require('proc-log') +}) + +// emits log events on the process object +// see `proc-log` for more info +process.on('log', (level, ...args) => { + console.log(level, ...args) }) // returns a promise that fails if config loading fails, and @@ -124,8 +127,6 @@ Options: Windows. - `execPath` Optional, defaults to `process.execPath`. Used to infer the `globalPrefix`. -- `log` Optional, the object used to log debug messages, warnings, and - errors. Defaults to emitting on the `process` object. - `env` Optional, defaults to `process.env`. Source of the environment variables for configuration. - `argv` Optional, defaults to `process.argv`. Source of the CLI options @@ -161,7 +162,6 @@ Fields: - `argv` The `argv` param - `execPath` The `execPath` param - `platform` The `platform` param -- `log` The `log` param - `defaults` The `defaults` param - `shorthands` The `shorthands` param - `types` The `types` param diff --git a/lib/index.js b/lib/index.js index 65615c7..82e3699 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,6 +5,7 @@ const nopt = require('nopt') const mkdirp = require('mkdirp-infer-owner') const mapWorkspaces = require('@npmcli/map-workspaces') const rpj = require('read-package-json-fast') +const log = require('proc-log') /* istanbul ignore next */ const myUid = process.getuid && process.getuid() @@ -88,7 +89,6 @@ class Config { // options just to override in tests, mostly env = process.env, argv = process.argv, - log = require('proc-log'), platform = process.platform, execPath = process.execPath, cwd = process.cwd(), @@ -114,7 +114,6 @@ class Config { this.defaults = defaults this.npmPath = npmPath - this.log = log this.argv = argv this.env = env this.execPath = execPath @@ -436,7 +435,7 @@ class Config { } invalidHandler (k, val, type, source, where) { - this.log.warn( + log.warn( 'invalid config', k + '=' + JSON.stringify(val), `set in ${source}` @@ -469,7 +468,7 @@ class Config { : mustBe.filter(m => m !== Array) .map(n => typeof n === 'string' ? n : JSON.stringify(n)) .join(', ') - this.log.warn('invalid config', msg, desc) + log.warn('invalid config', msg, desc) } [_loadObject] (obj, where, source, er = null) { @@ -491,7 +490,7 @@ class Config { if (er) { conf.loadError = er if (er.code !== 'ENOENT') { - this.log.verbose('config', `error loading ${where} config`, er) + log.verbose('config', `error loading ${where} config`, er) } } else { conf.raw = obj @@ -510,7 +509,7 @@ class Config { // XXX a future npm version will make this a warning. // An even more future npm version will make this an error. if (this.deprecated[key]) { - this.log.verbose('config', key, this.deprecated[key]) + log.verbose('config', key, this.deprecated[key]) } } @@ -607,14 +606,14 @@ class Config { .catch(() => false) if (hasNpmrc) { - this.log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`) + log.warn(`ignoring workspace config at ${this.localPrefix}/.npmrc`) } // set the workspace in the default layer, which allows it to be overridden easily const { data } = this.data.get('default') data.workspace = [this.localPrefix] this.localPrefix = p - this.log.info(`found workspace root at ${this.localPrefix}`) + log.info(`found workspace root at ${this.localPrefix}`) // we found a root, so we return now return } diff --git a/test/index.js b/test/index.js index 8a30f91..7e0aed4 100644 --- a/test/index.js +++ b/test/index.js @@ -140,11 +140,9 @@ loglevel = yolo }) const logs = [] - const log = { - info: (...msg) => logs.push(['info', ...msg]), - warn: (...msg) => logs.push(['warn', ...msg]), - verbose: (...msg) => logs.push(['verbose', ...msg]), - } + const logHandler = (...args) => logs.push(args) + process.on('log', logHandler) + t.teardown(() => process.off('log', logHandler)) const argv = [ process.execPath, @@ -217,7 +215,6 @@ loglevel = yolo env: {}, argv: [process.execPath, __filename, '--userconfig', `${path}/WEIRD-ERROR`], cwd: path, - log, shorthands, definitions, }) @@ -239,7 +236,6 @@ loglevel = yolo npmPath: `${path}/npm`, env, argv, - log, cwd: `${path}/project`, shorthands, @@ -437,6 +433,7 @@ loglevel = yolo message: 'invalid config location param: yolo', }) t.equal(config.valid, false, 'config should not be valid') + logs.length = 0 }) t.test('load configs from files, cli, and env, no builtin or project', async t => { @@ -450,7 +447,6 @@ loglevel = yolo npmPath: path, env, argv, - log, cwd: `${path}/project-no-config`, // should prepend DESTDIR to /global @@ -1032,11 +1028,9 @@ t.test('workspaces', async (t) => { })) const logs = [] - const log = { - info: (...msg) => logs.push(['info', ...msg]), - warn: (...msg) => logs.push(['warn', ...msg]), - verbose: (...msg) => logs.push(['verbose', ...msg]), - } + const logHandler = (...args) => logs.push(args) + process.on('log', logHandler) + t.teardown(() => process.off('log', logHandler)) t.afterEach(() => logs.length = 0) t.test('finds own parent', async (t) => { @@ -1051,7 +1045,6 @@ t.test('workspaces', async (t) => { cwd: `${path}/workspaces/one`, shorthands, definitions, - log, }) await config.load() @@ -1073,7 +1066,6 @@ t.test('workspaces', async (t) => { cwd: `${path}/workspaces/one`, shorthands, definitions, - log, }) await config.load() @@ -1095,7 +1087,6 @@ t.test('workspaces', async (t) => { cwd: `${path}/workspaces/three`, shorthands, definitions, - log, }) await config.load() @@ -1118,7 +1109,6 @@ t.test('workspaces', async (t) => { cwd: `${path}/workspaces/one`, shorthands, definitions, - log, }) await config.load() @@ -1139,7 +1129,6 @@ t.test('workspaces', async (t) => { cwd: `${path}/workspaces/one`, shorthands, definitions, - log, }) await config.load() @@ -1166,7 +1155,6 @@ t.test('workspaces', async (t) => { cwd: `${path}/workspaces/one`, shorthands, definitions, - log, }) await config.load() diff --git a/test/parse-field.js b/test/parse-field.js index bb804f3..1c4193b 100644 --- a/test/parse-field.js +++ b/test/parse-field.js @@ -7,7 +7,6 @@ t.strictSame(parseField({ a: 1 }, 'a'), { a: 1 }) const opts = { platform: 'posix', types: require('./fixtures/types.js'), - log: require('proc-log'), home: '/home/user', env: { foo: 'bar' }, } diff --git a/test/proc-log.js b/test/proc-log.js deleted file mode 100644 index 297bfe5..0000000 --- a/test/proc-log.js +++ /dev/null @@ -1,25 +0,0 @@ -const log = require('proc-log') -const t = require('tap') -const logs = [] -process.on('log', (...msg) => logs.push(msg)) -log.silly('OpAn SauCE lOl') -log.verbose('WHEN I WROTE the following pages, or rather the bulk of them, I lived alone,' + - ' in the woods, a mile from any neighbor, in a house which I had built myself, on the shore' + - ' of Walden Pond, in Concord, Massachusetts, and earned my living by the labor of my hands only' + - '. I lived there two years and two months. At present I am a sojourner in civilized life again.') -log.warn('cave canem') -log.error('i wrote a message but i accidentally it') - -t.strictSame(logs, [ - ['silly', 'OpAn SauCE lOl'], - [ - 'verbose', - 'WHEN I WROTE the following pages, or rather the bulk of them, I lived alone,' + - ' in the woods, a mile from any neighbor, in a house which I had built myself, on the shore' + - ' of Walden Pond, in Concord, Massachusetts, and earned my living by the labor of my hands' + - ' only. I lived there two years and two months. At present I am a sojourner in civilized' + - ' life again.', - ], - ['warn', 'cave canem'], - ['error', 'i wrote a message but i accidentally it'], -])