Last active
October 19, 2018 14:32
-
-
Save WebCloud/bc8166918f322fcf591fd0b63edfd557 to your computer and use it in GitHub Desktop.
Lighthouse CLI utils to help out on report and digest generation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { error } = require('./utils'); | |
const baseDir = __dirname; | |
const getReportFolder = hash => `${baseDir}reports/automated-lighthouse-${hash}`.replace('\n', ''); | |
const getReportPath = (dirName, format, fileName = 'report') => `${dirName}/${fileName}.${format}`; | |
/** | |
* Function that will generate the report based on the results for the lighthouse run | |
* @param LaunchedChrome chrome The Chrome instance | |
* @param { report: object | string | array, lhr: object } results For multi format run on lighthnouse it will be an array with both formats | |
* @param string reportFormat the format informed by the CLI command | |
*/ | |
const generateReportForHash = (chrome, results, reportFormat) => ({ stdout: hash, fileName }) => { | |
const fs = require('fs'); | |
const dirName = getReportFolder(hash.replace('\n', '')); | |
const reportFile = getReportPath(dirName, reportFormat, fileName); | |
let JSON = results.report; | |
let HTML; | |
if (Array.isArray(results.report)) { | |
[JSON, HTML] = results.report; | |
} | |
try { | |
fs.writeFileSync(reportFile, JSON); | |
if (HTML) { | |
fs.writeFileSync(reportFile.replace('.json', '.html'), HTML); | |
} | |
} catch (err) { | |
error(err); | |
process.exit(1); | |
} | |
return chrome ? chrome.kill().then(() => reportFile) : Promise.resolve(reportFile); | |
}; | |
/** | |
* Utility function to generate the digests | |
* @typedef ImprovementDigest { [metricName: string]: { improvement: number | string, message: string | undefined } | |
* @typedef RegressionDigest { [metricName: string]: { regression: number | string, message: string | undefined, infoMessage: string | undefined } | |
* @param { regressions: RegressionDigest, improvements: ImprovementDigest }} digest | |
* @param string workingBranch | |
*/ | |
function generateDigests(digest, workingBranch) { | |
const digests = Object.keys(digest); | |
return Promise.all(digests.map((digestName) => { | |
// if there's nothing on the digest, do not create a digest file | |
if (Object.keys(digest[digestName]).length === 0) { | |
return Promise.resolve(true); | |
} | |
return generateReportForHash(undefined, { report: JSON.stringify(digest[digestName], null, 2) }, 'json')({ stdout: workingBranch, fileName: `${digestName}-digest` }); | |
})); | |
} | |
module.exports = { | |
getReportFolder, | |
getReportPath, | |
generateReportForHash, | |
generateDigests | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util'); | |
const exec = util.promisify(require('child_process').exec); | |
const { | |
cyan, | |
red, | |
white | |
} = require('chalk'); | |
const log = (message, color = cyan) => console.log('\n', color(message)); | |
const error = message => console.error('\n', red(message)); | |
const executeWithMessage = (message, command) => async () => { | |
if (message && message !== '') log(message); | |
const { stdout, stderr } = await exec(command); | |
log(stdout, white.dim); | |
log(stderr, white.dim); | |
return { stdout, stderr }; | |
}; | |
function requireFromString(src, filename) { | |
const Module = module.constructor; | |
const m = new Module(); | |
m._compile(src, filename); | |
return m.exports; | |
} | |
module.exports = { | |
log, | |
error, | |
executeWithMessage, | |
requireFromString | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment