-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·195 lines (173 loc) · 6.23 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
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env node
const fs = require("fs");
const path = require('path');
var yargs = require('yargs');
const { hideBin } = require('yargs/helpers')
const flow = require('xml-flow');
const https = require('https');
const { readdir, writeFile } = fs.promises;
const puppeteer = require("puppeteer");
const lighthouse = require('lighthouse');
const chromeLauncher = require('chrome-launcher');
const log = require('lighthouse-logger');
const csvStringify = require('csv-stringify/lib/sync');
const csvParse = require('csv-parse/lib/sync');
const Desktopconfig = require('./desktop-config.js');
const Mobileconfig = require('./mobile-config.js');
async function cutOff(url){
const filename1 = url.slice(8);
const filename2 = filename1.replace(/\//g, "-");
return await filename2
}
function createHeadersRow(reportRows){
let headersRow = [
'Requested URL',
'Final URL',
]
for (row in reportRows) {
headersRow.push(`${reportRows[row].category}: ${reportRows[row].title} (${reportRows[row].type})`);
}
return headersRow
}
async function reportToRowHeaders(csvFileContents) {
const singleReportRows = await csvParse(
csvFileContents,
{
columns: true,
skip_empty_lines: true,
ltrim: true,
relax: true
}
);
const headers = await createHeadersRow(singleReportRows);
return headers;
};
async function createCSVRow(reportRows){
let csvRow = [
reportRows[0].requestedUrl,
reportRows[0].finalUrl
]
for (row in reportRows) {
csvRow.push(reportRows[row].score)
}
return csvRow
}
async function reportToRow(csvFileContents){
const reportRows = csvParse(
csvFileContents,
{
columns: true,
skip_empty_lines: true,
ltrim: true,
}
);
if (!reportRows || reportRows.length === 0) {
return false;
}
const csvRow = await createCSVRow(reportRows);
return csvRow;
};
async function aggregateCSVReports(site, dataDirPath){
const files = await readdir(dataDirPath);
const rows = [];
let headers = null;
for (file in files) {
const filePath = path.join(dataDirPath, files[file]);
const fileContents = fs.readFileSync(filePath, 'utf8');
if (headers == null) {
headers = await reportToRowHeaders(fileContents);
rows.push(headers);
}
const newRow = await reportToRow(fileContents);
rows.push(newRow);
}
const aggregatedReportData = csvStringify(rows);
await writeFile(`${site}aggregatedMobileReport.csv`, aggregatedReportData);
};
async function siteAudit(site, sitemap) {
sitemobile = site + "_mobile_";
sitedesktop = site + "_desktop_";
if (!fs.existsSync('audits')) {
await fs.mkdirSync('audits');
}
if (!fs.existsSync('audits/desktop')) {
await fs.mkdirSync('audits/desktop');
}
if (!fs.existsSync('audits/mobile')) {
await fs.mkdirSync('audits/mobile');
}
const dirnamedesktop = "audits/desktop/" + site
const dirnamemobile = "audits/mobile/" + site
if (!fs.existsSync(dirnamedesktop)) {
await fs.mkdirSync(dirnamedesktop);
}
if (!fs.existsSync(dirnamemobile)) {
await fs.mkdirSync(dirnamemobile);
}
log.setLevel('info');
const chrome = await chromeLauncher.launch({chromeFlags: ['--headless']});
const csvOptions = {output: 'csv', port: chrome.port};
const htmlOptions = {output: 'html', port: chrome.port};
for (const loc of sitemap) {
console.log('location: ', loc);
const filename = await cutOff(loc);
const runnerCSVDesktopResult = await lighthouse(loc, csvOptions, Desktopconfig);
const runnerCSVMobileResult = await lighthouse(loc, csvOptions, Mobileconfig);
// const runnerhtmlResult = await lighthouse(loc, htmlOptions);
// `.report` is the csv report as a string
const Desktopreportcsv = runnerCSVDesktopResult.report;
const Mobilereportcsv = runnerCSVMobileResult.report;
// const reporthtml = runnerhtmlResult.report;
fs.writeFileSync(`audits/desktop/${site}/${filename}.csv`, Desktopreportcsv);
fs.writeFileSync(`audits/mobile/${site}/${filename}.csv`, Mobilereportcsv);
// fs.writeFileSync(`audits/${filename}.html`, reporthtml);
// `.lhr` is the Lighthouse Result as a JS object
console.log('Report is done for', runnerCSVDesktopResult.lhr.finalUrl);
// console.log(runnerCSVResult.lhr.categories);
console.log('Performance score was', runnerCSVDesktopResult.lhr.categories.performance.score * 100);
console.log('Best Practices score was', runnerCSVDesktopResult.lhr.categories['best-practices'].score * 100);
console.log('Accessiblity score was', runnerCSVDesktopResult.lhr.categories.accessibility.score * 100);
console.log('SEO score was', runnerCSVDesktopResult.lhr.categories.seo.score * 100);
console.log('Report is done for', runnerCSVMobileResult.lhr.finalUrl);
// console.log(runnerCSVResult.lhr.categories);
console.log('Performance score was', runnerCSVMobileResult.lhr.categories.performance.score * 100);
console.log('Best Practices score was', runnerCSVMobileResult.lhr.categories['best-practices'].score * 100);
console.log('Accessiblity score was', runnerCSVMobileResult.lhr.categories.accessibility.score * 100);
console.log('SEO score was', runnerCSVMobileResult.lhr.categories.seo.score * 100);
}
await aggregateCSVReports(sitemobile, dirnamedesktop);
await aggregateCSVReports(sitedesktop, dirnamemobile);
await chrome.kill();
}
var argv = yargs.usage('This is my awesome program').options({
'site': {
description: 'site',
required: true,
alias: 's',
},
'sitemapurl': {
description: 'url of sitemap',
required: true,
alias: 'u'
}
}).argv;
// yargs.showHelp();
var file = fs.createWriteStream('site.xml');
let array = [];
https.get(argv.sitemapurl, function(res) {
res.on('data', function(data) {
file.write(data);
}).on('end', function() {
file.end();
var inFile = fs.createReadStream("site.xml"),
xmlStream = flow(inFile);
xmlStream.on('tag:loc', function(url) {
array.push(url.$text);
});
xmlStream.on('end', function() {
console.log(array);
})
});
});
siteAudit(argv.site, array);
// siteAudit('oldbubbies',oldbubbiessitemaparray);