-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (143 loc) · 3.98 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
/* System libraries */
const express = require("express");
const fs = require('fs');
const fsmonitor = require('fsmonitor');
const path = require('path');
const md5File = require('md5-file');
const recursiveReadDir = require("recursive-readdir");
/* Constants */
const ROOT_DIRECTORY = './testfolder/';
const MATCH_CONFIG = {
matches: function(relpath) {
return relpath.match(/\.lua$/i) !== null;
},
excludes: function(relpath) {
return relpath.match(/^\.git$/i) !== null;
},
};
/* App */
const util = require('./lib/util.js');
const app = express()
app.use(require('body-parser').json());
app.longpoll = require("express-longpoll")(app);
function validatePathMiddle(req, res, next)
{
let filepath = req.body.filepath;
if (!filepath) {
console.warn('Failed request, no file path');
return res.status(400).json({
error: 'No filepath'
});
}
if (!util.isValid(filepath)) {
console.warn('Failed request, bad file path,', filepath);
return res.status(422).json({
error: 'Bad file path'
});
}
req.filepath = path.resolve(ROOT_DIRECTORY, filepath) + '.lua';
return next();
};
/* Routing */
app.post('/filechanged', validatePathMiddle, function(req, res) {
let filepath = req.body.filepath;
let content = req.body.content;
if (!content) {
res.status(400).json({
error: 'No content'
});
return;
}
// let fileName = path;
// fs.writeFile(file, content);
})
app.get('/', function(req, res) {
res.send('Hello World');
});
app.post('/files/source', validatePathMiddle, function(req, res) {
console.assert(req.filepath, "expected filepath");
let content = fs.readFile(req.filepath, 'utf8', function(err, data) {
if (err) {
console.error(err);
return res.status(500).json({
error: err,
});
}
return res.send(data);
});
});
app.get('/files', function(req, res) {
recursiveReadDir(ROOT_DIRECTORY, function(err, items) {
if (err) {
return req.status(500).json({
error: err,
});
}
/* Parse items to include only items wanted */
let newItems = {};
for (let i in items) {
let basepath = items[i];
let relpath = path.relative(ROOT_DIRECTORY, basepath);
let totalpath = path.resolve(basepath);
/* Strip .lua */
if (MATCH_CONFIG.matches(relpath) && !MATCH_CONFIG.excludes(relpath))
{
// Could make this a promise.all()
let hash = md5File.sync(path.join(ROOT_DIRECTORY, relpath));
newItems[util.toLuaPath(relpath)] = hash;
} else {
console.warn('bad path', relpath);
}
}
return res.json(newItems);
})
})
// /* Check */
// app.post('/hashes/', function(req, res) {
// let fileToHash = req.body.fileToHash;
// if (!fileToHash) {
// res.status(400).json({
// error: 'No fileToHash';
// });
// return;
// }
// let different = [];
// for (let path in fileToHash) {
// if (!util.isValid(path)) {
// res.status(422).json({
// error: 'Bad file path'
// });
// return;
// }
// let hash = fileToHash[path];
// let systemHash = md5File(path);
// if (hash != systemHash) {
// different[path] = systemHash;
// }
// }
// });
let watcher = fsmonitor.watch(ROOT_DIRECTORY, MATCH_CONFIG);
app.longpoll.create('/filechanged/poll', function(req, res, next) {
// console.log('New listener to poll');
next();
});
function fixFilePaths(paths) {
let newPaths = {};
for (let i in paths) {
let relpath = paths[i];
let hash = md5File.sync(path.join(ROOT_DIRECTORY, relpath));
newPaths[util.toLuaPath(relpath)] = hash;
}
if (!newPaths) {
return undefined;
}
return newPaths;
}
watcher.on('change', function(changes) {
let changesClean = {};
changesClean['ModifiedFiles'] = fixFilePaths(changes['modifiedFiles']);
console.log(changesClean);
app.longpoll.publish('/filechanged/poll', changesClean);
});
/* Load server */
app.listen(3000, () => console.log('Example app listening on port 3000!'))