sipp
Version:
An Opinionated, High-Productivity MVC Web Framework in TypeScript
51 lines • 1.98 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Url = void 0;
const path_1 = require("path");
const fs_1 = require("fs");
class Url {
constructor(staticPath, routeMapper) {
this.staticPath = staticPath;
this.routeMapper = routeMapper;
}
alias(name, params, query, method) {
return this.routeMapper.resolve(name, params, query, method);
}
url(relativeUrl, params, query, method) {
return this.routeMapper.construcUrl(relativeUrl.split('/'), params, query, method);
}
asset(path) {
const absPath = path_1.join(this.staticPath, path);
const { dir, base } = path_1.parse(absPath);
if (base.indexOf('[hash]')) {
const directoryContents = fs_1.readdirSync(dir);
const parts = base.split('[hash]');
const re = new RegExp(`^${parts[0]}.+${parts[1]}$`);
const hashedFile = directoryContents.find((filePath) => re.test(filePath));
return hashedFile || path;
}
return path;
}
scriptTag(filePath, defer = true) {
let scriptPath = filePath;
if (!filePath.startsWith('http')) {
const { base } = path_1.parse(filePath);
const jsUrl = this.asset(`${base}.js`);
const { dir, name } = path_1.parse(jsUrl);
scriptPath = `${dir.replace(this.staticPath, '')}${name}.js`;
}
return `<script src="${scriptPath}" ${defer ? 'defer' : ''}></script>`;
}
styleTag(filePath) {
let stylePath = filePath;
if (!filePath.startsWith('http')) {
const { base } = path_1.parse(filePath);
const csUrl = this.asset(`${base}.css`);
const { dir, name } = path_1.parse(csUrl);
stylePath = `${dir.replace(this.staticPath, '')}${name}.css`;
}
return `<link rel="stylesheet" href="${stylePath}">`;
}
}
exports.Url = Url;
//# sourceMappingURL=Url.js.map