fix: run --silent

close #2660
PR #2665
This commit is contained in:
Zoltan Kochan
2020-07-05 01:40:42 +03:00
committed by GitHub
parent 88ba50bbd5
commit 76aaead321
14 changed files with 52 additions and 23 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/lifecycle": minor
---
Added an option for silent execution: opts.silent.

View File

@@ -0,0 +1,6 @@
---
"@pnpm/plugin-commands-script-runners": patch
"pnpm": patch
---
`run --silent <cmd>` should only print output of the command and nothing from pnpm.

View File

@@ -112,7 +112,7 @@ export interface HeadlessOptions {
export default async (opts: HeadlessOptions) => {
const reporter = opts.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
@@ -358,7 +358,7 @@ export default async (opts: HeadlessOptions) => {
)
}
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
}

View File

@@ -12,6 +12,7 @@ export type RunLifecycleHookOptions = {
pkgRoot: string,
rawConfig: object,
rootModulesDir: string,
silent?: boolean,
stdio?: string,
unsafePerm: boolean,
}
@@ -44,7 +45,9 @@ export default async function runLifecycleHook (
wd: opts.pkgRoot,
})
}
return lifecycle(m, stage, opts.pkgRoot, {
const logLevel = (opts.stdio !== 'inherit' || opts.silent)
? 'silent' : undefined
await lifecycle(m, stage, opts.pkgRoot, {
config: opts.rawConfig,
dir: opts.rootModulesDir,
extraBinPaths: opts.extraBinPaths || [],
@@ -52,7 +55,7 @@ export default async function runLifecycleHook (
log: {
clearProgress: noop,
info: noop,
level: opts.stdio === 'inherit' ? undefined : 'silent',
level: logLevel,
pause: noop,
resume: noop,
showProgress: noop,

View File

@@ -84,7 +84,7 @@ export async function rebuildPkgs (
maybeOpts: RebuildOptions
) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
@@ -129,7 +129,7 @@ export async function rebuild (
maybeOpts: RebuildOptions
) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)

View File

@@ -99,7 +99,7 @@ For options that may be used with \`-r\`, see "pnpm help recursive"`,
export type RunOpts =
& Omit<RecursiveRunOpts, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>
& { recursive?: boolean }
& Pick<Config, 'dir' | 'engineStrict'>
& Pick<Config, 'dir' | 'engineStrict' | 'reporter'>
& (
& { recursive?: false }
& Partial<Pick<Config, 'allProjects' | 'selectedProjectsGraph' | 'workspaceDir'>>
@@ -136,6 +136,7 @@ export async function handler (
pkgRoot: dir,
rawConfig: opts.rawConfig,
rootModulesDir: await realpathMissing(path.join(dir, 'node_modules')),
silent: opts.reporter === 'silent',
stdio: 'inherit',
unsafePerm: true, // when running scripts explicitly, assume that they're trusted.
}

View File

@@ -17,7 +17,7 @@ export default async function (
}
) {
const reporter = opts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
@@ -45,7 +45,7 @@ export default async function (
}
}))
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}

View File

@@ -9,13 +9,13 @@ export default async function (
}
) {
const reporter = opts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
await opts.storeController.prune()
await opts.storeController.close()
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
}

View File

@@ -14,7 +14,7 @@ import extendOptions, {
export default async function (maybeOpts: StoreStatusOptions) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
@@ -51,7 +51,7 @@ export default async function (maybeOpts: StoreStatusOptions) {
return (await dint.check(path.join(virtualStoreDir, pkgIdToFilename(depPath, opts.dir), 'node_modules', name), files)) === false
})
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}

View File

@@ -5,6 +5,9 @@ export default function err (error: Error) {
console.log(error)
process.exit(1)
}
if (global['reporterInitialized'] === 'silent') {
process.exit(1)
}
// bole passes only the name, message and stack of an error
// that is why we pass error as a message as well, to pass

View File

@@ -123,8 +123,7 @@ export default async function run (inputArgv: string[]) {
cmd,
config,
})
global['reporterInitialized'] = true
delete config.reporter // This is a silly workaround because supi expects a function as config.reporter
global['reporterInitialized'] = reporterType
if (selfUpdate) {
await pnpmCmds.server(config as any, ['stop']) // tslint:disable-line:no-any

View File

@@ -75,3 +75,15 @@ test('install-test: install dependencies and runs tests', async (t: tape.Test) =
'posttest',
])
})
test('silent run only prints the output of the child process', async (t: tape.Test) => {
prepare(t, {
scripts: {
hi: 'echo hi && exit 1',
},
})
const result = execPnpmSync(['run', '--silent', 'hi'])
t.ok(result.stdout.toString().trim() === 'hi')
})

View File

@@ -130,7 +130,7 @@ export async function mutateModules (
}
) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
@@ -152,7 +152,7 @@ export async function mutateModules (
const result = await _install()
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}

View File

@@ -42,7 +42,7 @@ export default async function link (
}
) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
@@ -164,7 +164,7 @@ export default async function link (
summaryLogger.debug({ prefix: opts.dir })
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
@@ -208,7 +208,7 @@ export async function linkFromGlobal (
maybeOpts: LinkOptions & {globalDir: string}
) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
const opts = await extendOptions(maybeOpts)
@@ -216,7 +216,7 @@ export async function linkFromGlobal (
const linkFromPkgs = pkgNames.map((pkgName) => path.join(globalPkgPath, 'node_modules', pkgName))
const newManifest = await link(linkFromPkgs, path.join(linkTo, 'node_modules'), opts)
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}
@@ -231,7 +231,7 @@ export async function linkToGlobal (
}
) {
const reporter = maybeOpts?.reporter
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.on('data', reporter)
}
maybeOpts.lockfileDir = maybeOpts.lockfileDir || maybeOpts.globalDir
@@ -243,7 +243,7 @@ export async function linkToGlobal (
linkToBin: maybeOpts.globalBin,
})
if (reporter) {
if (reporter && typeof reporter === 'function') {
streamParser.removeListener('data', reporter)
}