aboutsummaryrefslogtreecommitdiffstats
path: root/src/enforce-macos-app-location.ts
blob: 222127acf7bcfdd811fc622146ab240844d55411 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Enhanced from: https://github.com/dertieran/electron-util/blob/replace-remote/source/enforce-macos-app-location.js

import { isMac } from './environment';
import { isDevMode } from './environment-remote';
import { api } from './electron-util';

export default function enforceMacOSAppLocation(): void {
  if (isDevMode || !isMac || api.app.isInApplicationsFolder()) {
    return;
  }

  const clickedButtonIndex = api.dialog.showMessageBoxSync({
    type: 'error',
    message: 'Move to Applications folder?',
    detail:
      'Ferdium must live in the Applications folder to be able to run correctly.',
    buttons: ['Move to Applications folder', 'Quit Ferdium'],
    defaultId: 0,
    cancelId: 1,
  });

  if (clickedButtonIndex === 1) {
    api.app.quit();
    return;
  }

  api.app.moveToApplicationsFolder({
    conflictHandler: conflict => {
      if (conflict === 'existsAndRunning') {
        // Can't replace the active version of the app
        api.dialog.showMessageBoxSync({
          type: 'error',
          message:
            'Another version of Ferdium is currently running. Quit it, then launch this version of the app again.',
          buttons: ['OK'],
        });

        api.app.quit();
      }

      return true;
    },
  });
}