UNPKG

lakutata

Version:

An IoC-based universal application framework.

113 lines (93 loc) 3.45 kB
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */ "use strict"; Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" }); const e = require("path"); const a = require("../../exceptions/alias/InvalidAliasNameException.cjs"); const s = require("../../exceptions/alias/AliasExistsException.cjs"); const t = require("../../exceptions/alias/AliasNotFoundException.cjs"); require("../base/abstracts/Exception.cjs"); require("../../../vendor/Package.internal.5.cjs"); require("../helpers/As.cjs"); require("../../../vendor/Package.internal.2.cjs"); require("../../../vendor/Package.internal.6.cjs"); require("../base/internal/ThrowWarning.cjs"); require("../helpers/Templating.cjs"); require("../base/internal/CamelCase.cjs"); require("../helpers/NoCase.cjs"); require("../helpers/DevNull.cjs"); const i = e => e && e.__esModule ? e : { default: e }; const l = i(e); class Alias { constructor() { this.aliasNameRegExp = /^@[a-zA-Z0-9]+$/; this.aliasMap = new Map; const e = l.default.resolve; l.default.resolve = (...a) => { const s = a.map(e => this.resolve(e)); return e(...s); }; } static init() { const e = new this; Reflect.defineMetadata(this, e, this); } static getAliasInstance() { if (!Reflect.hasOwnMetadata(this, this)) this.init(); return Reflect.getMetadata(this, this); } set(e, t, i = false) { if (!this.aliasNameRegExp.test(e)) throw new a.InvalidAliasNameException("The alias should start with '@' and only contain characters a-z|A-Z|0-9. The current alias '{aliasName}' does not comply with this rule.", { aliasName: e }); if (this.aliasMap.has(e) && !i) throw new s.AliasExistsException("The alias '{aliasName}' already exists.", { aliasName: e }); this.aliasMap.set(e, t); } get(e) { if (!this.aliasNameRegExp.test(e)) throw new a.InvalidAliasNameException("The alias should start with '@' and only contain characters a-z|A-Z|0-9. The current alias '{aliasName}' does not comply with this rule.", { aliasName: e }); if (!this.aliasMap.has(e)) throw new t.AliasNotFoundException("The alias '{aliasName}' not found.", { aliasName: e }); const s = this.aliasMap.get(e); return this.resolve(s); } has(e) { if (!this.aliasNameRegExp.test(e)) throw new a.InvalidAliasNameException("The alias should start with '@' and only contain characters a-z|A-Z|0-9. The current alias '{aliasName}' does not comply with this rule.", { aliasName: e }); return this.aliasMap.has(e); } resolve(e) { let a = true; const s = []; this.aliasMap.forEach((e, a) => s.push({ name: a, path: e })); s.sort((e, a) => e.name.length < a.name.length ? 1 : -1); while (a) { let t = false; s.forEach(({name: a, path: s}) => { if (e.includes(a)) { t = true; e = e.replace(a, s); } }); a = t; } return l.default.normalize(e); } list() { const e = {}; this.aliasMap.forEach((a, s) => Reflect.set(e, s, a)); return e; } } exports.Alias = Alias;