-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·106 lines (87 loc) · 2.82 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
#!/usr/bin/env node
var Promise = require("bluebird");
const chalk = require("chalk");
const clear = require("clear");
const figlet = require("figlet");
const cliProgress = require("cli-progress");
const validUrl = require("valid-url");
const psi = require("./lib/psi");
const papa = require("./lib/papa");
const files = require("./lib/files");
clear();
console.log(
chalk.yellow(figlet.textSync("PSI csv", { horizontalLayout: "full" }))
);
console.log(" by Procoders.tech [https://procoders.tech/] \n\n");
const argv = require("minimist")(process.argv.slice(2));
const dataFilePath = argv["f"];
const threadsCount = argv["t"] || 5;
const columnFile = argv["c"] || "URL";
const headersFile = !(argv["h"] === "false");
if (!dataFilePath && dataFilePath.lastIndexOf(".csv") === -1) {
console.log(chalk.red("No CSV file in arguments. Add -f <file>"));
}
const dataFileOutputPath =
dataFilePath.substr(0, dataFilePath.lastIndexOf(".csv")) + ".output.csv";
const run = async () => {
try {
const { meta, data } = await papa.toJson(dataFilePath, {
header: headersFile
});
let urlsDone = [];
if (!files.fileExists(dataFileOutputPath)) {
papa.addToFile(
dataFileOutputPath,
meta.fields.concat(["PSI", "FirstContentfulPaint", "TimeToInteractive"])
);
} else {
urlsDone = (
await papa.toJson(dataFileOutputPath, {
header: headersFile
})
).data.map(row => row[columnFile]);
}
let stepProgress = 0;
await Promise.map(
data,
async row => {
const url = row[columnFile];
stepProgress++;
// progressBar.increment();
console.log(`Processing [${stepProgress}/${data.length}]: ${url}`);
if (!validUrl.isUri(url)) {
console.log(chalk.yellow(`Skipped on valid url: ${url}`));
return false;
}
if (urlsDone.includes(url)) {
console.log(chalk.yellow(`Already processed url: ${url}`));
return false;
}
const psiData = await psi.getUrlPSI(url);
const psiDataToSave = [
Math.round(
psiData.lighthouseResult.categories.performance.score * 100
),
(
psiData.lighthouseResult.audits.metrics.details.items[0]
.firstContentfulPaint / 1000
).toFixed(1) + " s",
(
psiData.lighthouseResult.audits.metrics.details.items[0]
.interactive / 1000
).toFixed(1) + " s"
];
papa.addToFile(
dataFileOutputPath,
Object.values(row).concat(psiDataToSave)
);
},
{ concurrency: threadsCount }
);
console.log(chalk.green(`All ${data.length} done!`));
console.log(chalk.blue(`Output file: ${dataFileOutputPath}`));
} catch (err) {
console.log(chalk.red(err));
}
};
run();