UNPKG

lakutata

Version:

An IoC-based universal application framework.

101 lines (85 loc) 3.18 kB
/* Build Date: Mon Jan 05 2026 23:52:23 GMT+0800 (China Standard Time) */ import a from "path"; import { InvalidAliasNameException as e } from "../../exceptions/alias/InvalidAliasNameException.mjs"; import { AliasExistsException as t } from "../../exceptions/alias/AliasExistsException.mjs"; import { AliasNotFoundException as s } from "../../exceptions/alias/AliasNotFoundException.mjs"; import "../base/abstracts/Exception.mjs"; import "../../../vendor/Package.internal.5.mjs"; import "../helpers/As.mjs"; import "../../../vendor/Package.internal.2.mjs"; import "../../../vendor/Package.internal.6.mjs"; import "../base/internal/ThrowWarning.mjs"; import "../helpers/Templating.mjs"; import "../base/internal/CamelCase.mjs"; import "../helpers/NoCase.mjs"; import "../helpers/DevNull.mjs"; class Alias { constructor() { this.aliasNameRegExp = /^@[a-zA-Z0-9]+$/; this.aliasMap = new Map; const e = a.resolve; a.resolve = (...a) => { const t = a.map(a => this.resolve(a)); return e(...t); }; } static init() { const a = new this; Reflect.defineMetadata(this, a, this); } static getAliasInstance() { if (!Reflect.hasOwnMetadata(this, this)) this.init(); return Reflect.getMetadata(this, this); } set(a, s, i = false) { if (!this.aliasNameRegExp.test(a)) throw new e("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: a }); if (this.aliasMap.has(a) && !i) throw new t("The alias '{aliasName}' already exists.", { aliasName: a }); this.aliasMap.set(a, s); } get(a) { if (!this.aliasNameRegExp.test(a)) throw new e("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: a }); if (!this.aliasMap.has(a)) throw new s("The alias '{aliasName}' not found.", { aliasName: a }); const t = this.aliasMap.get(a); return this.resolve(t); } has(a) { if (!this.aliasNameRegExp.test(a)) throw new e("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: a }); return this.aliasMap.has(a); } resolve(e) { let t = true; const s = []; this.aliasMap.forEach((a, e) => s.push({ name: e, path: a })); s.sort((a, e) => a.name.length < e.name.length ? 1 : -1); while (t) { let a = false; s.forEach(({name: t, path: s}) => { if (e.includes(t)) { a = true; e = e.replace(t, s); } }); t = a; } return a.normalize(e); } list() { const a = {}; this.aliasMap.forEach((e, t) => Reflect.set(a, t, e)); return a; } } export { Alias };