-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdater.js
137 lines (106 loc) · 4.45 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
const fs = require('fs');
const path = require('path');
const axios = require('axios');
const AdmZip = require('adm-zip');
const unknown_api_location = path.join(__dirname, 'unknownvps-api');
const env_path = path.join(unknown_api_location, 'dist/.env')
const temp_env = path.join(__dirname, '.env.temp')
const owner = 'unknownpersonog';
const repo = 'unknownvps-v2';
if (!fs.existsSync(unknown_api_location)) {
fs.mkdir(unknown_api_location, (err) => {
if (err) {
console.error('Error creating the directory:', err);
} else {
console.log('Directory created successfully.');
}
});
}
const appDirectory = path.join(__dirname, 'unknownvps-api');
// Replace 'app' with the directory name of your app
async function downloadLatestRelease() {
try {
// Fetch the latest version.txt from raw.githubusercontent.com
const versionUrl = `https://raw.githubusercontent.com/${owner}/${repo}/master/version.txt`;
console.log('Checking for updates...')
const response = await axios.get(versionUrl);
const latestVersion = response.data.trim();
console.log('Latest version:', latestVersion);
// Get the current version from the local version.txt file
const currentVersion = getCurrentVersion();
console.log('Current version:', currentVersion);
if (latestVersion === currentVersion) {
console.log('No update available.');
return;
}
// Run your installation process here
// This can be a script or a command specific to your app environment
// For example, you might use child_process.spawn() to run a shell command
console.log('Installing new update...');
// Fetch the latest release from GitHub
const apiUrl = `https://api.github.com/repos/${owner}/${repo}/releases/latest`;
const releaseResponse = await axios.get(apiUrl);
const release = releaseResponse.data;
const asset = release.assets.find((asset) => asset.name.endsWith('.zip'));
if (!asset) {
console.log('No suitable release asset found.');
return;
}
const downloadUrl = asset.browser_download_url;
const releaseFileName = path.basename(downloadUrl);
const downloadPath = path.join(__dirname, releaseFileName);
const zipExtractPath = path.join(__dirname, 'latest-release');
const writer = fs.createWriteStream(downloadPath);
console.log('Downloading latest release...');
const downloadResponse = await axios.get(downloadUrl, {
responseType: 'stream',
});
downloadResponse.data.pipe(writer);
await new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
console.log(`Download complete. Latest release saved to ${downloadPath}`);
if (fs.existsSync(env_path)) {
console.log('.env found, temporarily saving it...')
const envData = fs.readFileSync(env_path, 'utf8');
fs.writeFileSync(temp_env, envData, 'utf8');
}
const zip = new AdmZip(downloadPath);
zip.extractAllTo(zipExtractPath, true);
// Delete old app files
fs.rmdirSync(appDirectory, { recursive: true });
fs.renameSync(zipExtractPath, appDirectory);
// Delete the old version.txt file
const oldVersionFile = path.join(__dirname, 'version.txt');
if (fs.existsSync(oldVersionFile)) {
fs.unlinkSync(oldVersionFile);
}
if (fs.existsSync(temp_env)) {
const tempEnvData = fs.readFileSync(temp_env, 'utf8');
fs.writeFileSync(env_path, tempEnvData, 'utf8');
// Delete the temporary .env file
fs.unlinkSync(temp_env);
}
// Save the new version.txt file
const newVersionFile = path.join(__dirname, 'version.txt');
fs.writeFileSync(newVersionFile, latestVersion, 'utf8');
console.log('App has been updated. Restarting the app...');
// Code to restart the app goes here
// This can be done using process.exit() and your app's start command
} catch (err) {
console.error('Error fetching or downloading latest release:', err);
}
}
function getCurrentVersion() {
const versionFilePath = path.join(__dirname, 'version.txt');
if (!fs.existsSync(versionFilePath)) {
// Create the "version.txt" file with default content "0.1"
fs.writeFileSync(versionFilePath, '0.1', 'utf8');
console.log('File "version.txt" created with default content "0.1".');
}
// Read and return the current version from the file
const currentVersion = fs.readFileSync(versionFilePath, 'utf8').trim();
return currentVersion;
}
downloadLatestRelease();