alapa
Version:
A cutting-edge web development framework designed to revolutionize the way developers build modern web applications.
113 lines (112 loc) • 4.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.PathResolver = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const globals_1 = require("../shared/globals");
class PathResolver {
static rootDir = "";
static resolve(filePath, options) {
this.updateRoot("views");
filePath = this.replaceSeparator(this.normalizePath(filePath));
const dir = this.removeRootDir(this.replaceSeparator(this.dirname(options?.currentPath || "")));
if (filePath.startsWith("/") && !this.hasRoot(filePath)) {
return path_1.default.resolve(this.rootDir, filePath.slice(1));
}
if (!path_1.default.isAbsolute(filePath)) {
filePath = path_1.default.join(this.rootDir, dir, filePath);
}
filePath = path_1.default.resolve(filePath);
if (!fs_1.default.existsSync(filePath)) {
throw new Error("Could not find the template " + filePath);
}
return filePath;
}
static dirname(filePath) {
return path_1.default.dirname(filePath);
}
static removeEXTDot(extname) {
extname = extname.trim();
if (extname.startsWith("."))
return extname.slice(1, extname.length);
return extname;
}
static replaceSeparator(filePath) {
return this.removeDoubleSlashes(filePath.replace("\\", "/"));
}
static removeDoubleSlashes(filePath) {
return filePath.replace("//", "/");
}
static removeSlashFromTheEnd(filePath) {
return filePath.replace(/\/+$/, "");
}
static hasRoot(filePath) {
return filePath.startsWith(this.rootDir);
}
static updateRoot(defaultExtension) {
this.rootDir = this.replaceSeparator(this.removeSlashFromTheEnd(path_1.default.resolve(globals_1.GlobalConfig.view.dir || defaultExtension)));
}
static removeRootDir(filePath) {
return this.removeSlashFromBeginning(filePath.replace(this.rootDir, ""));
}
static removeSlashFromBeginning(filePath) {
return filePath.replace(/^\/+/, "");
}
static removeDotFromBeginning(filePath) {
return filePath.replace(/^\.+/, "");
}
static addExtension(filePath, ext) {
if (ext == null || ext.length == 0) {
ext = "html";
}
else if (!this.validExtension(ext)) {
throw new Error(`Invalid extension: ${ext}`);
}
return `${filePath}.${this.removeEXTDot(ext)}`;
}
static normalizePath(filePath) {
const startsWithDot = filePath.startsWith(".");
filePath = filePath.replace(/\.?\.[/\\]/g, (match) => {
if (match.includes(".."))
return "##/";
return "#/";
});
let ext = "";
if (this.validExtension(path_1.default.extname(filePath))) {
ext = this.removeEXTDot(path_1.default.extname(filePath));
filePath = filePath.replace(new RegExp(`\\.${ext}$`), "");
}
if (startsWithDot) {
filePath = this.removeDotFromBeginning(filePath);
}
filePath = filePath.replace(/(\\?\.)/g, (match, dot) => {
if (dot.startsWith("\\"))
return ".";
return "/";
});
if (startsWithDot) {
filePath = `./${filePath}`;
}
filePath = filePath.replace(/#/g, ".");
return this.addExtension(filePath, ext);
}
static validExtension(ext) {
if (ext.trim().length == 0)
return false;
ext = this.removeEXTDot(ext);
const commonFileExtension = ["html"];
const extensions = globals_1.GlobalConfig.view.extensions;
if (Array.isArray(extensions)) {
commonFileExtension.push(...extensions);
}
if (typeof extensions === "string") {
commonFileExtension.push(...extensions.trim().split(","));
}
return (commonFileExtension.includes(ext) ||
commonFileExtension.includes(`.${ext}`));
}
}
exports.PathResolver = PathResolver;