-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
226 lines (194 loc) · 6.73 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
#!/usr/bin/env node
var FirebaseServer = null
var express = null
var server = null
var webApp = null
var WebSocketServer = null
var wss = null
var spawn = null
var fs = null
var readline = null
var tidal = null
function sanitizeStringForTidal (x) {
var result = x.replace(/\n/g, '')
result = result.replace(/\t/g, ' ')
return result
}
/*************************
Start up any Command Line Interface stuff
*************************/
var prog = require('caporal')
const winston = require('winston');
const logger = winston.createLogger({
format: winston.format.combine(
//color must be first
winston.format.colorize(),
winston.format.simple()
),
transports: [
new winston.transports.Console()
]
});
prog
.version('0.1.0')
.logger(logger)
.option('-d, --debug <debug>', 'Enable Debugging', prog.BOOL, false)
.option('-f, --firebase <firebase>', 'Enable local Firebase', prog.BOOL, false)
.option('-o, --fireport <fireport>', 'Set firebase on this port', prog.INT, 5000)
.option('-n, --firehost <firehost>', 'Set firebase server', null, 'localhost.firebaseio.test')
.option('-w, --webhost <webhost>', 'Use Locahost website', prog.BOOL, true)
.option('-e, --webport <webport>', 'Set Locahost website port', prog.INT, 8000)
.option('-s, --websocket <websocket>', 'Use websocket', prog.BOOL, 'true')
.option('-k, --websocketport <websocketport>', 'Set websocket port', prog.INT, 8002)
.option('-i, --tidal <tidal>', 'Use TidalCycles', prog.BOOL, true)
.option('-l, --tidalbootfile <tidalbootfile>', 'Set tidalcycles boot file', null, 'ghciSuperDirt')
.action(function (args, options, logger) {
/*************************
Info about Degugging status
*************************/
if (options.debug) {
logger.info('Debugging Enabled')
}
/*************************
Start up any firebase stuff
*************************/
if (options.firebase) {
// if (options.debug) {
// debug.enable('firebase-server*');
// }
FirebaseServer = require('./localFirebase.js')
new FirebaseServer(options.fireport, options.firehost, {})
logger.info('Firebase Listening on: ' + options.fireport)
} else {
logger.warn('Not using local Firebase')
}
/*************************
Start up any Website stuff
*************************/
if (options.webhost) {
express = require('express')
server = require('http').createServer()
webApp = express()
webApp.use(express.static('public'))
server.on('request', webApp)
// console.log(options.webport);
server.listen(options.webport, function () {
logger.info('Webhost Listening on: ' + options.webport)
})
} else {
logger.info('Not webhosting site')
}
/*************************
Start up any Web Socket stuff
*************************/
if (options.websocket) {
WebSocketServer = require('ws').Server
wss = new WebSocketServer({
server: server,
port: options.websocketport
})
wss.on('connection', function (ws) {
ws.on('message', function (m) {
var n = JSON.parse(m)
if (n.request === 'eval') {
if (options.debug) {
logger.info(sanitizeStringForTidal(n.code))
}
tidal.stdin.write(sanitizeStringForTidal(n.code) + '\n')
}
})
ws.on('close', function (code, msg) {
logger.info('Web Socket closed')
})
ws.on('error', function () {
logger.error('Web Socket error')
})
// try to send hello message
try {
ws.send(JSON.stringify({
type: 'status',
message: 'websocket connected to server'
}))
} catch (e) {
logger.error('Web Socket send error: ' + e)
return
}
logger.info('Web Socket added connection on port: ' + options.websocketport)
})
// wss.mySend = function mySend(code, msg) {
// try {
// // wss.clients.forEach(function each(client) {
// console.log(ws);
// // if (client.readyState === WebSocket.OPEN) {
// wss.send(JSON.stringify({
// type: code,
// message: msg
// }));
// // }
// // });
// } catch (e) {
// logger.error("Web Socket send error: " + e);
// return;
// }
// };
} else {
logger.info('Not using websockets')
}
/*************************
Start up any Tidal stuff
*************************/
if (options.tidal) {
spawn = require('child_process').spawn
fs = require('fs')
readline = require('readline')
tidal = spawn('ghci', ['-XOverloadedStrings'])
var feedbackTimer = null
var feedbackToSend = ''
tidal.on('close', function (code) {
readline.close()
logger.error('Tidal process exited with code ' + code + '\n')
})
readline.createInterface({
input: tidal.stdout,
terminal: false
}).on('line', function (line) {
logger.info(line)
})
readline.createInterface({
input: tidal.stderr,
terminal: false
}).on('line', function (line) {
logger.error(line)
feedbackToSend += line + '\n'
clearTimeout(feedbackTimer)
feedbackTimer = setTimeout(sendIt, 100)
}).on('pause', function () {
logger.info('exit')
})
fs.readFile(options.tidalbootfile, 'utf8', function (err, data) {
if (err) {
logger.error('Tidal could not read file, ' + options.tidalbootfile + ' : ' + err)
return
}
tidal.stdin.write(data)
logger.info('Tidal & GHCI initialized')
})
var sendIt = function () {
if (options.websocket) {
try {
// wss.send(JSON.stringify({
// type: 'feedback',
// message: feedbackToSend
// }))
} catch (e) {
logger.error('Web Socket send error: ' + e)
return
}
feedbackToSend = ''
}
}
} else {
logger.warn('Not using Tidal')
}
})
prog.parse(process.argv)