|
1 | | -import chalk from 'chalk'; |
2 | 1 | import * as dotenv from 'dotenv'; |
3 | 2 |
|
4 | | -import processResults from './lib/process-results/index.js'; |
| 3 | +import runEvent from './lib/run-event/index.js'; |
5 | 4 | import getUtils from './lib/get-utils/index.js'; |
6 | | -import getConfiguration from './lib/get-configuration/index.js'; |
7 | | -import getSettings from './lib/get-settings/index.js'; |
8 | | -import runAudit from './lib/run-audit/index.js'; |
9 | 5 |
|
10 | 6 | dotenv.config(); |
11 | 7 |
|
12 | | -export const onPostBuild = async ({ constants, utils, inputs } = {}) => { |
13 | | - const { failBuild, show } = getUtils({ utils }); |
14 | | - let errorMetadata = []; |
15 | | - |
16 | | - try { |
17 | | - const { audits } = getConfiguration({ |
18 | | - constants, |
19 | | - inputs, |
20 | | - }); |
21 | | - |
22 | | - const settings = getSettings(inputs?.settings); |
23 | | - |
24 | | - const allErrors = []; |
25 | | - const data = []; |
26 | | - for (const { serveDir, path, url, thresholds, output_path } of audits) { |
27 | | - const { errors, summary, shortSummary, details, report, runtimeError } = |
28 | | - await runAudit({ |
29 | | - serveDir, |
30 | | - path, |
31 | | - url, |
32 | | - thresholds, |
33 | | - output_path, |
34 | | - settings, |
| 8 | +export default function lighthousePlugin(inputs) { |
| 9 | + // Run onPostBuild by default, unless RUN_ON_SUCCESS is set to true |
| 10 | + const defaultEvent = |
| 11 | + inputs?.run_on_success === 'true' || process.env.RUN_ON_SUCCESS === 'true' |
| 12 | + ? 'onSuccess' |
| 13 | + : 'onPostBuild'; |
| 14 | + |
| 15 | + if (defaultEvent === 'onSuccess') { |
| 16 | + return { |
| 17 | + onSuccess: async ({ constants, utils, inputs } = {}) => { |
| 18 | + // Mock the required `utils` functions if running locally |
| 19 | + const { failPlugin, show } = getUtils({ utils }); |
| 20 | + |
| 21 | + await runEvent({ |
| 22 | + event: 'onSuccess', |
| 23 | + constants, |
| 24 | + inputs, |
| 25 | + onComplete: show, |
| 26 | + onFail: failPlugin, |
35 | 27 | }); |
36 | | - |
37 | | - if (summary && !runtimeError) { |
38 | | - console.log({ results: summary }); |
39 | | - } |
40 | | - if (runtimeError) { |
41 | | - console.log({ runtimeError }); |
42 | | - } |
43 | | - |
44 | | - const fullPath = [serveDir, path].join('/'); |
45 | | - if (report) { |
46 | | - const size = Buffer.byteLength(JSON.stringify(report)); |
47 | | - console.log( |
48 | | - `Report collected: audited_uri: '${chalk.magenta( |
49 | | - url || fullPath, |
50 | | - )}', html_report_size: ${chalk.magenta( |
51 | | - +(size / 1024).toFixed(2), |
52 | | - )} KiB`, |
53 | | - ); |
54 | | - } |
55 | | - |
56 | | - if (Array.isArray(errors) && errors.length > 0) { |
57 | | - allErrors.push({ serveDir, url, errors }); |
58 | | - } |
59 | | - data.push({ |
60 | | - path: fullPath, |
61 | | - url, |
62 | | - summary, |
63 | | - shortSummary, |
64 | | - details, |
65 | | - report, |
66 | | - runtimeError, |
67 | | - }); |
68 | | - } |
69 | | - |
70 | | - const { error, summary, extraData } = processResults({ |
71 | | - data, |
72 | | - errors: allErrors, |
73 | | - show, |
74 | | - }); |
75 | | - errorMetadata.push(...extraData); |
76 | | - |
77 | | - if (error && Object.keys(error).length !== 0) { |
78 | | - throw error; |
79 | | - } |
80 | | - |
81 | | - show({ summary, extraData }); |
82 | | - } catch (error) { |
83 | | - if (error.details) { |
84 | | - console.error(error.details); |
85 | | - failBuild(`${chalk.red('Failed with error:\n')}${error.message}`, { |
86 | | - errorMetadata, |
87 | | - }); |
88 | | - } else { |
89 | | - failBuild(`${chalk.red('Failed with error:\n')}`, { |
90 | | - error, |
91 | | - errorMetadata, |
92 | | - }); |
93 | | - } |
| 28 | + }, |
| 29 | + }; |
| 30 | + } else { |
| 31 | + return { |
| 32 | + onPostBuild: async ({ constants, utils, inputs } = {}) => { |
| 33 | + // Mock the required `utils` functions if running locally |
| 34 | + const { failBuild, show } = getUtils({ utils }); |
| 35 | + |
| 36 | + await runEvent({ |
| 37 | + event: 'onPostBuild', |
| 38 | + constants, |
| 39 | + inputs, |
| 40 | + onComplete: show, |
| 41 | + onFail: failBuild, |
| 42 | + }); |
| 43 | + }, |
| 44 | + }; |
94 | 45 | } |
95 | | -}; |
| 46 | +} |
0 commit comments