-
Notifications
You must be signed in to change notification settings - Fork 9
/
mod.ts
131 lines (111 loc) · 3.13 KB
/
mod.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
import HandlebarsJS from "https://dev.jspm.io/handlebars@4.7.6";
import { walk } from "https://deno.land/std@0.51.0/fs/mod.ts";
import {
globToRegExp,
normalize,
join,
} from "https://deno.land/std@0.51.0/path/mod.ts";
const { readFile } = Deno;
interface HandlebarsConfig {
baseDir: string;
extname: string;
layoutsDir: string;
partialsDir: string;
defaultLayout: string;
helpers: any;
compilerOptions: any;
}
const DEFAULT_HANDLEBARS_CONFIG: HandlebarsConfig = {
baseDir: "views",
extname: ".hbs",
layoutsDir: "layouts/",
partialsDir: "partials/",
defaultLayout: "main",
helpers: undefined,
compilerOptions: undefined,
};
function getNormalizePath(path: string) {
return normalize(path).replace(/\\/g, "/");
}
export class Handlebars {
constructor(private config: HandlebarsConfig = DEFAULT_HANDLEBARS_CONFIG) {
this.config = { ...DEFAULT_HANDLEBARS_CONFIG, ...config };
console.log(this.config);
}
/**
* Processes of rendering view
*
* @param view
* @param context
* @param layout
*/
public async renderView(
view: string,
context?: Object,
layout?: string,
): Promise<string> {
if (!view) {
console.warn("View is null");
return "";
}
const config: HandlebarsConfig = this.config as HandlebarsConfig;
const partialsPathes = await this.getTemplatesPath(
join(config.baseDir, config.partialsDir),
);
partialsPathes && (await this.registerPartials(partialsPathes));
const path = join(config.baseDir, view + config.extname);
const body: string = await this.render(path, context);
layout = (layout as string) || config.defaultLayout;
if (layout) {
const layoutPath: string = join(
config.baseDir,
config.layoutsDir,
layout + config.extname,
);
return this.render(layoutPath, { ...context, body });
}
return body;
}
/**
* Processes on render without partials and layouts
*/
public async render(path: string, context?: Object): Promise<string> {
// TODO: use cashe
const source: string = new TextDecoder().decode(await readFile(path));
const template = HandlebarsJS.compile(source, this.config!.compilerOptions);
return template(context);
}
/**
* Processes on register partials
*/
private async registerPartials(pathes: string[]) {
for (const path of pathes) {
const templateName: string = path
.replace(
getNormalizePath(this.config.baseDir) + "/" +
this.config!.partialsDir,
"",
)
.replace(new RegExp(this.config!.extname + "$"), "");
const source: string = new TextDecoder().decode(await readFile(path));
HandlebarsJS.registerPartial(templateName, source);
}
}
/**
* Gets template pathes with glob match
*/
private async getTemplatesPath(path: string): Promise<string[]> {
const arr: string[] = [];
for await (
const w of walk(
path,
{ match: [globToRegExp("**/*" + this.config!.extname)] },
)
) {
if (w.isFile) {
arr.push(getNormalizePath(w.path));
}
}
return arr;
}
}