From 01e27b26911d64ac43aa9356d04812970d6cda4c Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 25 Mar 2016 18:56:22 -0700 Subject: [PATCH 1/2] Linux: Ensure ".local/share/{applications,icons}" exists --- main/handlers.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/main/handlers.js b/main/handlers.js index 87643f72..75f8572a 100644 --- a/main/handlers.js +++ b/main/handlers.js @@ -2,6 +2,8 @@ module.exports = { init } +var mkdirp = require('mkdirp') + var log = require('./log') function init () { @@ -26,17 +28,23 @@ function installDesktopFile () { var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop') var desktopFile = fs.readFileSync(templatePath, 'utf8') - desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) - var appPath = config.IS_PRODUCTION ? path.dirname(process.execPath) : config.ROOT_PATH - desktopFile = desktopFile.replace(/\$APP_PATH/g, appPath) - var execPath = process.execPath + (config.IS_PRODUCTION ? '' : ' \.') + var tryExecPath = process.execPath + + desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) + desktopFile = desktopFile.replace(/\$APP_PATH/g, appPath) desktopFile = desktopFile.replace(/\$EXEC_PATH/g, execPath) + desktopFile = desktopFile.replace(/\$TRY_EXEC_PATH/g, tryExecPath) - desktopFile = desktopFile.replace(/\$TRY_EXEC_PATH/g, process.execPath) - - var desktopFilePath = path.join(os.homedir(), '.local', 'share', 'applications', 'webtorrent.desktop') + var desktopFilePath = path.join( + os.homedir(), + '.local', + 'share', + 'applications', + 'webtorrent.desktop' + ) + mkdirp(path.dirname(desktopFilePath)) fs.writeFileSync(desktopFilePath, desktopFile) } @@ -49,7 +57,14 @@ function installDesktopIcon () { var iconStaticPath = path.join(config.STATIC_PATH, 'WebTorrent.png') var iconFile = fs.readFileSync(iconStaticPath) - var iconFilePath = path.join(os.homedir(), '.local', 'share', 'icons', 'webtorrent.png') + var iconFilePath = path.join( + os.homedir(), + '.local', + 'share', + 'icons', + 'webtorrent.png' + ) + mkdirp(path.dirname(iconFilePath)) fs.writeFileSync(iconFilePath, iconFile) } From 59a1bc03f2b95d58ef8864b3a4b1152d7f40990e Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Fri, 25 Mar 2016 21:47:49 -0700 Subject: [PATCH 2/2] Perf: Remove all *Sync methods for Linux startup --- main/handlers.js | 109 +++++++++++++++++++++++++++-------------------- 1 file changed, 62 insertions(+), 47 deletions(-) diff --git a/main/handlers.js b/main/handlers.js index 75f8572a..371d4bfe 100644 --- a/main/handlers.js +++ b/main/handlers.js @@ -2,70 +2,85 @@ module.exports = { init } -var mkdirp = require('mkdirp') +var path = require('path') var log = require('./log') function init () { if (process.platform === 'win32') { - var path = require('path') - var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico') - registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', iconPath, process.execPath) - registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', iconPath, process.execPath) + initWindows() } if (process.platform === 'linux') { - installDesktopFile() - installDesktopIcon() + initLinux() } } -function installDesktopFile () { - var config = require('../config') - var fs = require('fs') - var path = require('path') - var os = require('os') - - var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop') - var desktopFile = fs.readFileSync(templatePath, 'utf8') - - var appPath = config.IS_PRODUCTION ? path.dirname(process.execPath) : config.ROOT_PATH - var execPath = process.execPath + (config.IS_PRODUCTION ? '' : ' \.') - var tryExecPath = process.execPath - - desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) - desktopFile = desktopFile.replace(/\$APP_PATH/g, appPath) - desktopFile = desktopFile.replace(/\$EXEC_PATH/g, execPath) - desktopFile = desktopFile.replace(/\$TRY_EXEC_PATH/g, tryExecPath) - - var desktopFilePath = path.join( - os.homedir(), - '.local', - 'share', - 'applications', - 'webtorrent.desktop' - ) - mkdirp(path.dirname(desktopFilePath)) - fs.writeFileSync(desktopFilePath, desktopFile) +function initWindows () { + var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico') + registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', iconPath, process.execPath) + registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', iconPath, process.execPath) } -function installDesktopIcon () { +function initLinux () { var config = require('../config') var fs = require('fs') - var path = require('path') + var mkdirp = require('mkdirp') var os = require('os') + var path = require('path') - var iconStaticPath = path.join(config.STATIC_PATH, 'WebTorrent.png') - var iconFile = fs.readFileSync(iconStaticPath) + installDesktopFile() + installIconFile() - var iconFilePath = path.join( - os.homedir(), - '.local', - 'share', - 'icons', - 'webtorrent.png' - ) - mkdirp(path.dirname(iconFilePath)) - fs.writeFileSync(iconFilePath, iconFile) + function installDesktopFile () { + var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop') + fs.readFile(templatePath, 'utf8', writeDesktopFile) + } + + function writeDesktopFile (err, desktopFile) { + if (err) return console.error(err.message) + + var appPath = config.IS_PRODUCTION ? path.dirname(process.execPath) : config.ROOT_PATH + var execPath = process.execPath + (config.IS_PRODUCTION ? '' : ' \.') + var tryExecPath = process.execPath + + desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) + desktopFile = desktopFile.replace(/\$APP_PATH/g, appPath) + desktopFile = desktopFile.replace(/\$EXEC_PATH/g, execPath) + desktopFile = desktopFile.replace(/\$TRY_EXEC_PATH/g, tryExecPath) + + var desktopFilePath = path.join( + os.homedir(), + '.local', + 'share', + 'applications', + 'webtorrent.desktop' + ) + mkdirp(path.dirname(desktopFilePath)) + fs.writeFile(desktopFilePath, desktopFile, function (err) { + if (err) return console.error(err.message) + }) + } + + function installIconFile () { + var iconStaticPath = path.join(config.STATIC_PATH, 'WebTorrent.png') + fs.readFile(iconStaticPath, writeIconFile) + } + + function writeIconFile (err, iconFile) { + if (err) return console.error(err.message) + + var iconFilePath = path.join( + os.homedir(), + '.local', + 'share', + 'icons', + 'webtorrent.png' + ) + mkdirp(path.dirname(iconFilePath)) + fs.writeFile(iconFilePath, iconFile, function (err) { + if (err) return console.error(err.message) + }) + } } /**