Windows: base Squirrel shortcut code on Nylas N1

This commit is contained in:
Feross Aboukhadijeh
2016-03-25 02:24:08 -07:00
parent ae6b86d233
commit 0681169653
2 changed files with 91 additions and 21 deletions

View File

@@ -4,44 +4,54 @@ module.exports = {
var cp = require('child_process') var cp = require('child_process')
var electron = require('electron') var electron = require('electron')
var fs = require('fs')
var os = require('os')
var path = require('path') var path = require('path')
var pathExists = require('path-exists')
var app = electron.app var app = electron.app
var config = require('../config')
var handlers = require('./handlers') var handlers = require('./handlers')
var exeName = path.basename(process.execPath)
var updateDotExe = path.join(process.execPath, '..', '..', 'Update.exe')
function handleEvent (cmd) { function handleEvent (cmd) {
if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') { if (cmd === '--squirrel-install') {
// App was installed/updated. (Called on new version of app.) // App was installed.
// Install protocol/file handlers, desktop/start menu shortcuts. // Install protocol/file handlers, desktop/start menu shortcuts.
handlers.init() handlers.init()
createShortcuts()
createShortcuts(function () {
// Ensure user sees install splash screen so they realize that Setup.exe actually // Ensure user sees install splash screen so they realize that Setup.exe actually
// installed an application and isn't the application itself. // installed an application and isn't the application itself.
if (cmd === '--squirrel-install') {
setTimeout(function () { setTimeout(function () {
app.quit() app.quit()
}, 5000) }, 5000)
} else { })
app.quit() return true
} }
if (cmd === '--squirrel-updated') {
// App was updated. (Called on new version of app)
updateShortcuts(function () {
app.quit()
})
return true return true
} }
if (cmd === '--squirrel-uninstall') { if (cmd === '--squirrel-uninstall') {
// App was just uninstalled. Undo anything we did in the --squirrel-install and // App was just uninstalled. Undo anything we did in the --squirrel-install and
// --squirrel-updated handlers // --squirrel-updated handlers
removeShortcuts(function () {
// TODO: implement this
app.quit() app.quit()
})
return true return true
} }
if (cmd === '--squirrel-obsolete') { if (cmd === '--squirrel-obsolete') {
// App will be updated. (Called on outgoing version of app.) // App will be updated. (Called on outgoing version of app)
app.quit() app.quit()
return true return true
} }
@@ -54,12 +64,71 @@ function handleEvent (cmd) {
return false return false
} }
function createShortcuts () { // Spawn a command and invoke the callback when it completes with an error and the output
var updateExe = path.join(process.execPath, '..', 'Update.exe') // from standard out.
var args = [ function spawn (command, args, cb) {
'--createShortcut="' + config.APP_NAME + '.exe"', var stdout = ''
'--shortcut-locations="Desktop,StartMenu,Startup"',
'--process-start-args="--autostart"' var child
] try {
cp.execSync(updateExe + args.join(' ')) child = cp.spawn(command, args)
} catch (err) {
// Spawn can throw an error
process.nextTick(function () {
cb(error, stdout)
})
return
}
child.stdout.on('data', function (data) {
stdout += data
})
var error = null
child.on('error', function (processError) {
error = processError
})
child.on('close', function (code, signal) {
if (code !== 0 && !error) error = new Error('Command failed: #{signal || code}')
if (error) error.stdout = stdout
cb(error, stdout)
})
}
// Spawn Squirrel's Update.exe with the given arguments and invoke the callback when the
// command completes.
function spawnUpdate (args, cb) {
spawn(updateDotExe, args, cb)
}
// Create desktop/start menu shortcuts using the Squirrel Update.exe command line API
function createShortcuts (cb) {
spawnUpdate(['--createShortcut', exeName], cb)
}
// Update desktop/start menu shortcuts using the Squirrel Update.exe command line API
function updateShortcuts (cb) {
var homeDir = os.homedir()
if (homeDir) {
var desktopShortcutPath = path.join(homeDir, 'Desktop', 'WebTorrent.lnk')
// Check if the desktop shortcut has been previously deleted and and keep it deleted
// if it was
pathExists(desktopShortcutPath).then(function (desktopShortcutExists) {
createShortcuts(function () {
if (desktopShortcutExists) {
cb()
} else {
// Remove the unwanted desktop shortcut that was recreated
fs.unlink(desktopShortcutPath, cb)
}
})
})
} else {
createShortcuts(cb)
}
}
// Remove desktop/start menu shortcuts using the Squirrel Update.exe command line API
function removeShortcuts (cb) {
spawnUpdate(['--removeShortcut', exeName], cb)
} }

View File

@@ -24,6 +24,7 @@
"mkdirp": "^0.5.1", "mkdirp": "^0.5.1",
"musicmetadata": "^2.0.2", "musicmetadata": "^2.0.2",
"network-address": "^1.1.0", "network-address": "^1.1.0",
"path-exists": "^2.1.0",
"prettier-bytes": "^1.0.1", "prettier-bytes": "^1.0.1",
"upload-element": "^1.0.1", "upload-element": "^1.0.1",
"virtual-dom": "^2.1.1", "virtual-dom": "^2.1.1",