-
Notifications
You must be signed in to change notification settings - Fork 40
/
PageSpeed.js
79 lines (71 loc) · 2.01 KB
/
PageSpeed.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
const psi = require("psi");
const process = require("process");
const parseInputs = (inputs) => {
const keyValuePairs = inputs.split(",");
let url, strategy, threshold, apiKey;
keyValuePairs.forEach((pair) => {
const [key, value] = pair.split("=");
switch (key) {
case "url":
url = value;
break;
case "strategy":
strategy = value;
break;
case "threshold":
threshold = value;
break;
case "apiKey":
apiKey = value;
break;
default:
throw new Error(
"Invalid input format, please use the following: url=<your url>,strategy=<desktop/mobile>,threshold=<number(1-100)/none>,apiKey=<yourkey/none"
);
}
});
if (!url || !strategy || !threshold || !apiKey) {
throw new Error(
"Invalid input format,please use the following: url=<your url> strategy=<desktop/mobile>,threshold=<number 1-100/none>,apiKey=<yourkey/none"
);
}
if (strategy !== "desktop" && strategy !== "mobile") {
throw new Error('Invalid strategy. Enter "desktop" or "mobile".');
}
if (threshold !== "none") {
const numThreshold = Number(threshold);
if (isNaN(numThreshold) || numThreshold < 0 || numThreshold > 100) {
throw new Error(
'Invalid threshold. Enter a number between 0 and 100 or "none".'
);
}
}
return {
url,
strategy,
threshold: threshold === "none" ? 70 : Number(threshold),
key: apiKey === "none" ? undefined : apiKey,
};
};
const runPageSpeed = async (inputs) => {
const { url, strategy, threshold, key } = { ...inputs };
try {
console.log(`Page Speed results for ${url} using ${strategy}`);
await psi.output(url, {
...(key ? { key } : undefined),
...(key ? undefined : { nokey: "true" }),
strategy,
format: "cli",
threshold,
});
} catch (e) {
throw e.message;
}
};
let parsedInputs;
try {
parsedInputs = parseInputs(process.argv[2]);
} catch (e) {
throw e.message;
}
runPageSpeed(parsedInputs);