-
Notifications
You must be signed in to change notification settings - Fork 0
/
filehelper.js
104 lines (78 loc) · 2.97 KB
/
filehelper.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
/**
* Created by jacob on 6/5/2017.
*/
const fs = require('fs');
//Builds the output string line by line.
let output = [];
exports.buildArray = function (input) {
for (let i = 0; i < input.length; i++) {
output.push(input[i]);
}
};
//Writes the file.
exports.writeFile = function (location, name, format) {
console.log('You chose : ' + format + ' as your format');
switch (format) {
case 'csv':
fs.writeFile(location + '\/output.csv', output.join(', '), function (err) {
if (err) {
console.error(err);
} else {
console.log('Wrote file to: ', location + '\/output.csv');
}
});
break;
case 'json':
fs.writeFile(location + '\/output.json', JSON.stringify(output), function (err) {
if (err) {
console.error(err);
} else {
console.log('Wrote file to: ', location + '\/output.json');
}
});
console.log('Wrote JSON');
break;
case 'txt':
fs.writeFile(location + '\/output.txt', output.join("\/n"), function (err) {
if (err) {
console.error(err);
} else {
console.log('Wrote file to: ', location + '\/output.txt');
}
});
console.log('Wrote TXT');
break;
case 'html':
console.log('Wrote HTML');
fs.writeFile(location + '\/output.html', builtHtml(), function (err) {
if (err) {
console.error(err);
} else {
console.log('Wrote file to: ', location + '\/output.html');
}
});
break;
case 'excel':
console.log('Wrote Excel File');
break;
}
};
//TODO Rewrite this to be better.
function builtHtml() {
let outputString = "";
outputString += "<style type='text/css'>.tg {border-collapse:collapse;border-spacing:0;margin:0px auto;}.tg td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}.tg th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}.tg .tg-yw4l{vertical-align:top} </style>";
outputString += "<html>";
outputString += "<table>";
let value = 1;
for (let i = 0; i < output.length; i += 2) {
outputString += "<tr>";
outputString += "<th>" + output[i] + "</th>";
outputString += "<th>" + '<a href="' + output[i + 1] + '"> ' + output[i + 1] + '</a></th>';
outputString += "<th>" + value.toString() + '</a></th>';
outputString += "</tr>";
value++;
}
outputString += "</table>";
outputString += "</html>";
return outputString;
}