|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const versionColumnWidth = 12; |
| 4 | + |
| 5 | +/** |
| 6 | + * Create a CLIUI column to display a version number |
| 7 | + * @private |
| 8 | + * @param {string} text - The header text |
| 9 | + * @returns {object} A CLIUI column |
| 10 | + */ |
| 11 | +function createVersionHeaderColumn(text) { |
| 12 | + return { |
| 13 | + text, |
| 14 | + align: 'right', |
| 15 | + border: true, |
| 16 | + width: versionColumnWidth, |
| 17 | + }; |
| 18 | +} |
| 19 | + |
| 20 | +/** |
| 21 | + * Create a CLIUI column to display a version number |
| 22 | + * @private |
| 23 | + * @param {string|undefined} version - The version number, if one exists |
| 24 | + * @returns {object} A CLIUI column |
| 25 | + */ |
| 26 | +function createVersionColumn(version) { |
| 27 | + return { |
| 28 | + text: version || 'n/a', |
| 29 | + align: 'right', |
| 30 | + padding: [0, 1, 0, 0], |
| 31 | + width: versionColumnWidth, |
| 32 | + }; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Generates a table to be displayed via the CLI which lists the version |
| 37 | + * upgrades available for the packages |
| 38 | + * |
| 39 | + * @param {object[]} packages - The package name, current version, and a Map of |
| 40 | + * available upgrade versions |
| 41 | + * @returns {string} A CLI UI string |
| 42 | + */ |
| 43 | +function formatPackageUpgrades(packages) { |
| 44 | + const maxNameLength = packages.reduce( |
| 45 | + (max, { name }) => Math.max(max, name.length), |
| 46 | + 0 |
| 47 | + ); |
| 48 | + const nameColumnWidth = 1 + maxNameLength + 1; |
| 49 | + |
| 50 | + const ui = require('cliui')(); |
| 51 | + ui.div( |
| 52 | + { |
| 53 | + text: 'Name', |
| 54 | + align: 'left', |
| 55 | + border: true, |
| 56 | + width: nameColumnWidth, |
| 57 | + }, |
| 58 | + createVersionHeaderColumn('Current'), |
| 59 | + createVersionHeaderColumn('Patch'), |
| 60 | + createVersionHeaderColumn('Minor'), |
| 61 | + createVersionHeaderColumn('Major') |
| 62 | + ); |
| 63 | + |
| 64 | + packages.forEach(({ name, version, upgrades }) => |
| 65 | + ui.div( |
| 66 | + { |
| 67 | + text: name, |
| 68 | + align: 'left', |
| 69 | + padding: [0, 0, 0, 1], |
| 70 | + width: nameColumnWidth, |
| 71 | + }, |
| 72 | + createVersionColumn(version), |
| 73 | + createVersionColumn(upgrades.get('patch')), |
| 74 | + createVersionColumn(upgrades.get('minor')), |
| 75 | + createVersionColumn(upgrades.get('major')) |
| 76 | + ) |
| 77 | + ); |
| 78 | + |
| 79 | + return ui.toString(); |
| 80 | +} |
| 81 | + |
| 82 | +module.exports = { |
| 83 | + formatPackageUpgrades, |
| 84 | +}; |
0 commit comments