1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const { createGzip } = require('zlib');
|
15 | const { join } = require('path');
|
16 | const { html } = require('./response');
|
17 | const color = require('chalk');
|
18 |
|
19 | const cwd = process.cwd();
|
20 | const isProduction = false;
|
21 |
|
22 | async function page(name, bindings) {
|
23 | const template = require(join(cwd, 'pages', `${name}.marko`));
|
24 | const error = require(join(__dirname, 'util', 'error.marko'));
|
25 |
|
26 | let r;
|
27 | try {
|
28 | r = await template.render(bindings);
|
29 | } catch (_) {
|
30 | console.error(
|
31 | `${color.bold.red(
|
32 | 'Error:'
|
33 | )} there's a problem generating the page '${color.blue(name)}.marko'`
|
34 | );
|
35 | r = await error.render({ _ });
|
36 | }
|
37 |
|
38 | return html(r.getOutput());
|
39 | }
|
40 |
|
41 | function gzip(body) {
|
42 | return {
|
43 | body: body.pipe(createGzip()),
|
44 | encoding: 'gzip'
|
45 | };
|
46 | }
|
47 |
|
48 | module.exports = {
|
49 | page,
|
50 | gzip
|
51 | };
|