Skip to content

Instantly share code, notes, and snippets.

@soulteary
Created May 19, 2018 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulteary/62ab24ad8d31d4a8cac856b2c291fbd4 to your computer and use it in GitHub Desktop.
Save soulteary/62ab24ad8d31d4a8cac856b2c291fbd4 to your computer and use it in GitHub Desktop.
简单的页面性能报告获取方式。
/**
* Simple Example For Web Page Performance
*
* @date 2018.05.19
* @author soulteary
*/
const {resolve, relative, join} = require('path');
const {readdirSync, existsSync, mkdirSync} = require('fs');
const {execSync} = require('child_process');
/**
* example:
* {
* "storage":"./reports"
* }
*/
const {storage} = require('./config.json');
const now = new Date();
const targetURL = process.argv.slice(2)[0];
if (!targetURL) {
console.error(`# Need TargetURL\n> ${relative('.', process.argv[1])} $TARGET_URL`);
process.exit(1);
}
const date = `${now.getFullYear()}-${now.getMonth()}-${now.getDay()}`;
const basePath = resolve('.', join(storage, date, targetURL.replace(/^https?:\/\/(.*?)\/?/, '$1')));
basePath.split('/').slice(1).reduce((prev, item) => {
prev = `${prev}/${item}`;
if (!existsSync(prev)) mkdirSync(prev);
return prev;
}, '');
const distPath = join(basePath, `${readdirSync(basePath).length}`);
if (!existsSync(distPath)) mkdirSync(distPath);
execSync(`npx lighthouse ${targetURL} --verbose --save-assets=true --output-path=${relative('.', join(distPath, 'index.html'))} --view`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment