-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-website.js
56 lines (51 loc) · 2.59 KB
/
build-website.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
const fs = require('fs');
let path = "/home/iowa-state-surplus/"
let surplusDb = {};
try{
surplusDb = JSON.parse(fs.readFileSync(path + 'surplus-inventory.json', 'utf8'));
} catch {
surplusDb = JSON.parse(fs.readFileSync('surplus-inventory.json', 'utf8'));
path = "";
}
let finalHTML = [];
for(i=0;i<surplusDb.items.length;i++){
let itemName = Object.values(surplusDb.items[i])[0]
let googleLink = "https://www.google.com/search?q=" + itemName.replace(/ /g, "+");
let quantity = Object.values(surplusDb.items[i])[1]
let orgQuantity = Object.values(surplusDb.items[i])[2]
let dateAdded = Object.values(surplusDb.items[i])[3]
let tags = Object.values(surplusDb.items[i])[4]
let html = "";
html += ' <tr class=\"item hover\">\n';
//html += ' <td></td>\n'
html += ' <td class=\"flex\">\n'
if(dateAdded == surplusDb.latestUpdate){
html += ' <div class=\"tag-New overflow-x-clip text-black badge mr-1 overflow-x-hidden\">New</div>\n'
}
html += ' <div class=\"overflow-x-hidden item-name\">'+itemName+'</div>\n'
html += ' </td>\n'
html += ' <td class="flex-row">\n'
for(j=0;j<tags.length;j++){
html += ' <div class=\"font-semibold tag-' + tags[j] + ' mx-1 text-black badge\" style=\"background-color:#b2ccd6;\">' + tags[j] + '</div>\n';
}
html += ' </td>\n'
html += ' <td>'+quantity+'</td>\n'
html += ' <td>'+(orgQuantity)+'</td>\n'
html += ' <td>'+dateAdded+'</td>\n'
html += ' <td>\n'
html += ' <a class=\"link-primary\" href=\"' + googleLink + '\" target=\"_blank\">Search Product<a/>\n'
html += ' </td>\n'
html += ' </tr>\n'
finalHTML.push(html);
}
const fileData = fs.readFileSync(path + 'index-base.html', { encoding: "utf8" });
const fileDataArray = fileData.split("\n");
const divStartRegex = /<tbody\s*>/i;
const newData = finalHTML.join("");
const index = fileDataArray.findIndex(line => divStartRegex.test(line));
if (index !== -1) {
fileDataArray.splice(index + 1, 0, newData);
const newFileData = fileDataArray.join("\n");
fs.writeFileSync(path + "index.html", newFileData, { encoding: "utf8" });
}
console.log('built index.html file');