Use electron-lets-move instead of custom code to move to Applications directory

This commit is contained in:
Ben Gotow 2017-09-27 23:11:19 -07:00
parent 3fb28dda1b
commit d6b918ef95
4 changed files with 15 additions and 111 deletions

View file

@ -1,4 +1,4 @@
import { ipcRenderer, remote } from 'electron';
import { moveToApplications } from 'electron-lets-move';
/**
* We want to make sure that people have installed the app in a
@ -9,75 +9,25 @@ import { ipcRenderer, remote } from 'electron';
* leave their app in the /Downloads folder (which frequently gets
* erased!).
*/
function onDialogActionTaken(numAsks) {
return buttonIndex => {
if (numAsks >= 1) {
if (buttonIndex === 1) {
AppEnv.config.set('asksAboutAppMove', 5);
} else {
AppEnv.config.set('asksAboutAppMove', numAsks + 1);
}
} else {
AppEnv.config.set('asksAboutAppMove', numAsks + 1);
}
};
}
export function activate() {
if (AppEnv.inDevMode() || AppEnv.inSpecMode()) {
return;
}
if (process.platform !== 'darwin') {
if (AppEnv.config.get('askedAboutAppMove')) {
return;
}
const appRe = /Applications/gi;
if (appRe.test(process.argv[0])) {
return;
}
// If we're in Volumes, that means we've launched from the DMG. This
// is unsupported. We should optimistically move.
const volTest = /Volumes/gi;
if (volTest.test(process.argv[0])) {
ipcRenderer.send('move-to-applications');
return;
}
const numAsks = AppEnv.config.get('asksAboutAppMove') || 0;
if (numAsks <= 0) {
AppEnv.config.set('asksAboutAppMove', 1);
return;
}
AppEnv.config.set('asksAboutAppMove', numAsks + 1);
if (numAsks >= 5) return;
let buttons;
if (numAsks >= 1) {
buttons = ['Okay', "Don't ask again"];
} else {
buttons = ['Okay'];
}
const msg = `We recommend that you move Mailspring to your Applications folder to get updates correctly and keep this folder uncluttered.`;
const CANCEL_ID = 0;
remote.dialog.showMessageBox(
{
type: 'warning',
buttons: buttons,
title: 'A Better Place to Install Mailspring',
message: 'Please move Mailspring to your Applications folder',
detail: msg,
defaultId: 0,
cancelId: CANCEL_ID,
},
onDialogActionTaken(numAsks)
);
moveToApplications(function(err, moved) {
if (err) {
// log error, something went wrong whilst moving the app.
}
if (!moved) {
// the user asked not to move the app, it's up to the parent application
// to store this information and not hassle them again.
AppEnv.config.set('askedAboutAppMove', true);
}
});
}
export function deactivate() {}

View file

@ -2,7 +2,7 @@
"name": "verify-install-location",
"main": "./lib/main",
"version": "0.0.1",
"description": "Verifies the install location for N1",
"description": "Verifies the install location and moves the app to Applications",
"license": "GPL-3.0",
"engines": {
"mailspring": "*"

View file

@ -10,8 +10,7 @@
"@segment/loosely-validate-event": "^1.1.2",
"async": "^0.9",
"babel-core": "6.22.0",
"babel-preset-electron":
"bengotow/babel-preset-electron#00783dfc438f122997993ae597a41ec315ba121b",
"babel-preset-electron": "bengotow/babel-preset-electron#00783dfc438f122997993ae597a41ec315ba121b",
"babel-preset-react": "6.22.0",
"babel-regenerator-runtime": "6.5.0",
"better-sqlite3": "bengotow/better-sqlite3#dcd5b6e73c9a5329fd72c85be3316131fcfb83ab",
@ -23,6 +22,7 @@
"coffeestack": "^1.1",
"color": "^0.7.3",
"debug": "github:emorikawa/debug#nylas",
"electron-lets-move": "0.0.5",
"electron-spellchecker": "1.0.1",
"emissary": "^1.3.1",
"emoji-data": "^0.2.0",

View file

@ -600,52 +600,6 @@ export default class Application extends EventEmitter {
}
event.returnValue = true;
});
ipcMain.on('move-to-applications', () => {
if (process.platform !== 'darwin') {
return;
}
const re = /(^.*?\.app)/i;
const appPath = (re.exec(process.argv[0]) || [])[0];
if (!appPath) {
throw new Error(`Couldn't find .app in launch path: ${process.argv[0]}`);
}
let appName = appPath.split('/');
appName = appName[appName.length - 1];
if (!appName) {
throw new Error(`Couldn't find .app in app path: ${appPath}`);
}
const escapedName = this._escapeShell(appName);
const escapedPath = this._escapeShell(appPath);
if (!escapedName || escapedName.trim().length === 0) {
throw new Error(`escapedName is invalid: ${escapedName}`);
}
// We separate the commands with a `;` instead of `&&` so in case the
// mv fails, the open will still run.
// We need the sleep to let the first app fully finish quitting.
// Otherwise it'll attempt to re-open the existing app (the one in
// the process of quitting)
const newAppDest = `/Applications/${escapedName}`;
let move = `mv`;
try {
fs.accessSync(appPath, fs.W_OK);
} catch (e) {
move = `cp -r`;
}
const cmd = `rm -rf ${newAppDest}; ${move} ${escapedPath} ${newAppDest}; sleep 0.5; open ${newAppDest}`;
app.once('will-quit', () => {
// We need to use `exec` since that will start a new shell process and
// allow us to kill this one.
proc.exec(cmd);
});
app.quit();
});
}
_escapeShell(cmd) {
return cmd.replace(/(["\s'$`\\])/g, '\\$1');
}
// Public: Executes the given command.