Skip to content

Commit

Permalink
Merge pull request #80 from KotRikD/fix/updater
Browse files Browse the repository at this point in the history
fix: updater can't unarchive and restart downloaded update :/
  • Loading branch information
KotRikD authored Mar 2, 2024
2 parents 3dc8061 + 753cfb1 commit 767311d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 65 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
Copyright (C) 2023-2024 Mikhail Babynichev <https://kotrik.ru/>
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,5 @@ Contributions, issues and feature requests are welcome!<br />Feel free to check

## 📝 License

Copyright © 2023 [Mikhail Babynichev](https://github.com/KotRikD).<br />
This project is [GPL--3.0](https://github.com/KotRikD/tosu/blob/master/LICENSE) licensed.
Copyright © 2023-2024 [Mikhail Babynichev](https://github.com/KotRikD).<br />
This project is [LGPL-3.0](https://github.com/KotRikD/tosu/blob/master/LICENSE) licensed.
32 changes: 0 additions & 32 deletions packages/common/utils/unzip.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,8 @@
import AdmZip from 'adm-zip';
import fs from 'fs';
import path from 'path';

import { wLogger } from './logger';

export const unzipTosu = (
zipPath: string,
extractPath: string
): Promise<string> =>
new Promise((resolve, reject) => {
const zip = new AdmZip(zipPath);

zip.getEntries().some((entry) => {
if (entry.entryName === 'tosu' || entry.entryName === 'tosu.exe') {
const fileName = path.basename(entry.entryName);
const { name, ext } = path.parse(fileName);
const modifyName = path.join(extractPath, `${name}_new${ext}`);

return fs.writeFile(
modifyName,
entry.getData(),
{ flag: 'w' },
(err) => {
if (err) {
return reject(err);
}

return resolve(modifyName);
}
);
}
});

reject('No matching entry found in the zip file.');
});

export const unzip = (zipPath: string, extractPath: string): Promise<string> =>
new Promise((resolve, reject) => {
if (!fs.existsSync(extractPath)) {
Expand Down
42 changes: 12 additions & 30 deletions packages/updater/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {
downloadFile,
platformResolver,
sleep,
unzipTosu,
unzip,
wLogger
} from '@tosu/common';
import { exec, spawn } from 'child_process';
import { spawn } from 'child_process';
import fs from 'fs';
import path from 'path';

Expand All @@ -14,7 +14,6 @@ const currentVersion = require(process.cwd() + '/_version.js');

const repositoryName = 'tosu';
const fileDestination = path.join(process.cwd(), 'update.zip');
const newExecutablePath = path.join(process.cwd(), 'tosu.exe');
const backupExecutablePath = path.join(process.cwd(), 'tosu_old.exe');

const deleteNotLocked = async (filePath: string) => {
Expand All @@ -35,9 +34,7 @@ export const autoUpdater = () =>
new Promise(async (resolve) => {
wLogger.info('Checking updates');

const { platformType, platformFileType } = platformResolver(
process.platform
);
const { platformType } = platformResolver(process.platform);

if (platformType === '') {
wLogger.warn(
Expand All @@ -64,6 +61,7 @@ export const autoUpdater = () =>

if (fs.existsSync(fileDestination))
await deleteNotLocked(fileDestination);
await sleep(5 * 1000);
if (fs.existsSync(backupExecutablePath))
await deleteNotLocked(backupExecutablePath);

Expand All @@ -86,39 +84,23 @@ export const autoUpdater = () =>
fileDestination
);

const unzipExecutable = await unzipTosu(downloadAsset, process.cwd());

const currentExecutablePath = process.argv[0]; // Path to the current executable

await fs.promises.rename(currentExecutablePath, backupExecutablePath);
await fs.promises.rename(unzipExecutable, newExecutablePath);

await unzip(downloadAsset, process.cwd());

wLogger.info('Restarting program');

// Start the updated executable
const oldProcess = spawn(backupExecutablePath, [], {
spawn(process.argv[0], process.argv.slice(1), {
detached: true,
shell: true,
stdio: 'ignore'
});

oldProcess.unref();
}).unref();

exec(
`start "" "${newExecutablePath}"`,
async (error, stdout, stderr) => {
if (error) {
console.error(`Error starting updated process: ${error}`);
return;
}
wLogger.info('Closing program');

await sleep(2500);
await sleep(1000);

wLogger.info('Closing program');

await sleep(1000);

oldProcess.kill();
process.exit();
}
);
process.exit();
});

0 comments on commit 767311d

Please sign in to comment.