-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
128 lines (120 loc) · 3.7 KB
/
index.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import * as fs from 'node:fs';
import { execSync, exec } from 'node:child_process';
import * as chromeLauncher from "chrome-launcher";
import lighthouse from "lighthouse";
import { sortResult } from './resultSort.js';
function install(project) {
console.log('Installing: ', project.name);
if (project.install) {
runCommand(project.install, project.path);
} else {
runCommand('pnpm i', project.path);
}
}
function build(project) {
console.log('Building project: ', project.name);
if (project.build) {
runCommand(project.build, project.path);
} else {
runCommand('pnpm build', project.path);
}
}
function preview(project, port) {
let serve;
if (project.preview) {
const cmd = project.preview;
console.log(cmd);
serve = exec(`${cmd} --port ${port}`, { cwd: project.path }, (error) => {
throw error;
});
} else {
const cmd = `"node_modules/.bin/serve" ./${project.path}/${project.dist ? project.dist : 'dist'} -l ${port}`;
console.log(cmd);
serve = exec(cmd, (error) => {
throw error;
});
}
return serve;
}
function runCommand(command, cwd) {
console.log(command);
execSync(command, { stdio: 'inherit', cwd });
}
async function runLighthouse(url) {
const debuggingPort = 9999;
const options = {
chromeFlags: [
"--headless",
"--no-sandbox",
"--no-first-run",
"--enable-automation",
"--disable-infobars",
"--disable-background-networking",
"--disable-background-timer-throttling",
"--disable-cache",
"--disable-translate",
"--disable-sync",
"--disable-extensions",
"--disable-default-apps",
"--window-size=1200,800",
"--remote-debugging-port=" + debuggingPort,
],
onlyCategories: ["performance"],
port: debuggingPort,
logLevel: "info",
};
const chrome = await chromeLauncher.launch(options);
let results = null;
try {
results = await lighthouse(url, options);
await chrome.kill();
} catch (error) {
console.log("error running lighthouse", error);
await chrome.kill();
throw error;
}
return results.lhr;
}
async function main() {
process.stdout.setEncoding("utf-8");
const args = process.argv.length <= 2 ? [] : process.argv.slice(2, process.argv.length);
const projects = JSON.parse(fs.readFileSync("projectList.json"));
// install
if (args.length == 0 || args.includes("--install")) {
for (const project of projects) {
if (project.name) {
install(project);
}
}
}
// build
if (args.length == 0 || args.includes("--build")) {
for (const project of projects) {
if (project.name) {
build(project);
}
}
}
// lighthouse benchmark
if (args.length == 0 || args.includes("--bench")) {
let port = 5000;
if (!fs.existsSync('./results')) {
fs.mkdirSync('./results');
}
for (const project of projects) {
if (project.name) {
console.log(`Start testing ${project.name}...`)
let serve = preview(project, port);
const result = await runLighthouse(`http://localhost:${port}/`);
fs.writeFileSync(`./results/${project.name}.json`, JSON.stringify(result));
if (serve) {
serve.kill('SIGINT');
}
port++;
}
}
sortResult('./results');
}
// generate result
}
main();