Skip to content

Commit 594a5f4

Browse files
authored
Merge pull request #123 from Munter/fix/silentMode
Respect silent:true wrt. the console output
2 parents 48bad16 + 74aa5e4 commit 594a5f4

File tree

3 files changed

+29
-34
lines changed

3 files changed

+29
-34
lines changed

lib/subfont.js

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ module.exports = async function subfont(
2929
},
3030
console
3131
) {
32+
function logToConsole(severity, ...args) {
33+
if (!silent && console) {
34+
console[severity](...args);
35+
}
36+
}
37+
function log(...args) {
38+
logToConsole('log', ...args);
39+
}
40+
function warn(...args) {
41+
logToConsole('warn', ...args);
42+
}
43+
3244
let selectedBrowsers;
3345
if (browsers) {
3446
selectedBrowsers = browsersList(browsers);
@@ -75,15 +87,15 @@ module.exports = async function subfont(
7587

7688
if (rootUrl) {
7789
if (rootUrl.startsWith('file:')) {
78-
console.warn(`Guessing --root from input files: ${rootUrl}`);
90+
warn(`Guessing --root from input files: ${rootUrl}`);
7991
} else {
8092
rootUrl = urlTools.ensureTrailingSlash(rootUrl);
8193
}
8294
}
8395
}
8496
} else if (rootUrl && rootUrl.startsWith('file:')) {
8597
inputUrls = [`${rootUrl}**/*.html`];
86-
console.warn(`No input files specified, defaulting to ${inputUrls[0]}`);
98+
warn(`No input files specified, defaulting to ${inputUrls[0]}`);
8799
} else {
88100
throw new SyntaxError(
89101
"No input files and no --root specified (or it isn't file:), cannot proceed.\n"
@@ -161,10 +173,9 @@ module.exports = async function subfont(
161173

162174
if (silent) {
163175
// Avoid failing on assetGraph.warn
164-
// It would be better if logEvents supported a custom console implementation
165176
assetGraph.on('warn', () => {});
166177
} else {
167-
await assetGraph.logEvents();
178+
await assetGraph.logEvents({ console });
168179
}
169180

170181
await assetGraph.loadAssets(inputUrls);
@@ -319,7 +330,7 @@ module.exports = async function subfont(
319330
}
320331

321332
if (debug) {
322-
console.log(util.inspect(fontInfo, false, 99));
333+
log(util.inspect(fontInfo, false, 99));
323334
}
324335

325336
let totalSavings = sumSizesBefore - sumSizesAfter;
@@ -345,15 +356,15 @@ module.exports = async function subfont(
345356
(fontUsage) => fontUsage.props['font-family']
346357
);
347358
const numFonts = Object.keys(fontUsagesByFontFamily).length;
348-
console.log(
359+
log(
349360
`${htmlAsset}: ${numFonts} font${numFonts === 1 ? '' : 's'} (${
350361
fontUsages.length
351362
} variant${fontUsages.length === 1 ? '' : 's'}) in use, ${prettyBytes(
352363
sumSmallestOriginalSize
353364
)} total. Created subsets: ${prettyBytes(sumSmallestSubsetSize)} total`
354365
);
355366
for (const fontFamily of Object.keys(fontUsagesByFontFamily).sort()) {
356-
console.log(` ${fontFamily}:`);
367+
log(` ${fontFamily}:`);
357368
for (const fontUsage of fontUsagesByFontFamily[fontFamily]) {
358369
const variantShortName = `${fontUsage.props['font-weight']}${
359370
fontUsage.props['font-style'] === 'italic' ? 'i' : ' '
@@ -383,17 +394,16 @@ module.exports = async function subfont(
383394
} else {
384395
status += ', no subset font created';
385396
}
386-
console.log(status);
397+
log(status);
387398
}
388399
}
389400
}
390-
console.log(
401+
log(
391402
`HTML/JS/CSS size increase: ${prettyBytes(sumSizesAfter - sumSizesBefore)}`
392403
);
393-
console.log(`Total savings: ${prettyBytes(totalSavings)}`);
404+
log(`Total savings: ${prettyBytes(totalSavings)}`);
394405
if (!dryRun) {
395-
console.log('Output written to', outRoot || assetGraph.root);
406+
log('Output written to', outRoot || assetGraph.root);
396407
}
397-
398408
return assetGraph;
399409
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"homepage": "https://github.com/Munter/subfont#readme",
4848
"dependencies": {
4949
"@gustavnikolaj/async-main-wrap": "^3.0.1",
50-
"assetgraph": "^6.1.1",
50+
"assetgraph": "^6.2.0",
5151
"browserslist": "^4.13.0",
5252
"css-font-parser": "^0.3.0",
5353
"css-font-weight-names": "^0.2.1",

test/subfont.js

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ const openSansBold = require('fs').readFileSync(
1919
describe('subfont', function () {
2020
let mockConsole;
2121
beforeEach(async function () {
22-
mockConsole = { log: sinon.spy(), warn: sinon.spy(), error: sinon.spy() };
22+
mockConsole = {
23+
info: sinon.spy(),
24+
log: sinon.spy(),
25+
warn: sinon.spy(),
26+
error: sinon.spy(),
27+
};
2328
});
2429

2530
afterEach(function () {
@@ -75,7 +80,6 @@ describe('subfont', function () {
7580
{
7681
root,
7782
inputFiles: [`${root}/index.html`],
78-
silent: true,
7983
dryRun: true,
8084
},
8185
mockConsole
@@ -137,7 +141,6 @@ describe('subfont', function () {
137141
{
138142
root,
139143
inputFiles: [`${root}/index.html`],
140-
silent: true,
141144
dryRun: true,
142145
},
143146
mockConsole
@@ -200,7 +203,6 @@ describe('subfont', function () {
200203
root,
201204
inputFiles: [root],
202205
fallbacks: false,
203-
silent: true,
204206
dryRun: true,
205207
},
206208
mockConsole
@@ -280,7 +282,6 @@ describe('subfont', function () {
280282
root,
281283
inputFiles: [root],
282284
fallbacks: false,
283-
silent: true,
284285
dryRun: true,
285286
},
286287
mockConsole
@@ -314,7 +315,6 @@ describe('subfont', function () {
314315
root,
315316
inputFiles: [root],
316317
fallbacks: false,
317-
silent: true,
318318
dryRun: true,
319319
},
320320
mockConsole
@@ -400,7 +400,6 @@ describe('subfont', function () {
400400
root,
401401
inputFiles: [root, `${root}page2`],
402402
fallbacks: false,
403-
silent: true,
404403
dryRun: true,
405404
},
406405
mockConsole
@@ -446,7 +445,6 @@ describe('subfont', function () {
446445
await subfont(
447446
{
448447
subsetPerPage: false,
449-
silent: true,
450448
dryRun: true,
451449
root,
452450
inputFiles: [`${root}/first.html`, `${root}/second.html`],
@@ -483,7 +481,6 @@ describe('subfont', function () {
483481

484482
await subfont(
485483
{
486-
silent: true,
487484
dryRun: true,
488485
root,
489486
inputFiles: [`${root}/first.html`, `${root}/second.html`],
@@ -507,7 +504,6 @@ describe('subfont', function () {
507504
await subfont(
508505
{
509506
subsetPerPage: true,
510-
silent: true,
511507
dryRun: true,
512508
root,
513509
inputFiles: [`${root}/first.html`, `${root}/second.html`],
@@ -539,7 +535,6 @@ describe('subfont', function () {
539535

540536
await subfont(
541537
{
542-
silent: true,
543538
dryRun: true,
544539
dynamic: true,
545540
debug: true,
@@ -567,7 +562,6 @@ describe('subfont', function () {
567562

568563
await subfont(
569564
{
570-
silent: true,
571565
dryRun: true,
572566
dynamic: true,
573567
debug: true,
@@ -595,7 +589,6 @@ describe('subfont', function () {
595589

596590
await subfont(
597591
{
598-
silent: true,
599592
dryRun: true,
600593
dynamic: true,
601594
debug: true,
@@ -624,7 +617,6 @@ describe('subfont', function () {
624617

625618
await subfont(
626619
{
627-
silent: true,
628620
dryRun: true,
629621
dynamic: true,
630622
debug: true,
@@ -653,7 +645,6 @@ describe('subfont', function () {
653645

654646
await subfont(
655647
{
656-
silent: true,
657648
dryRun: true,
658649
dynamic: true,
659650
debug: true,
@@ -687,7 +678,6 @@ describe('subfont', function () {
687678

688679
await subfont(
689680
{
690-
silent: true,
691681
dryRun: true,
692682
dynamic: true,
693683
debug: true,
@@ -737,7 +727,6 @@ describe('subfont', function () {
737727

738728
const assetGraph = await subfont(
739729
{
740-
silent: true,
741730
dryRun: true,
742731
debug: true,
743732
canonicalRoot: 'https://www.netlify.com/',
@@ -779,7 +768,6 @@ describe('subfont', function () {
779768
{
780769
root,
781770
inputFiles: [`${root}/index.html`],
782-
silent: true,
783771
dryRun: true,
784772
},
785773
mockConsole
@@ -817,7 +805,6 @@ describe('subfont', function () {
817805
{
818806
root,
819807
inputFiles: [`${root}/index.html`],
820-
silent: true,
821808
dryRun: true,
822809
},
823810
mockConsole
@@ -853,7 +840,6 @@ describe('subfont', function () {
853840
{
854841
root,
855842
inputFiles: [`${root}/index.html`],
856-
silent: true,
857843
dryRun: true,
858844
browsers: 'IE 11, Chrome 80',
859845
},
@@ -890,7 +876,6 @@ describe('subfont', function () {
890876
{
891877
root,
892878
inputFiles: [`${root}/index.html`],
893-
silent: true,
894879
dryRun: true,
895880
},
896881
mockConsole

0 commit comments

Comments
 (0)