|
| 1 | +/* @flow */ |
| 2 | + |
| 3 | +import type {Reporter} from '../../reporters/index.js'; |
| 4 | +import type Config from '../../config.js'; |
| 5 | +import inquirer from 'inquirer'; |
| 6 | +import repeat from 'repeating'; |
| 7 | +import {MessageError} from '../../errors.js'; |
| 8 | +import PackageRequest from '../../package-request.js'; |
| 9 | +import {Add} from './add.js'; |
| 10 | +import {Install} from './install.js'; |
| 11 | +import Lockfile from '../../lockfile/wrapper.js'; |
| 12 | + |
| 13 | +export const requireLockfile = true; |
| 14 | + |
| 15 | +export function setFlags(commander: Object) { |
| 16 | + // TODO: support some flags that install command has |
| 17 | + commander.usage('update'); |
| 18 | +} |
| 19 | + |
| 20 | +type Dependency = { |
| 21 | + name: string, |
| 22 | + current: string, |
| 23 | + wanted: string, |
| 24 | + latest: string, |
| 25 | + hint: ?string |
| 26 | +}; |
| 27 | + |
| 28 | +type InquirerResponses<K, T> = {[key: K]: Array<T>}; |
| 29 | + |
| 30 | +// Prompt user with Inquirer |
| 31 | +async function prompt(choices): Promise<Array<Dependency>> { |
| 32 | + const answers: InquirerResponses<'packages', Dependency> = await inquirer.prompt([{ |
| 33 | + name: 'packages', |
| 34 | + type: 'checkbox', |
| 35 | + message: 'Choose which packages to update.', |
| 36 | + choices, |
| 37 | + // Couldn't make it work, I guess I'm missing something here |
| 38 | + // $FlowFixMe: https://github.com/facebook/flow/blob/f41e66e27b227235750792c34f5a80f38bde6320/lib/node.js#L1197 |
| 39 | + pageSize: process.stdout.rows - 2, |
| 40 | + validate: (answer) => !!answer.length || 'You must choose at least one package.', |
| 41 | + }]); |
| 42 | + return answers.packages; |
| 43 | +} |
| 44 | + |
| 45 | +export async function run( |
| 46 | + config: Config, |
| 47 | + reporter: Reporter, |
| 48 | + flags: Object, |
| 49 | + args: Array<string>, |
| 50 | +): Promise<void> { |
| 51 | + const lockfile = await Lockfile.fromDirectory(config.cwd); |
| 52 | + const install = new Install(flags, config, reporter, lockfile); |
| 53 | + const [deps] = await install.fetchRequestFromCwd(); |
| 54 | + |
| 55 | + const allDeps = (await Promise.all(deps.map(async ({pattern, hint}): Promise<Dependency> => { |
| 56 | + const locked = lockfile.getLocked(pattern); |
| 57 | + if (!locked) { |
| 58 | + throw new MessageError(reporter.lang('lockfileOutdated')); |
| 59 | + } |
| 60 | + |
| 61 | + const {name, version: current} = locked; |
| 62 | + let latest = ''; |
| 63 | + let wanted = ''; |
| 64 | + |
| 65 | + const normalized = PackageRequest.normalizePattern(pattern); |
| 66 | + |
| 67 | + if (PackageRequest.getExoticResolver(pattern) || |
| 68 | + PackageRequest.getExoticResolver(normalized.range)) { |
| 69 | + latest = wanted = 'exotic'; |
| 70 | + } else { |
| 71 | + ({latest, wanted} = await config.registries[locked.registry].checkOutdated(config, name, normalized.range)); |
| 72 | + } |
| 73 | + |
| 74 | + return ({name, current, wanted, latest, hint}); |
| 75 | + }))); |
| 76 | + |
| 77 | + const isDepOld = ({latest, current}) => latest !== current; |
| 78 | + const isDepExpected = ({current, wanted}) => current === wanted; |
| 79 | + |
| 80 | + const outdatedDeps = allDeps |
| 81 | + .filter(isDepOld) |
| 82 | + .sort((depA, depB) => { |
| 83 | + if (isDepExpected(depA) && !isDepExpected(depB)) { |
| 84 | + return 1; |
| 85 | + } |
| 86 | + return -1; |
| 87 | + }); |
| 88 | + |
| 89 | + const getNameFromHint = (hint) => hint ? `${hint}Dependencies` : 'dependencies'; |
| 90 | + |
| 91 | + const maxLengthArr = {name: 0, current: 0, latest: 0}; |
| 92 | + outdatedDeps.forEach((dep) => |
| 93 | + ['name', 'current', 'latest'].forEach((key) => { |
| 94 | + maxLengthArr[key] = Math.max(maxLengthArr[key], dep[key].length); |
| 95 | + }), |
| 96 | + ); |
| 97 | + |
| 98 | + // Depends on maxLengthArr |
| 99 | + const addPadding = (dep) => (key) => |
| 100 | + `${dep[key]}${repeat(' ', maxLengthArr[key] - dep[key].length)}`; |
| 101 | + |
| 102 | + const colorizeName = ({current, wanted}) => |
| 103 | + (current === wanted) ? reporter.format.yellow : reporter.format.red; |
| 104 | + |
| 105 | + const makeRow = (dep) => { |
| 106 | + const padding = addPadding(dep); |
| 107 | + const name = colorizeName(dep)(padding('name')); |
| 108 | + const current = reporter.format.blue(padding('current')); |
| 109 | + const latest = reporter.format.green(padding('latest')); |
| 110 | + return `${name} ${current} ❯ ${latest}`; |
| 111 | + }; |
| 112 | + |
| 113 | + const groupedDeps = outdatedDeps.reduce((acc, dep) => { |
| 114 | + const {hint, name, latest} = dep; |
| 115 | + const key = getNameFromHint(hint); |
| 116 | + const xs = acc[key] || []; |
| 117 | + acc[key] = xs.concat({ |
| 118 | + name: makeRow(dep), |
| 119 | + value: dep, |
| 120 | + short: `${name}@${latest}`, |
| 121 | + }); |
| 122 | + return acc; |
| 123 | + }, {}); |
| 124 | + |
| 125 | + const flatten = (xs) => xs.reduce( |
| 126 | + (ys, y) => ys.concat(Array.isArray(y) ? flatten(y) : y), [], |
| 127 | + ); |
| 128 | + |
| 129 | + const choices = Object.keys(groupedDeps).map((key) => [ |
| 130 | + new inquirer.Separator(reporter.format.bold.underline.green(key)), |
| 131 | + groupedDeps[key], |
| 132 | + new inquirer.Separator(' '), |
| 133 | + ]); |
| 134 | + |
| 135 | + const answers = await prompt(flatten(choices)); |
| 136 | + |
| 137 | + const getName = ({name}) => name; |
| 138 | + const isHint = (x) => ({hint}) => hint === x; |
| 139 | + |
| 140 | + await [null, 'dev', 'optional', 'peer'].reduce(async (promise, hint) => { |
| 141 | + // Wait for previous promise to resolve |
| 142 | + await promise; |
| 143 | + // Reset dependency flags |
| 144 | + flags.dev = hint === 'dev'; |
| 145 | + flags.peer = hint === 'peer'; |
| 146 | + flags.optional = hint === 'optional'; |
| 147 | + |
| 148 | + const deps = answers.filter(isHint(hint)).map(getName); |
| 149 | + if (deps.length) { |
| 150 | + reporter.info(reporter.lang('updateInstalling', getNameFromHint(hint))); |
| 151 | + const add = new Add(deps, flags, config, reporter, lockfile); |
| 152 | + return await add.init(); |
| 153 | + } |
| 154 | + return Promise.resolve(); |
| 155 | + }, Promise.resolve()); |
| 156 | +} |
0 commit comments