-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
157 lines (134 loc) · 3.92 KB
/
setup.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
152
153
154
155
156
157
'use strict';
const path = require('path'),
_ = require('lodash'),
glob = require('glob'),
fs = require('fs'),
handlebars = require('handlebars'),
render = require('./render'),
{safeGetComponentName} = require('./util'),
nymagHbs = require('clayhandlebars'),
hbs = nymagHbs(handlebars.create()),
TEMPLATE_NAME = 'amp.template';
let amphoraFs = require('amphora-fs'),
log = require('./log').setup({file: __filename});
/**
* Initialize the package on load
*/
function init() {
hbs.registerPartial('noop-component', '');
// Override the `getComponentName` helper provided by clayhandlebars so that it will log on unfound
// partials instead of breaking the render.
hbs.registerHelper('getComponentName', safeGetComponentName(hbs, log));
// Register partials
registerPartials();
// Set partials for render
render.setHbs(module.exports.hbs);
}
/**
* @typedef {Object} renderableItem
* @property {string} name
* @property {string} path
* @returns {renderableItem[]}
*/
function getRenderableItems() {
const items = [];
_.each(amphoraFs.getComponents(), function (cmpt) {
items.push({
name: cmpt,
path: amphoraFs.getComponentPath(cmpt)
});
});
_.each(amphoraFs.getLayouts(), function (layout) {
items.push({
name: layout,
path: amphoraFs.getLayoutPath(layout)
});
});
return items;
}
/**
* Register the partial with `nymag-handlebars` for reference later
*/
function registerPartials() {
const renderableItems = getRenderableItems();
/**
*
* @param {renderableItem} renderableItem
*/
function registerRenderableItem(renderableItem) {
const templateFile = getPossibleTemplates(renderableItem.path, TEMPLATE_NAME),
isHandlebars = _.includes(templateFile, '.handlebars') || _.includes(templateFile, '.hbs');
let templateFileContents,
modifiedTemplateFile;
if (isHandlebars) {
templateFileContents = fs.readFileSync(templateFile, 'utf8');
// this wrapper guarantees we'll never render a component in a partial if it doesn't have a _ref
modifiedTemplateFile = nymagHbs.wrapPartial(renderableItem.name, templateFileContents);
hbs.registerPartial(renderableItem.name, modifiedTemplateFile);
}
}
_.each(renderableItems, registerRenderableItem);
// Compile all the loaded partials
_.forIn(hbs.partials, function (value, key) {
if (_.isString(value)) {
hbs.partials[key] = hbs.compile(value);
}
});
module.exports.hbs = hbs;
}
/**
* Grab the possible template files based on the name.
* Returns a path to the file so that it can be read.
*
* @param {String} filePath
* @param {String} templateName
* @return {String}
*/
function getPossibleTemplates(filePath, templateName) {
if (_.isString(filePath)) {
return _.get(glob.sync(path.join(filePath, `${templateName}.*`)), 0, []);
}
return [];
}
/**
* Pass Handlebars helpers to `nymag-handlebars`
*
* @param {Object} payload
*/
function addHelpers(payload) {
_.forIn(payload, function (value, key) {
// set up handlebars helpers that rely on internal services
hbs.registerHelper(`${key}`, value);
});
}
/**
* Pass settings to the renderer
*
* @param {Object} options
*/
function configureRender(options) {
render.configure(options);
module.exports.renderSettings = options;
}
/**
* Add in the resolveMedia function
*
* @param {Function} fn
*/
function addResolveMedia(fn) {
if (typeof fn === 'function') module.exports.resolveMedia = fn;
}
// Initialize
init();
// Values assigned via functions post-instantiation
module.exports.hbs = undefined;
module.exports.renderSettings = undefined;
module.exports.resolveMedia = undefined;
module.exports.plugins = [];
// Setup functions
module.exports.init = init;
module.exports.addHelpers = addHelpers;
module.exports.configureRender = configureRender;
module.exports.addResolveMedia = addResolveMedia;
// For Testing
module.exports.setLog = (fakeLog) => { log = fakeLog; };