Skip to content

Commit 588c3cd

Browse files
committed
Test update process using libraries
1 parent de10c74 commit 588c3cd

File tree

3 files changed

+82
-59
lines changed

3 files changed

+82
-59
lines changed

lib/menus/update.js

Lines changed: 15 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Imports the necessary libraries
22
import { select } from '@inquirer/prompts';
33
import { splashLogo } from 'helpbot/loaders';
4+
import AutoGitUpdate from 'auto-git-update';
45

56
// Stores the name of the menu
67
const menuName = new URL(import.meta.url).pathname.split('/').pop().replace('.js', '');
@@ -166,67 +167,22 @@ export default async (beforeError, forceUpdate) => {
166167
// Shows a message indicating that the bot is downloading the new version
167168
console.log(`⬇️ ${await client.functions.utils.parseLocale(locale.downloadingNewVersion, { targetVersion: `v${latestRemoteTag}`, repositoryURL: repoUrl })} ...`);
168169

169-
// Stores the working directory
170-
const workingDirectory = String(process.cwd());
171-
172-
// Stores the temporary directory
173-
const tmpDir = `${workingDirectory}/tmp`;
174-
175170
try {
176171

177-
// Creates the temporary directory if it doesn't exist, or empties it if it does
178-
if (!fs.existsSync(tmpDir)) await fs.promises.mkdir(tmpDir);
179-
else await fs.promises.rm(tmpDir, { recursive: true });
180-
181-
// Clones the repository in the temporary directory
182-
await git.clone(repoUrl, tmpDir);
183-
184-
// Changes the working directory to the temporary directory
185-
process.chdir(tmpDir);
186-
187-
// Stops the process
188-
process.exit(0); // --- EXPERIMENTAL ---
189-
190-
// Switches to the latest remote tag
191-
await git.fetch(['--all']);
192-
await git.reset(['--hard', latestRemoteTag]);
193-
194-
// Changes the working directory back to the original directory
195-
process.chdir(workingDirectory);
196-
197-
// Creates an array with the files to exclude from the update
198-
const excludeFiles = ['./config.json'];
199-
200-
// Copied the files from the temporary directory to the original directory
201-
await fs.readdir('./tmp', async (err, files) => {
202-
203-
// If an error occurred, throws it
204-
if (err) throw err;
205-
206-
// For each file in the temporary directory
207-
for (const file of files) {
208-
209-
// If the file is not in the exclude list, copies it
210-
if (!excludeFiles.includes(file)) await fs.copy(`./tmp/${file}`, `./${file}`);
211-
};
212-
});
213-
214-
// Deletes the files that are not in the temporary directory
215-
await fs.readdir('.', async (err, files) => {
216-
217-
// If an error occurred, throws it
218-
if (err) throw err;
219-
220-
// For each file in the original directory
221-
for (const file of files) {
222-
223-
// If the file is not in the temporary directory, deletes it
224-
if (!fs.existsSync(`./tmp/${file}`)) await fs.unlink(`./${file}`);
225-
};
226-
});
227-
228-
// Deletes the temporary directory
229-
await fs.promises.rmdir('./tmp');
172+
// Generates athe configuration for the updater
173+
const updateConfiguration = {
174+
repository: repoUrl,
175+
fromReleases: false,
176+
branch: localConfig.betaEnabled = localConfig.betaEnabled ? 'beta' : 'master',
177+
tempLocation: './tmp/',
178+
ignoreFiles: ['config.json']
179+
}
180+
181+
// Creates a new instance of the updater
182+
const updater = new AutoGitUpdate(updateConfiguration);
183+
184+
// Updates the bot
185+
await updater.autoUpdate();
230186

231187
// Requires the child_process library
232188
const { exec } = require('child_process');

package-lock.json

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"@sentry/node": "^7.64.0",
2929
"@sentry/tracing": "^7.74.1",
3030
"asciiart-logo": "^0.2.7",
31+
"auto-git-update": "^1.1.1",
3132
"bufferutil": "^4.0.8",
3233
"discord.js": "^14.14.1",
3334
"inquirer": "^9.2.11",

0 commit comments

Comments
 (0)