|
|
|
|
| const ciInfo = require('ci-info')
|
| const gt = require('semver/functions/gt')
|
| const gte = require('semver/functions/gte')
|
| const parse = require('semver/functions/parse')
|
| const { stat, writeFile } = require('node:fs/promises')
|
| const { resolve } = require('node:path')
|
|
|
|
|
| const DAILY = 1000 * 60 * 60 * 24
|
| const WEEKLY = DAILY * 7
|
|
|
|
|
| const lastCheckedFile = npm => resolve(npm.flatOptions.cache, '../_update-notifier-last-checked')
|
|
|
|
|
| const updateCheck = async (npm, spec, version, current) => {
|
| const pacote = require('pacote')
|
|
|
| const mani = await pacote.manifest(`npm@${spec}`, {
|
|
|
| defaultTag: 'latest',
|
| ...npm.flatOptions,
|
| cache: false,
|
| }).catch(() => null)
|
|
|
|
|
| if (!mani) {
|
| return null
|
| }
|
|
|
| const latest = mani.version
|
|
|
|
|
|
|
| if (gt(version, latest) && spec === '*') {
|
| return updateNotifier(npm, `^${version}`)
|
| }
|
|
|
|
|
| if (gte(version, latest)) {
|
| return null
|
| }
|
|
|
| const chalk = npm.logChalk
|
|
|
|
|
|
|
| const update = parse(mani.version)
|
| const type = update.major !== current.major ? 'major'
|
| : update.minor !== current.minor ? 'minor'
|
| : update.patch !== current.patch ? 'patch'
|
| : 'prerelease'
|
| const typec = type === 'major' ? 'red'
|
| : type === 'minor' ? 'yellow'
|
| : 'cyan'
|
| const message = [
|
| '',
|
| `New ${chalk[typec](type)} version of npm available! ${chalk[typec](current)} -> ${chalk.blue(latest)}`,
|
| `Changelog: ${chalk.blue(`https://github.com/npm/cli/releases/tag/v${latest}`)}`,
|
| `To update run: ${chalk.underline(`npm install -g npm@${latest}`)}`,
|
| '',
|
| ].join('\n')
|
|
|
| return message
|
| }
|
|
|
| const updateNotifier = async (npm, spec = '*') => {
|
|
|
|
|
| const { version } = npm
|
| const current = parse(version)
|
|
|
|
|
| if (current.prerelease.length) {
|
| spec = `^${version}`
|
| }
|
|
|
|
|
| const duration = current.prerelease.length ? DAILY : WEEKLY
|
|
|
| const t = new Date(Date.now() - duration)
|
|
|
| const st = await stat(lastCheckedFile(npm)).catch(() => ({ mtime: t - 1 }))
|
|
|
|
|
| if (!(t > st.mtime)) {
|
| return null
|
| }
|
|
|
|
|
|
|
|
|
| writeFile(lastCheckedFile(npm), '').catch(() => {})
|
|
|
| return updateCheck(npm, spec, version, current)
|
| }
|
|
|
| module.exports = async npm => {
|
| if (
|
|
|
| !npm.config.get('update-notifier')
|
|
|
| || (npm.flatOptions.global &&
|
| ['install', 'update'].includes(npm.command) &&
|
| npm.argv.some(arg => /^npm(@|$)/.test(arg)))
|
|
|
| || ciInfo.isCI
|
| ) {
|
| return null
|
| }
|
|
|
| return updateNotifier(npm)
|
| }
|
|
|