Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit icon path during --createShortcut on install #10

Closed
35 changes: 31 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var path = require('path');
var spawn = require('child_process').spawn;
var debug = require('debug')('electron-squirrel-startup');
var app = require('electron').app;
var fs = require('fs');

var run = function(args, done) {
var updateExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe');
Expand All @@ -11,26 +12,52 @@ var run = function(args, done) {
}).on('close', done);
};

var check = function() {
var copyIcon = function(sourceFile) {
var output, targetFile, targetFileName,
reservedName = "app.ico"; // used by Squirrel uninstall regkey. Will cause conflict if pre-exists.
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(options) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding an options object seems ok. Let's clearly document options using JSDoc

http://usejsdoc.org/tags-param.html

if (process.platform === 'win32') {
var cmd = process.argv[1];
var cmd = process.argv[1], args;
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);

run(args, app.quit);
options && options.onInstall && options.onInstall(cmd === '--squirrel-install');
return true;
}
if (cmd === '--squirrel-uninstall') {
run(['--removeShortcut=' + target + ''], app.quit);
options && options.onUninstall && options.onUninstall();
return true;
}
if (cmd === '--squirrel-obsolete') {
app.quit();
options && options.onObsolete && options.onObsolete();
return true;
}
}
return false;
};

module.exports = check();
module.exports = check;