Merge pull request #226 from feross/fix-ubuntu-crash

Linux: Ensure ".local/share/{applications,icons}" exists; plus perf fix
This commit is contained in:
Feross Aboukhadijeh
2016-03-25 22:58:16 -07:00

View File

@@ -2,55 +2,85 @@ module.exports = {
init init
} }
var path = require('path')
var log = require('./log') var log = require('./log')
function init () { function init () {
if (process.platform === 'win32') { if (process.platform === 'win32') {
var path = require('path') initWindows()
}
if (process.platform === 'linux') {
initLinux()
}
}
function initWindows () {
var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico') var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico')
registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', iconPath, process.execPath) registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', iconPath, process.execPath)
registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', iconPath, process.execPath) registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', iconPath, process.execPath)
}
if (process.platform === 'linux') {
installDesktopFile()
installDesktopIcon()
}
} }
function installDesktopFile () { function initLinux () {
var config = require('../config') var config = require('../config')
var fs = require('fs') var fs = require('fs')
var path = require('path') var mkdirp = require('mkdirp')
var os = require('os') var os = require('os')
var path = require('path')
installDesktopFile()
installIconFile()
function installDesktopFile () {
var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop') var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop')
var desktopFile = fs.readFileSync(templatePath, 'utf8') fs.readFile(templatePath, 'utf8', writeDesktopFile)
}
desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME) function writeDesktopFile (err, desktopFile) {
if (err) return console.error(err.message)
var appPath = config.IS_PRODUCTION ? path.dirname(process.execPath) : config.ROOT_PATH 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 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(/\$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(),
var desktopFilePath = path.join(os.homedir(), '.local', 'share', 'applications', 'webtorrent.desktop') '.local',
fs.writeFileSync(desktopFilePath, desktopFile) 'share',
} 'applications',
'webtorrent.desktop'
function installDesktopIcon () { )
var config = require('../config') mkdirp(path.dirname(desktopFilePath))
var fs = require('fs') fs.writeFile(desktopFilePath, desktopFile, function (err) {
var path = require('path') if (err) return console.error(err.message)
var os = require('os') })
}
function installIconFile () {
var iconStaticPath = path.join(config.STATIC_PATH, 'WebTorrent.png') var iconStaticPath = path.join(config.STATIC_PATH, 'WebTorrent.png')
var iconFile = fs.readFileSync(iconStaticPath) fs.readFile(iconStaticPath, writeIconFile)
}
var iconFilePath = path.join(os.homedir(), '.local', 'share', 'icons', 'webtorrent.png') function writeIconFile (err, iconFile) {
fs.writeFileSync(iconFilePath, 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)
})
}
} }
/** /**