/
reportUpdateCheck.ts
65 lines (59 loc) · 1.93 KB
/
reportUpdateCheck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { type UpdateCheckLog } from '@pnpm/core-loggers'
import boxen from 'boxen'
import chalk from 'chalk'
import * as Rx from 'rxjs'
import { filter, map, take } from 'rxjs/operators'
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 updateMessage = renderUpdateMessage({
currentPkgIsExecutable: detectIfCurrentPkgIsExecutable(opts.process),
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}
${updateMessage}
Follow ${chalk.magenta('@pnpmjs')} for updates: https://twitter.com/pnpmjs`,
{
padding: 1,
margin: 1,
align: 'center',
borderColor: 'yellow',
borderStyle: 'round',
}
),
})
})
)
}
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 = opts.currentPkgIsExecutable ? '@pnpm/exe' : 'pnpm'
return `pnpm add -g ${pkgName}`
}
function detectIfCurrentPkgIsExecutable (process: NodeJS.Process) {
return process['pkg'] != null
}