cli_command_plugin_upgrade.js

import epilog from '../../lib/epilog.js'
import listPackages from '../../lib/list-packages.js'
import { fatal, __, print } from '../../lib/translate.js'
import isEmpty from 'lodash-es/isEmpty.js'
import ora from 'ora'
import semver from 'semver'
import select from '@inquirer/select'
import { addDependency } from 'nypm'
import { globalScope, registry } from '../../lib/option.js'
import { getFiles } from './list.js'

/**
 * Command definition object for upgrading all installed plugins to the latest version.
 *
 * @memberof module:CLI/Command/Plugin
 * @type {TCommand}
 */
const upgrade = {
  command: 'upgrade',
  aliases: ['u'],
  describe: __('Upgrade all plugins to the latest version'),
  builder (yargs) {
    globalScope(yargs)
    registry(yargs)
    yargs.epilog(epilog)
  },
  async handler (argv) {
    const files = await getFiles(argv, 'plugin')
    argv.onlyUnmatch = true
    argv.npmVersion = true
    const coll = await listPackages(files, 'plugin', argv)
    print('Following plugin(s) will be upgraded to the latest versions:')
    const upgradable = []
    for (const c of coll) {
      if (semver.valid(c.npmVersion) && semver.gt(c.npmVersion, c.version)) {
        ora(__('%s: %s -> %s, %s', c.name, c.version, c.npmVersion, 'OK')).succeed()
        upgradable.push(c)
      } else ora(__('%s: %s -> %s, %s', c.name, c.version, isEmpty(c.npmVersion) ? '???' : c.npmVersion, 'SKIP')).fail()
    }
    if (upgradable.length === 0) fatal('No upgradeable package found, sorry')
    const answer = await select({
      message: __('Continue to upgrade %d package(s)?', upgradable.length),
      choices: [
        { value: 'y', name: __('Yes, continue') },
        { value: 'n', name: __('No, abort') }
      ]
    })
    if (answer === 'n') fatal('Aborted')
    const opts = {
      global: argv.global,
      packageManager: 'npm'
    }
    for (const u of upgradable) {
      await addDependency(`${u.name}@${u.npmVersion}`, opts)
    }
    print('All done!')
  }
}

export default upgrade