Skip to content

Commit ce7691a

Browse files
Final cleanups for CLI output
1 parent b52fb21 commit ce7691a

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

log.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ kleur.enabled = !('AVA_PATH' in process.env);
2424
// Aliases to colors used.
2525
// @ts-ignore
2626
const { red, grey, yellow, green, bold, dim } = kleur;
27+
const ICONS = {
28+
tick: ['✔', '√'],
29+
cross: ['✖', '×'],
30+
tada: ['🎉', '🎉'],
31+
};
2732

2833
/**
2934
* Format output as an error message.
@@ -49,6 +54,18 @@ function prettyBytes(size: number): string {
4954
return bytes(size, { unit: 'kb', fixedDecimals: true, unitSeparator: ' ' });
5055
}
5156

57+
/**
58+
* Retrieve an icon appropriate for your OS.
59+
* @param name
60+
*/
61+
function getIcon(name: 'tick' | 'cross' | 'tada') {
62+
if (process.platform === 'win32') {
63+
return ICONS[name][1];
64+
}
65+
66+
return ICONS[name][0];
67+
}
68+
5269
/**
5370
*
5471
* @param size
@@ -108,24 +125,26 @@ export function LogReport({ silent }: Context, report: Map<ItemConfig['path'], C
108125
}
109126

110127
const paths = Array.from(report.keys());
128+
const maxDisplayablePath = 30;
111129
const pathMaxLength = Math.max.apply(
112130
null,
113-
paths.map(path => path.length + 2),
131+
paths.map(path => Math.min(path.length, maxDisplayablePath) + 2),
114132
);
115133
const formatMaxLengths = OrderedCompressionValues.map(compression => maxLengthForCompression(report, paths, compression));
116134
const compressionHeaders = OrderedCompressionValues.map((compression, index) => compressedExtension(compression, formatMaxLengths[index]));
117135
let success: number = 0;
118136
let failure: number = 0;
119137

120-
console.log(bold('\nFilesizes'));
121-
console.log(''.padEnd(pathMaxLength) + ' ' + compressionHeaders.join(''));
138+
console.log(bold('\n Filesizes'));
139+
console.log(''.padEnd(pathMaxLength + 4) + ' ' + compressionHeaders.join(''));
122140
for (const path of paths) {
123141
const compressionMap = report.get(path);
124142
if (!compressionMap) {
125143
continue;
126144
}
127145

128-
let message = path.padEnd(pathMaxLength) + ' ';
146+
let includesFailure = false;
147+
let message = ' ' + path.substring(path.length - maxDisplayablePath).padEnd(pathMaxLength) + ' ';
129148
let compressionIndex = 0;
130149
for (const compression of OrderedCompressionValues) {
131150
const padding = compressionHeaders[compressionIndex].length;
@@ -136,16 +155,23 @@ export function LogReport({ silent }: Context, report: Map<ItemConfig['path'], C
136155
success++;
137156
} else if (successful !== null) {
138157
failure++;
158+
includesFailure = true;
139159
}
140160
message += compressionMessage;
141161
compressionIndex++;
142162
}
163+
if (includesFailure) {
164+
message = ' ' + red(getIcon('cross')) + message;
165+
} else {
166+
message = ' ' + dim().green(getIcon('tick')) + message;
167+
}
143168
console.log(message);
144169
}
145170
if (success > 0 || failure > 0) {
146-
console.log('\n ' + green(success + ` ${success === 1 ? 'check' : 'checks'} passed`) + (failure === 0 ? ' 🎉' : ''));
147-
const failureColor = failure < 1 ? grey : red;
148-
console.log(' ' + failureColor(failure + ` ${failure === 1 ? 'check' : 'checks'} failed`));
171+
console.log('\n ' + green(success + ` ${success === 1 ? 'check' : 'checks'} passed`) + (failure === 0 ? ` ${getIcon('tada')}` : ''));
172+
if (failure > 0) {
173+
console.log(' ' + red(failure + ` ${failure === 1 ? 'check' : 'checks'} failed`));
174+
}
149175
}
150176
console.log();
151177
}

0 commit comments

Comments
 (0)