botpress
Version:
The world's first CMS for bots. Easily create, manage and extend chatbots.
127 lines (97 loc) • 4.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _lodash = require('lodash');
var _lodash2 = _interopRequireDefault(_lodash);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
class ModuleConfiguration {
constructor(options) {
this.manager = options.manager;
this.module = options.module;
this.logger = options.logger;
this.configLocation = options.configLocation;
}
_getFileName() {
const sanitizedName = this.module.name.replace(/^@botpress(-)?\//i, '').replace(/^botpress(-)?/i, '').replace(_path2.default.delimiter, '_');
return `${sanitizedName}.json`;
}
_getOptions() {
return this.module.options;
}
_hasDefaultConfig() {
const filePath = _path2.default.resolve(this.module.path, 'config.json');
return _fs2.default.existsSync(filePath);
}
_readDefaultConfig() {
const filePath = _path2.default.resolve(this.module.path, 'config.json');
return _fs2.default.readFileSync(filePath, 'utf8');
}
loadAll(caching = true) {
var _this = this;
return _asyncToGenerator(function* () {
const config = yield _this.manager.loadAll(_this._getFileName(), _this._getOptions(), caching);
const file = _this._getFileName();
const filePath = _path2.default.resolve(_this.configLocation, file);
_lodash2.default.mapValues(config, function (value, key) {
if (value === '<<UPDATE_ME>>') {
const message = `[${_this.module.name}] Missing mandatory configuration for "${key}".
You can provide this value in "${filePath}"`;
_this.logger.error(message);
throw new Error(message);
}
});
return config;
})();
}
saveAll(newConfig) {
const configPath = _path2.default.join(this.configLocation, this._getFileName());
const oldConfig = JSON.parse(_fs2.default.readFileSync(configPath, 'utf8'));
const config = Object.assign(oldConfig, newConfig);
_fs2.default.writeFileSync(configPath, JSON.stringify(config), 'utf8');
}
get(key, caching = true) {
var _this2 = this;
return _asyncToGenerator(function* () {
return _this2.manager.get(_this2._getFileName(), key, _this2._getOptions(), caching);
})();
}
/**
* Copy the module's default configuration file to the bot's config directory
* @private
*/
bootstrap() {
var _this3 = this;
return _asyncToGenerator(function* () {
if (!_this3._hasDefaultConfig()) {
return;
}
const file = _this3._getFileName();
const filePath = _path2.default.resolve(_this3.configLocation, file);
const content = _this3._readDefaultConfig();
_fs2.default.writeFileSync(filePath, content, 'utf8');
_this3.logger.info(`Configuration for module "${_this3.module.name}" has been created at ${filePath}`);
})();
}
/**
* Checks whether the module has a configuration file
* and if the bot doesn't have the configuration file for it.
* @private
* @return {Boolean}
*/
isConfigMissing() {
var _this4 = this;
return _asyncToGenerator(function* () {
const file = _this4._getFileName();
const filePath = _path2.default.resolve(_this4.configLocation, file);
return _this4._hasDefaultConfig() && !_fs2.default.existsSync(filePath);
})();
}
}
exports.default = ModuleConfiguration;
//# sourceMappingURL=module.js.map