Skip to content

Commit

Permalink
Improve debug logging of plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Munter committed May 23, 2020
1 parent 57c8968 commit 2c8d651
Showing 1 changed file with 46 additions and 17 deletions.
63 changes: 46 additions & 17 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
const { existsSync } = require('fs');
const { appendFile } = require('fs/promises');
const fs = require('fs');
const { existsSync } = fs;
const { appendFile } = fs.promises;
const { resolve } = require('path');
const subfont = require('subfont');
const globby = require('globby');

const Logger = require('./Logger');

const canonicalRoot = process.env.URL;

const immutableHeaders = `
Expand All @@ -21,12 +24,27 @@ module.exports = {
build: { failPlugin },
},
}) => {
const logger = new Logger();

function fail(message, { error } = {}) {
failPlugin([message, logger.toString()].join('\n\n'), { error });
}

console.log(
`Running subfont version ${require('subfont/package.json').version}`
);

logger.debug('verions', {
'netlify-build': require('@netlify/build/package.json').version,
'netlify-plugin-subfont': require('../package.json').version,
subfont: require('subfont/package.json').version,
assetgraph: require('assetgraph/package.json').version,
});

logger.debug('Plugin configuration', { entryPoints, ...subfontConfig });

if (!existsSync(PUBLISH_DIR)) {
console.log(
logger.log(
`Skipping plugin. 'build.publish' directory '${PUBLISH_DIR} does not exist'`
);
return;
Expand All @@ -36,24 +54,35 @@ module.exports = {
.sync(entryPoints, { cwd: PUBLISH_DIR })
.map((path) => resolve(PUBLISH_DIR, path));

const headerPromise = appendFile(
logger.debug('Resolved entry points', resolvedEntryPoints);

const resolvedConfig = {
...subfontConfig,
inputFiles: resolvedEntryPoints,
root: PUBLISH_DIR,
canonicalRoot,
inPlace: true,
};

logger.debug('Subfont called with', resolvedConfig);

await subfont(resolvedConfig, console).catch((error) =>
fail('Failed during font subsetting', { error })
);

logger.debug('Subfont succeeded');

logger.debug('Adding immutable cache headers to subfont assets');

await appendFile(
resolve(PUBLISH_DIR, '_headers'),
immutableHeaders
).catch((error) =>
failPlugin('Failed to write immutable cache headers', { error })
fail('Failed to write immutable cache headers', { error })
);

const resultPromise = subfont(
{
...subfontConfig,
inputFiles: resolvedEntryPoints,
root: PUBLISH_DIR,
canonicalRoot,
inPlace: true,
},
console
).catch((error) => failPlugin('Failed during font subsetting', { error }));

await Promise.all([headerPromise, resultPromise]);
logger.debug('Immutable cache headers added');

logger.debug('SUCCESS');
},
};

0 comments on commit 2c8d651

Please sign in to comment.