autoStart: rewrite into monolithic file

This commit is contained in:
Ducko 2023-03-23 21:55:09 +00:00
parent a1be7204df
commit 3a52bd2ac7
5 changed files with 75 additions and 54 deletions

75
src/autoStart.js Normal file
View File

@ -0,0 +1,75 @@
const fs = require('fs');
const { join, basename, dirname } = require('path');
const { app } = require('electron');
const buildInfo = require('./utils/buildInfo');
const desktopPath = join(app.getPath('appData'), 'autostart', app.name + '-' + buildInfo.releaseChannel + '.desktop');
const exec = app.getPath('exe');
// Windows registry util
const reg = (a, c) => require('child_process').execFile('reg.exe', a, c);
const appName = basename(exec, '.exe');
const queuePrefix = [ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', '/v', appName ];
exports.install = cb => {
switch (process.platform) {
case 'win32':
// Make reg (with Electron args if start min)
reg([ 'add', ...queuePrefix, '/d', '"' + join(exec, '..', '..', 'Update.exe') + '" --processStart ' + basename(exec) + (settings.get('START_MINIMIZED') ? ' --process-start-args --start-minimized' : ''), '/f' ], cb);
break;
case 'linux':
// Write desktop file for autostart
fs.mkdirSync(dirname(desktopPath), { recursive: true });
fs.writeFile(desktopPath, `[Desktop Entry]
Type=Application
Exec=${exec}
Name=${basename(exec)}
Icon=${join(global.systemElectron ? '/usr/share/pixmaps' : dirname(exec), 'discord.png')}
Comment=Text and voice chat for gamers.
X-GNOME-Autostart-enabled=true`, cb);
break;
default:
cb();
}
};
exports.uninstall = cb => {
switch (process.platform) {
case 'win32':
// Delete reg
reg([ 'delete', ...queuePrefix, '/f' ], () => cb());
break;
case 'linux':
// Delete autostart desktop file
fs.unlink(desktopPath, cb);
break;
default:
cb();
}
};
exports.update = cb => exports.isInstalled(installed => installed ? exports.install(cb) : cb());
exports.isInstalled = cb => {
switch (process.platform) {
case 'win32':
// Check reg
reg([ 'query', ...queuePrefix ], (e, out) => cb(out.includes(appName)));
break;
case 'linux':
// Check autostart desktop file exists
fs.access(desktopPath, fs.constants.F_OK, cb);
break;
default:
cb(false);
}
};

View File

@ -1,5 +0,0 @@
// Stub (Darwin)
exports.install = c => c();
exports.update = c => c();
exports.uninstall = c => c();
exports.isInstalled = c => c(false);

View File

@ -1 +0,0 @@
module.exports = require('./' + process.platform);

View File

@ -1,33 +0,0 @@
const fs = require('fs');
const { join, basename, dirname } = require('path');
const { app } = require('electron');
const buildInfo = require('../utils/buildInfo');
const desktopPath = join(app.getPath('appData'), 'autostart', app.name + '-' + buildInfo.releaseChannel + '.desktop');
// Vars for use in desktop file content template
const exec = app.getPath('exe');
// Template for desktop file
const desktopContent = `[Desktop Entry]
Type=Application
Exec=${exec}
Name=${basename(process.execPath)}
Icon=${join(global.systemElectron ? '/usr/share/pixmaps' : dirname(exec), 'discord.png')}
Comment=Text and voice chat for gamers.`;
exports.install = (cb) => {
try {
fs.mkdirSync(dirname(desktopPath), { recursive: true });
fs.writeFile(desktopPath, desktopContent, cb);
} catch {
cb(); // Callback anyway
}
};
exports.update = (cb) => cb();
exports.uninstall = (cb) => fs.unlink(desktopPath, cb);
exports.isInstalled = (cb) => fs.access(desktopPath, fs.constants.F_OK, cb);

View File

@ -1,15 +0,0 @@
const { join, basename } = require('path');
const reg = (a, c) => require('child_process').execFile('reg.exe', a, c);
const appName = basename(process.execPath, '.exe');
const queuePrefix = [ 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Run', '/v', appName ];
exports.install = (cb) => reg([ 'add', ...queuePrefix, '/d', '"' + join(process.execPath, '..', '..', 'Update.exe') + '" --processStart ' + basename(process.execPath) + (settings.get('START_MINIMIZED') ? ' --process-start-args --start-minimized' : ''), '/f' ], cb); // Make reg (with Electron args if start min)
exports.update = (cb) => exports.isInstalled(installed => installed ? exports.install(cb) : cb()); // Reinstall if installed, else just cb
exports.uninstall = (cb) => reg([ 'delete', ...queuePrefix, '/f' ], () => cb()); // Delete reg
exports.isInstalled = (cb) => reg([ 'query', ...queuePrefix ], (e, out) => cb(out.includes(appName))); // Check reg