UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

125 lines (102 loc) 2.91 kB
import debug from 'debug' import { GenericSystemService } from './GenericSystemService' import { LaunchCtl } from './LaunchCtl' import { Plist } from './Plist' import { join, dirname } from 'path' import { Environment } from './Environment' import { Logger } from './Logger' const d = debug('remoteit:MacSystemService') interface SystemServicePlist { Label: string ProgramArguments: string[] RunAtLoad?: boolean KeepAlive?: boolean StartInterval?: number WorkingDirectory?: string StandardOutPath?: string StandardErrorPath?: string WatchPaths?: string[] EnvironmentVariables?: { [key: string]: any } } export class MacSystemService extends GenericSystemService { label: string = 'it.remote.core' keepAlive: boolean = true private plist: Plist<SystemServicePlist> private launchCtl: LaunchCtl private runAsAgent: boolean = false constructor(protected config: string = Environment.configPath) { super(config) // this.command = this.plist = new Plist<SystemServicePlist>(Environment.launchctlPlistPath) this.launchCtl = new LaunchCtl(this.plist) } async install() { d('install system service', this.command) // If a plist exists, lets remove it so we can // create the most up-to-date version we have. if (this.exists) this.uninstall() // if (!this.exists) this.createPlist() this.createPlist() // this.launchCtl.load() // this.start() d('service installed:', this.command) return this } /** * Starts a system service on a Mac an installs the plist file * if it is missing. */ async start() { d('start system service') if (!this.exists) this.install() this.launchCtl.load() // this.launchCtl.start() return this } async stop() { d('stop system service') // this.launchCtl.stop() this.launchCtl.unload() return this } async uninstall() { d('uinstall system service') if (!this.plist.exists) return this try { this.stop() this.plist.remove() } catch (error) { Logger.error( 'error when attempting to uninstall Mac OSX system service', { error } ) } // this.launchCtl.remove() return this } get exists() { return this.plist.exists } private createPlist() { this.plist.write({ Label: this.label, ProgramArguments: this.command.split(' '), RunAtLoad: true, KeepAlive: this.keepAlive, // TODO: make these configurable by user StandardOutPath: join( Environment.logDirectory, 'remoteit.service.info.log' ), StandardErrorPath: join( Environment.logDirectory, 'remoteit.service.errors.log' ), EnvironmentVariables: { PATH: dirname(process.execPath), }, }) d('plist location:', this.plist.location) d('plist content:', this.plist.read()) } }