Clean up handlers.js

This commit is contained in:
Michael George Attard
2017-10-30 22:49:34 +01:00
parent cae40b44e6
commit 962c563f2e

View File

@@ -7,26 +7,24 @@ const config = require('../config')
const path = require('path') const path = require('path')
function install () { function install () {
if (process.platform === 'darwin') { switch (process.platform) {
installDarwin() case 'darwin': installDarwin()
} break
if (process.platform === 'win32') { case 'win32': installWin32()
installWin32() break
} case 'linux': installLinux()
if (process.platform === 'linux') { break
installLinux()
} }
} }
function uninstall () { function uninstall () {
if (process.platform === 'darwin') { switch (process.platform) {
uninstallDarwin() case 'darwin': uninstallDarwin()
} break
if (process.platform === 'win32') { case 'win32': uninstallWin32()
uninstallWin32() break
} case 'linux': uninstallLinux()
if (process.platform === 'linux') { break
uninstallLinux()
} }
} }
@@ -42,9 +40,9 @@ function installDarwin () {
// File handlers are defined in `Info.plist`. // File handlers are defined in `Info.plist`.
} }
function uninstallDarwin () {} function uninstallDarwin () { }
const EXEC_COMMAND = [ process.execPath ] const EXEC_COMMAND = [process.execPath]
if (!config.IS_PRODUCTION) { if (!config.IS_PRODUCTION) {
EXEC_COMMAND.push(config.ROOT_PATH) EXEC_COMMAND.push(config.ROOT_PATH)
@@ -108,37 +106,37 @@ function installWin32 () {
setProtocol() setProtocol()
function setProtocol (err) { function setProtocol (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
protocolKey.set('', Registry.REG_SZ, name, setURLProtocol) protocolKey.set('', Registry.REG_SZ, name, setURLProtocol)
} }
function setURLProtocol (err) { function setURLProtocol (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
protocolKey.set('URL Protocol', Registry.REG_SZ, '', setIcon) protocolKey.set('URL Protocol', Registry.REG_SZ, '', setIcon)
} }
function setIcon (err) { function setIcon (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
const iconKey = new Registry({ const iconKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + protocol + '\\DefaultIcon' key: `\\Software\\Classes\\${protocol}\\DefaultIcon`
}) })
iconKey.set('', Registry.REG_SZ, icon, setCommand) iconKey.set('', Registry.REG_SZ, icon, setCommand)
} }
function setCommand (err) { function setCommand (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
const commandKey = new Registry({ const commandKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + protocol + '\\shell\\open\\command' key: `\\Software\\Classes\\${protocol}\\shell\\open\\command`
}) })
commandKey.set('', Registry.REG_SZ, `${commandToArgs(command)} "%1"`, done) commandKey.set('', Registry.REG_SZ, `${commandToArgs(command)} "%1"`, done)
} }
function done (err) { function done (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
} }
} }
@@ -163,43 +161,43 @@ function installWin32 () {
function setExt () { function setExt () {
const extKey = new Registry({ const extKey = new Registry({
hive: Registry.HKCU, // HKEY_CURRENT_USER hive: Registry.HKCU, // HKEY_CURRENT_USER
key: '\\Software\\Classes\\' + ext key: `\\Software\\Classes\\${ext}`
}) })
extKey.set('', Registry.REG_SZ, id, setId) extKey.set('', Registry.REG_SZ, id, setId)
} }
function setId (err) { function setId (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
const idKey = new Registry({ const idKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + id key: `\\Software\\Classes\\${id}`
}) })
idKey.set('', Registry.REG_SZ, name, setIcon) idKey.set('', Registry.REG_SZ, name, setIcon)
} }
function setIcon (err) { function setIcon (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
const iconKey = new Registry({ const iconKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + id + '\\DefaultIcon' key: `\\Software\\Classes\\${id}\\DefaultIcon`
}) })
iconKey.set('', Registry.REG_SZ, icon, setCommand) iconKey.set('', Registry.REG_SZ, icon, setCommand)
} }
function setCommand (err) { function setCommand (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
const commandKey = new Registry({ const commandKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + id + '\\shell\\open\\command' key: `\\Software\\Classes\\${id}\\shell\\open\\command`
}) })
commandKey.set('', Registry.REG_SZ, `${commandToArgs(command)} "%1"`, done) commandKey.set('', Registry.REG_SZ, `${commandToArgs(command)} "%1"`, done)
} }
function done (err) { function done (err) {
if (err) log.error(err.message) if (err) return log.error(err.message)
} }
} }
} }
@@ -217,9 +215,9 @@ function uninstallWin32 () {
function getCommand () { function getCommand () {
const commandKey = new Registry({ const commandKey = new Registry({
hive: Registry.HKCU, // HKEY_CURRENT_USER hive: Registry.HKCU, // HKEY_CURRENT_USER
key: '\\Software\\Classes\\' + protocol + '\\shell\\open\\command' key: `\\Software\\Classes\\${protocol}\\shell\\open\\command`
}) })
commandKey.get('', function (err, item) { commandKey.get('', (err, item) => {
if (!err && item.value.indexOf(commandToArgs(command)) >= 0) { if (!err && item.value.indexOf(commandToArgs(command)) >= 0) {
destroyProtocol() destroyProtocol()
} }
@@ -229,9 +227,9 @@ function uninstallWin32 () {
function destroyProtocol () { function destroyProtocol () {
const protocolKey = new Registry({ const protocolKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + protocol key: `\\Software\\Classes\\${protocol}`
}) })
protocolKey.destroy(function () {}) protocolKey.destroy(() => { })
} }
} }
@@ -241,7 +239,7 @@ function uninstallWin32 () {
function eraseId () { function eraseId () {
const idKey = new Registry({ const idKey = new Registry({
hive: Registry.HKCU, // HKEY_CURRENT_USER hive: Registry.HKCU, // HKEY_CURRENT_USER
key: '\\Software\\Classes\\' + id key: `\\Software\\Classes\\${id}`
}) })
idKey.destroy(getExt) idKey.destroy(getExt)
} }
@@ -249,9 +247,9 @@ function uninstallWin32 () {
function getExt () { function getExt () {
const extKey = new Registry({ const extKey = new Registry({
hive: Registry.HKCU, hive: Registry.HKCU,
key: '\\Software\\Classes\\' + ext key: `\\Software\\Classes\\${ext}`
}) })
extKey.get('', function (err, item) { extKey.get('', (err, item) => {
if (!err && item.value === id) { if (!err && item.value === id) {
destroyExt() destroyExt()
} }
@@ -261,9 +259,9 @@ function uninstallWin32 () {
function destroyExt () { function destroyExt () {
const extKey = new Registry({ const extKey = new Registry({
hive: Registry.HKCU, // HKEY_CURRENT_USER hive: Registry.HKCU, // HKEY_CURRENT_USER
key: '\\Software\\Classes\\' + ext key: `\\Software\\Classes\\${ext}`
}) })
extKey.destroy(function () {}) extKey.destroy(() => { })
} }
} }
} }
@@ -300,10 +298,11 @@ function installLinux () {
? path.dirname(process.execPath) ? path.dirname(process.execPath)
: config.ROOT_PATH : config.ROOT_PATH
desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) desktopFile = desktopFile
desktopFile = desktopFile.replace(/\$APP_PATH/g, appPath) .replace(/\$APP_NAME/g, config.APP_NAME)
desktopFile = desktopFile.replace(/\$EXEC_PATH/g, EXEC_COMMAND.join(' ')) .replace(/\$APP_PATH/g, appPath)
desktopFile = desktopFile.replace(/\$TRY_EXEC_PATH/g, process.execPath) .replace(/\$EXEC_PATH/g, EXEC_COMMAND.join(' '))
.replace(/\$TRY_EXEC_PATH/g, process.execPath)
const desktopFilePath = path.join( const desktopFilePath = path.join(
os.homedir(), os.homedir(),
@@ -313,7 +312,7 @@ function installLinux () {
'webtorrent-desktop.desktop' 'webtorrent-desktop.desktop'
) )
fs.mkdirp(path.dirname(desktopFilePath)) fs.mkdirp(path.dirname(desktopFilePath))
fs.writeFile(desktopFilePath, desktopFile, function (err) { fs.writeFile(desktopFilePath, desktopFile, (err) => {
if (err) return log.error(err.message) if (err) return log.error(err.message)
}) })
} }