Mailspring/src/system-start-service.es6

132 lines
3.3 KiB
Plaintext
Raw Normal View History

import path from 'path'
import fs from 'fs'
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) => {
feat(error): improve error reporting. Now `NylasEnv.reportError` Summary: The goal is to let us see what plugins are throwing errors on Sentry. We are using a Sentry `tag` to identify and group plugins and their errors. Along the way, I cleaned up the error catching and reporting system. There was a lot of duplicate error logic (that wasn't always right) and some legacy Atom error handling. Now, if you catch an error that we should report (like when handling extensions), call `NylasEnv.reportError`. This used to be called `emitError` but I changed it to `reportError` to be consistent with the ErrorReporter and be a bit more indicative of what it does. In the production version, the `ErrorLogger` will forward the request to the `nylas-private-error-reporter` which will report to Sentry. The `reportError` function also now inspects the stack to determine which plugin(s) it came from. These are passed along to Sentry. I also cleaned up the `console.log` and `console.error` code. We were logging errors multiple times making the console confusing to read. Worse is that we were logging the `error` object, which would print not the stack of the actual error, but rather the stack of where the console.error was logged from. Printing `error.stack` instead shows much more accurate stack traces. See changes in the Edgehill repo here: https://github.com/nylas/edgehill/commit/8c4a86eb7ee1a06249a9ae35397e2084a09ad1dc Test Plan: Manual Reviewers: juan, bengotow Reviewed By: bengotow Differential Revision: https://phab.nylas.com/D2509
2016-02-04 07:06:52 +08:00
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 {
}
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