forked from somiibo/tiktok-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
98 lines (81 loc) · 2.31 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
// Modules
const os = require('os');
const fs = require('fs-jetpack');
const path = require('path');
const fetch = require('wonderful-fetch');
const downloads = require('downloads-folder');
const util = require('util');
const execute = util.promisify(require('child_process').exec);
// Main
module.exports = async function () {
// Options
const url = getURL();
const location = path.join(downloads(), url.split('/').slice(-1)[0]);
const interval = setInterval(() => {
log('Still downloading, please be patient! :)');
}, 3000);
// Clear
fs.remove(location)
log(`Downloading app to ${location}`)
// Process
const res = await fetch(url);
const fileStream = fs.createWriteStream(location);
await new Promise((resolve, reject) => {
res.body.pipe(fileStream);
res.body.on('error', reject);
fileStream.on('finish', resolve);
})
.then((r) => {
log('Download finished!');
launch(location);
})
.catch((e) => {
error('Download failed!', e);
})
.finally(() => {
clearInterval(interval);
})
return
};
if (require.main === module) {
module.exports();
}
function getURL() {
const name = os.type();
if (name === 'Darwin') {
return 'https://github.com/somiibo/download-server/releases/download/installer/Somiibo.dmg'
} else if (name === 'Windows_NT') {
return 'https://github.com/somiibo/download-server/releases/download/installer/Somiibo-Setup.exe'
} else {
return 'https://github.com/somiibo/download-server/releases/download/installer/Somiibo_amd64.deb'
}
}
function launch(location) {
const name = os.type();
try {
if (name === 'Darwin') {
execute(`open "${location}"`)
.then(() => {
})
} else if (name === 'Windows_NT') {
execute(`"${location}"`)
.then(() => {
})
} else {
execute(`sudo apt install "${location}"`)
.then(() => {
execute(`restart-manager`).catch(e => {console.error(e)})
})
}
} catch (e) {
error('Application failed to execute:', e);
console.log('\n\n\n')
log(`Please launch the app manually: ${location}`);
}
}
function log() {
console.log(`[${new Date().toLocaleTimeString()}]`, ...arguments)
}
function error() {
console.error(`[${new Date().toLocaleTimeString()}]`, ...arguments)
}