Skip to content

Commit 12621b9

Browse files
committed
Adjust UI for outdated cmd
1 parent f1d8ca7 commit 12621b9

File tree

5 files changed

+138
-79
lines changed

5 files changed

+138
-79
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ module.exports = {
4949
'func-call-spacing': 'error',
5050
'func-name-matching': 'error',
5151
'func-names': 'error',
52-
'func-style': 'error',
52+
'func-style': 'off',
5353
'function-paren-newline': 'off',
5454
'generator-star-spacing': 'error',
5555
'getter-return': 'error',

gulpfile.js

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ const glob = require('glob');
77
const ejs = require('gulp-ejs');
88
const zip = require('gulp-zip');
99
const mergeStream = require('merge-stream');
10-
const { formatVersionFolder, compareStrings } = require('./utility');
10+
const {
11+
formatVersionFolder,
12+
compareStrings,
13+
formatPackageUpgrades,
14+
} = require('./utility');
1115

1216
const { dependencies } = require('./package.json');
1317

@@ -57,7 +61,6 @@ gulp.task('default', libraryTaskNames);
5761
gulp.task('outdated', () => {
5862
const packageJson = require('package-json');
5963
const semver = require('semver');
60-
const cliui = require('cliui');
6164

6265
const allUpgradesPromises = Object.keys(dependencies).map(name => {
6366
const currentVersion = dependencies[name];
@@ -79,11 +82,12 @@ gulp.task('outdated', () => {
7982

8083
return packageUpgrades.then(upgrades => ({
8184
name,
85+
version: currentVersion,
8286
upgrades,
8387
}));
8488
});
8589

86-
Promise.all(allUpgradesPromises).then(allUpgrades => {
90+
return Promise.all(allUpgradesPromises).then(allUpgrades => {
8791
const validUpgrades = allUpgrades
8892
.filter(({ upgrades }) => upgrades.size > 0)
8993
.sort(({ name: a }, { name: b }) => compareStrings(a, b));
@@ -94,55 +98,7 @@ gulp.task('outdated', () => {
9498
return;
9599
}
96100

97-
const ui = cliui();
98-
ui.div(
99-
{
100-
text: 'Name',
101-
align: 'left',
102-
border: true,
103-
},
104-
{
105-
text: 'Patch',
106-
align: 'right',
107-
border: true,
108-
},
109-
{
110-
text: 'Minor',
111-
align: 'right',
112-
border: true,
113-
},
114-
{
115-
text: 'Major',
116-
align: 'right',
117-
border: true,
118-
}
119-
);
120-
validUpgrades.forEach(({ name, upgrades }) =>
121-
ui.div(
122-
{
123-
text: name,
124-
align: 'left',
125-
padding: [0, 0, 0, 1],
126-
},
127-
{
128-
text: upgrades.get('patch') || '',
129-
align: 'right',
130-
padding: [0, 1, 0, 0],
131-
},
132-
{
133-
text: upgrades.get('minor') || '',
134-
align: 'right',
135-
padding: [0, 1, 0, 0],
136-
},
137-
{
138-
text: upgrades.get('major') || '',
139-
align: 'right',
140-
padding: [0, 1, 0, 0],
141-
}
142-
)
143-
);
144-
145101
log.info(`
146-
${ui.toString()}`);
102+
${formatPackageUpgrades(validUpgrades)}`);
147103
});
148104
});

utility.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

utility/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
const { formatPackageUpgrades } = require('./ui');
4+
5+
/**
6+
* Format a version number in the format used by DNN JS library folders
7+
*
8+
* @param {string} version - the version number
9+
* @returns {string} the version number in the correct format
10+
*/
11+
function formatVersionFolder(version) {
12+
const versionFolderPadding = 2;
13+
14+
return version
15+
.split('.')
16+
.map(n => n.padStart(versionFolderPadding, '0'))
17+
.join('_');
18+
}
19+
20+
/**
21+
* Compares two strings, case-insensitively, for use in Array.sort
22+
*
23+
* @param {string|undefined} a - One string value
24+
* @param {string|undefined} b - The other string value
25+
* @returns {-1|0|1} The comparison value (-1 if a is less than b, 1 if a is greater than b, 0 if they're equal)
26+
*/
27+
function compareStrings(a, b) {
28+
const upperA = a ? a.toUpperCase() : a;
29+
const upperB = b ? b.toUpperCase() : b;
30+
if (upperA < upperB) {
31+
return -1;
32+
}
33+
34+
if (upperA > upperB) {
35+
return 1;
36+
}
37+
38+
return 0;
39+
}
40+
41+
module.exports = {
42+
formatVersionFolder,
43+
compareStrings,
44+
formatPackageUpgrades,
45+
};

utility/ui.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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

Comments
 (0)