-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
277 lines (261 loc) · 12.3 KB
/
index.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
const fs = require("fs");
const path = require('path');
const crypto = require('crypto');
const ProgressBar = require('progress');
const fetch = require("node-fetch");
const writeFile = fs.writeFile;
const promisify = require("util").promisify;
const writeFilePromise = promisify(writeFile);
const chokidar = require("chokidar");
const {spawn, exec} = require('child_process');
const VERSION = "0.3";
let version = "";
class RessourcesGenerator {
static #exclude = [
"/ressources.json",
];
static getAllFiles(dir) {
return new Promise((resolve, reject) => {
let result = [];
fs.readdir(dir, async (err, files) => {
for (let i = 0; i < files.length; i++) {
const file = path.join(dir, files[i]);//path.resolve(dir, files[i]);
const stat = fs.statSync(file);
if (stat.isDirectory())
result = result.concat(await this.getAllFiles(file));
else result.push(file);
}
resolve(result);
});
});
}
static generate(src, dest) {
return new Promise((resolve, reject) => {
this.getAllFiles(src).then(list => {
this.#exclude.forEach(file => {
list.splice(list.indexOf(path.join(src, file)), 1);
});
const ressources = [];
list.forEach(p => {
const file = fs.readFileSync(p);
const hash = crypto.createHash("sha512");
hash.update(file);
const hex = hash.digest('hex');
ressources.push({
"url": p.replace(src, ""),
"hash": hex
});
});
fs.writeFileSync(dest, JSON.stringify(ressources));
resolve();
});
});
}
}
class Dependencies {
static downloadDependencies(dependencies, location) {
return new Promise(async (resolve, reject) => {
console.log("Downloading dependencies...");
const bar = new ProgressBar('[:bar] :current/:total', {
complete: '=',
incomplete: ' ',
head: '>',
width: 20,
total: dependencies.length
});
for (const dependency of dependencies) {
const bar2 = new ProgressBar('Downloading ' + dependency.name + ' [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
head: '>',
width: 20,
clear: true,
total: dependencies.length
});
for (const data of dependency.data) {
try {
fs.mkdirSync(path.join(location, data.dest.split("/").slice(0, -1).join("/")), {recursive: true});
} catch (e) {
console.error(e);
}
await fetch(data.src, {
"headers": {
"accept-language": "en-US,en;q=0.9"
},
"body": null,
"method": "GET",
"mode": "cors"
}).then(x => x.arrayBuffer()).then(async function (x) {
writeFilePromise(path.join(location, data.dest), Buffer.from(x))
});
bar2.tick(1);
}
bar.tick(1);
}
console.log("Done!");
resolve();
});
}
}
class Arguments {
static parse(args) {
const result = {
environment: "release",
config: "./conf.js",
action: null,
changelog: null
};
for (let i = 0; i < args.length; i++) {
if (args[i].indexOf("env:") === 0) {
switch (args[i].slice(4)) {
case "release":
result.environment = "release";
break;
case "debug":
result.environment = "debug";
break;
default:
throw "Unknown environment. Please specify either `release` or `debug`.";
}
} else if (args[i].indexOf("conf:") === 0)
result.config = args[i].slice(5);
else if (args[i].indexOf("changelog:") === 0)
result.changelog = args[i].slice(10);
else if (result.action)
throw "An action is already specified.";
else {
switch (args[i]) {
case "dist":
result.action = "dist";
break;
case "dev":
result.action = "dev";
break;
case "help":
result.action = "help";
break;
default:
throw "Unknown action."
}
}
}
if (!result.action)
throw "Missing action."
return result;
}
}
const args = Arguments.parse(process.argv.slice(2));
function dist(configuration, root) {
fs.mkdirSync(path.join(root, "dependencies"), {recursive: true});
return new Promise((resolve, reject) => {
Dependencies.downloadDependencies(configuration.dependencies, path.join(root, "dependencies"))
.then(() => {
return RessourcesGenerator.getAllFiles("src")
})
.then(list => {
console.log("Copying src files...");
list.forEach(file => {
try {
fs.mkdirSync(path.join(root, file.slice(3)).split("/").slice(0, -1).join("/"), {recursive: true});
} catch (e) {
console.error(e);
}
fs.copyFileSync(file, path.join(root, file.slice(3)));
});
console.log("Done!");
console.log("Creating `departments.json`...");
fs.writeFileSync(path.join(root, "config", "departments.json"), JSON.stringify(configuration.departments));
console.log("Done!");
console.log("Setting up backend url and client id...");
fs.writeFileSync(path.join(root, "js", "api.js"), fs.readFileSync(path.join(root, "js", "api.js")).toString().replace("__BACKEND_URL__", configuration.backend));
fs.writeFileSync(path.join(root, "js", "api.js"), fs.readFileSync(path.join(root, "js", "api.js")).toString().split("__CLIENT_ID__").join(configuration.client_id));
console.log("Done!");
console.log("Creating version file...");
exec("git log -1 --format=%cd --date=raw", (error, stdout, stderr) => {
if (error) {
console.error(`error: ${error.message}`);
return;
}
if (stderr) {
console.error(`stderr: ${stderr}`);
return;
}
version = VERSION + "." + stdout.split(" ")[0];
fs.writeFileSync(path.join(root, "version"), version);
fs.writeFileSync(path.join(root, "settings.html"), fs.readFileSync(path.join(root, "settings.html")).toString().replace("__CYREL_WEB_VERSION__", version));
console.log("Done!");
console.log("Generating `changelog.json`...");
const changelog = {};
if (args.changelog) {
changelog.exist = true;
changelog.content = fs.readFileSync(args.changelog, {encoding: "utf8"});
} else
changelog.exist = false;
fs.writeFileSync(path.join(root, "changelog.json"), JSON.stringify(changelog));
console.log("Done!");
console.log("Generating `ressources.json`...");
RessourcesGenerator.generate(root, path.join(root, "ressources.json")).then(() => resolve());
});
});
});
}
switch (args.action) {
case "help":
console.log("cyrel-web set-up tool.");
console.log("Arguments:");
console.log(" - env:[environment] - Set the environment can be debug or release.");
console.log(" - conf:[path] - Set the config file.");
console.log(" - changelog:[path] - Set the changelog path.");
console.log("Commands:");
console.log(" - help - Show this page.");
console.log(" - dist - Generate the website.");
console.log(" - dev - Run a local webserver on 8080 for dev.");
break;
case "dist":
dist(require(args.config), path.join("dist", args.environment)).then(() => console.log("Dist finished"));
break;
case "dev":
const configuration = require(args.config);
const root = path.join("dist", "debug");
dist(configuration, root).then(() => {
const watcher = chokidar.watch("src", {persistent: true});
watcher.on("ready", () => {
console.log("Ready to watch changes")
watcher
.on("add", file => {
console.log(`File ${file} has been added`);
fs.copyFileSync(file, path.join(root, file.slice(3)));
fs.writeFileSync(path.join(root, "js", "api.js"), fs.readFileSync(path.join(root, "js", "api.js")).toString().replace("__BACKEND_URL__", configuration.backend));
fs.writeFileSync(path.join(root, "js", "api.js"), fs.readFileSync(path.join(root, "js", "api.js")).toString().replaceAll("__CLIENT_ID__", configuration.client_id));
fs.writeFileSync(path.join(root, "settings.html"), fs.readFileSync(path.join(root, "settings.html")).toString().replace("__CYREL_WEB_VERSION__", version));
RessourcesGenerator.generate(root, path.join(root, "ressources.json"));
})
.on("change", file => {
console.log(`File ${file} has been changed`);
fs.copyFileSync(file, path.join(root, file.slice(3)));
fs.writeFileSync(path.join(root, "js", "api.js"), fs.readFileSync(path.join(root, "js", "api.js")).toString().replace("__BACKEND_URL__", configuration.backend));
fs.writeFileSync(path.join(root, "js", "api.js"), fs.readFileSync(path.join(root, "js", "api.js")).toString().replaceAll("__CLIENT_ID__", configuration.client_id));
fs.writeFileSync(path.join(root, "settings.html"), fs.readFileSync(path.join(root, "settings.html")).toString().replace("__CYREL_WEB_VERSION__", version));
RessourcesGenerator.generate(root, path.join(root, "ressources.json"));
})
.on('unlink', file => {
console.log(`File ${file} has been removed`);
fs.rmSync(path.join(root, file.slice(3)));
RessourcesGenerator.generate(root, path.join(root, "ressources.json"));
})
.on('addDir', dir => {
console.log(`Directory ${dir} has been added`);
fs.mkdirSync(path.join(root, dir.slice(3)), {recursive: true});
RessourcesGenerator.generate(root, path.join(root, "ressources.json"));
})
.on('unlinkDir', dir => {
console.log(`Directory ${dir} has been removed`);
fs.rmdirSync(path.join(root, dir.slice(3)));
RessourcesGenerator.generate(root, path.join(root, "ressources.json"));
});
console.log("Starting http-server")
const server = spawn("node_modules/http-server/bin/http-server", [`${root}`], {stdio: [process.stdin, process.stdout, process.stderr]});
});
});
break;
}