-
Notifications
You must be signed in to change notification settings - Fork 0
/
updater.js
152 lines (138 loc) · 4.16 KB
/
updater.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
import inquirer from "inquirer";
import fs from 'fs';
import ProgressBar from 'progress';
import {purpurAxios} from './config.js'
const { SERVER_DIR_NAME, EXTRA_DIR_NAME } = process.env;
const VERSIONS_FILEPATH = `./${EXTRA_DIR_NAME}/versions.json`;
const CANCEL = 'cancel';
const getVerionsList = async () => {
try {
const response = await purpurAxios.get("/purpur")
return response.data.versions;
} catch(err) {
console.error(err);
}
}
const getBuildsList = async (version) => {
try {
const response = await purpurAxios.get(`/purpur/${version}`);
return response.data.builds.all;
} catch(err) {
console.error(err);
}
}
const getCurrentVersion = () => {
try {
const data = JSON.parse(fs.readFileSync(VERSIONS_FILEPATH));
const {pupurmcVersion, pupurmcBuild} = data;
return {pupurmcVersion, pupurmcBuild}
} catch(err) {
if(err.code === 'ENOENT') {
return {pupurmcVersion: null, pupurmcBuild: null}
}
console.error(err);
}
}
const setCurrentVersion = ({version, build}) => {
try {
const payload = {
pupurmcVersion: version,
pupurmcBuild: build
}
const dataStr = JSON.stringify(payload, null, 2);
fs.writeFileSync(VERSIONS_FILEPATH, dataStr);
return true;
} catch(err) {
console.error(err);
}
}
const askQuestions = async () => {
const {pupurmcVersion: currentVersion, pupurmcBuild: currentBuild} = await getCurrentVersion();
if (currentVersion) {
console.log(`📌 Current pupurmc server is version: ${currentVersion} build: ${currentBuild}`);
}
const versions = await getVerionsList();
const questions1 = [
{
type: 'list',
name: 'chosedVersion',
message: 'chose version of purpurmc server for install',
default: versions[versions.length-1],
choices: [...versions, CANCEL],
loop: false,
}]
const {chosedVersion} = await inquirer.prompt(questions1);
if (chosedVersion === CANCEL) {
console.log('🤔 nothing to update');
return false;
}
const builds = await getBuildsList(chosedVersion);
const questions2 = [
{
type: 'list',
name: 'chosedBuild',
message: `chose build of ${chosedVersion} version purpurmc server for install`,
default: builds[builds.length-1],
choices: [...builds, CANCEL],
loop: false,
},
{
type: 'confirm',
name: 'sameVersionConfirm',
message: 'this version build already installed. Reinstall?',
when(answers) {
const {chosedBuild} = answers;
return chosedVersion === currentVersion && chosedBuild === currentBuild;
},
default: false,
}
]
const {chosedBuild, sameVersionConfirm} = await inquirer.prompt(questions2);
if (chosedVersion === CANCEL || chosedBuild === CANCEL || sameVersionConfirm === false) {
console.log('🤔 nothing to update');
return false;
}
return {chosedVersion, chosedBuild};
}
const downloadJar = async ({version, build}) => {
try {
const { data, headers } = await purpurAxios.get(`/purpur/${version}/${build}/download`, {
responseType: 'stream'
})
const totalLength = headers['content-length']
const progressBar = new ProgressBar('🚧 downloading [:bar] :percent :etas 🚧', {
width: 80,
complete: '\u2588',
incomplete: '\u2591',
renderThrottle: 1,
total: parseInt(totalLength)
})
data.on('data', (chunk) => progressBar.tick(chunk.length))
const writer = fs.createWriteStream(`./${SERVER_DIR_NAME}/server.jar`, {recursive: true});
data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve.bind(null, true))
writer.on('error', reject)
})
} catch(err) {
console.error(err);
}
}
export const update = async () => {
try {
const result = await askQuestions();
if (result) {
const {chosedVersion, chosedBuild} = result;
const payload = {version: chosedVersion, build: chosedBuild};
const downloaded = await downloadJar(payload);
if (downloaded) {
const result = setCurrentVersion(payload)
if (result) {
console.log('Update completed 🙌')
}
}
}
} catch(err) {
console.error(err);
}
}