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.defaults = function(target) {
|
244 | var args = [].slice.call(arguments, 1);
|
245 | var len = args.length;
|
246 | var i = -1;
|
247 |
|
248 | while (++i < len) {
|
249 | var obj = args[i];
|
250 |
|
251 | for (var key in obj) {
|
252 | target[key] = target[key] || obj[key];
|
253 | }
|
254 | }
|
255 | return target;
|
256 | };
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 |
|
263 | utils.isOptions = function(val) {
|
264 | return utils.isObject(val) && val.hasOwnProperty('hash');
|
265 | };
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 | utils.helperError = function(app, helperName, viewName, cb) {
|
273 | var err = new Error('helper "' + helperName + '" cannot find "' + viewName + '"');
|
274 | app.emit('error', err);
|
275 | if (typeof cb === 'function') {
|
276 | return cb(err);
|
277 | } else {
|
278 | throw err;
|
279 | }
|
280 | };
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 | utils.setInstanceNames = function setInstanceNames(app, name) {
|
288 | utils.define(app, 'is' + utils.pascal(name), true);
|
289 | utils.define(app, '_name', name);
|
290 | };
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 | utils.single = function single(name) {
|
297 | return utils.inflect.singularize(name);
|
298 | };
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 | utils.plural = function plural(name) {
|
305 | return utils.inflect.pluralize(name);
|
306 | };
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 |
|
324 | utils.formatExt = function formatExt(ext) {
|
325 | if (typeof ext !== 'string') {
|
326 | throw new Error('utils.formatExt() expects `ext` to be a string.');
|
327 | }
|
328 | if (ext.charAt(0) !== '.') {
|
329 | return '.' + ext;
|
330 | }
|
331 | return ext;
|
332 | };
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | utils.isItem = utils.isView = function(val) {
|
340 | if (!utils.isObject(val)) return false;
|
341 | return val.hasOwnProperty('content')
|
342 | || val.hasOwnProperty('contents')
|
343 | || val.hasOwnProperty('path');
|
344 | };
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 | utils.getLocals = function(locals, options) {
|
354 | options = options || {};
|
355 | locals = locals || {};
|
356 | var ctx = {};
|
357 |
|
358 | if (options.hasOwnProperty('hash')) {
|
359 | utils.extend(ctx, options.hash);
|
360 | delete options.hash;
|
361 | }
|
362 | if (locals.hasOwnProperty('hash')) {
|
363 | utils.extend(ctx, locals.hash);
|
364 | delete locals.hash;
|
365 | }
|
366 | utils.extend(ctx, options);
|
367 | utils.extend(ctx, locals);
|
368 | return ctx;
|
369 | };
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | utils.resolveEngineExt = function(view, locals, opts) {
|
385 | var engine = locals.engine || view.engine || opts.engine;
|
386 | var fn = opts.resolveEngine
|
387 | || locals.resolveEngine
|
388 | || utils.identity;
|
389 | return fn(engine);
|
390 | };
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 | module.exports = utils;
|