-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspawn.js
74 lines (64 loc) · 2.67 KB
/
spawn.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
const fs = require('fs')
const http = require('http');
const axios = require('axios').default;
const { resolve } = require('path')
const { createCfg } = require('./createCfg');
async function Download(url, dest, func) {
const file = fs.createWriteStream(dest);
axios.get(url, {responseType: 'stream'})
.then(res => res.data)
.then(data => {
data.pipe(file)
file.on("finish", async () => {
console.log('Downloaded Lavlink.')
file.close()
await func()
})
})
}
async function spawnLv() {
if (!fs.existsSync(resolve(__dirname, 'application.yml'))) await createCfg()
else {
fs.unlinkSync(resolve(__dirname, 'application.yml'))
await createCfg();
}
let child = require('child_process').spawn('java', [
`-jar`,
`-Xmx128m`, // fixed memory :D
`-XX:ActiveProcessorCount=${require('os').cpus().length}`,
`-XX:CICompilerCount=${require('os').cpus().length}`,
`-XX:+UseParallelGC`,
resolve(__dirname, 'Lavalink', 'Lavalink.jar')
]);
child.stdout.setEncoding('utf-8')
child.stderr.setEncoding('utf-8')
child.on('spawn', () => console.log('Successfully started Lavalink.'))
.on('exit', (code) => console.log('Exited Lavalink with code • ' + code))
child.stdout.on('data', (data) => {
console.log(`${data}`)
});
child.stderr.on('data', (data) => {
console.error(`${data}`)
});
}
console.log(`Getting Latest Lavalink.jar, Wait for a moment please...`)
axios.get("https://api.github.com/repos/DespenserTeam/Lavalink-arm64/releases/latest")
.then(res => Object(res.data))
.then(json => {
if (json.assets[0] && json.assets[0].browser_download_url) {
console.log(`Found • ${json.assets[0].browser_download_url}`)
Download(json.assets[0].browser_download_url, resolve(__dirname, 'Lavalink', 'Lavalink.jar'), spawnLv)
} else {
console.warn('Unable to find latest .jar')
console.warn('Attempting to download previous version')
let priorVersion = json["tag_name"].split(".")
priorVersion[priorVersion.length -1] = Number(priorVersion[priorVersion.length-1])-1
priorVersion[0] = priorVersion[0].replace("v", "")
priorVersion = priorVersion.join('.')
let priorDL_URL = `https://github.com/freyacodes/Lavalink/releases/download/${priorVersion}/Lavalink.jar`
console.log(`Found • ${priorDL_URL.toString()}`)
Download(priorDL_URL, resolve(__dirname, 'Lavalink', 'Lavalink.jar'), spawnLv)
}
}).catch(err => {
console.error("Error occured when fetching latest release : " + err)
})