-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.js
74 lines (63 loc) · 1.9 KB
/
srv.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
var program = require('commander');
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs-extra');
var babel = require("babel-core");
var staticfolder = __dirname + '/static';
var vendfolder = __dirname + '/node_modules';
// CLI
program
.version('0.0.1')
.option('-s, --style [stil]', 'Einen Stil auswählen', '')
.parse(process.argv);
// Collect Style
var style;
if (program.style) {
style = program.style.toLowerCase();
}
else {
style = 'default';
}
fs.copySync(staticfolder + '/' + style + '.css', staticfolder + '/style.css');
// Generate ES5
var generate_es5 = function(filename) {
var options = {presets: ["es2015"]},
code = babel.transformFileSync(
staticfolder + '/' + filename + '.es6.js',
options
).code;
fs.writeFileSync(staticfolder + '/' + filename + '.js', code);
};
generate_es5('digispt');
// Static Data
app.use('/vend', express.static(vendfolder));
app.use(express.static(staticfolder));
app.get('/', function(req, res){
res.sendFile('view.html', {root: staticfolder});
});
app.get('/admin', function(req, res){
res.sendFile('admin.html', {root: staticfolder});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
// Connections
var data = { last: "{}" };
data.last = JSON.parse(fs.readFileSync(__dirname + '/data.default.json'));
var datafile = __dirname + '/data.json';
try {
data.last = JSON.parse(fs.readFileSync(datafile));
} catch (ex) { }
io.on('connection', function(socket){
console.log('a user connected');
socket.on('_changedata', function(msg){
console.log("change", msg);
io.emit('changedata', msg);
data.last = JSON.parse(msg);
fs.writeFileSync(datafile, JSON.stringify(data.last));
});
io.emit('init', JSON.stringify(data.last));
io.emit('changedata', JSON.stringify(data.last));
});