This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
forked from GovTechSG/oobee
-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
91 lines (77 loc) · 2.79 KB
/
utils.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
const fs = require('fs-extra');
const {
a11yDataStoragePath,
allIssueFileName,
invalidURLends
} = require('./constants/constants');
exports.getHostnameFromRegex = url => {
// run against regex
const matches = url.match(/^https?:\/\/([^/?#]+)(?:[/?#]|$)/i);
// extract hostname (will be null if no match is found)
return matches && matches[1];
};
exports.getCurrentDate = () => {
const date = new Date();
return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
};
exports.getCurrentTime = () => {
return new Date().toLocaleTimeString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour12: true,
hour: 'numeric',
minute: '2-digit',
});
};
const getStoragePath = randomToken => {
const date = new Date();
const currentDate = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
const storagePath = `results/${currentDate}/${randomToken}`;
return storagePath;
};
exports.getStoragePath = getStoragePath;
exports.createAndUpdateFolders = async (scanDetails, randomToken) => {
const storagePath = getStoragePath(randomToken);
const logPath = `logs/${randomToken}`;
await fs.ensureDir(`${storagePath}/reports`);
console.log("Write details.json file.")
await fs.writeFile(`${storagePath}/details.json`, JSON.stringify(scanDetails, 0, 2));
await fs.copy(`${a11yDataStoragePath}/${randomToken}`, `${storagePath}/${allIssueFileName}`);
console.log("Writing urls crawled to ./reports/urls.csv");
var scannedURLs = scanDetails.urlsCrawled.scanned.join();
// const fs = require('fs');
// console.log(scannedURLs);
// await fs.writeFile(`${storagePath}/scannedURLs.csv`, scanDetails.urlsCrawled.scanned);
// const writeStream = fs.createWriteStream(`${storagePath}/reports/scannedURLs.csv`);
// writeStream.write(`urls \n`);
// writeStream.write( scannedURLs.replace(/,/g, '\n') );
// From https://stackoverflow.com/questions/64904803/how-to-wait-for-loop-of-stream-write-to-end
var scannedPath = storagePath + "/reports/scannedURLs.csv";
var scannedData = scannedURLs.replace(/,/g, '\n');
async function writeFile(scannedPath, scannedData) {
try {
const writeStream = fs.createWriteStream(path, {
flags: "w"
});
const promisify = util.promisify(writeStream.write);
for (const file of data) {
await promisify(`${file.name}\n`);
}
writeStream.end();
writeStream.on("finish", () => {
console.log("All files were written.");
});
} catch (error) {
console.log('error',error);
throw (error);
}
}
// update logs
await fs.ensureDir(logPath);
await fs.pathExists('errors.txt').then(async exists => {
if (exists) {
await fs.copy('errors.txt', `${logPath}/${randomToken}.txt`);
}
});
};