fix(reporter): update notifier instructions (#5750)

ref #5747
This commit is contained in:
Zoltan Kochan
2022-12-06 01:49:38 +02:00
committed by GitHub
parent 568dc3ab21
commit 3f644a514d
6 changed files with 79 additions and 4 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/default-reporter": patch
"pnpm": patch
---
The update notifier should suggest using the standalone script, when pnpm was installed using a standalone script.

View File

@@ -28,6 +28,7 @@ export function initDefaultReporter (
argv: string[]
config?: Config
env?: NodeJS.ProcessEnv
process?: NodeJS.Process
}
}
): () => void {
@@ -95,6 +96,7 @@ export function toOutput$ (
argv: string[]
config?: Config
env?: NodeJS.ProcessEnv
process?: NodeJS.Process
}
}
): Rx.Observable<string> {
@@ -235,6 +237,7 @@ export function toOutput$ (
cmd: opts.context.argv[0],
config: opts.context.config,
env: opts.context.env ?? process.env,
process: opts.context.process ?? process,
isRecursive: opts.context.config?.['recursive'] === true,
logLevel: opts.reportingOptions?.logLevel,
pnpmConfig: opts.context.config,

View File

@@ -57,6 +57,7 @@ export function reporterForClient (
cmd: string
config?: Config
env: NodeJS.ProcessEnv
process: NodeJS.Process
isRecursive: boolean
logLevel?: LogLevel
pnpmConfig?: Config

View File

@@ -7,12 +7,14 @@ import semver from 'semver'
export function reportUpdateCheck (log$: Rx.Observable<UpdateCheckLog>, opts: {
env: NodeJS.ProcessEnv
process: NodeJS.Process
}) {
return log$.pipe(
take(1),
filter((log) => semver.gt(log.latestVersion, log.currentVersion)),
map((log) => {
const updateCommand = renderUpdateCommand({
const updateMessage = renderUpdateMessage({
currentPkgIsExecutable: detectIfCurrentPkgIsExecutable(opts.process),
latestVersion: log.latestVersion,
env: opts.env,
})
@@ -20,7 +22,7 @@ export function reportUpdateCheck (log$: Rx.Observable<UpdateCheckLog>, opts: {
msg: boxen(`\
Update available! ${chalk.red(log.currentVersion)}${chalk.green(log.latestVersion)}.
${chalk.magenta('Changelog:')} https://github.com/pnpm/pnpm/releases/tag/v${log.latestVersion}
Run "${chalk.magenta(updateCommand)}" to update.
${updateMessage}
Follow ${chalk.magenta('@pnpmjs')} for updates: https://twitter.com/pnpmjs`,
{
@@ -36,10 +38,28 @@ Follow ${chalk.magenta('@pnpmjs')} for updates: https://twitter.com/pnpmjs`,
)
}
function renderUpdateCommand (opts: { env: NodeJS.ProcessEnv, latestVersion: string }) {
interface UpdateMessageOptions {
currentPkgIsExecutable: boolean
env: NodeJS.ProcessEnv
latestVersion: string
}
function renderUpdateMessage (opts: UpdateMessageOptions) {
if (opts.currentPkgIsExecutable && opts.env.PNPM_HOME) {
return 'Run a script from: https://pnpm.io/installation'
}
const updateCommand = renderUpdateCommand(opts)
return `Run "${chalk.magenta(updateCommand)}" to update.`
}
function renderUpdateCommand (opts: UpdateMessageOptions) {
if (opts.env.COREPACK_ROOT) {
return `corepack prepare pnpm@${opts.latestVersion} --activate`
}
const pkgName = process['pkg'] != null ? '@pnpm/exe' : 'pnpm'
const pkgName = opts.currentPkgIsExecutable ? '@pnpm/exe' : 'pnpm'
return `pnpm add -g ${pkgName}`
}
function detectIfCurrentPkgIsExecutable (process: NodeJS.Process) {
return process['pkg'] != null
}

View File

@@ -27,3 +27,17 @@ exports[`print update notification if the latest version is greater than the cur
╰──────────────────────────────────────────────────────────────────╯
"
`;
exports[`print update notification that suggests to use the standalone scripts for the upgrade 1`] = `
"
╭──────────────────────────────────────────────────────────────────╮
│ │
│ Update available! 10.0.0 → 11.0.0. │
│ Changelog: https://github.com/pnpm/pnpm/releases/tag/v11.0.0 │
│ Run a script from: https://pnpm.io/installation │
│ │
│ Follow @pnpmjs for updates: https://twitter.com/pnpmjs │
│ │
╰──────────────────────────────────────────────────────────────────╯
"
`;

View File

@@ -86,3 +86,34 @@ test('print update notification for Corepack if the latest version is greater th
},
})
})
test('print update notification that suggests to use the standalone scripts for the upgrade', (done) => {
const output$ = toOutput$({
context: {
argv: ['install'],
config: { recursive: true } as Config,
env: {
PNPM_HOME: '/home/user/.local/share/pnpm',
},
process: {
pkg: true,
} as any, // eslint-disable-line
},
streamParser: createStreamParser(),
})
updateCheckLogger.debug({
currentVersion: '10.0.0',
latestVersion: '11.0.0',
})
expect.assertions(1)
output$.pipe(take(1)).subscribe({
complete: () => done(),
error: done,
next: output => {
expect(stripAnsi(output)).toMatchSnapshot()
},
})
})