deno.land / x / eta@v3.4.0 / src / file-handling.ts

file-handling.ts
نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { EtaFileResolutionError } from "./err.ts";
import * as path from "node:path";
import * as fs from "node:fs";
/* TYPES */import type { Eta as EtaCore } from "./core.ts";import type { Options } from "./config.ts";/* END TYPES */
export function readFile(this: EtaCore, path: string): string { let res = "";
try { res = fs.readFileSync(path, "utf8"); // eslint-disable-line @typescript-eslint/no-explicit-any } catch (err: any) { if (err?.code === "ENOENT") { throw new EtaFileResolutionError(`Could not find template: ${path}`); } else { throw err; } }
return res;}
export function resolvePath( this: EtaCore, templatePath: string, options?: Partial<Options>): string { let resolvedFilePath = "";
const views = this.config.views;
if (!views) { throw new EtaFileResolutionError("Views directory is not defined"); }
const baseFilePath = options && options.filepath; const defaultExtension = this.config.defaultExtension === undefined ? ".eta" : this.config.defaultExtension;
// how we index cached template paths const cacheIndex = JSON.stringify({ filename: baseFilePath, // filename of the template which called includeFile() path: templatePath, views: this.config.views, });
templatePath += path.extname(templatePath) ? "" : defaultExtension;
// if the file was included from another template if (baseFilePath) { // check the cache
if (this.config.cacheFilepaths && this.filepathCache[cacheIndex]) { return this.filepathCache[cacheIndex]; }
const absolutePathTest = absolutePathRegExp.exec(templatePath);
if (absolutePathTest && absolutePathTest.length) { const formattedPath = templatePath.replace(/^\/*|^\\*/, ""); resolvedFilePath = path.join(views, formattedPath); } else { resolvedFilePath = path.join(path.dirname(baseFilePath), templatePath); } } else { resolvedFilePath = path.join(views, templatePath); }
if (dirIsChild(views, resolvedFilePath)) { // add resolved path to the cache if (baseFilePath && this.config.cacheFilepaths) { this.filepathCache[cacheIndex] = resolvedFilePath; }
return resolvedFilePath; } else { throw new EtaFileResolutionError(`Template '${templatePath}' is not in the views directory`); }}
function dirIsChild(parent: string, dir: string) { const relative = path.relative(parent, dir); return relative && !relative.startsWith("..") && !path.isAbsolute(relative);}
const absolutePathRegExp = /^\\|^\//;
eta

Version Info

Tagged at
2 months ago