Skip to content

Commit

Permalink
Merge pull request #123 from Munter/fix/silentMode
Browse files Browse the repository at this point in the history
Respect silent:true wrt. the console output
  • Loading branch information
papandreou authored Sep 1, 2020
2 parents 48bad16 + 74aa5e4 commit 594a5f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 34 deletions.
34 changes: 22 additions & 12 deletions lib/subfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ module.exports = async function subfont(
},
console
) {
function logToConsole(severity, ...args) {
if (!silent && console) {
console[severity](...args);
}
}
function log(...args) {
logToConsole('log', ...args);
}
function warn(...args) {
logToConsole('warn', ...args);
}

let selectedBrowsers;
if (browsers) {
selectedBrowsers = browsersList(browsers);
Expand Down Expand Up @@ -75,15 +87,15 @@ module.exports = async function subfont(

if (rootUrl) {
if (rootUrl.startsWith('file:')) {
console.warn(`Guessing --root from input files: ${rootUrl}`);
warn(`Guessing --root from input files: ${rootUrl}`);
} else {
rootUrl = urlTools.ensureTrailingSlash(rootUrl);
}
}
}
} else if (rootUrl && rootUrl.startsWith('file:')) {
inputUrls = [`${rootUrl}**/*.html`];
console.warn(`No input files specified, defaulting to ${inputUrls[0]}`);
warn(`No input files specified, defaulting to ${inputUrls[0]}`);
} else {
throw new SyntaxError(
"No input files and no --root specified (or it isn't file:), cannot proceed.\n"
Expand Down Expand Up @@ -161,10 +173,9 @@ module.exports = async function subfont(

if (silent) {
// Avoid failing on assetGraph.warn
// It would be better if logEvents supported a custom console implementation
assetGraph.on('warn', () => {});
} else {
await assetGraph.logEvents();
await assetGraph.logEvents({ console });
}

await assetGraph.loadAssets(inputUrls);
Expand Down Expand Up @@ -319,7 +330,7 @@ module.exports = async function subfont(
}

if (debug) {
console.log(util.inspect(fontInfo, false, 99));
log(util.inspect(fontInfo, false, 99));
}

let totalSavings = sumSizesBefore - sumSizesAfter;
Expand All @@ -345,15 +356,15 @@ module.exports = async function subfont(
(fontUsage) => fontUsage.props['font-family']
);
const numFonts = Object.keys(fontUsagesByFontFamily).length;
console.log(
log(
`${htmlAsset}: ${numFonts} font${numFonts === 1 ? '' : 's'} (${
fontUsages.length
} variant${fontUsages.length === 1 ? '' : 's'}) in use, ${prettyBytes(
sumSmallestOriginalSize
)} total. Created subsets: ${prettyBytes(sumSmallestSubsetSize)} total`
);
for (const fontFamily of Object.keys(fontUsagesByFontFamily).sort()) {
console.log(` ${fontFamily}:`);
log(` ${fontFamily}:`);
for (const fontUsage of fontUsagesByFontFamily[fontFamily]) {
const variantShortName = `${fontUsage.props['font-weight']}${
fontUsage.props['font-style'] === 'italic' ? 'i' : ' '
Expand Down Expand Up @@ -383,17 +394,16 @@ module.exports = async function subfont(
} else {
status += ', no subset font created';
}
console.log(status);
log(status);
}
}
}
console.log(
log(
`HTML/JS/CSS size increase: ${prettyBytes(sumSizesAfter - sumSizesBefore)}`
);
console.log(`Total savings: ${prettyBytes(totalSavings)}`);
log(`Total savings: ${prettyBytes(totalSavings)}`);
if (!dryRun) {
console.log('Output written to', outRoot || assetGraph.root);
log('Output written to', outRoot || assetGraph.root);
}

return assetGraph;
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"homepage": "https://github.com/Munter/subfont#readme",
"dependencies": {
"@gustavnikolaj/async-main-wrap": "^3.0.1",
"assetgraph": "^6.1.1",
"assetgraph": "^6.2.0",
"browserslist": "^4.13.0",
"css-font-parser": "^0.3.0",
"css-font-weight-names": "^0.2.1",
Expand Down
27 changes: 6 additions & 21 deletions test/subfont.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ const openSansBold = require('fs').readFileSync(
describe('subfont', function () {
let mockConsole;
beforeEach(async function () {
mockConsole = { log: sinon.spy(), warn: sinon.spy(), error: sinon.spy() };
mockConsole = {
info: sinon.spy(),
log: sinon.spy(),
warn: sinon.spy(),
error: sinon.spy(),
};
});

afterEach(function () {
Expand Down Expand Up @@ -75,7 +80,6 @@ describe('subfont', function () {
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -137,7 +141,6 @@ describe('subfont', function () {
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -200,7 +203,6 @@ describe('subfont', function () {
root,
inputFiles: [root],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -280,7 +282,6 @@ describe('subfont', function () {
root,
inputFiles: [root],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -314,7 +315,6 @@ describe('subfont', function () {
root,
inputFiles: [root],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -400,7 +400,6 @@ describe('subfont', function () {
root,
inputFiles: [root, `${root}page2`],
fallbacks: false,
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -446,7 +445,6 @@ describe('subfont', function () {
await subfont(
{
subsetPerPage: false,
silent: true,
dryRun: true,
root,
inputFiles: [`${root}/first.html`, `${root}/second.html`],
Expand Down Expand Up @@ -483,7 +481,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
root,
inputFiles: [`${root}/first.html`, `${root}/second.html`],
Expand All @@ -507,7 +504,6 @@ describe('subfont', function () {
await subfont(
{
subsetPerPage: true,
silent: true,
dryRun: true,
root,
inputFiles: [`${root}/first.html`, `${root}/second.html`],
Expand Down Expand Up @@ -539,7 +535,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
dynamic: true,
debug: true,
Expand Down Expand Up @@ -567,7 +562,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
dynamic: true,
debug: true,
Expand Down Expand Up @@ -595,7 +589,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
dynamic: true,
debug: true,
Expand Down Expand Up @@ -624,7 +617,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
dynamic: true,
debug: true,
Expand Down Expand Up @@ -653,7 +645,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
dynamic: true,
debug: true,
Expand Down Expand Up @@ -687,7 +678,6 @@ describe('subfont', function () {

await subfont(
{
silent: true,
dryRun: true,
dynamic: true,
debug: true,
Expand Down Expand Up @@ -737,7 +727,6 @@ describe('subfont', function () {

const assetGraph = await subfont(
{
silent: true,
dryRun: true,
debug: true,
canonicalRoot: 'https://www.netlify.com/',
Expand Down Expand Up @@ -779,7 +768,6 @@ describe('subfont', function () {
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -817,7 +805,6 @@ describe('subfont', function () {
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true,
},
mockConsole
Expand Down Expand Up @@ -853,7 +840,6 @@ describe('subfont', function () {
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true,
browsers: 'IE 11, Chrome 80',
},
Expand Down Expand Up @@ -890,7 +876,6 @@ describe('subfont', function () {
{
root,
inputFiles: [`${root}/index.html`],
silent: true,
dryRun: true,
},
mockConsole
Expand Down

0 comments on commit 594a5f4

Please sign in to comment.