Created
May 19, 2018 03:25
-
-
Save soulteary/62ab24ad8d31d4a8cac856b2c291fbd4 to your computer and use it in GitHub Desktop.
简单的页面性能报告获取方式。
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
/** | |
* 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