forked from bepass-org/oblivion-desktop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlWp.ts
150 lines (131 loc) · 4.4 KB
/
dlWp.ts
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
// download warp-plus binary
import fs from 'fs';
import axios from 'axios';
import decompress from 'decompress';
import { doesDirectoryExist, doesFileExist } from '../src/main/lib/utils';
import { wpVersion } from '../src/main/config';
let forceDownload = false;
if (process.argv[2] && process.argv[2] === 'force') {
forceDownload = true;
}
let platform: any = process.platform;
if (typeof process.argv[3] === 'string') {
platform = process.argv[3];
}
console.log('➡️ platform:', platform);
let arch: any = process.arch;
if (typeof process.argv[4] === 'string') {
arch = process.argv[4];
}
console.log('➡️ arch:', arch);
async function downloadFile(uri: string, destPath: string) {
return axios
.get(uri, {
responseType: 'arraybuffer',
onDownloadProgress: (progressEvent) => {
// TODO improve DX
const percentCompleted = Math.round(
// @ts-ignore
(progressEvent.loaded * 100) / progressEvent.total
);
try {
process?.stdout?.clearLine(0);
process?.stdout?.cursorTo(0);
process?.stdout?.write(`Downloading ${uri}: ${percentCompleted}%`);
} catch (error) {
if (
!String(error).includes(
'TypeError: process?.stdout?.clearLine is not a function'
)
) {
console.error(error);
}
}
}
})
.then((response) => {
const arrayBufferView = new Uint8Array(response.data);
const buffer = Buffer.from(arrayBufferView);
fs.writeFileSync(destPath, buffer);
// console.log(`Downloaded ${uri} and saved it to ${destPath}`);
console.log();
})
.catch((error) => {
console.error(`Failed to download ${uri}:`, error.message);
});
}
// download, unzip and move(rename)
const dlUnzipMove = async (url: string) => {
const binPath = './assets/bin';
const isBinDirExist = await doesDirectoryExist(binPath);
if (!isBinDirExist) {
fs.mkdir(binPath, { recursive: true }, (err) => {
if (err) {
console.error(`Error creating directory ${binPath}:`, err);
}
});
}
const zipFilePath = `./warp-plus-${wpVersion}.zip`;
const isZipFileExist = await doesFileExist(zipFilePath);
if (!isZipFileExist || forceDownload) {
console.log('downloading warp-plus binary based on your platform and architecture...');
await downloadFile(url, zipFilePath);
} else {
console.log(`➡️ Skipping Download as ${zipFilePath} already exist.`);
}
decompress(zipFilePath, binPath)
.then(() => {
console.log('✅ warp-plus binary is ready to use.');
// removing wintun.dll cause it's getting flagged by antivirus's
const wintunPath = binPath + '/wintun.dll';
doesFileExist(wintunPath).then((isExist) => {
if (isExist)
fs.rm(wintunPath, (err) => {
if (err) {
console.error(`Error removing ${wintunPath}:`, err);
}
});
});
})
.catch((error) => {
console.log(error);
});
};
const baseUrl = `https://github.com/bepass-org/warp-plus/releases/download/${wpVersion}/warp-plus_`;
const urls: any = {
linux: {
x64: baseUrl + 'linux-amd64.zip',
arm64: baseUrl + 'linux-arm64.zip'
},
win32: {
x64: baseUrl + 'windows-amd64.zip',
arm64: baseUrl + 'windows-arm64.zip',
ia32: baseUrl + 'windows-386.zip'
},
darwin: {
x64: baseUrl + 'darwin-amd64.zip',
arm64: baseUrl + 'darwin-arm64.zip'
}
};
const notSupported = () => {
console.log('your platform/architecture is not supported.');
};
switch (platform) {
case 'linux':
case 'win32':
case 'darwin':
switch (arch) {
case 'x64':
case 'arm64':
case 'ia32':
dlUnzipMove(urls[platform][arch]);
break;
default:
notSupported();
break;
}
break;
default:
notSupported();
break;
}