-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
138 lines (116 loc) · 3.89 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
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const execSync = require('child_process').execSync;
const fs = require('fs');
const path = require('path');
const extname = path.extname;
const games = require('./games.json');
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path+'/'+file).isDirectory();
});
}
// Check that all the required repos exist.
for(var game in games){
if (!fs.existsSync("git_clones/" + games[game].repo_name)) {
const { stdout, stderr } = execSync('sh partial_clone_game.sh ' + games[game].repo_name + ' ' + games[game].repo_url, {cwd: "git_clones"});
}
}
// Repo pull function, repeats once a minute, loops over all repos in git_clones/ and updates.
async function pull_repos() {
const clone_dirs = getDirectories('git_clones');
for (var i in clone_dirs)
{
const { stdout, stderr } = await exec('git pull', {cwd: "git_clones/" + clone_dirs[i]});
// If we weren't up to date, create a new .tar.gz archive for the files.
await exec("tar --exclude .git -zcvf " + clone_dirs[i] + ".tar.gz " + clone_dirs[i], {cwd: "git_clones/"});
}
// tar -cvf name.tar /path/to/directory
const minutes = 1, pull_interval = minutes * 30 * 1000;
setTimeout(pull_repos, pull_interval);
}
pull_repos();
async function ls_dist_files(dir = "") {
let dirs = getDirectories("git_clones");
if (dir.length > 0)
{
dirs = [dir];
}
var files_json = {};
for (var i in dirs)
{
const dir = dirs[i];
// ChobbyLauncher has directory dist for distribution files
// Games have directory chobbylauncher for chobby launcher config distribution files.
var dist_dir = "";
if (dir !== "spring-launcher-dist")
dist_dir = "dist_cfg/";
const { stdout, stderr } = await exec('git ls-files -s ' + dist_dir + "*", {cwd: "git_clones/" + dir});
// Split by newline after removing trailing newline
const stdout_array = stdout.slice(0, stdout.length - 1).split(/\r?\n/);
var dist_files_json = {};
stdout_array.forEach(function(value)
{
var file_json = {};
// Split by whitespaces or tabulator
const file_array = value.split(/[ \t]+/);
// Element 3 is file name, element 1 is checksum as in the git index. Remove dist/ from file name
file_json["checksum"] = file_array[1];
file_json["path"] = dir + "/" + file_array[3];
dist_files_json[file_array[3].replace(dist_dir, "")] = file_json;
});
files_json[dir] = dist_files_json;
}
return files_json;
}
// Web server.
const Koa = require('koa');
const app = module.exports = new Koa();
var Router = require('koa-router');
var router = new Router();
router.get('/files', async (ctx) => {
ctx.body = await ls_dist_files();
});
router.get('/files/:short', async (ctx) => {
let dir = "";
if (ctx.params.short in games)
dir = games[ctx.params.short].repo_name;
if (ctx.params.short == "launcher")
{
dir = "spring-launcher-dist";
}
if (ctx.params.short == "sb")
dir = "SpringBoard-Core";
ctx.body = await ls_dist_files(dir);
});
router.get('/download', async (ctx) =>
{
const fpath = path.join("git_clones", ctx.query.path);
const fstat = await stat(fpath);
if (fstat.isFile()) {
ctx.type = extname(fpath);
ctx.response.set('Content-disposition', 'attachment; filename=' + ctx.query.path.substring(ctx.query.path.lastIndexOf('/')+1));
ctx.response.set('Content-length', fstat.size);
ctx.body = fs.createReadStream(fpath);
}
});
router.get('/games', async(ctx) =>
{
ctx.body = games;
});
app.use(router.routes());
app.listen(4445);
/**
* thunkify stat. Straight outta koa examples lul.
*/
function stat(file) {
return new Promise(function(resolve, reject) {
fs.stat(file, function(err, stat) {
if (err) {
reject(err);
} else {
resolve(stat);
}
});
});
}