-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·163 lines (142 loc) · 4.25 KB
/
cli.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env node
const path = require("path");
const argv = require("yargs").argv;
const Table = require("cli-table3");
const c = require("ansi-colors");
const fs = require("fs").promises;
const mkdirp = require("mkdirp");
const PageSpeeder = require("./Pagespeeder.js");
// Get Parameters
const outputType = argv.output || argv.o || "cli";
const url = argv.url || argv.u;
const device = argv.device || argv.d;
let outputPath = argv.outputPath || argv.p || path.join(".");
let runCount = Number(argv.runs || argv.r) || 1;
// Constants
const isOutputJSON = outputType === "json";
const defaultFileName = "lighthouse_result.json";
// Variables
/** @type {PageSpeeder} pagespeeder */
let pagespeeder = null;
// Exit Handlers
const exitHandler = async () => {
console.log("\nGracefully shutting down from Signal SIGINT or SIGTERM");
if (pagespeeder) {
await pagespeeder.shutdown();
}
process.exit(0);
};
process.on("SIGINT", exitHandler);
process.on("SIGTERM", exitHandler);
// Handle wrong parameters or url missmatches
if (!url) {
console.error(
`You need to set an url with "--url https://google.com" or "-u https://google.com" in order to recieve pagespeed information.`
);
process.exit(1);
} else if (
!url.match(
/^(https?|ftp|torrent|image|irc):\/\/(-\.)?([^\s\/?\.#-]+\.?)+(\/[^\s]*)?$/i
)
) {
console.error(`The given url doesn't seem to be a valid. Abort!`);
process.exit(1);
}
// Capture missconfiguration
if (
outputType === "json" &&
(outputPath === null || outputPath === undefined)
) {
console.error(
"OutputType is set to json but no valid output path is set. (--outputPath, -p)"
);
process.exit(1);
} else {
outputPath = path.resolve(outputPath);
const ext = path.extname(outputPath);
if (ext !== ".json" || ext !== ".JSON") {
// If path has no json file ext write default filename
outputPath = path.join(outputPath, defaultFileName);
}
}
const colorScore = (score) => {
const rating = {
90: "green",
40: "yellow",
0: "red",
};
const ratingKeys = Object.keys(rating);
const colorKey = ratingKeys.reduce((prev, curr) => {
return prev === null ? curr : curr < score ? curr : prev;
}, null);
score = c.bold[rating[colorKey]](score);
return score;
};
const createResultTable = (device, scores) => {
console.log(
c.bold.bgBlue(
c.whiteBright(
`\tLighthouse Performance Check - Device ${c.blackBright.italic(
device
)}`
)
)
);
console.log(`\t${c.bold("Score:")} ${colorScore(scores.mainScore)}`);
// Create Table head
const table = new Table({
chars: {
"top-left": "\t╔",
"bottom-left": "\t╚",
left: "\t║",
"left-mid": "\t╟",
},
head: ["Audit", "Score", "Value"],
});
const scoresArr = Object.entries(scores.auditScores);
// Create table rows
for (const [scoreName, scoreObj] of scoresArr) {
table.push([scoreName, colorScore(scoreObj.score), scoreObj.displayValue]);
}
console.log(table.toString() + "\n\n");
};
(async () => {
console.log(`
\t${c.yellow.bold("Lighthouse Performance Measurement")}
\t${c.italic.grey("Version:")} ${c.italic.grey(
require("lighthouse/package.json").version
)}`);
pagespeeder = new PageSpeeder(url, device, runCount, {
hooks: {
beforeRunDevice: (device, options) => {
if (options.silent === false) {
console.log(`\n\tCalculating score for ${c.magentaBright(device)}`);
}
},
beforeRunIteration: (run, runCount, options) => {
if (options.silent === false) {
process.stdout.write(
`\tRun ${c.greenBright(run)} of ${c.blueBright(runCount)} ...\r`
);
}
},
},
});
const scores = await pagespeeder.run();
for (const scoreObj of scores) {
if (isOutputJSON) {
await mkdirp(path.dirname(outputPath));
let fileName = path.basename(outputPath, ".json");
const newFileName = fileName + "_" + scoreObj.device;
const deviceOutputPath = outputPath.replace(fileName, newFileName);
await fs.writeFile(
deviceOutputPath,
JSON.stringify(scoreObj.score),
"utf8"
);
} else {
createResultTable(scoreObj.device, scoreObj.score);
}
}
console.log(`\t${c.green("Lighthouse run finished")}`);
})();