UNPKG

rhamt-core

Version:
125 lines 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const model_1 = require("./model"); const fs = require("fs"); const events_1 = require("./events"); const PRETTIFY_WS = 4; class RhamtModelService { constructor(model) { this.model = model; this.loaded = false; this.onLoaded = new events_1.rhamtEvents.TypedEvent(); this.onCreated = new events_1.rhamtEvents.TypedEvent(); this.onDeleted = new events_1.rhamtEvents.TypedEvent(); this.onChanged = new events_1.rhamtEvents.TypedEvent(); this.onSaved = new events_1.rhamtEvents.TypedEvent(); } init(stateLocation) { return this.load(stateLocation); // __dirname+ '/../../data/model.json' } load(location) { return new Promise((resolve, reject) => { fs.readFile(location, (err, data) => { if (err) { reject(err); } else { this.parse(data).then(() => resolve(this.model)).catch(reject); } }); }); } parse(data) { return new Promise(resolve => { if (data.byteLength > 0) { const configs = JSON.parse(data).configurations; for (const entry of configs) { const stub = new model_1.RhamtConfiguration(); const config = Object.assign(stub, entry); this.model.configurations.set(config.id, config); } } this.loaded = true; this.onLoaded.emit(this.model); resolve(); }); } save(stateLocation) { const data = { configurations: this.model.getConfigurations() }; this.doSave(stateLocation, data); } doSave(stateLocation, data) { fs.writeFile(stateLocation, JSON.stringify(data, null, PRETTIFY_WS)); } getConfiguration(id) { return this.model.configurations.get(id); } createConfiguration() { return this.createConfigurationWithName(this.generateConfigurationName()); } createConfigurationWithName(name) { const config = new model_1.RhamtConfiguration(); this.model.configurations.set(name, config); this.onCreated.emit(config); return config; } deleteConfiguration(id, configuration) { id = id ? id : configuration ? configuration.id : undefined; if (id) { const toDelete = this.model.configurations.get(id); const deleted = this.model.configurations.delete(id); if (deleted && toDelete) { this.onDeleted.emit(toDelete); } return deleted; } return false; } update(id, name, cli, javaHome, options) { const config = this.getConfiguration(id); /*if (config) { if (name) { config.name = name; } if (cli) { config.rhamtCli = cli; } if (javaHome) { config.javaHome = javaHome; } if (options) { config.options = options; } }*/ return config; } replace(configuration) { const old = this.model.configurations.get(configuration.id); if (old) { this.model.configurations.set(configuration.id, configuration); this.onChanged.emit(old); } } generateConfigurationName() { let newName = 'rhamtConfiguration'; if (this.model.configurations.has(newName)) { for (let i = 0; i < 1000; i++) { if (!this.model.configurations.has(`${newName}-${i}`)) { newName = `${newName}-${i}`; break; } } } return newName; } dispose() { } onModelLoaded(listen) { if (this.loaded) { listen(this.model); } return this.onLoaded.on(listen); } } exports.RhamtModelService = RhamtModelService; //# sourceMappingURL=modelService.js.map