forked from joytocode/lighthouse-lambda
-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
25 lines (24 loc) · 972 Bytes
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
const path = require('path')
const fs = require('fs')
const createLighthouse = require('.')
exports.handler = function (event, context, callback) {
Promise.resolve()
.then(() => createLighthouse(event.url || 'https://example.com', { logLevel: 'info' }))
.then(({ chrome, results }) => {
if (event.saveResults) {
const filename = results.lhr.lighthouseVersion.split('-')[0]
fs.writeFileSync(path.join(__dirname, `results/${filename}.json`), `${JSON.stringify(results.lhr, null, 2)}\n`)
fs.writeFileSync(path.join(__dirname, `results/${filename}.html`), results.report)
}
return chrome.kill().then(() => callback(null, {
url: results.lhr.requestedUrl,
timing: results.lhr.timing,
userAgent: results.lhr.userAgent,
lighthouseVersion: results.lhr.lighthouseVersion
}))
})
.catch((error) => {
return chrome.kill().then(() => callback(error))
})
.catch(callback)
}