Skip to content

Commit

Permalink
feat: simplify logging options
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Jan 26, 2024
1 parent be382e7 commit d1c9d74
Show file tree
Hide file tree
Showing 9 changed files with 1,100 additions and 1,173 deletions.
4 changes: 1 addition & 3 deletions src/commands/plugins/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import chalk from 'chalk'
import {readFile} from 'node:fs/promises'
import {dirname, join, sep} from 'node:path'

import {determineLogLevel, npmLogLevelFlag} from '../../log-level.js'
import Plugins from '../../plugins.js'
import {sortBy} from '../../util.js'

Expand Down Expand Up @@ -53,7 +52,6 @@ export default class PluginsInspect extends Command {

static flags = {
help: Flags.help({char: 'h'}),
'npm-log-level': npmLogLevelFlag({exclusive: ['verbose']}),
verbose: Flags.boolean({char: 'v'}),
}

Expand Down Expand Up @@ -144,7 +142,7 @@ export default class PluginsInspect extends Command {
const {argv, flags} = await this.parse(PluginsInspect)
this.plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(flags),
verbose: flags.verbose,
})
const aliases = this.config.pjson.oclif.aliases ?? {}
const plugins: PluginWithDeps[] = []
Expand Down
5 changes: 2 additions & 3 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {Args, Command, Errors, Flags, Interfaces, ux} from '@oclif/core'
import chalk from 'chalk'
import validate from 'validate-npm-package-name'

import {determineLogLevel, npmLogLevelFlag} from '../../log-level.js'
import Plugins from '../../plugins.js'

export default class PluginsInstall extends Command {
Expand Down Expand Up @@ -63,9 +62,9 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins
return input
},
}),
'npm-log-level': npmLogLevelFlag({exclusive: ['silent', 'verbose']}),
silent: Flags.boolean({
char: 's',
default: true,
description: 'Silences npm output.',
exclusive: ['verbose'],
}),
Expand Down Expand Up @@ -141,7 +140,7 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins

const plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(this.flags),
verbose: this.flags.verbose,
})
const aliases = this.config.pjson.oclif.aliases || {}
for (let name of argv as string[]) {
Expand Down
4 changes: 1 addition & 3 deletions src/commands/plugins/link.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import chalk from 'chalk'

import {determineLogLevel, npmLogLevelFlag} from '../../log-level.js'
import Plugins from '../../plugins.js'

export default class PluginsLink extends Command {
Expand All @@ -24,7 +23,6 @@ e.g. If you have a user-installed or core plugin that has a 'hello' command, ins
default: true,
description: 'Install dependencies after linking the plugin.',
}),
'npm-log-level': npmLogLevelFlag({exclusive: ['verbose']}),
verbose: Flags.boolean({char: 'v'}),
}

Expand All @@ -33,7 +31,7 @@ e.g. If you have a user-installed or core plugin that has a 'hello' command, ins

const plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(flags),
verbose: flags.verbose,
})

ux.action.start(`Linking plugin ${chalk.cyan(args.path)}`)
Expand Down
4 changes: 1 addition & 3 deletions src/commands/plugins/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import {Args, Command, Flags, ux} from '@oclif/core'
import chalk from 'chalk'

import {determineLogLevel, npmLogLevelFlag} from '../../log-level.js'
import Plugins from '../../plugins.js'

