-
Notifications
You must be signed in to change notification settings - Fork 6
/
adapter.js
63 lines (49 loc) · 1.74 KB
/
adapter.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
'use strict';
const consolidate = require('consolidate');
const Path = require('path');
const _ = require('lodash');
const Adapter = require('@frctl/fractal').Adapter;
class ConsolidateAdapter extends Adapter {
constructor(engineName, instance, source, app) {
if (instance) {
consolidate.requires[engineName] = instance;
}
super(instance || require(engineName), source);
this._engineName = engineName;
this._app = app;
}
get engine() {
return this._engine;
}
render(tplPath, str, context, meta) {
meta = meta || {};
setEnv('_self', meta.self, context);
setEnv('_target', meta.target, context);
setEnv('_env', meta.env, context);
setEnv('_config', this._app.config(), context);
context.partials = {};
_.each(this._views, function(view){
if (tplPath != view.path) {
const relPath = Path.relative(tplPath, view.path).replace('../', '');
const parts = Path.parse(relPath);
if ( !_.isEmpty(parts.name) && (Path.extname(tplPath) == Path.extname(view.path))) {
context.partials[view.handle] = Path.join(parts.dir, parts.name);
}
}
});
return Promise.resolve(consolidate[this._engineName](tplPath, context));
}
}
function setEnv(key, value, context) {
if (_.isUndefined(context[key]) && ! _.isUndefined(value)) {
context[key] = value;
}
}
module.exports = function(engineName, instance) {
return {
register(source, app) {
return new ConsolidateAdapter(engineName, instance, source, app);
}
}
};
module.exports.consolidate = consolidate;