1 | 'use strict';
|
2 |
|
3 | var fs = require('fs');
|
4 | var utils = require('lazy-cache')(require);
|
5 | var fn = require;
|
6 | require = utils;
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | require('array-sort', 'sortBy');
|
13 | require('async-each', 'each');
|
14 | require('base-data');
|
15 | require('base-engines', 'engines');
|
16 | require('base-helpers', 'helpers');
|
17 | require('base-option', 'option');
|
18 | require('base-plugins', 'plugin');
|
19 | require('base-routes', 'routes');
|
20 | require('deep-bind', 'bindAll');
|
21 | require('define-property', 'define');
|
22 | require('engine-base', 'engine');
|
23 | require('extend-shallow', 'extend');
|
24 | require('falsey', 'isFalsey');
|
25 | require('get-value', 'get');
|
26 | require('get-view');
|
27 | require('group-array', 'groupBy');
|
28 | require('has-glob');
|
29 | require('has-value', 'has');
|
30 | require('inflection', 'inflect');
|
31 | require('is-valid-app', 'isValid');
|
32 | require('layouts');
|
33 | require('match-file');
|
34 | require('mixin-deep', 'merge');
|
35 | require('paginationator');
|
36 | require('pascalcase', 'pascal');
|
37 | require('set-value', 'set');
|
38 | require('template-error', 'rethrow');
|
39 | require = fn;
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | utils.methods = [
|
46 | 'onLoad',
|
47 | 'preCompile',
|
48 | 'preLayout',
|
49 | 'onLayout',
|
50 | 'postLayout',
|
51 | 'onMerge',
|
52 | 'onStream',
|
53 | 'postCompile',
|
54 | 'preRender',
|
55 | 'postRender',
|
56 | 'preWrite',
|
57 | 'postWrite'
|
58 | ];
|
59 |
|
60 | utils.constructorKeys = [
|
61 | 'Collection',
|
62 | 'Group',
|
63 | 'Item',
|
64 | 'List',
|
65 | 'View',
|
66 | 'Views'
|
67 | ];
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | utils.optsKeys = [
|
74 | 'renameKey',
|
75 | 'namespaceData',
|
76 | 'mergePartials',
|
77 | 'rethrow',
|
78 | 'nocase',
|
79 | 'nonull',
|
80 | 'rename',
|
81 | 'cwd'
|
82 | ];
|
83 |
|
84 | utils.endsWith = function(str, sub) {
|
85 | return str.slice(-sub.length) === sub;
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | utils.fileExists = function(filepath) {
|
93 | try {
|
94 | return fs.statSync(filepath).isDirectory() === false;
|
95 | } catch (err) {}
|
96 | return false;
|
97 | };
|
98 |
|
99 |
|
100 |
|
101 |
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | utils.engineStack = function(view, engine, fn, content) {
|
111 | if (typeof view.engineStack === 'undefined') {
|
112 | view.engineStack = {};
|
113 | }
|
114 | if (engine && engine.charAt(0) !== '.') {
|
115 | engine = '.' + engine;
|
116 | }
|
117 | view.engineStack[engine] = fn;
|
118 | view.engineStack[engine].content = content;
|
119 | };
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | utils.isPartial = function(view) {
|
126 | if (typeof view.isType !== 'function') {
|
127 | return false;
|
128 | }
|
129 | if (typeof view.options === 'undefined') {
|
130 | return false;
|
131 | }
|
132 | if (typeof view.options.viewType === 'undefined') {
|
133 | return false;
|
134 | }
|
135 | return view.isType('partial');
|
136 | };
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 | utils.isRenderable = function(view) {
|
143 | if (typeof view.isType !== 'function') {
|
144 | return false;
|
145 | }
|
146 | if (typeof view.options === 'undefined') {
|
147 | return false;
|
148 | }
|
149 | if (typeof view.options.viewType === 'undefined') {
|
150 | return false;
|
151 | }
|
152 | return view.isType('renderable') && view.viewType.length === 1;
|
153 | };
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 | utils.updateOptions = function(app, key, value) {
|
161 | var k = utils.constructorKeys;
|
162 | if (k.indexOf(key) > -1) {
|
163 | app.define(key, value);
|
164 | }
|
165 | if (key === 'layout') {
|
166 | app.viewTypes.renderable.forEach(function(name) {
|
167 | app[name].option('layout', value);
|
168 | });
|
169 | }
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 | utils.identity = function(val) {
|
177 | return val;
|
178 | };
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | utils.arrayify = function(val) {
|
185 | return val ? (Array.isArray(val) ? val : [val]) : [];
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 | utils.last = function(array, n) {
|
193 | return array[array.length - (n || 1)];
|
194 | };
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 | utils.isObject = function(val) {
|
202 | if (!val || Array.isArray(val)) {
|
203 | return false;
|
204 | }
|
205 | return typeof val === 'function'
|
206 | || typeof val === 'object';
|
207 | };
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 | utils.isString = function(val) {
|
214 | return val && typeof val === 'string';
|
215 | };
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 | utils.isBuffer = function(val) {
|
222 | if (val && val.constructor && typeof val.constructor.isBuffer === 'function') {
|
223 | return val.constructor.isBuffer(val);
|
224 | }
|
225 | return false;
|
226 | };
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 | utils.isStream = function(val) {
|
233 | return utils.isObject(val)
|
234 | && (typeof val.pipe === 'function')
|
235 | && (typeof val.on === 'function');
|
236 | };
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | utils.isOptions = function(val) {
|
244 | return utils.isObject(val) && val.hasOwnProperty('hash');
|
245 | };
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 | utils.helperError = function(app, helperName, viewName, cb) {
|
253 | var err = new Error('helper "' + helperName + '" cannot find "' + viewName + '"');
|
254 | app.emit('error', err);
|
255 | if (typeof cb === 'function') {
|
256 | return cb(err);
|
257 | } else {
|
258 | throw err;
|
259 | }
|
260 | };
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 | utils.setInstanceNames = function setInstanceNames(app, name) {
|
268 | utils.define(app, 'is' + utils.pascal(name), true);
|
269 | utils.define(app, '_name', name);
|
270 | };
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | utils.single = function single(name) {
|
277 | return utils.inflect.singularize(name);
|
278 | };
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 | utils.plural = function plural(name) {
|
285 | return utils.inflect.pluralize(name);
|
286 | };
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | utils.formatExt = function formatExt(ext) {
|
305 | if (typeof ext !== 'string') {
|
306 | throw new Error('utils.formatExt() expects `ext` to be a string.');
|
307 | }
|
308 | if (ext.charAt(0) !== '.') {
|
309 | return '.' + ext;
|
310 | }
|
311 | return ext;
|
312 | };
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 | utils.isItem = utils.isView = function(val) {
|
320 | if (!utils.isObject(val)) return false;
|
321 | return val.hasOwnProperty('content')
|
322 | || val.hasOwnProperty('contents')
|
323 | || val.hasOwnProperty('path');
|
324 | };
|
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 | utils.getLocals = function(locals, options) {
|
334 | options = options || {};
|
335 | locals = locals || {};
|
336 | var ctx = {};
|
337 |
|
338 | if (options.hasOwnProperty('hash')) {
|
339 | utils.extend(ctx, options.hash);
|
340 | delete options.hash;
|
341 | }
|
342 | if (locals.hasOwnProperty('hash')) {
|
343 | utils.extend(ctx, locals.hash);
|
344 | delete locals.hash;
|
345 | }
|
346 | utils.extend(ctx, options);
|
347 | utils.extend(ctx, locals);
|
348 | return ctx;
|
349 | };
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | utils.resolveEngineExt = function(view, locals, opts) {
|
365 | var engine = locals.engine || view.engine || opts.engine;
|
366 | var fn = opts.resolveEngine
|
367 | || locals.resolveEngine
|
368 | || utils.identity;
|
369 | return fn(engine);
|
370 | };
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 | module.exports = utils;
|