feat: dlx should support the --silent option (#3839)

ref #3687
This commit is contained in:
Zoltan Kochan
2021-10-10 02:21:42 +03:00
committed by GitHub
parent c83488d01f
commit 1efaaf706b
6 changed files with 54 additions and 26 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": minor
"pnpm": minor
---
`pnpm dlx` supports the `--silent` option.

View File

@@ -0,0 +1,5 @@
---
"@pnpm/common-cli-options-help": patch
---
`OUTPUT_OPTIONS` added.

View File

@@ -115,3 +115,27 @@ export const FILTERING = {
],
title: 'Filtering options (run the command only on packages that satisfy at least one of the selectors)',
}
export const OUTPUT_OPTIONS = {
title: 'Output',
list: [
{
description: 'No output is logged to the console, except fatal errors',
name: '--silent, --reporter silent',
shortAlias: '-s',
},
{
description: 'The default reporter when the stdout is TTY',
name: '--reporter default',
},
{
description: 'The output is always appended to the end. No cursor manipulations are performed',
name: '--reporter append-only',
},
{
description: 'The most verbose reporter. Prints all logs in ndjson format',
name: '--reporter ndjson',
},
],
}

View File

@@ -1,5 +1,5 @@
import { docsUrl } from '@pnpm/cli-utils'
import { FILTERING, OPTIONS, UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { FILTERING, OPTIONS, OUTPUT_OPTIONS, UNIVERSAL_OPTIONS } from '@pnpm/common-cli-options-help'
import { Config, types as allTypes } from '@pnpm/config'
import { WANTED_LOCKFILE } from '@pnpm/constants'
import { CreateStoreControllerOptions } from '@pnpm/store-connection-manager'
@@ -207,29 +207,7 @@ by any dependencies, so it is an emulation of a flat node_modules',
...UNIVERSAL_OPTIONS,
],
},
{
title: 'Output',
list: [
{
description: 'No output is logged to the console, except fatal errors',
name: '--silent, --reporter silent',
shortAlias: '-s',
},
{
description: 'The default reporter when the stdout is TTY',
name: '--reporter default',
},
{
description: 'The output is always appended to the end. No cursor manipulations are performed',
name: '--reporter append-only',
},
{
description: 'The most verbose reporter. Prints all logs in ndjson format',
name: '--reporter ndjson',
},
],
},
OUTPUT_OPTIONS,
FILTERING,
{
title: 'Experimental options',

View File

@@ -2,6 +2,8 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import { docsUrl } from '@pnpm/cli-utils'
import { OUTPUT_OPTIONS } from '@pnpm/common-cli-options-help'
import { Config } from '@pnpm/config'
import rimraf from '@zkochan/rimraf'
import execa from 'execa'
import PATH from 'path-name'
@@ -31,6 +33,7 @@ export function help () {
},
],
},
OUTPUT_OPTIONS,
],
url: docsUrl('dlx'),
usages: ['pnpm dlx <command> [args...]'],
@@ -40,7 +43,7 @@ export function help () {
export async function handler (
opts: {
package?: string[]
},
} & Pick<Config, 'reporter'>,
params: string[]
) {
const prefix = path.join(fs.realpathSync(os.tmpdir()), `dlx-${process.pid.toString()}`)
@@ -58,7 +61,11 @@ export async function handler (
})
await rimraf(bins)
const pkgs = opts.package ?? params.slice(0, 1)
await execa('pnpm', ['add', ...pkgs, '--global', '--global-dir', prefix, '--dir', prefix], {
const pnpmArgs = ['add', ...pkgs, '--global', '--global-dir', prefix, '--dir', prefix]
if (opts.reporter) {
pnpmArgs.push(`--reporter=${opts.reporter}`)
}
await execa('pnpm', pnpmArgs, {
stdio: 'inherit',
})
await execa(params[0], params.slice(1), {

View File

@@ -77,3 +77,11 @@ test('silent run only prints the output of the child process', async () => {
expect(result.stdout.toString().trim()).toBe('hi')
})
test('silent dlx prints the output of the child process only', async () => {
prepare({})
const result = execPnpmSync(['--silent', 'dlx', 'shx', 'echo', 'hi'])
expect(result.stdout.toString().trim()).toBe('hi')
})