forked from nylas/nylas-mail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsystem-start-service.es6
168 lines (142 loc) · 4.42 KB
/
system-start-service.es6
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import path from 'path'
import fs from 'fs'
import os from 'os'
import {exec} from 'child_process'
import ws from 'windows-shortcuts'
class SystemStartServiceBase {
checkAvailability() {
return Promise.resolve(false);
}
doesLaunchOnSystemStart() {
throw new Error("doesLaunchOnSystemStart is not available");
}
configureToLaunchOnSystemStart() {
throw new Error("configureToLaunchOnSystemStart is not available")
}
dontLaunchOnSystemStart() {
throw new Error("dontLaunchOnSystemStart is not available")
}
}
class SystemStartServiceDarwin extends SystemStartServiceBase {
checkAvailability() {
return new Promise((resolve) => {
fs.access(this._launcherPath(), fs.R_OK | fs.W_OK, (err) => {
if (err) { resolve(false) } else { resolve(true) }
});
});
}
doesLaunchOnSystemStart() {
return new Promise((resolve) => {
fs.access(this._plistPath(), fs.R_OK | fs.W_OK, (err) => {
if (err) { resolve(false) } else { resolve(true) }
});
});
}
configureToLaunchOnSystemStart() {
fs.writeFile(this._plistPath(), JSON.stringify(this._launchdPlist()), (err) => {
if (!err) {
exec(`plutil -convert xml1 ${this._plistPath()}`)
}
})
}
dontLaunchOnSystemStart() {
return fs.unlink(this._plistPath())
}
_launcherPath() {
return path.join("/", "Applications", "Nylas N1.app", "Contents",
"MacOS", "Nylas")
}
_plistPath() {
return path.join(process.env.HOME, "Library",
"LaunchAgents", "com.nylas.plist");
}
_launchdPlist() {
return {
Label: "com.nylas.n1",
Program: this._launcherPath(),
ProgramArguments: ["--background"],
RunAtLoad: true,
}
}
}
class SystemStartServiceWin32 extends SystemStartServiceBase {
checkAvailability() {
return new Promise((resolve) => {
fs.access(this._launcherPath(), fs.R_OK | fs.W_OK, (err) => {
if (err) { resolve(false) } else { resolve(true) }
});
});
}
doesLaunchOnSystemStart() {
return new Promise((resolve) => {
fs.access(this._shortcutPath(), fs.R_OK | fs.W_OK, (err) => {
if (err) { resolve(false) } else { resolve(true) }
});
});
}
configureToLaunchOnSystemStart() {
ws.create(this._shortcutPath(), {
target: this._launcherPath(),
args: "--processStart=nylas.exe --process-start-args=--background",
runStyle: ws.MIN,
desc: "An extensible, open-source mail client built on the modern web.",
}, (err) => {
if (err) NylasEnv.reportError(err)
});
}
dontLaunchOnSystemStart() {
return fs.unlink(this._shortcutPath())
}
_launcherPath() {
return path.join(process.env.LOCALAPPDATA, "nylas", "Update.exe")
}
_shortcutPath() {
return path.join(process.env.APPDATA, "Microsoft", "Windows",
"Start Menu", "Programs", "Startup", "Nylas.lnk")
}
}
class SystemStartServiceLinux extends SystemStartServiceBase {
checkAvailability() {
return new Promise((resolve) => {
fs.access(this._launcherPath(), fs.R_OK, (err) => {
if (err) { resolve(false) } else { resolve(true) }
});
});
}
doesLaunchOnSystemStart() {
return new Promise((resolve) => {
fs.access(this._shortcutPath(), fs.R_OK | fs.W_OK, (err) => {
if (err) { resolve(false) } else { resolve(true) }
});
});
}
configureToLaunchOnSystemStart() {
fs.readFile(this._launcherPath(), 'utf8', (error, data) => {
// Append the --background flag before the Exec key
const parsedData = data.replace('%U', '--background %U');
fs.writeFile(this._shortcutPath(), parsedData, () => {});
});
}
dontLaunchOnSystemStart() {
return fs.unlink(this._shortcutPath())
}
_launcherPath() {
return path.join('/', 'usr', 'share', 'applications', 'nylas.desktop');
}
_shortcutPath() {
const configDir = process.env.XDG_CONFIG_HOME || path.join(os.homedir(), '.config');
return path.join(configDir, 'autostart', 'nylas-n1.desktop');
}
}
/* eslint import/no-mutable-exports: 0*/
let SystemStartService;
if (process.platform === "darwin") {
SystemStartService = SystemStartServiceDarwin;
} else if (process.platform === "linux") {
SystemStartService = SystemStartServiceLinux;
} else if (process.platform === "win32") {
SystemStartService = SystemStartServiceWin32;
} else {
SystemStartService = SystemStartServiceBase;
}
export default SystemStartService