UNPKG

@wbg-mde/model

Version:

Metadata Editor Model Definitions

304 lines (303 loc) 10.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const fs = require("fs"); const path = require("path"); const os = require('os'); const util_1 = require("util"); const index_1 = require("../index"); const readFileAsync = util_1.promisify(fs.readFile); const writeFileAsync = util_1.promisify(fs.writeFile); const existsSync = util_1.promisify(fs.exists); const configPath = path.join(os.userInfo().homedir, 'metadata-editor-config.json'); class ProjectConfiguration { constructor() { try { const config = JSON.parse(fs.readFileSync(configPath).toString()); this._userdatapath = config._userdatapath; this._masterdatapath = config._masterdatapath; this._appLogConfig = config._appLogConfig; this._rPath = config._rPath; this._RFrequencyCalcLimit = config._RFrequencyCalcLimit || 50; this._templateTypes = config._templateTypes; this._defaultTemplates = config._defaultTemplates; this._i18nResources = config._i18nResources || { "en": "English" }; this._customDataTypes = config._customDataTypes; this._enableDevTools = config._enableDevTools === false ? false : true; this._QC_CONFIG = config._QC_CONFIG; this._variableRepoConfig = config._variableRepoConfig; this._publishConfig = config._publishConfig; this._serverPort = parseInt(config._serverPort || 4040); this._RServerPort = parseInt(config._RServerPort || 9090); this._RServer = config._RServer || "opencpu"; this._RTimeLimit = config._RTimeLimit || 900; this._db = config._db; } catch (e) { } } ready() { return this.readConfig().then((config) => { this._userdatapath = config._userdatapath; this._masterdatapath = config._masterdatapath; this._appLogConfig = config._appLogConfig; this._rPath = config._rPath; this._RFrequencyCalcLimit = config._RFrequencyCalcLimit || 50; this._templateTypes = config._templateTypes; this._defaultTemplates = config._defaultTemplates; this._i18nResources = config._i18nResources || { "en": "English" }; this._customDataTypes = config._customDataTypes; this._enableDevTools = config._enableDevTools === false ? false : true; this._QC_CONFIG = config._QC_CONFIG; this._variableRepoConfig = config._variableRepoConfig; this._publishConfig = config._publishConfig; this._serverPort = parseInt(config._serverPort || 4040); this._RServerPort = parseInt(config._RServerPort || 9090); this._RServer = config._RServer || "opencpu"; this._RTimeLimit = config._RTimeLimit || 900; this._db = config._db; }); } set(config) { Object.keys(config).forEach((prop) => { switch (prop) { case 'userDataPath': this._userdatapath = config.userDataPath; break; case 'masterDataPath': this._masterdatapath = config.masterDataPath; break; case 'appLogConfig': this._appLogConfig = config.appLogConfig; break; case 'rPath': this._rPath = config.rPath; break; case 'RFrequencyCalcLimit': this._RFrequencyCalcLimit = config.RFrequencyCalcLimit; break; case 'templateTypes': this._templateTypes = config.templateTypes; break; case 'i18nResources': this._i18nResources = config.i18nResources; break; case 'enableDevTools': this._enableDevTools = config.enableDevTools; case 'customDataTypes': this._customDataTypes = config.customDataTypes; break; case 'QC_CONFIG': this._QC_CONFIG = config.QC_CONFIG; break; break; case 'variableRepoConfig': this._variableRepoConfig = config.variableRepoConfig; break; case 'publishConfig': this._publishConfig = config.publishConfig; break; case 'serverPort': this._serverPort = parseInt(config.serverPort); break; case 'RServerPort': this._RServerPort = parseInt(config.RServerPort); break; case 'RServer': this._RServer = config.RServer; break; case 'RTimeLimit': this._RTimeLimit = parseInt(config.RTimeLimit); break; case 'db': this._db = config.db; break; } }); return this.saveConfig(); } get userDataPath() { return (this._userdatapath || path.join(os.userInfo().homedir, '.metadata-editor')); } set userDataPath(path) { this._userdatapath = path; this.saveConfig(); } get enableDevTools() { return this._enableDevTools; } set enableDevTools(value) { this._enableDevTools = value; this.saveConfig(); } get templateTypes() { return this._templateTypes; } set templateTypes(path) { this._templateTypes = path; this.saveConfig(); } get customDataTypes() { return this._customDataTypes; } set customDataTypes(types) { this._customDataTypes = types; this.saveConfig(); } get i18nResources() { return this._i18nResources; } set i18nResources(resource) { this._i18nResources = this.i18nResources; this.saveConfig(); } get defaultTemplates() { return this._defaultTemplates || {}; } set defaultTemplates(defaults) { this._defaultTemplates = defaults; this.saveConfig(); } get publishConfig() { return this._publishConfig; } set publishConfig(config) { this._publishConfig = config; this.saveConfig(); } get appLogConfig() { if (!this._appLogConfig) { this._appLogConfig = { level: index_1.LogPreferences.writeOnlyErrors, includeLongParams: index_1.LogBoolStatus.disabled, includeRecMethods: index_1.LogBoolStatus.disabled }; } return this._appLogConfig; } set appLogConfig(__appLogConfig) { this._appLogConfig = __appLogConfig; this.saveConfig(); } get variableRepoConfig() { if (!this._variableRepoConfig) { this._variableRepoConfig = { donotUseGlobal: true, localPath: undefined, globalPath: undefined }; } return this._variableRepoConfig; } set variableRepoConfig(__repoConfig) { this._variableRepoConfig = __repoConfig; this.saveConfig(); } get masterDataPath() { let default_path = path.join(process.cwd(), 'dist'); return path.join(this._masterdatapath || default_path, 'assets'); } set masterDataPath(path) { this._masterdatapath = path; this.saveConfig(); } get workingDirectory() { let default_path = path.join(process.cwd(), 'dist'); return (this._masterdatapath || default_path); } get dbConnectionString() { let default_connectionString = 'mongodb://localhost:27017'; return (this.db.connectionData || default_connectionString); } get rPath() { return (this._rPath); } set rPath(rPackagePath) { this._rPath = rPackagePath; this.saveConfig(); } get RFrequencyCalcLimit() { return (this._RFrequencyCalcLimit); } set RFrequencyCalcLimit(limit) { this._RFrequencyCalcLimit = limit; this.saveConfig(); } get QC_CONFIG() { return this._QC_CONFIG; } set QC_CONFIG(config) { this._QC_CONFIG = config; } get serverPort() { return this._serverPort || 4040; } set serverPort(config) { this._serverPort = config; this.saveConfig(); } get RServerPort() { return this._RServerPort || 9090; } set RServerPort(config) { this._RServerPort = config; this.saveConfig(); } get RServer() { return this._RServer || 'opencpu'; } set RServer(config) { this._RServer = config; this.saveConfig(); } get RTimeLimit() { return this._RTimeLimit || 900; } set RTimeLimit(config) { this._RTimeLimit = config; this.saveConfig(); } get db() { if (!this._db) { this._db = { db: index_1.Db.nedb, connectionData: '' }; } return this._db; } set db(config) { this._db = config; this.saveConfig(); } readConfig() { return existsSync(configPath).then((exists) => { if (exists) { return readFileAsync(configPath, 'utf8').then((datum) => { if (typeof datum === 'string') { try { return JSON.parse(datum); } catch (e) { console.error(e); return {}; } } else { return datum; } }); } else { return {}; } }).catch((e) => { return {}; }); } saveConfig() { return writeFileAsync(configPath, JSON.stringify(this, null, 4)) .then((resp) => { return true; }).catch((e) => { return false; }); } } exports.ProjectConfiguration = ProjectConfiguration; exports.configuaration = new ProjectConfiguration();