Skip to content

Commit

Permalink
feat(default-reporter): a new option added (#6373)
Browse files Browse the repository at this point in the history
In order to filter out packages from the installation summary,
a filter function may be passed to the reporter: filterPkgsDiff.

This feature is only useful for Bit CLI currently.
Related PR: teambit/bit#7176
  • Loading branch information
zkochan committed Apr 9, 2023
1 parent d52c6d7 commit 6cfaf31
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-balloons-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@pnpm/default-reporter": minor
---

In order to filter out packages from the installation summary, a filter function may be passed to the reporter: filterPkgsDiff.
4 changes: 4 additions & 0 deletions cli/default-reporter/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { mergeOutputs } from './mergeOutputs'
import { reporterForClient } from './reporterForClient'
import { formatWarn } from './reporterForClient/utils/formatWarn'
import { reporterForServer } from './reporterForServer'
import { type FilterPkgsDiff } from './reporterForClient/reportSummary'

export { formatWarn }

Expand All @@ -30,6 +31,7 @@ export function initDefaultReporter (
env?: NodeJS.ProcessEnv
process?: NodeJS.Process
}
filterPkgsDiff?: FilterPkgsDiff
}
): () => void {
if (opts.context.argv[0] === 'server') {
Expand Down Expand Up @@ -108,6 +110,7 @@ export function toOutput$ (
env?: NodeJS.ProcessEnv
process?: NodeJS.Process
}
filterPkgsDiff?: FilterPkgsDiff
}
): Rx.Observable<string> {
opts = opts || {}
Expand Down Expand Up @@ -247,6 +250,7 @@ export function toOutput$ (
cmd: opts.context.argv[0],
config: opts.context.config,
env: opts.context.env ?? process.env,
filterPkgsDiff: opts.filterPkgsDiff,
process: opts.context.process ?? process,
isRecursive: opts.context.config?.['recursive'] === true,
logLevel: opts.reportingOptions?.logLevel,
Expand Down
4 changes: 3 additions & 1 deletion cli/default-reporter/src/reporterForClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { reportRequestRetry } from './reportRequestRetry'
import { reportScope } from './reportScope'
import { reportSkippedOptionalDependencies } from './reportSkippedOptionalDependencies'
import { reportStats } from './reportStats'
import { reportSummary } from './reportSummary'
import { reportSummary, type FilterPkgsDiff } from './reportSummary'
import { reportUpdateCheck } from './reportUpdateCheck'

const PRINT_EXECUTION_TIME_IN_COMMANDS = {
Expand Down Expand Up @@ -57,6 +57,7 @@ export function reporterForClient (
cmd: string
config?: Config
env: NodeJS.ProcessEnv
filterPkgsDiff?: FilterPkgsDiff
process: NodeJS.Process
isRecursive: boolean
logLevel?: LogLevel
Expand Down Expand Up @@ -139,6 +140,7 @@ export function reporterForClient (
outputs.push(reportSummary(log$, {
cwd,
env: opts.env,
filterPkgsDiff: opts.filterPkgsDiff,
pnpmConfig: opts.pnpmConfig,
}))
}
Expand Down
16 changes: 13 additions & 3 deletions cli/default-reporter/src/reporterForClient/reportSummary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@ export function reportSummary (
opts: {
cwd: string
env: NodeJS.ProcessEnv
filterPkgsDiff?: FilterPkgsDiff
pnpmConfig?: Config
}
) {
const pkgsDiff$ = getPkgsDiff(log$, { prefix: opts.cwd })

const summaryLog$ = log$.summary.pipe(take(1))
const _printDiffs = printDiffs.bind(null, { prefix: opts.cwd, filterPkgsDiff: opts.filterPkgsDiff })

return Rx.combineLatest(
pkgsDiff$,
Expand All @@ -62,7 +64,7 @@ export function reportSummary (
msg += chalk.cyanBright(`${propertyByDependencyType[depType] as string}:`)
}
msg += EOL
msg += printDiffs(diffs, { prefix: opts.cwd })
msg += _printDiffs(diffs)
msg += EOL
} else if (opts.pnpmConfig?.[CONFIG_BY_DEP_TYPE[depType]] === false) {
msg += EOL
Expand All @@ -78,12 +80,20 @@ export function reportSummary (
)
}

export type FilterPkgsDiff = (pkgsDiff: PackageDiff) => boolean

function printDiffs (
pkgsDiff: PackageDiff[],
opts: {
prefix: string
}
filterPkgsDiff?: FilterPkgsDiff
},
pkgsDiff: PackageDiff[]
) {
// This filtering is only used by Bit CLI currently.
// Related PR: https://github.com/teambit/bit/pull/7176
if (opts.filterPkgsDiff) {
pkgsDiff = pkgsDiff.filter((pkgDiff) => opts.filterPkgsDiff!(pkgDiff))
}
// Sorts by alphabet then by removed/added
// + ava 0.10.0
// - chalk 1.0.0
Expand Down
64 changes: 63 additions & 1 deletion cli/default-reporter/test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const h1 = chalk.cyanBright

const EOL = '\n'

test.only('prints summary (of current package only)', (done) => {
test('prints summary (of current package only)', (done) => {
const prefix = '/home/jane/project'
const output$ = toOutput$({
context: {
Expand Down Expand Up @@ -230,6 +230,68 @@ ${ADD} is-linked2 ${chalk.grey(`<- ${path.relative(prefix, '/src/is-linked2')}`)
})
})

test('prints summary without the filtered out entries', (done) => {
const prefix = '/home/jane/project'
const output$ = toOutput$({
context: {
argv: ['install'],
config: {
dir: prefix,
} as Config,
},
streamParser: createStreamParser(),
filterPkgsDiff: (diff) => diff.name !== 'bar',
})

rootLogger.debug({
added: {
dependencyType: 'prod',
id: 'registry.npmjs.org/foo/1.0.0',
latest: '2.0.0',
name: 'foo',
realName: 'foo',
version: '1.0.0',
},
prefix,
})
rootLogger.debug({
added: {
dependencyType: 'prod',
id: 'registry.npmjs.org/bar/2.0.0',
latest: '1.0.0', // this won't be printed in summary because latest is less than current version
name: 'bar',
realName: 'bar',
version: '2.0.0',
},
prefix,
})
packageManifestLogger.debug({
prefix,
updated: {
dependencies: {
'is-negative': '^1.0.0',
},
devDependencies: {
'is-13': '^1.0.0',
},
},
})
summaryLogger.debug({ prefix })

expect.assertions(1)

output$.pipe(take(1), map(normalizeNewline)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(output).toBe(EOL + `\
${h1('dependencies:')}
${ADD} foo ${versionColor('1.0.0')} ${versionColor('(2.0.0 is available)')}
`)
},
})
})

test('does not print deprecation message when log level is set to error', (done) => {
const prefix = '/home/jane/project'
const output$ = toOutput$({
Expand Down

0 comments on commit 6cfaf31

Please sign in to comment.