-
Notifications
You must be signed in to change notification settings - Fork 5
/
gen.js
128 lines (108 loc) · 4.49 KB
/
gen.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import YAML from 'yaml';
import * as fsp from 'fs/promises';
import * as path from 'path';
import axios from 'axios';
console.log("Getting files and directories...");
const dirname = path.dirname(process.argv[1]);
const username = process.argv[2];
const token = process.argv[3];
let { addons, loader, manager } = (await axios.get("https://gw2-addon-loader.github.io/addon-repo/addons.json")).data;
async function fetch_addon_loader() {
try {
const { data } = await axios.get("https://api.github.com/repos/gw2-addon-loader/loader-core/releases/latest", { auth: { username: username, password: token } });
return {
name: "Addon Loader",
developer: "megai2",
website: "https://github.com/gw2-addon-loader/loader-core",
version_id: data.tag_name,
download_url: data.assets[0].browser_download_url,
wrapper_nickname: "d3d9_wrapper"
};
} catch (ex) {
console.log("Error: Could not successfully fetch version information for addon loader: " + ex);
return loader;
}
}
async function fetch_addon_manager() {
try {
const { data } = await axios.get("https://api.github.com/repos/gw2-addon-loader/GW2-Addon-Manager/releases/latest", { auth: { username: username, password: token } });
return {
name: "GW2 Unofficial Addon Manager",
developer: "FriendlyFire",
website: "https://github.com/gw2-addon-loader/GW2-Addon-Manager",
version_id: data.tag_name,
download_url: data.assets[0].browser_download_url
};
} catch (ex) {
console.log("Error: Could not successfully fetch version information for addon manager: " + ex);
return manager;
}
}
async function insert_github(addon) {
const { data } = await axios.get(addon.host_url, { auth: { username: username, password: token } });
addon.version_id = data.tag_name;
addon.version_id_is_human_readable = true;
addon.download_url = data.assets[0].browser_download_url;
}
async function insert_standalone(addon) {
addon.version_id_is_human_readable = false;
if (addon.version_url != null) {
const { data } = await axios.get(addon.version_url, { responseType: 'text', transitional: { forcedJSONParsing: false } });
addon.version_id = data;
}
addon.download_url = addon.host_url;
}
async function add_addon(name, file) {
console.log("Loading " + name + " from YAML...");
try {
let yaml = YAML.parse(file);
yaml.nickname = name;
yaml.fetch_time = Date.now();
try {
if (yaml.host_type === 'github')
await insert_github(yaml);
else if (yaml.host_type === 'standalone')
await insert_standalone(yaml);
addons[name] = yaml;
} catch (ex) {
console.log("Error: Could not successfully fetch version information for addon '" + name + "': " + ex);
}
} catch (ex) {
console.log("Error: Could not successfully parse YAML for addon '" + name + "': " + ex);
}
}
const files = await fsp.readdir(dirname, { withFileTypes: true });
for (const file of files) {
if (!file.isDirectory() || file.name[0] == '.' || file.name == "node_modules")
continue;
try {
const update_file = await fsp.readFile(dirname + "/" + file.name + "/update.yaml", "utf-8");
await add_addon(file.name, update_file);
continue;
} catch (ex) {
if(ex.code !== 'ENOENT')
console.log("Info: Encountered exception " + ex);
}
console.log("Notice: Addon " + file.name + " does not contain an 'update.yaml' file.");
try {
const update_file = await fsp.readFile(dirname + "/" + file.name + "/update-placeholder.yaml", "utf-8");
await add_addon(file.name, update_file);
continue;
} catch (ex) {
if(ex.code !== 'ENOENT')
console.log("Info: Encountered exception " + ex);
}
console.log("Warning: Folder " + file.name + " does not appear to be a valid addon description.");
}
console.log("Found " + Object.keys(addons).length + " addons.");
const addon_loader = await fetch_addon_loader();
const addon_manager = await fetch_addon_manager();
try {
await fsp.mkdir(dirname + "/.build");
} catch { }
await fsp.writeFile(dirname + "/.build/addons.json", JSON.stringify({
"addons": addons,
"loader": addon_loader,
"manager": addon_manager
}), "utf-8");
await fsp.copyFile(dirname + "/index.html", dirname + "/.build/index.html");