Skip to content

Commit

Permalink
feat: add --silent flag to install
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 18, 2023
1 parent 699e926 commit dbdb4df
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
14 changes: 12 additions & 2 deletions src/commands/plugins/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins
return input
},
}),
verbose: Flags.boolean({char: 'v'}),
silent: Flags.boolean({
char: 's',
description: 'Silences yarn output.',
exclusive: ['verbose'],
}),
verbose: Flags.boolean({
char: 'v',
description: 'Show verbose yarn output.',
exclusive: ['silent'],
}),
}

static strict = false
Expand Down Expand Up @@ -124,7 +133,8 @@ e.g. If you have a core plugin that has a 'hello' command, installing a user-ins
async run(): Promise<void> {
const {argv, flags} = await this.parse(PluginsInstall)
this.flags = flags
if (flags.verbose) this.plugins.verbose = true
if (flags.verbose && !flags.silent) this.plugins.verbose = true
if (flags.silent && !flags.verbose) this.plugins.silent = true
const aliases = this.config.pjson.oclif.aliases || {}
for (let name of argv as string[]) {
if (aliases[name] === null) this.error(`${name} is blocked`)
Expand Down
10 changes: 7 additions & 3 deletions src/plugins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async function fileExists(filePath: string): Promise<boolean> {
}

export default class Plugins {
silent = false
verbose = false

readonly yarn: Yarn
Expand Down Expand Up @@ -78,7 +79,7 @@ export default class Plugins {
async install(name: string, {force = false, tag = 'latest'} = {}): Promise<Interfaces.Config> {
try {
this.debug(`installing plugin ${name}`)
const yarnOpts = {cwd: this.config.dataDir, verbose: this.verbose}
const yarnOpts = {cwd: this.config.dataDir, silent: this.silent, verbose: this.verbose}
await this.createPJSON()
let plugin
const add = force ? ['add', '--force'] : ['add']
Expand All @@ -104,7 +105,7 @@ export default class Plugins {
// CJS plugins can be auto-transpiled at runtime but ESM plugins
// cannot. To support ESM plugins we need to compile them after
// installing them.
await this.yarn.exec(['run', 'tsc'], {cwd: plugin.root, verbose: this.verbose})
await this.yarn.exec(['run', 'tsc'], {...yarnOpts, cwd: plugin.root})
} catch (error) {
this.debug(error)
}
Expand Down Expand Up @@ -210,6 +211,7 @@ export default class Plugins {
const doRefresh = async (root: string) => {
await this.yarn.exec(options.prod ? ['--prod'] : [], {
cwd: root,
silent: this.silent,
verbose: this.verbose,
})
}
Expand Down Expand Up @@ -265,6 +267,7 @@ export default class Plugins {
if ((pjson.oclif.plugins ?? []).some((p) => typeof p === 'object' && p.type === 'user' && p.name === name)) {
await this.yarn.exec(['remove', name], {
cwd: this.config.dataDir,
silent: this.silent,
verbose: this.verbose,
})
}
Expand Down Expand Up @@ -296,6 +299,7 @@ export default class Plugins {
if (plugins.some((p) => Boolean(p.url))) {
await this.yarn.exec(['upgrade'], {
cwd: this.config.dataDir,
silent: this.silent,
verbose: this.verbose,
})
}
Expand Down Expand Up @@ -323,7 +327,7 @@ export default class Plugins {
return `${p.name}@${tag}`
}),
],
{cwd: this.config.dataDir, verbose: this.verbose},
{cwd: this.config.dataDir, silent: this.silent, verbose: this.verbose},
)
}

Expand Down
11 changes: 7 additions & 4 deletions src/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export default class Yarn {
return require.resolve('yarn/bin/yarn.js')
}

async exec(args: string[] = [], opts: {cwd: string; verbose: boolean}): Promise<void> {
const {cwd, verbose} = opts
async exec(args: string[] = [], opts: {cwd: string; silent: boolean; verbose: boolean}): Promise<void> {
const {cwd, silent, verbose} = opts
if (args[0] !== 'run') {
// https://classic.yarnpkg.com/lang/en/docs/cli/#toc-concurrency-and-mutex
// Default port is: 31997
Expand All @@ -36,7 +36,8 @@ export default class Yarn {
const networkTimeout = this.config.scopedEnvVar('NETWORK_TIMEOUT')
if (networkTimeout) args.push(`--network-timeout=${networkTimeout}`)

if (verbose) args.push('--verbose')
if (verbose && !silent) args.push('--verbose')
if (silent && !verbose) args.push('--silent')

if (this.config.npmRegistry) args.push(`--registry=${this.config.npmRegistry}`)
}
Expand Down Expand Up @@ -82,7 +83,9 @@ export default class Yarn {
fork(modulePath: string, args: string[] = [], options: Record<string, unknown> = {}): Promise<void> {
return new Promise((resolve, reject) => {
const forked = fork(modulePath, args, options)
forked.stderr?.on('data', (d) => process.stderr.write(d))
forked.stderr?.on('data', (d) => {
if (!options.silent) process.stderr.write(d)
})
forked.stdout?.setEncoding('utf8')
forked.stdout?.on('data', (d) => {
if (options.verbose) process.stdout.write(d)
Expand Down

0 comments on commit dbdb4df

Please sign in to comment.