forked from LocalSEOGuide/lighthouse-reporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (55 loc) · 1.68 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
// dependencies
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv");
const neat_csv = require("neat-csv");
const db = require("./src/database");
const doReporting = require("./src/doReporting");
// Load environment variables
dotenv.config();
async function getCSVdata(file_path) {
// Read the file
const file = fs.readFileSync(file_path);
const csv_data = await neat_csv(file);
// Validate that input CSV has URL and Template columns
if (
!csv_data[0].hasOwnProperty("URL") ||
!csv_data[0].hasOwnProperty("Template")
) {
console.log(
"$$$Sorry, please make sure your CSV contains two columns labeled 'URL' and 'Template'."
);
db.disconnect();
process.exit(-1);
} else {
console.log(`CSV read successful! - ${csv_data.length} rows`);
}
return csv_data;
}
// Process a csv
async function processCSV(csv_data) {
try {
console.time("Do Reporting");
await doReporting(csv_data);
console.timeEnd("Do Reporting");
} catch (err) {
console.log("$$$Something went wrong trying to read that file.");
console.error(err);
}
}
db.connect(async () => {
// Check for file input
const input_files = fs.readdirSync(path.join(__dirname, "input"));
console.log("We got a file! Process it...");
const file_path = path.join(__dirname, "input", input_files[0]);
const csv_data = await getCSVdata(file_path);
await processCSV(csv_data);
db.disconnect();
});
const colors = require("colors");
Error.prepareStackTrace = (err, arr) => {
let lines = err.stack.split("\n");
lines = lines.map((x) => (x.includes("node_modules") ? colors.grey(x) : x));
lines = lines.join("\n");
err.stack = lines;
};