-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
159 lines (141 loc) · 4.46 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
const globals = require('./globals'),
{
ipc,
Spawner,
logger,
History,
io,
response,
log,
currentTime,
current,
pastSongs,
isPlaying,
http,
url,
fs,
path,
ipcResponse,
shortcuts,
pianobarLog,
localtunnel,
opn,
config,
notifier,
express,
nodeCleanup,
pianobarConfig,
chalk,
ServerCommands
} = globals,
SpawnImmediately = true,
spawnInstance = new Spawner(SpawnImmediately, {
onExit: function (exitCode, signal) {
if (signal === 'SIGINT')
process.kill(process.pid, 'SIGINT');
ipc.server.stop()
globals.shortcuts.destroy()
process.exit()
},
onEnd: function () {
ipc.server.stop()
globals.shortcuts.destroy()
process.exit()
},
onData: function (data) {
const getTime = /(\d\d:\d\d).(\d\d:\d\d)/
if (getTime.test(data)) {
const [now, ofTotal] = Array.from(data.match(getTime)).slice(1)
currentTime.push({ now, ofTotal })
//ipc.server.emit('currentTime',currentTime)
const lastTimes = currentTime.getNewest(7)
if (lastTimes.every(({ now }) => now === lastTimes[0].now) && isPlaying.getNewest() === true && lastTimes.length == 7) {
isPlaying.push(false)
}
} else {
console.log(data.trim())
pianobarLog.push(data.trim())
}
}
}),
app = express(),
server = http.Server(app),
socket = io.listen(server),
subdomain = process.argv[3] || process.argv[2] || 'pandora',
port = process.argv[2] || globals.port
globals.spawnInstance = spawnInstance
globals.shortcuts = shortcuts({ ipc, current, pastSongs, log, currentTime, isPlaying, spawnInstance, logger, response, notifier, config })
if (config.get('listenShortcuts')) {
globals.shortcuts.init()
}
app.use(express.static(path.resolve(__dirname, 'client/dist')))
server.listen(port)
// Add a connect listener
socket.on('connection', function (client) {
const obj = {
spawnInstance,
isPlaying,
current,
currentTime,
pastSongs,
log,
logger,
pianobarLog,
pianobarConfig,
config,
ServerCommands,
shortcuts: globals.shortcuts
}
//needs to be seperate from globals because attachments work per instance of connection
Object.keys(response).forEach(key => {
client.on(key, response[key](client, obj))
})
response.init(client, obj)()
});
currentTime.push({ now: '00:01', ofTotal: '59:59' })
isPlaying.push(true)
ipc.config.id = 'pianobar-server';
ipc.config.retry = 100;
ipc.config.silent = true;
ipc.serve(() => {
const response = ipcResponse(globals)
Object.keys(response).forEach(key => {
const responseFunc = response[key]
ipc.server.on(key, responseFunc)
})
})
ipc.server.start();
ServerCommands.checkIfRunning(ServerCommands.launcherName).then(() => { console.log("Launcher already running") }).catch(() => {
ServerCommands.startLauncher(port + 1).catch(() => console.log('unable to start launcher'))
});
(function tunneller() {
var store;
localtunnel(port, { subdomain }, function (err, tunnel) {
if (err) {
pianobarLog.unpush(store)
pianobarLog.push('Restarting Local Tunnel...')
console.log('Restarting Local Tunnel...', err)
setTimeout(tunneller, 500)
return
//process.exit(2)
}
const message = chalk.green(`Your local tunnel URL: ${chalk.blue(tunnel.url)}\nYour local URL: ${chalk.blue(`http://localhost:${port}/`)}`)
console.log(message)
if (!pianobarLog.getOldest(1).startsWith("Your")) {
pianobarLog.store.unshift(message)
}
store = pianobarLog.onpush((state, length) => {
if (length === pianobarLog.size) {
pianobarLog.store.unshift(message)
}
})
if (config.get('openTunnelURL') && !config.get('willRestart')) {
opn(tunnel.url)
config.set('willRestart', false)
}
})
})()
nodeCleanup(function (exitCode, signal) {
spawnInstance.pianobar.kill()
spawnInstance.options.onEnd(exitCode, signal)
})