-
Notifications
You must be signed in to change notification settings - Fork 1
/
osiris.js
151 lines (130 loc) · 3.91 KB
/
osiris.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
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
const srcFolder = process.cwd() + '/src/'; // we will look for templateMap folders here
const atjs = require('./atjs');
// html entity quote function, exposed for changes
module.exports.qMap = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
};
// default template folders & functions, exposed for changes
module.exports.templateMap = {
snippet: 'snippets',
element: 'elements'
};
// symbol map, for privatish object members
const s = {
jsBundle: Symbol('jsBundle'),
cssBundle: Symbol('cssBundle')
};
let mode = 'development';
Object.defineProperty(module.exports, 'mode', {
get () {
return mode;
},
set (val) {
if (val !== 'development' && val !== 'production') {
throw new Error('osiris.mode can only be set to development or production');
}
mode = val;
if (mode === 'development') atjs.cache.reset();
},
enumerable: true,
configurable: false
});
// override atjs cache
atjs.cache = {
_data: {},
set: function (key, val) {
if (mode === 'development') return; // disable caching in dev mode
this._data[key] = val;
},
get: function (key) {
return this._data[key];
},
remove: function (key) {
delete this._data[key];
},
reset: function () {
this._data = {};
}
};
// return a constructor to hold all the variables for a single page render, takes a writableStream
const Osiris = function (writeStream) {
this[s.jsBundle] = [];
this[s.cssBundle] = [];
// setup variables in "this" scope
this.locals = {}; // global scope for templates
// our render function, atjs needs a filename and an object representing local scope
const render = async (filename, args = {}) => {
// copy args to scope and preserve previous scopes args
const previousArgs = this.args;
this.args = args;
await atjs.renderFile(writeStream, filename, this);
this.args = previousArgs;
};
// call this to render a file
this.render = async (filename) => {
delete this.render; // run once
await render(filename, {}); // render with empty args
writeStream.end(); // we're done
};
this.onClose = () => {
// user closed before template finished
};
this.onError = async (message) => {
if (mode === 'development') {
await this.print('<pre>' + this.q(message) + '</pre>');
} else {
throw message;
}
writeStream.end(); // we've failed
};
// setup template functions from map
for (let funcName of Object.keys(module.exports.templateMap)) {
let folderName = module.exports.templateMap[funcName];
this[funcName] = async (filename, args) => {
await render(srcFolder + folderName + '/' + filename + '.atjs', args);
return '';
};
}
};
Osiris.prototype = {
// quote a function according to exports.qMap, return a promise if given a promise
q: (str='') => {
const doQ = (str) => str.split('').map(c => module.exports.qMap[c] || c).join('');
if (str instanceof Promise) return new Promise(async (res, rej) => {
res(doQ(await str));
});
return doQ(str);
},
// collection points for js and css
js: function (str) {
this[s.jsBundle].push(str);
return '';
},
bundleJs: function () {
return this[s.jsBundle].join('\n');
},
css: function (str) {
this[s.cssBundle].push(str);
return '';
},
bundleCss: function () {
return this[s.cssBundle].join('\n');
},
};
const copyScopes = (obj, scopes) => {
for (let copy of scopes) for (let i of Object.keys(copy)) {
Object.defineProperty(obj, i, Object.getOwnPropertyDescriptor(copy, i));
}
};
module.exports.use = (...modules) => {
copyScopes(Osiris.prototype, modules);
};
module.exports.render = (writeStream, filename, ...modules) => {
let osiris = new Osiris(writeStream);
copyScopes(osiris, modules);
osiris.render(filename);
};