function removeTags(plugin: string): string {
Expand Down Expand Up @@ -33,7 +32,6 @@ export default class PluginsUninstall extends Command {

static flags = {
help: Flags.help({char: 'h'}),
'npm-log-level': npmLogLevelFlag({exclusive: ['verbose']}),
verbose: Flags.boolean({char: 'v'}),
}

Expand All @@ -46,7 +44,7 @@ export default class PluginsUninstall extends Command {

const plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(flags),
verbose: flags.verbose,
})

if (argv.length === 0) argv.push('.')
Expand Down
4 changes: 1 addition & 3 deletions src/commands/plugins/update.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import {Command, Flags, ux} from '@oclif/core'

import {determineLogLevel, npmLogLevelFlag} from '../../log-level.js'
import Plugins from '../../plugins.js'

export default class PluginsUpdate extends Command {
static description = 'Update installed plugins.'

static flags = {
help: Flags.help({char: 'h'}),
'npm-log-level': npmLogLevelFlag({exclusive: ['verbose']}),
verbose: Flags.boolean({char: 'v'}),
}

Expand All @@ -17,7 +15,7 @@ export default class PluginsUpdate extends Command {

const plugins = new Plugins({
config: this.config,
logLevel: determineLogLevel(flags),
verbose: flags.verbose,
})

ux.action.start(`${this.config.name}: Updating plugins`)
Expand Down
23 changes: 0 additions & 23 deletions src/log-level.ts

This file was deleted.

34 changes: 16 additions & 18 deletions src/npm.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
import {Errors, Interfaces} from '@oclif/core'
import {Errors, Interfaces, ux} from '@oclif/core'
import makeDebug from 'debug'
import {fork as cpFork} from 'node:child_process'
import {readFile} from 'node:fs/promises'
import {createRequire} from 'node:module'
import {join, sep} from 'node:path'
import {npmRunPathEnv} from 'npm-run-path'

import {LogLevel} from './log-level.js'

const debug = makeDebug('@oclif/plugin-plugins:npm')

type ExecOptions = {
cwd: string
silent?: boolean
verbose?: boolean
}

type InstallOptions = ExecOptions & {
prod?: boolean
}

async function fork(modulePath: string, args: string[] = [], {cwd, silent}: ExecOptions): Promise<void> {
async function fork(modulePath: string, args: string[] = [], {cwd, verbose}: ExecOptions): Promise<void> {
return new Promise((resolve, reject) => {
const forked = cpFork(modulePath, args, {
cwd,
Expand All @@ -42,12 +40,14 @@ async function fork(modulePath: string, args: string[] = [], {cwd, silent}: Exec

forked.stderr?.setEncoding('utf8')
forked.stderr?.on('data', (d: Buffer) => {
if (!silent) process.stderr.write(d)
if (verbose) ux.logToStderr(d.toString())
else debug(d.toString().trimEnd())
})

forked.stdout?.setEncoding('utf8')
forked.stdout?.on('data', (d) => {
if (!silent) process.stdout.write(d)
forked.stdout?.on('data', (d: Buffer) => {
if (verbose) ux.log(d.toString())
else debug(d.toString().trimEnd())
})

forked.on('error', reject)
Expand All @@ -57,9 +57,7 @@ async function fork(modulePath: string, args: string[] = [], {cwd, silent}: Exec
} else {
reject(
new Errors.CLIError(`${modulePath} ${args.join(' ')} exited with code ${code}`, {
suggestions: [
'Try running with DEBUG=@oclif/plugin-plugins* and --npm-log-level=verbose to see debug output.',
],
suggestions: ['Run with DEBUG=@oclif/plugin-plugins* to see debug output.'],
}),
)
}
Expand All @@ -70,21 +68,21 @@ async function fork(modulePath: string, args: string[] = [], {cwd, silent}: Exec
export class NPM {
private bin: string | undefined
private config: Interfaces.Config
private logLevel: LogLevel | undefined
private verbose: boolean | undefined

public constructor({config, logLevel}: {config: Interfaces.Config; logLevel?: LogLevel}) {
public constructor({config, verbose}: {config: Interfaces.Config; verbose?: boolean}) {
this.config = config
this.logLevel = logLevel
this.verbose = verbose
}

async exec(args: string[] = [], options: ExecOptions): Promise<void> {
const bin = await this.findNpm()
debug('npm binary path', bin)
if (this.logLevel) args.push(`--loglevel=${this.logLevel}`)
if (this.verbose) args.push('--loglevel=verbose')
if (this.config.npmRegistry) args.push(`--registry=${this.config.npmRegistry}`)

if (this.logLevel && this.logLevel !== 'silent') {
process.stderr.write(`${options.cwd}: ${bin} ${args.join(' ')}\n`)
if (this.verbose) {
ux.logToStderr(`${options.cwd}: ${bin} ${args.join(' ')}`)
}

debug(`${options.cwd}: ${bin} ${args.join(' ')}`)
Expand All @@ -111,7 +109,7 @@ export class NPM {
}

async view(args: string[], opts: ExecOptions): Promise<void> {
await this.exec(['view', ...args], {...opts, silent: this.logLevel === 'silent'})
await this.exec(['view', ...args], {...opts, verbose: this.verbose})
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {access, mkdir, readFile, rm, writeFile} from 'node:fs/promises'
import {dirname, join, resolve} from 'node:path'
import {gt, valid, validRange} from 'semver'

import {LogLevel} from './log-level.js'
import {NPM} from './npm.js'
import {uniqWith} from './util.js'

Expand Down Expand Up @@ -48,7 +47,7 @@ export default class Plugins {

private readonly debug: ReturnType<typeof makeDebug>

constructor(options: {config: Interfaces.Config; logLevel?: LogLevel}) {
constructor(options: {config: Interfaces.Config; verbose?: boolean}) {
this.config = options.config
this.debug = makeDebug('@oclif/plugin-plugins')
this.npm = new NPM(options)
Expand Down
Loading

0 comments on commit d1c9d74

Please sign in to comment.