-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender-file.node.js
45 lines (38 loc) · 1.27 KB
/
render-file.node.js
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
#!/usr/bin/env node
"use strict"
/* Compile script for ddr0.ca.
Ingests .html.js files and evaluates them, outputting the last statement
to stdout. Usually this is the results of a big ol' format string.
This script can be run manually, of course, but it is primarily intended
to be used by the makefile.
*/
const vm = require('vm')
const fs = require('fs')
const render = (filename, constants={}) => {
const results = vm.runInContext(
fs.readFileSync(filename, {encoding:'utf8'}),
vm.createContext({
require, render,
include: render,
dump: (...args) => (console.error.apply(console, args), args.slice(-1)[0]),
paste: filename => fs.readFileSync(filename, {encoding:'utf8'}),
indent: (level, text) =>
level > 0
? '\t'.repeat(level)+text.trim('\n').split('\n').join('\n'+'\t'.repeat(level))
: text.split('\n').map(line => line.slice(-level)).join('\n'),
page: process.argv[2],
global: constants, //Look up a constant in the script.
...constants, //Or just reference it, which is easier but may fail if missing.
}),
{
filename,
timeout: 200,
}
)
if (typeof results === 'string') {
return results.trim('\n')
} else {
throw new Error(`Compilation failed, expected string got ${typeof results}.`)
}
}
module.exports = render