-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
86 lines (69 loc) · 2.21 KB
/
index.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
/*!
* async-helper-base <https://github.com/jonschlinkert/async-helper-base>
*
* Copyright (c) 2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var path = require('path');
var chalk = require('chalk');
var _ = require('lodash');
/**
* Module dependencies
*/
module.exports = function asyncHelperBase(name, options, fn) {
if (typeof name !== 'string') {
throw new TypeError('async-helper-base expects `name` to be a string.');
}
if (typeof options === 'function') {
fn = options;
options = {};
}
options = options || {};
// the helper
return function (key, locals, next) {
if (typeof locals === 'function') {
next = locals; locals = {};
}
var len = arguments.length;
var args = [].slice.call(arguments, 1, len - 1);
if (typeof next !== 'function') {
next = arguments[len - 1];
}
var opts = _.extend({}, this.options.helper && this.options.helper[name], options, locals);
var app = this.app;
var map = app.inflections || app.collection;
// get the matching (plural) collection name
var collection = map[name];
// if a `cwd` is defined the actual helper, load the templates(s) from
// the glob patterns defined by the helper
if (opts.cwd) {
if (opts.cwd === '.') opts.cwd = process.cwd();
// define the pattern as an array so it's definitely globbed by the loader
var glob = path.resolve(opts.cwd, key + (opts.extPattern || '{.md,.hbs}'));
app[collection](glob);
}
var template = app.lookup(collection, key);
var dot;
if (!template && (dot = key.indexOf('.')) !== -1) {
template = app.lookup(collection, key.slice(0, dot));
}
var ctx = _.merge({}, this.context, opts);
// call the template's render method
template.render(ctx, function render(err, content) {
if (err) {
error(name, key, err);
return next(err);
}
if (typeof fn === 'function') {
args.unshift(content);
return next(null, fn.apply(fn, args));
}
next(null, content);
});
};
};
function error(name, key, err) {
var msg = chalk.red('async-helper-base: {%= ' + name + '("' + key + '") %} error: %j', err);
return console.error(msg);
}