Skip to content

Commit

Permalink
fix(cli): suggest to update with Corepack (#4854)
Browse files Browse the repository at this point in the history
When pnpm was installed with Corepack,
suggest to update it with Corepack as well.
  • Loading branch information
zkochan committed Jun 4, 2022
1 parent 5cfd6d0 commit 2493b8e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/few-horses-happen.md
@@ -0,0 +1,6 @@
---
"@pnpm/default-reporter": minor
"pnpm": patch
---

Suggest to update using Corepack when pnpm was installed via Corepack.
2 changes: 1 addition & 1 deletion packages/default-reporter/src/reporterForClient/index.ts
Expand Up @@ -85,7 +85,7 @@ export default function (
reportSkippedOptionalDependencies(log$.skippedOptionalDependency, { cwd }),
reportHooks(log$.hook, { cwd, isRecursive: opts.isRecursive }),
reportContext(log$, { cwd }),
reportUpdateCheck(log$.updateCheck),
reportUpdateCheck(log$.updateCheck, opts),
]

// logLevelNumber: 0123 = error warn info debug
Expand Down
Expand Up @@ -5,26 +5,41 @@ import * as Rx from 'rxjs'
import { filter, map, take } from 'rxjs/operators'
import semver from 'semver'

export default (log$: Rx.Observable<UpdateCheckLog>) => {
const pkgName = process['pkg'] != null ? '@pnpm/exe' : 'pnpm'
export default (log$: Rx.Observable<UpdateCheckLog>, opts: {
env: NodeJS.ProcessEnv
}) => {
return log$.pipe(
take(1),
filter((log) => semver.gt(log.latestVersion, log.currentVersion)),
map((log) => Rx.of({
msg: boxen(`\
map((log) => {
const updateCommand = renderUpdateCommand({
latestVersion: log.latestVersion,
env: opts.env,
})
return Rx.of({
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(`pnpm add -g ${pkgName}`)} to update.
Run "${chalk.magenta(updateCommand)}" to update.
Follow ${chalk.magenta('@pnpmjs')} for updates: https://twitter.com/pnpmjs`,
{
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round',
}
),
}))
{
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round',
}
),
})
})
)
}

function renderUpdateCommand (opts: { env: NodeJS.ProcessEnv, latestVersion: string }) {
if (opts.env.COREPACK_ROOT) {
return `corepack prepare pnpm@${opts.latestVersion} --activate`
}
const pkgName = process['pkg'] != null ? '@pnpm/exe' : 'pnpm'
return `pnpm add -g ${pkgName}`
}
@@ -1,12 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`print update notification for Corepack if the latest version is greater than the current 1`] = `
"
╭──────────────────────────────────────────────────────────────────╮
│ │
│ Update available! 10.0.0 → 11.0.0. │
│ Changelog: https://github.com/pnpm/pnpm/releases/tag/v11.0.0 │
│ Run \\"corepack prepare pnpm@11.0.0 --activate\\" to update. │
│ │
│ Follow @pnpmjs for updates: https://twitter.com/pnpmjs │
│ │
╰──────────────────────────────────────────────────────────────────╯
"
`;

exports[`print update notification if the latest version is greater than the current 1`] = `
"
╭──────────────────────────────────────────────────────────────────╮
│ │
│ Update available! 10.0.0 → 11.0.0. │
│ Changelog: https://github.com/pnpm/pnpm/releases/tag/v11.0.0 │
Run pnpm add -g pnpm to update.
│ Run \\"pnpm add -g pnpm\\" to update. │
│ │
│ Follow @pnpmjs for updates: https://twitter.com/pnpmjs │
│ │
Expand Down
28 changes: 28 additions & 0 deletions packages/default-reporter/test/reportingUpdateCheck.ts
Expand Up @@ -56,3 +56,31 @@ test('print update notification if the latest version is greater than the curren
},
})
})

test('print update notification for Corepack if the latest version is greater than the current', (done) => {
const output$ = toOutput$({
context: {
argv: ['install'],
config: { recursive: true } as Config,
env: {
COREPACK_ROOT: '/usr/bin/corepack',
},
},
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()
},
})
})

0 comments on commit 2493b8e

Please sign in to comment.