-
Notifications
You must be signed in to change notification settings - Fork 0
/
export.js
50 lines (42 loc) · 1.45 KB
/
export.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
'use strict';
const Promise = require("bluebird");
const config = require('./config');
const writeFile = Promise.promisify(require("fs").writeFile);
const chalk = require('chalk');
const json2csv = require('json2csv');
const LinkStatus = require('./linkStatus');
const Export = {
write: (data) => {
const csvfile = json2csv({ data: transformData(data), fields: [
"file", "url", "code"
]});
console.log("\n___________________________\n");
return writeFile(config.outputFilename, csvfile, 'utf8')
.then(() => {
console.log(chalk.white("📗 📗 📗 Successfully written possible broken links report to ") + chalk.white.bold.underline(config.outputFilename));
})
.catch(() => {
console.log(chalk.red.bold("❌ Error exporting to file!"));
});
}
};
const transformData = (data) => {
let formattedData = [];
for (let property in data) {
if (data.hasOwnProperty(property)) {
data[property].forEach((item) => {
if (item.status !== LinkStatus.SUCESSFUL) {
formattedData.push(
{
file: property,
url: item.url,
code: item.code
}
);
}
});
}
}
return formattedData;
};
module.exports = Export;