-
Notifications
You must be signed in to change notification settings - Fork 5
/
database.js
89 lines (68 loc) · 1.93 KB
/
database.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
require("dotenv").config();
const path = require("path");
const fs = require("fs");
// Create path of files
const directoryPath = path.join(
process.env.DUMP1090_FOLDER,
"/public_html/db/"
);
console.info("Scanning directory", directoryPath);
// Read the directory
fs.readdir(directoryPath, function (err, files) {
// Handling error
if (err) {
return console.log("Unable to scan directory: " + err);
}
// Filter only JSON
const jsonFiles = files.filter((el) => path.extname(el) === ".json");
console.info("Found " + jsonFiles.length + " files");
// Object with all entries
let database = {};
// Read content of each file
for (let i = 0; i < jsonFiles.length; i++) {
let entries = readFile(jsonFiles[i]);
database = {
...database,
...entries,
};
}
// Custom entries
let entries = readDataFile();
if (entries) {
database = {
...database,
...entries,
};
}
// Save the object
let data = `window.fr24db = JSON.parse('${JSON.stringify(database)}');`;
fs.writeFileSync("./dist/fr24_database.js", data);
});
readFile = function (file) {
console.info("- " + file);
// Read content of file
let content = fs.readFileSync(path.join(directoryPath, file), "utf8");
// Parse content of file
let json = JSON.parse(content);
console.info(" " + Object.keys(json).length + " entries");
// Build object with content of file
let entries = {};
for (const key in json) {
let hex = file.replace(".json", "") + key;
let data = json[key];
if (data.hasOwnProperty("r")) {
entries[hex] = data.r;
}
}
return entries;
};
readDataFile = function () {
let file = "registrations.json";
console.info("- " + file);
// Read content of file
let content = fs.readFileSync(path.join("./build/data/", file), "utf8");
// Parse content of file
let json = JSON.parse(content);
console.info(" " + Object.keys(json).length + " entries");
return json;
};