Skip to content

Commit 988e2ac

Browse files
committed
refactor(wpt): refactor wpt tools system changing reporter objective to functional functions
1 parent add1721 commit 988e2ac

File tree

11 files changed

+117
-117
lines changed

11 files changed

+117
-117
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Read lighthouse contents, [Here](https://developer.chrome.com/docs/lighthouse/ov
3434
"pathname": "/web-performance-tester"
3535
}
3636
}], // Required
37-
"timeout": 20000, // default : 20000
38-
"concurrency": 1, // default : 1
37+
"timeout": 30000, // default : 30000
38+
"useReporter": true, // default : true
3939
"options": {
4040
"chromeFlags": ["--headless"], // default : ["--headless"] - ["--show-paint-rects", "--headless", "--disable-gpu"]
4141
"formFactor": "desktop", // default : "mobile" - desktop, mobile

examples/default/wpt.config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"pathname": "/web-performance-tester"
88
}
99
],
10-
"timeout": 20000,
10+
"timeout": 30000,
11+
"useReporter": true,
1112
"options": {
1213
"chromeFlags": ["--headless"],
1314
"formFactor": "desktop",

src/wpt/Wpt.ts

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,20 @@ import { launch } from 'chrome-launcher';
44
import { pipe, concurrent, toAsync, toArray, map } from '@fxts/core';
55
import EventEmitter from 'events';
66

7-
import { Reporter } from './tools';
8-
import { WptContext, WptConfig, WptAuditPath } from './context';
7+
import { WptContext, WptConfig } from './context';
98
import * as constants from './constants';
10-
import { WptEventHandlersEventMap } from './Wpt.types';
11-
12-
export interface WebPerformanceTesterProps {
13-
reporter?: Reporter;
14-
}
15-
16-
export interface WptAuditPathItem extends WptAuditPath {
17-
url: string;
18-
}
9+
import { WptAuditPathItem, WptEventHandlersEventMap } from './Wpt.types';
10+
import { writeAuditsReportJsonFile, writeResultReportFile } from './tools';
1911

2012
const context = WptContext();
2113

2214
class WebPerformanceTester extends EventEmitter {
23-
private reporter: Reporter;
24-
25-
constructor(props?: WebPerformanceTesterProps) {
15+
constructor() {
2616
super();
27-
this.reporter = props?.reporter ?? new Reporter();
28-
this.typedEmit('onReadyWpt', { context });
17+
this._emit('onReadyWpt', { context });
2918
}
3019

31-
private typedEmit(
20+
private _emit(
3221
eventName: keyof WptEventHandlersEventMap,
3322
eventArgs: WptEventHandlersEventMap[keyof WptEventHandlersEventMap],
3423
) {
@@ -39,10 +28,12 @@ class WebPerformanceTester extends EventEmitter {
3928
lighthouseResult: RunnerResult,
4029
auditPath: WptAuditPathItem,
4130
) {
42-
this.typedEmit('onReportStart', { auditPath, context });
43-
this.reporter.saveAuditsReportFile(lighthouseResult.report, auditPath.name);
44-
await this.reporter.createAuditsReport(lighthouseResult.lhr.audits);
45-
this.typedEmit('onReportEnd', { auditPath, context });
31+
if (context.config.useReporter) {
32+
this._emit('onReportStart', { auditPath, context });
33+
writeResultReportFile(lighthouseResult.report, auditPath.name);
34+
writeAuditsReportJsonFile(lighthouseResult.lhr.audits, auditPath.name);
35+
this._emit('onReportEnd', { auditPath, context });
36+
}
4637
}
4738

4839
private async runLighthouse(
@@ -55,22 +46,21 @@ class WebPerformanceTester extends EventEmitter {
5546
port: options.port + index,
5647
});
5748
try {
58-
this.typedEmit('onStartedLighthouse', { auditPath, context });
49+
this._emit('onStartedLighthouse', { auditPath, context });
50+
const isDesktop = options.formFactor === 'desktop';
5951
const lighthouseResult: RunnerResult = await lighthouse(auditPath.url, {
6052
...options,
61-
screenEmulation:
62-
options.formFactor === 'desktop' &&
63-
constants.ScreenEmulationMetrics.desktop,
53+
screenEmulation: isDesktop && constants.ScreenEmulationMetrics.desktop,
6454
maxWaitForLoad: context.config.timeout,
6555
port: options.port + index,
6656
});
6757
await this.createLighthouseReport(lighthouseResult, auditPath);
68-
this.typedEmit('onFinishedLighthouse', { auditPath, context });
58+
this._emit('onFinishedLighthouse', { auditPath, context });
6959
} catch (error) {
7060
if (error instanceof Error) {
71-
this.typedEmit('onErrorLighthouse', { auditPath, context, error });
61+
this._emit('onErrorLighthouse', { auditPath, context, error });
7262
} else {
73-
this.typedEmit('onErrorLighthouse', { auditPath, context });
63+
this._emit('onErrorLighthouse', { auditPath, context });
7464
}
7565
} finally {
7666
chrome.kill();
@@ -79,7 +69,7 @@ class WebPerformanceTester extends EventEmitter {
7969

8070
async run() {
8171
try {
82-
this.typedEmit('onStartedWpt', { context });
72+
this._emit('onStartedWpt', { context });
8373
let index = 0;
8474
const { concurrency, options } = context.config;
8575
await pipe(
@@ -92,12 +82,12 @@ class WebPerformanceTester extends EventEmitter {
9282
concurrent(concurrency),
9383
(values) => toArray(values),
9484
);
95-
this.typedEmit('onFinishedWpt', { context });
85+
this._emit('onFinishedWpt', { context });
9686
} catch (error) {
9787
if (error instanceof Error) {
98-
this.typedEmit('onErrorWpt', { context, error });
88+
this._emit('onErrorWpt', { context, error });
9989
} else {
100-
this.typedEmit('onErrorWpt', { context });
90+
this._emit('onErrorWpt', { context });
10191
}
10292
}
10393
}

src/wpt/Wpt.types.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { WptContextValue } from './context';
2-
import { WptAuditPathItem } from './Wpt';
32

43
export interface WptErrorEvent {
54
error: Error;
@@ -28,3 +27,12 @@ export interface WptEventHandlersEventMap {
2827
onReportStart: WptReportEvent;
2928
onReportEnd: WptReportEvent;
3029
}
30+
31+
export interface WptAuditPath {
32+
name: string;
33+
pathname: string;
34+
}
35+
36+
export interface WptAuditPathItem extends WptAuditPath {
37+
url: string;
38+
}

src/wpt/context/Wpt.Config.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import fs from 'fs';
2+
import { WptAuditPath } from '../Wpt.types';
23
import {
34
WptLighthouseConfig,
45
WptLighthouseConfigProps,
56
} from './Wpt.LighthouseConfig';
67

7-
export interface WptAuditPath {
8-
name: string;
9-
pathname: string;
10-
}
11-
128
export interface WptConfigProps {
139
name: string;
1410

@@ -21,6 +17,12 @@ export interface WptConfigProps {
2117
*/
2218
timeout?: number;
2319

20+
/**
21+
* @default true
22+
* Whether to use the default "Wpt" reporter system.
23+
*/
24+
useReporter?: boolean;
25+
2426
/**
2527
* @see https://github.com/GoogleChrome/lighthouse/issues/7187#issuecomment-569133443
2628
* @default 1
@@ -42,27 +44,29 @@ class WptConfig implements WptConfigProps {
4244
origin: string;
4345
auditPaths: WptAuditPath[];
4446
timeout: number;
47+
useReporter: boolean;
4548
concurrency: number;
4649
options: WptLighthouseConfig;
4750

4851
constructor() {
4952
const jsonConfigPath = `${process.cwd()}/wpt.config.json`;
5053
const configString = fs.readFileSync(jsonConfigPath, 'utf-8') ?? '{}';
5154
const config = JSON.parse(configString) as WptConfigProps;
55+
5256
if (!config?.name) {
5357
throw new Error('name is required');
5458
}
5559
if (!config?.origin) {
5660
throw new Error('origin is required');
5761
}
58-
if (!config?.auditPaths) {
62+
if (!Array.isArray(config?.auditPaths) || config?.auditPaths.length === 0) {
5963
throw new Error('auditPaths are required');
6064
}
61-
6265
this.name = config.name;
6366
this.origin = config.origin;
6467
this.auditPaths = config.auditPaths ?? [];
6568
this.timeout = config.timeout ?? 30000;
69+
this.useReporter = config.useReporter ?? true;
6670
this.concurrency = config.concurrency ?? 1;
6771
this.options = new WptLighthouseConfig(config.options ?? {});
6872
}

src/wpt/context/Wpt.Context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { pipe, map, toArray } from '@fxts/core';
22

3-
import { WptAuditPathItem } from '../Wpt';
3+
import { WptAuditPathItem } from '../Wpt.types';
44
import { WptConfig } from './Wpt.Config';
55
import { WptLighthouseConfig } from './Wpt.LighthouseConfig';
66

src/wpt/tools/reporter/Reporter.ts

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/wpt/tools/reporter/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './Reporter.types';
2-
export * from './Reporter';
2+
export * from './writeAuditsReportJsonFile';
3+
export * from './writeResultReportFile';
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import { pipe, filter, join } from '@fxts/core';
4+
5+
import { WptContext } from '../../context';
6+
7+
const context = WptContext();
8+
9+
const rootDirectoryPath = path.resolve(
10+
process.cwd(),
11+
context.lighthouseConfig.outputPath,
12+
);
13+
14+
export function makeReportDirectories(directoryName: string) {
15+
const reportDirectoryPath = pipe(
16+
[rootDirectoryPath, directoryName],
17+
filter((value) => value),
18+
join('/'),
19+
);
20+
21+
if (!fs.existsSync(reportDirectoryPath)) {
22+
fs.mkdirSync(reportDirectoryPath, { recursive: true });
23+
}
24+
return reportDirectoryPath;
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import fs from 'fs';
2+
import dayjs from 'dayjs';
3+
import { pipe, join } from '@fxts/core';
4+
5+
import { makeReportDirectories } from './makeReportDirectories';
6+
import { WptLightAuditResult } from './Reporter.types';
7+
8+
export function writeAuditsReportJsonFile(
9+
lighthouseAudits: WptLightAuditResult,
10+
directoryName = '',
11+
) {
12+
const targetDirectoryPath = makeReportDirectories(directoryName);
13+
14+
const outputName = dayjs().format('YY-MM-DD_HH:mm');
15+
const reportFile = `${outputName}_audits.json`;
16+
const targetPath = pipe([targetDirectoryPath, reportFile], join('/'));
17+
18+
const data = JSON.stringify(lighthouseAudits, null, 2);
19+
fs.writeFileSync(targetPath, data, 'utf-8');
20+
}

0 commit comments

Comments
 (0)