-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
view.ts
149 lines (130 loc) · 3.15 KB
/
view.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {
extname,
resolve,
dirname,
basename,
join,
fromFileUrl,
} from "../deps.ts";
/**
* Return a stat, maybe.
*
* @param {string} path
* @return {Deno.FileInfo}
* @private
*/
function tryStat(path: string) {
try {
return Deno.statSync(path);
} catch (e) {
return undefined;
}
}
function toPath(pathLike: string) {
return pathLike.startsWith("file:") ? fromFileUrl(pathLike) : pathLike;
}
export class View {
defaultEngine!: any;
ext!: any;
name!: any;
root!: any;
engine!: any;
path!: any;
/**
* Initialize a new `View` with the given `name`.
*
* Options:
*
* - `defaultEngine` the default template engine name
* - `engines` template engine require() cache
* - `root` root path for view lookup
*
* @param {string} name
* @param {object} options
*/
constructor(fileName: string, options: any = {}) {
this.defaultEngine = options.defaultEngine;
this.ext = extname(fileName);
this.name = fileName;
if (Array.isArray(options.root)) {
this.root = options.root.map(toPath);
} else {
this.root = toPath(options.root);
}
if (!this.ext && !this.defaultEngine) {
throw new Error(
"No default engine was specified and no extension was provided.",
);
}
if (!this.ext) {
// get extension from default engine name
this.ext = this.defaultEngine[0] !== "."
? `.${this.defaultEngine}`
: this.defaultEngine;
fileName += this.ext;
}
if (!options.engines[this.ext]) {
// TODO: consider if any way to dynamic import view engines based
// off of extension name - likely difficult without import maps
// or equivalent.
throw new Error(
`Could not find a view engine for extension "${this.ext}"`,
);
}
// store loaded engine
this.engine = options.engines[this.ext];
// lookup path
this.path = this.lookup(fileName);
}
/**
* Resolve the file within the given directory.
*
* @param {string} dir
* @param {string} file
* @private
*/
resolve(dir: string, file: string) {
let path = join(dir, file);
let stat = tryStat(path);
if (stat && stat.isFile) {
return path;
}
// <path>/index.<ext>
const ext = this.ext;
path = join(dir, basename(file, ext), `index${ext}`);
stat = tryStat(path);
if (stat && stat.isFile) {
return path;
}
}
/**
* Lookup view by the given `name`
*
* @param {string} name
* @private
*/
lookup(name: string) {
const roots = [].concat(this.root);
let path;
for (let i = 0; i < roots.length && !path; i++) {
const root = roots[i];
const loc = resolve(root, name);
const dir = dirname(loc);
const file = basename(loc);
path = this.resolve(dir, file);
}
return path;
}
// TODO: move to / support Deno async style over callbacks
/**
* Render with the given options.
*
* @param {object} options
* @param {Function} callback
* @private
*/
async render(options: object, callback: Function) {
const out = await this.engine(this.path, options);
callback(undefined, out);
}
}