/
file-handlers.ts
317 lines (281 loc) · 8.57 KB
/
file-handlers.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// express is set like: app.engine('html', require('eta').renderFile)
import EtaErr from "./err.ts";
import compile from "./compile.ts";
import { getConfig } from "./config.ts";
import { getPath, readFile } from "./file-utils.ts";
import { promiseImpl } from "./polyfills.ts";
/* TYPES */
import type {
EtaConfig,
EtaConfigWithFilename,
PartialConfig,
} from "./config.ts";
import type { TemplateFunction } from "./compile.ts";
export type CallbackFn = (err: Error | null, str?: string) => void;
interface DataObj {
[key: string]: any; // eslint-disable-line @typescript-eslint/no-explicit-any
}
interface PartialConfigWithFilename extends Partial<EtaConfig> {
filename: string;
}
/* END TYPES */
/**
* Reads a template, compiles it into a function, caches it if caching isn't disabled, returns the function
*
* @param filePath Absolute path to template file
* @param options Eta configuration overrides
* @param noCache Optionally, make Eta not cache the template
*/
export function loadFile(
filePath: string,
options: PartialConfigWithFilename,
noCache?: boolean,
): TemplateFunction {
const config = getConfig(options);
const template = readFile(filePath);
try {
const compiledTemplate = compile(template, config);
if (!noCache) {
config.templates.define(
(config as EtaConfigWithFilename).filename,
compiledTemplate,
);
}
return compiledTemplate;
} catch (e) {
throw EtaErr(
"Loading file: " + filePath + " failed:\n\n" + (e as Error).message,
);
}
}
/**
* Get the template from a string or a file, either compiled on-the-fly or
* read from cache (if enabled), and cache the template if needed.
*
* If `options.cache` is true, this function reads the file from
* `options.filename` so it must be set prior to calling this function.
*
* @param options compilation options
* @return Eta template function
*/
function handleCache(options: EtaConfigWithFilename): TemplateFunction {
const filename = options.filename;
if (options.cache) {
const func = options.templates.get(filename);
if (func) {
return func;
}
return loadFile(filename, options);
}
// Caching is disabled, so pass noCache = true
return loadFile(filename, options, true);
}
/**
* Try calling handleCache with the given options and data and call the
* callback with the result. If an error occurs, call the callback with
* the error. Used by renderFile().
*
* @param data template data
* @param options compilation options
* @param cb callback
*/
function tryHandleCache(
data: object,
options: EtaConfigWithFilename,
cb: CallbackFn | undefined,
) {
if (cb) {
try {
// Note: if there is an error while rendering the template,
// It will bubble up and be caught here
const templateFn = handleCache(options);
templateFn(data, options, cb);
} catch (err) {
return cb(err as Error);
}
} else {
// No callback, try returning a promise
if (typeof promiseImpl === "function") {
return new promiseImpl<string>(
function (resolve: Function, reject: Function) {
try {
const templateFn = handleCache(options);
const result = templateFn(data, options);
resolve(result);
} catch (err) {
reject(err);
}
},
);
} else {
throw EtaErr(
"Please provide a callback function, this env doesn't support Promises",
);
}
}
}
/**
* Get the template function.
*
* If `options.cache` is `true`, then the template is cached.
*
* This returns a template function and the config object with which that template function should be called.
*
* @remarks
*
* It's important that this returns a config object with `filename` set.
* Otherwise, the included file would not be able to use relative paths
*
* @param path path for the specified file (if relative, specify `views` on `options`)
* @param options compilation options
* @return [Eta template function, new config object]
*/
function includeFile(
path: string,
options: EtaConfig,
): [TemplateFunction, EtaConfig] {
// the below creates a new options object, using the parent filepath of the old options object and the path
const newFileOptions = getConfig(
{ filename: getPath(path, options) },
options,
);
// TODO: make sure properties are currectly copied over
return [handleCache(newFileOptions as EtaConfigWithFilename), newFileOptions];
}
/**
* Render a template from a filepath.
*
* @param filepath Path to template file. If relative, specify `views` on the config object
*
* This can take two different function signatures:
*
* - `renderFile(filename, data, [cb])`
* - `renderFile(filename, data, [config], [cb])`
*
* Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
*
* **Examples**
*
* ```js
* eta.renderFile("./template.eta", data, {cache: true}, function (err, rendered) {
* if (err) console.log(err)
* console.log(rendered)
* })
*
* let rendered = await eta.renderFile("./template.eta", data, {cache: true})
*
* ```
*/
function renderFile(
filename: string,
data: DataObj,
config?: PartialConfig,
): Promise<string>;
function renderFile(
filename: string,
data: DataObj,
config: PartialConfig,
cb: CallbackFn,
): void;
function renderFile(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn,
): Promise<string> | void;
function renderFile(filename: string, data: DataObj, cb: CallbackFn): void;
function renderFile(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn,
): Promise<string> | void {
/*
Here we have some function overloading.
Essentially, the first 2 arguments to renderFile should always be the filename and data
Express will call renderFile with (filename, data, cb)
We also want to make (filename, data, options, cb) available
*/
let renderConfig: EtaConfigWithFilename;
let callback: CallbackFn | undefined;
data = data || {};
// First, assign our callback function to `callback`
// We can leave it undefined if neither parameter is a function;
// Callbacks are optional
if (typeof cb === "function") {
// The 4th argument is the callback
callback = cb;
} else if (typeof config === "function") {
// The 3rd arg is the callback
callback = config;
}
// If there is a config object passed in explicitly, use it
if (typeof config === "object") {
renderConfig = getConfig(
(config as PartialConfig) || {},
) as EtaConfigWithFilename;
} else {
// Otherwise, get the default config
renderConfig = getConfig({}) as EtaConfigWithFilename;
}
// Set the filename option on the template
// This will first try to resolve the file path (see getPath for details)
renderConfig.filename = getPath(filename, renderConfig);
return tryHandleCache(data, renderConfig, callback);
}
/**
* Render a template from a filepath asynchronously.
*
* @param filepath Path to template file. If relative, specify `views` on the config object
*
* This can take two different function signatures:
*
* - `renderFile(filename, data, [cb])`
* - `renderFile(filename, data, [config], [cb])`
*
* Note that renderFile does not immediately return the rendered result. If you pass in a callback function, it will be called with `(err, res)`. Otherwise, `renderFile` will return a `Promise` that resolves to the render result.
*
* **Examples**
*
* ```js
* eta.renderFile("./template.eta", data, {cache: true}, function (err, rendered) {
* if (err) console.log(err)
* console.log(rendered)
* })
*
* let rendered = await eta.renderFile("./template.eta", data, {cache: true})
*
* ```
*/
function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig,
): Promise<string>;
function renderFileAsync(
filename: string,
data: DataObj,
config: PartialConfig,
cb: CallbackFn,
): void;
function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn,
): Promise<string> | void;
function renderFileAsync(filename: string, data: DataObj, cb: CallbackFn): void;
function renderFileAsync(
filename: string,
data: DataObj,
config?: PartialConfig,
cb?: CallbackFn,
): Promise<string> | void {
return renderFile(
filename,
typeof config === "function" ? { ...data, async: true } : data,
typeof config === "object" ? { ...config, async: true } : config,
cb,
);
}
export { includeFile, renderFile, renderFileAsync };