diff --git a/index.js b/index.js index 46a76fc..9d7c86f 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,66 @@ -var path = require('path'); -var spawn = require('child_process').spawn; -var debug = require('debug')('electron-squirrel-startup'); -var app = require('electron').app; +var path = require('path'), + child_process = require('child_process'), + debug = require('debug')('electron-squirrel-startup'), + electron = require('electron'), + fs = require('fs'); -var run = function(args, done) { - var updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe'); - debug('Spawning `%s` with args `%s`', updateExe, args); - spawn(updateExe, args, { - detached: true - }).on('close', done); +var debug_local = debug('electron-squirrel-startup'), + spawn = child_process.spawn, + app = electron.app, + run = function(args, done) { + var updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe'); + debug('Spawning `%s` with args `%s`', updateExe, args); + spawn(updateExe, args, { + detached: true + }).on('close', done); + }; + +var copyIcon = function(sourceFile) { + // used by Squirrel uninstall regkey. Will cause conflict if pre-exists. + var output, targetFile, targetFileName, + reservedName = 'app.ico'; + try { + // Build target path + targetFileName = path.basename(process.execPath, '.exe') + '.ico'; + if (targetFileName.toLowerCase() === reservedName.toLowerCase()) { + targetFileName = '_' + targetFileName; + } + targetFile = path.resolve(path.dirname(process.execPath), '..', targetFileName); + + // Perform copy + fs.writeFileSync(targetFile, fs.readFileSync(sourceFile)); + output = targetFile; + } catch (err) { + debug('Failed to copy icon `%s` to `%s` %s', sourceFile, targetFile, err.message); + } + return output; }; -var check = function() { +var check = function(options) { if (process.platform === 'win32') { - var cmd = process.argv[1]; + var cmd = process.argv[1], args, + target = path.basename(process.execPath); debug('processing squirrel command `%s`', cmd); - var target = path.basename(process.execPath); if (cmd === '--squirrel-install' || cmd === '--squirrel-updated') { - run(['--createShortcut=' + target + ''], app.quit); + args = ['--createShortcut=' + target + '']; + + var iconPath = options && options.iconPath && copyIcon(options.iconPath); + if (iconPath) { + args.push('--i=' + iconPath); + } + + options && options.onInstall && options.onInstall(cmd === '--squirrel-install'); + run(args, app.quit); return true; } if (cmd === '--squirrel-uninstall') { + options && options.onUninstall && options.onUninstall(); run(['--removeShortcut=' + target + ''], app.quit); return true; } if (cmd === '--squirrel-obsolete') { + options && options.onObsolete && options.onObsolete(); app.quit(); return true; } @@ -33,4 +68,4 @@ var check = function() { return false; }; -module.exports = check(); +module.exports = check;