-
Notifications
You must be signed in to change notification settings - Fork 904
Expand file tree
/
Copy pathauto-launch.js
More file actions
111 lines (91 loc) · 2.77 KB
/
auto-launch.js
File metadata and controls
111 lines (91 loc) · 2.77 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// @ts-check
const { app } = require('electron')
const i18n = require('i18next')
const os = require('node:os')
const path = require('node:path')
const fs = require('fs-extra')
const createToggler = require('./utils/create-toggler')
const logger = require('./common/logger')
const store = require('./common/store')
const { IS_MAC, IS_WIN } = require('./common/consts')
const { AUTO_LAUNCH: CONFIG_KEY } = require('./common/config-keys')
const { showDialog, recoverableErrorDialog } = require('./dialogs')
function isSupported () {
const plat = os.platform()
return plat === 'linux' || plat === 'win32' || plat === 'darwin'
}
function getDesktopFile () {
return path.join(os.homedir(), '.config/autostart/', 'ipfs-desktop.desktop')
}
async function enable () {
if (app.setLoginItemSettings && (IS_MAC || IS_WIN)) {
app.setLoginItemSettings({ openAtLogin: true })
return
}
const desktop = `[Desktop Entry]
Type=Application
Version=1.0
Name=IPFS Desktop
Comment=IPFS Desktop Startup Script
Exec="${process.execPath}"
Icon=ipfs-desktop
StartupNotify=false
Terminal=false`
await fs.outputFile(getDesktopFile(), desktop)
}
async function disable () {
if (app.setLoginItemSettings && (IS_MAC || IS_WIN)) {
app.setLoginItemSettings({ openAtLogin: false })
return
}
await fs.remove(getDesktopFile())
}
module.exports = async function () {
const activate = async ({ newValue, oldValue, feedback }) => {
if (process.env.NODE_ENV === 'development') {
logger.info('[launch on startup] unavailable during development')
if (feedback) {
showDialog({
title: 'Launch at Login',
message: 'Not available during development.',
buttons: [i18n.t('close')]
})
}
return
}
if (!isSupported()) {
logger.info('[launch on startup] not supported on this platform')
if (feedback) {
showDialog({
title: i18n.t('launchAtLoginNotSupported.title'),
message: i18n.t('launchAtLoginNotSupported.message'),
buttons: [i18n.t('close')]
})
}
return false
}
if (newValue === oldValue) return
try {
if (newValue === true) {
await enable()
logger.info('[launch on startup] enabled')
} else {
await disable()
logger.info('[launch on startup] disabled')
}
return true
} catch (err) {
logger.error(`[launch on startup] ${err.toString()}`)
if (feedback) {
recoverableErrorDialog(err, {
title: i18n.t('launchAtLoginFailed.title'),
message: i18n.t('launchAtLoginFailed.message')
})
}
return false
}
}
activate({ newValue: store.get(CONFIG_KEY, false) })
createToggler(CONFIG_KEY, activate)
}
module.exports.isSupported = isSupported