forked from GovTechSG/oobee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
74 lines (66 loc) · 1.89 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
const fs = require('fs-extra');
const { a11yDataStoragePath, allIssueFileName } = 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.validateUrl = url => {
const invalidURLends = [
'.gif',
'.jpg',
'.jpeg',
'.png',
'.pdf',
'.doc',
'.css',
'.svg',
'.js',
'.ts',
'.xml',
'.csv',
'.tgz',
'.zip',
'.xls',
'.ppt',
'.ico',
'.woff',
];
return !invalidURLends.some(urlEnd => url.includes(urlEnd));
};
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`);
await fs.writeFile(`${storagePath}/details.json`, JSON.stringify(scanDetails, 0, 2));
await fs.copy(`${a11yDataStoragePath}/${randomToken}`, `${storagePath}/${allIssueFileName}`);
// update logs
await fs.ensureDir(logPath);
await fs.pathExists('errors.txt').then(async exists => {
if (exists) {
await fs.copy('errors.txt', `${logPath}/${randomToken}.txt`);
}
});
};
exports.getCurrentTime = () => {
return new Date().toLocaleTimeString('en-GB', {
year: 'numeric',
month: 'short',
day: 'numeric',
hour12: true,
hour: 'numeric',
minute: '2-digit',
});
};