Skip to content

Commit b52fb21

Browse files
Another log reformat
1 parent 397a97c commit b52fb21

File tree

5 files changed

+92
-57
lines changed

5 files changed

+92
-57
lines changed

log.ts

Lines changed: 85 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
const kleur = require('kleur');
1818
const bytes = require('bytes');
19-
import { ItemConfig, CompressionMap, Context } from './validation/Condition';
19+
import { ItemConfig, CompressionMap, Context, OrderedCompressionValues } from './validation/Condition';
2020

2121
// Disable output colors for test runs.
2222
kleur.enabled = !('AVA_PATH' in process.env);
2323

2424
// Aliases to colors used.
25-
const { red, grey, green, bold } = kleur;
25+
// @ts-ignore
26+
const { red, grey, yellow, green, bold, dim } = kleur;
2627

2728
/**
2829
* Format output as an error message.
@@ -49,71 +50,102 @@ function prettyBytes(size: number): string {
4950
}
5051

5152
/**
52-
* Given a compression type, format it to a human readable file extension.
53+
*
54+
* @param size
55+
* @param maxSize
56+
* @param maxWidth
57+
*/
58+
function displaySize(size: number | null, maxSize: number | null, maxWidth: number): [boolean | null, string] {
59+
if (size === null || maxSize === null) {
60+
return [null, dim().grey('–'.padEnd(maxWidth))];
61+
} else if (size < maxSize) {
62+
if (1 - size / maxSize < 0.05) {
63+
return [true, yellow(prettyBytes(size).padEnd(maxWidth))];
64+
}
65+
return [true, dim().green(prettyBytes(size).padEnd(maxWidth))];
66+
} else {
67+
return [false, red(prettyBytes(size).padEnd(maxWidth))];
68+
}
69+
}
70+
71+
/**
72+
*
73+
* @param report
74+
* @param paths
5375
* @param compression
5476
*/
55-
function compressedExtension(compression: string): string {
56-
if (compression === 'none') {
57-
return ''.padEnd(3);
77+
function maxLengthForCompression(report: Map<ItemConfig['path'], CompressionMap>, paths: Array<string>, compression: string): number {
78+
const reportedValueStrings: Array<number> = [];
79+
80+
for (const path of paths) {
81+
const value = report.get(path)?.get(compression);
82+
if (value) {
83+
const [size] = value;
84+
if (size !== null) {
85+
reportedValueStrings.push(prettyBytes(size).length);
86+
}
87+
}
5888
}
59-
return '.' + compression.substring(0, 2);
89+
90+
return Math.max.apply(null, reportedValueStrings) + 2;
91+
}
92+
93+
/**
94+
* Given a compression type, format it to a human readable file extension.
95+
* @param compression
96+
*/
97+
function compressedExtension(compression: string, padEnd: number): string {
98+
return compression.padEnd(padEnd);
6099
}
61100

62101
/**
63102
* Display report to the console.
64103
* @param report
65104
*/
66105
export function LogReport({ silent }: Context, report: Map<ItemConfig['path'], CompressionMap>) {
106+
if (silent) {
107+
return;
108+
}
109+
110+
const paths = Array.from(report.keys());
111+
const pathMaxLength = Math.max.apply(
112+
null,
113+
paths.map(path => path.length + 2),
114+
);
115+
const formatMaxLengths = OrderedCompressionValues.map(compression => maxLengthForCompression(report, paths, compression));
116+
const compressionHeaders = OrderedCompressionValues.map((compression, index) => compressedExtension(compression, formatMaxLengths[index]));
67117
let success: number = 0;
68118
let failure: number = 0;
69119

70-
if (!silent && [...report.keys()].length > 0) {
71-
console.log(bold('\nFilesize Report'));
72-
for (const [originalPath, values] of report) {
73-
const multipleOutputs = Array.from(values.values()).filter(([before]) => before !== null).length > 1;
74-
if (multipleOutputs) {
75-
console.log(grey(`\npath: ${originalPath}`));
76-
for (const [compression, compressionResults] of values) {
77-
const [size, maxSize] = compressionResults;
78-
const compressedPath = originalPath + grey(compressedExtension(compression));
79-
if (size === null || maxSize === null) {
80-
continue;
81-
} else if (size < maxSize) {
82-
success++;
83-
console.log(` ✔️ ${compressedPath} ${prettyBytes(size)} ${green('<')} ${prettyBytes(maxSize)}`);
84-
} else {
85-
failure++;
86-
console.log(` ❌ ${compressedPath} ${prettyBytes(size)} ${red('>')} ${prettyBytes(maxSize)}`);
87-
}
88-
}
89-
} else {
90-
const maximumPath =
91-
Math.max.apply(
92-
null,
93-
Array.from(report.keys()).map(item => item.length),
94-
) + 1;
95-
for (const [compression, compressionResults] of values) {
96-
const [size, maxSize] = compressionResults;
97-
const compressedPath = originalPath + grey(compressedExtension(compression)) + new Array(maximumPath - originalPath.length).join(' ');
98-
if (size === null || maxSize === null) {
99-
continue;
100-
} else if (size < maxSize) {
101-
success++;
102-
console.log(` ✔️ ${compressedPath} ${prettyBytes(size)} ${green('<')} ${prettyBytes(maxSize)}`);
103-
} else {
104-
failure++;
105-
console.log(` ❌ ${compressedPath} ${prettyBytes(size)} ${red('>')} ${prettyBytes(maxSize)}`);
106-
}
107-
}
108-
}
120+
console.log(bold('\nFilesizes'));
121+
console.log(''.padEnd(pathMaxLength) + ' ' + compressionHeaders.join(''));
122+
for (const path of paths) {
123+
const compressionMap = report.get(path);
124+
if (!compressionMap) {
125+
continue;
109126
}
110-
if (success > 0 || failure > 0) {
111-
console.log('\n ' + green(success + ` ${success === 1 ? 'check' : 'checks'} passed`) + (failure === 0 ? ' 🎉' : ''));
112-
const failureColor = failure < 1 ? grey : red;
113-
console.log(' ' + failureColor(failure + ` ${failure === 1 ? 'check' : 'checks'} failed`));
127+
128+
let message = path.padEnd(pathMaxLength) + ' ';
129+
let compressionIndex = 0;
130+
for (const compression of OrderedCompressionValues) {
131+
const padding = compressionHeaders[compressionIndex].length;
132+
const [size, maxSize] = compressionMap.get(compression) as [number | null, number | null];
133+
134+
const [successful, compressionMessage] = displaySize(size, maxSize, padding);
135+
if (successful) {
136+
success++;
137+
} else if (successful !== null) {
138+
failure++;
139+
}
140+
message += compressionMessage;
141+
compressionIndex++;
114142
}
115-
console.log();
116-
} else if (!silent) {
117-
MakeError('No report available.');
143+
console.log(message);
144+
}
145+
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`));
118149
}
150+
console.log();
119151
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,15 @@
5858
},
5959
"filesize": {
6060
"./dist/filesize": {
61-
"brotli": "3.5 kB",
61+
"brotli": "3 kB",
6262
"gzip": "3.6 kB",
6363
"none": "20 kB"
6464
},
6565
"./dist/index.js": {
6666
"brotli": "10 kB"
67+
},
68+
"./dist/api.mjs": {
69+
"gzip": "10 kB"
6770
}
6871
},
6972
"ava": {

test/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"outDir": "output",
88
"sourceMap": true,
99
"moduleResolution": "node",
10-
"target": "esnext",
10+
"target": "ES2019",
1111
"module": "commonjs",
1212
"allowJs": false,
1313
"noUnusedLocals": true,

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"outDir": "output",
88
"sourceMap": true,
99
"moduleResolution": "node",
10-
"target": "esnext",
10+
"target": "ES2019",
1111
"module": "esnext",
1212
"allowJs": false,
1313
"noUnusedLocals": true,

validation/Condition.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export enum Compression {
2121
GZIP = 'gzip',
2222
BROTLI = 'brotli',
2323
}
24-
const OrderedCompressionValues = [Compression.BROTLI, Compression.GZIP, Compression.NONE];
24+
export const OrderedCompressionValues = [Compression.BROTLI, Compression.GZIP, Compression.NONE];
2525

2626
export type CompressionMapValue = [number | null, number | null];
2727
export type CompressionMap = Map<string, CompressionMapValue>;

0 commit comments

Comments
 (0)