1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.RuleLoader = void 0;
|
4 | const tslib_1 = require("tslib");
|
5 | const inversify_1 = require("inversify");
|
6 | const ymir_1 = require("@fimbul/ymir");
|
7 | const debug = require("debug");
|
8 | const bind_decorator_1 = require("bind-decorator");
|
9 | const utils_1 = require("../utils");
|
10 | const log = debug('wotan:ruleLoader');
|
11 | let RuleLoader = class RuleLoader {
|
12 | constructor(host, logger, cache) {
|
13 | this.host = host;
|
14 | this.logger = logger;
|
15 | this.cache = cache.create();
|
16 | }
|
17 | loadRule(name, directories) {
|
18 | if (directories === undefined) {
|
19 | const ctor = utils_1.resolveCachedResult(this.cache, name, this.loadCoreRule);
|
20 | if (ctor === undefined)
|
21 | this.logger.warn(`Could not find core rule '${name}'.`);
|
22 | return ctor;
|
23 | }
|
24 | for (const dir of directories) {
|
25 | const ctor = utils_1.resolveCachedResult(this.cache, `${dir}💩${name}`, this.loadCustomRule);
|
26 | if (ctor !== undefined)
|
27 | return ctor;
|
28 | }
|
29 | this.logger.warn(`Could not find rule '${name}' in '${directories.join()}'.`);
|
30 | return;
|
31 | }
|
32 | loadCoreRule(name) {
|
33 | log('Loading core rule %s', name);
|
34 | return this.host.loadCoreRule(name);
|
35 | }
|
36 | loadCustomRule(cacheKey) {
|
37 | const [directory, name] = cacheKey.split('💩');
|
38 | log('Looking for %s in directory %s', name, directory);
|
39 | return this.host.loadCustomRule(name, directory);
|
40 | }
|
41 | };
|
42 | tslib_1.__decorate([
|
43 | bind_decorator_1.default,
|
44 | tslib_1.__metadata("design:type", Function),
|
45 | tslib_1.__metadata("design:paramtypes", [String]),
|
46 | tslib_1.__metadata("design:returntype", void 0)
|
47 | ], RuleLoader.prototype, "loadCoreRule", null);
|
48 | tslib_1.__decorate([
|
49 | bind_decorator_1.default,
|
50 | tslib_1.__metadata("design:type", Function),
|
51 | tslib_1.__metadata("design:paramtypes", [String]),
|
52 | tslib_1.__metadata("design:returntype", void 0)
|
53 | ], RuleLoader.prototype, "loadCustomRule", null);
|
54 | RuleLoader = tslib_1.__decorate([
|
55 | inversify_1.injectable(),
|
56 | tslib_1.__metadata("design:paramtypes", [ymir_1.RuleLoaderHost, ymir_1.MessageHandler, ymir_1.CacheFactory])
|
57 | ], RuleLoader);
|
58 | exports.RuleLoader = RuleLoader;
|
59 |
|
\ | No newline at end of file |