-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.js
86 lines (75 loc) · 2.5 KB
/
build.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
const argio = require('argio');
const parser = argio();
const fs = require("fs");
const https = require("https");
const init = async () => {
if (parser.get("d")) {
await downloadInputs(true);
if (!parser.get("f")) return;
}
if (parser.get("f")) {
let formats = parser.params.f;
await downloadInputs();
const data = readInputs();
build(data, formats);
} else {
console.log("Usage: node build.js -f [formats]\n");
console.log("Options:\n\t-f\t\tDefine output formats")
}
}
const build = (data, formats) => {
formats.forEach(format => {
try {
const builder = require(`./formats/${format}`);
builder.build(data);
} catch (error) {
console.log(`Error: Invalid Format ${format}.`);
}
})
}
const readInputs = () => {
try {
const en2sn = require("./inputs/en2sn.json");
const sn2en = require("./inputs/sn2en.json");
return { en2sn, sn2en };
} catch (e) {
console.log(`Unable to read input files. : ${e}`);
}
}
const downloadInputs = async (forced = false) => {
try {
const en2snPath = "./inputs/en2sn.json";
const sn2enPath = "./inputs/sn2en.json";
// check if inputs exist
if (fs.existsSync(en2snPath) && fs.existsSync(sn2enPath) & !forced) return;
// or download them
console.log("Status: [OSDB] Downloading databases...");
const en2sn = await request("https://raw.githubusercontent.com/ipmanlk/OSDB/master/db/en2sn.json");
fs.writeFileSync(en2snPath, JSON.stringify(en2sn));
const sn2en = await request("https://raw.githubusercontent.com/ipmanlk/OSDB/master/db/sn2en.json");
fs.writeFileSync(sn2enPath, JSON.stringify(sn2en));
console.log("Status: [OSDB] Databases have been downloaded!.");
return true;
} catch (error) {
console.log(`Error: [OSDB] Unable to download databases!. ${error}`);
return false;
}
}
const request = (url) => {
return new Promise((resolve, reject) => {
https.get(encodeURI(url), (resp) => {
let data = "";
// A chunk of data has been recieved.
resp.on("data", (chunk) => {
data += chunk;
});
// The whole response has been received.
resp.on("end", () => {
resolve(JSON.parse(data));
});
}).on("error", (err) => {
reject(err.message);
});
});
}
init();