-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (40 loc) · 994 Bytes
/
index.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
46
47
48
var yate = require('yate');
var fs = require('fs');
//var runtime = require('yate/lib/runtime')
var settings = {
'caching': false
};
exports.set = function(setting, val) {
if (1 == arguments.length) {
if (settings.hasOwnProperty(setting)) {
return settings[setting];
}
} else {
settings[setting] = val;
return this;
}
return exports;
};
exports.__express = function(path, options, fn) {
if (!settings.caching || (settings.caching && !this.yateFn)) {
var runtimeJS = fs.readFileSync(__dirname + '/node_modules/yate/lib/runtime.js');
var compiledJS = yate.compile(path).js;
this.yateFn = this.yateFn || function(data) {
var yateJS = [
runtimeJS,
compiledJS,
'return yr.run("main", data, "")'
].join(';');
return new Function('data', yateJS)(data);
};
}
if ('function' == typeof options) {
fn = options, options = {};
}
var data = JSON.parse(JSON.stringify(options))
try {
fn(null, this.yateFn(data));
} catch (err) {
fn(err);
}
}