Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 60 additions & 47 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,58 +30,71 @@ export default async ({
...customOptions
};

const chrome = await chromeLauncher.launch({
chromeFlags: options.chromeFlags,
port: options.port
});
// we need to kill chrome if something goes wrong, so we pull it up
// into the function scope to be accessible in the catch block.
let chrome;

options.output = 'html';
try {
chrome = await chromeLauncher.launch({
chromeFlags: options.chromeFlags,
port: options.port
});

// the default config combined with overriding query params
const fullConfig = {
...config,
...customConfig
};
options.output = 'html';

const results = await lighthouse(url, options, fullConfig);

// a remote URL
let report;

// a local file path
let localReport;

if (isS3) {
// upload to S3
const s3Response = await upload({
s3bucket: new AWS.S3({
accessKeyId,
Bucket,
region,
secretAccessKey
}),
params: {
ACL: 'public-read',
Body: results.report,
Bucket,
ContentType: 'text/html',
Key: `lighthouse-report-${Date.now()}.html`
}
});
// the default config combined with overriding query params
const fullConfig = {
...config,
...customConfig
};

report = s3Response.Location;
}
const results = await lighthouse(url, options, fullConfig);

if (outputDirectory) {
localReport = `${outputDirectory}/lighthouse-report-${Date.now()}.html`;
fs.writeFileSync(localReport, results.report);
}
// a remote URL
let report;

await chrome.kill();
// a local file path
let localReport;

return {
localReport,
result: JSON.parse(JSON.stringify(results.lhr)),
report
};
if (isS3) {
// upload to S3
const s3Response = await upload({
s3bucket: new AWS.S3({
accessKeyId,
Bucket,
region,
secretAccessKey
}),
params: {
ACL: 'public-read',
Body: results.report,
Bucket,
ContentType: 'text/html',
Key: `lighthouse-report-${Date.now()}.html`
}
});

report = s3Response.Location;
}

if (outputDirectory) {
localReport = `${outputDirectory}/lighthouse-report-${Date.now()}.html`;
fs.writeFileSync(localReport, results.report);
}

await chrome.kill();

return {
localReport,
result: JSON.parse(JSON.stringify(results.lhr)),
report
};
} catch (error) {
// make sure we kill chrome
if (chrome) {
await chrome.kill();
}

throw error;
}
};