Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

feat: remove log option #40

Merged
merged 1 commit into from
Feb 14, 2022
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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(),
Expand All @@ -114,7 +114,6 @@ class Config {
this.defaults = defaults

this.npmPath = npmPath
this.log = log
this.argv = argv
this.env = env
this.execPath = execPath
Expand Down Expand Up @@ -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}`
Expand Down Expand Up @@ -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) {
Expand All @@ -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
Expand All @@ -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])
}
}

Expand Down Expand Up @@ -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
}
Expand Down
26 changes: 7 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -217,7 +215,6 @@ loglevel = yolo
env: {},
argv: [process.execPath, __filename, '--userconfig', `${path}/WEIRD-ERROR`],
cwd: path,
log,
shorthands,
definitions,
})
Expand All @@ -239,7 +236,6 @@ loglevel = yolo
npmPath: `${path}/npm`,
env,
argv,
log,
cwd: `${path}/project`,

shorthands,
Expand Down Expand Up @@ -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 => {
Expand All @@ -450,7 +447,6 @@ loglevel = yolo
npmPath: path,
env,
argv,
log,
cwd: `${path}/project-no-config`,

// should prepend DESTDIR to /global
Expand Down Expand Up @@ -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) => {
Expand All @@ -1051,7 +1045,6 @@ t.test('workspaces', async (t) => {
cwd: `${path}/workspaces/one`,
shorthands,
definitions,
log,
})

await config.load()
Expand All @@ -1073,7 +1066,6 @@ t.test('workspaces', async (t) => {
cwd: `${path}/workspaces/one`,
shorthands,
definitions,
log,
})

await config.load()
Expand All @@ -1095,7 +1087,6 @@ t.test('workspaces', async (t) => {
cwd: `${path}/workspaces/three`,
shorthands,
definitions,
log,
})

await config.load()
Expand All @@ -1118,7 +1109,6 @@ t.test('workspaces', async (t) => {
cwd: `${path}/workspaces/one`,
shorthands,
definitions,
log,
})

await config.load()
Expand All @@ -1139,7 +1129,6 @@ t.test('workspaces', async (t) => {
cwd: `${path}/workspaces/one`,
shorthands,
definitions,
log,
})

await config.load()
Expand All @@ -1166,7 +1155,6 @@ t.test('workspaces', async (t) => {
cwd: `${path}/workspaces/one`,
shorthands,
definitions,
log,
})

await config.load()
Expand Down
1 change: 0 additions & 1 deletion test/parse-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
}
Expand Down
25 changes: 0 additions & 25 deletions test/proc-log.js

This file was deleted.