From 3a52bd2ac704bf43e57e55d4adb9c8971c5ff9cb Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Thu, 23 Mar 2023 21:55:09 +0000 Subject: [PATCH] autoStart: rewrite into monolithic file --- src/autoStart.js | 75 +++++++++++++++++++++++++++++++++++++++++ src/autoStart/darwin.js | 5 --- src/autoStart/index.js | 1 - src/autoStart/linux.js | 33 ------------------ src/autoStart/win32.js | 15 --------- 5 files changed, 75 insertions(+), 54 deletions(-) create mode 100644 src/autoStart.js delete mode 100644 src/autoStart/darwin.js delete mode 100644 src/autoStart/index.js delete mode 100644 src/autoStart/linux.js delete mode 100644 src/autoStart/win32.js diff --git a/src/autoStart.js b/src/autoStart.js new file mode 100644 index 0000000..90cfbfc --- /dev/null +++ b/src/autoStart.js @@ -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); + } +}; \ No newline at end of file diff --git a/src/autoStart/darwin.js b/src/autoStart/darwin.js deleted file mode 100644 index 8222a21..0000000 --- a/src/autoStart/darwin.js +++ /dev/null @@ -1,5 +0,0 @@ -// Stub (Darwin) -exports.install = c => c(); -exports.update = c => c(); -exports.uninstall = c => c(); -exports.isInstalled = c => c(false); \ No newline at end of file diff --git a/src/autoStart/index.js b/src/autoStart/index.js deleted file mode 100644 index e2da895..0000000 --- a/src/autoStart/index.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./' + process.platform); \ No newline at end of file diff --git a/src/autoStart/linux.js b/src/autoStart/linux.js deleted file mode 100644 index cd8f7fc..0000000 --- a/src/autoStart/linux.js +++ /dev/null @@ -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); \ No newline at end of file diff --git a/src/autoStart/win32.js b/src/autoStart/win32.js deleted file mode 100644 index 08888af..0000000 --- a/src/autoStart/win32.js +++ /dev/null @@ -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 \ No newline at end of file