@remote.it/core
Version:
Core remote.it JavasScript/TypeScript library
110 lines (92 loc) • 2.3 kB
text/typescript
import { JSONFile } from './JSONFile'
import { Environment } from './Environment'
import { Logger } from './Logger'
export interface ConfigFileContent {
registrationKey?: string
target?: TargetConfig | undefined
services?: ServiceConfig[]
initiators?: InitiatorConfig[]
user?: UserConfig | undefined
}
export interface ServiceConfig {
name: string
hardwareID: string
hostname: string
overload?: number
port: number
secret: string
type: number
uid: string
}
export interface TargetConfig extends ServiceConfig {}
export interface UserConfig {
username: string
authHash: string
}
export interface InitiatorConfig {
autoStart?: boolean
hostname?: string
name?: string
pid?: number
port: number
type: number
uid: string
}
export class ConfigFile extends JSONFile<ConfigFileContent> {
constructor(location: string = Environment.configPath) {
super(location)
// Make sure file exists
if (!this.exists || !this.content) {
Logger.info('config file does not exist, creating', { location })
this.write({
registrationKey: undefined,
initiators: [],
services: [],
target: undefined,
user: undefined,
})
}
}
get registrationKey(): string | undefined {
return this.read().registrationKey
}
set registrationKey(registrationKey: string | undefined) {
const json = this.read()
json.registrationKey = registrationKey
this.write(json)
}
get target() {
return this.read().target || undefined
}
set target(target: TargetConfig | undefined) {
const json = this.read()
json.target = target
this.write(json)
}
get services() {
return this.read().services || []
}
set services(services: ServiceConfig[]) {
const json = this.read()
json.services = services
this.write(json)
}
get initiators() {
return this.read().initiators || []
}
set initiators(initiators: InitiatorConfig[]) {
const json = this.read()
json.initiators = initiators
this.write(json)
}
get user() {
const content = this.read()
if (!content || !content.user) return
return content.user
}
set user(user: UserConfig | undefined) {
const json = this.read()
json.user = user
this.write(json)
}
}