@kobold/core
Version:
Kobold core
83 lines (60 loc) • 2.72 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
var _Object$defineProperty = require("@babel/runtime-corejs3/core-js/object/define-property");
_Object$defineProperty(exports, "__esModule", {
value: true
});
exports.Kobold = void 0;
var _entries = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/object/entries"));
var _forEach = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/instance/for-each"));
var _map = _interopRequireDefault(require("@babel/runtime-corejs3/core-js/map"));
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/defineProperty"));
var _path = require("./path");
var _repository = require("./repository");
var _utilities = require("./utilities");
class Kobold {
constructor() {
(0, _defineProperty2.default)(this, "categoryIdMap", new _map.default());
(0, _defineProperty2.default)(this, "repositories", new _map.default());
(0, _defineProperty2.default)(this, "defaultRepository", void 0);
}
addCategories(categories) {
var _context;
(0, _forEach.default)(_context = (0, _entries.default)(categories)).call(_context, ([name, id]) => this.addCategory(name, id));
}
addCategory(name, id) {
this.categoryIdMap.set(name, id);
}
addRepository(opts) {
this.repositories.set(opts.name, new _repository.Repository(opts));
if (opts.default) {
this.defaultRepository = opts.name;
}
}
async getFile(stringOrPath, FileClass) {
const fileBuffer = await this.getFileRaw(stringOrPath);
return new FileClass({
data: fileBuffer
});
} // TODO: Might be worth refactoring Buffer->DataView so we're not quite so tied to node?
getFileRaw(stringOrPath) {
const path = typeof stringOrPath === 'string' ? this.parsePath(stringOrPath) : stringOrPath;
const repository = this.repositories.get(path.repository);
(0, _utilities.assert)(repository != null);
return repository.getFile(path);
}
parsePath(path) {
const [catName, maybeRepoName] = path.split('/', 3);
(0, _utilities.assert)(catName != null && maybeRepoName != null, `Path ${path} is malformed.`);
const catId = this.categoryIdMap.get(catName);
(0, _utilities.assert)(catId != null, `No mapping found for category "${catName}".`);
const repoName = this.repositories.has(maybeRepoName) ? maybeRepoName : this.defaultRepository;
(0, _utilities.assert)(repoName != null, `No mapping found for repository "${maybeRepoName}", and no default repository is configured.`);
return new _path.Path({
category: catId,
repository: repoName,
path
});
}
}
exports.Kobold = Kobold;