summaryrefslogtreecommitdiffhomepage
path: root/app/lib/platform.js
blob: 0e46341149e4afceefb5a77a2d7099655265a22e (plain)
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
45
46
47
48
49
50
51
52
53
54
// @flow
import { remote, shell } from 'electron';
import electronLog from 'electron-log';
import { execFile } from 'child_process';
import { promisify } from 'util';

const execFileAsync = promisify(execFile);

const log = electronLog;

const getAppVersion = () => {
  return remote.app.getVersion();
};

const getOpenAtLogin = () => {
  return remote.app.getLoginItemSettings().openAtLogin;
};

const setOpenAtLogin = async (openAtLogin: boolean) => {
  // setLoginItemSettings is broken on macOS and cannot delete login items.
  // Issue: https://github.com/electron/electron/issues/10880
  if (process.platform === 'darwin' && openAtLogin === false) {
    // process.execPath in renderer process points to the sub-bundle of Electron Helper.
    // This regular expression extracts the path to the app bundle, which is the first occurrence of
    // file with .app extension.
    const matches = process.execPath.match(/([a-z0-9 ]+)\.app/i);
    if (matches && matches.length > 1) {
      const bundleName = matches[1];
      const appleScript = `on run argv
        set itemName to item 1 of argv
        tell application "System Events" to delete login item itemName
      end run`;
      await execFileAsync('osascript', ['-e', appleScript, bundleName]);
    } else {
      log.error(`Cannot extract the app bundle name from ${process.execPath}`);
    }
  } else {
    remote.app.setLoginItemSettings({ openAtLogin });
  }
};

const exit = () => {
  remote.app.quit();
};

const openLink = (link: string) => {
  shell.openExternal(link);
};

const openItem = (path: string) => {
  shell.openItem(path);
};

export { log, exit, openLink, openItem, getAppVersion, getOpenAtLogin, setOpenAtLogin };