-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (107 loc) · 2.87 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
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
'use strict';
var ap = require('ap');
var d = require('themeleon/decorators');
var fs = require('fs');
var path = require('path');
var q = require('q');
var readFile = q.denodeify(fs.readFile);
var glob = q.denodeify(require('glob'));
/**
* Given partials are in the following format:
*
* {
* name: 'path/to/partial.handlebars',
* ...
* }
*
* The promise will resolve to the following:
*
* {
* name: 'template file content',
* ...
* }
*
* The template files are relative to `src` directory.
*
* @param {String} src
* @param {Object} partials
* @return {Promise}
*/
function readPartialsObject(src, partials) {
var promises = [];
for (var name in partials) {
var file = path.resolve(src, partials[name]);
var promise = q.all([q(name), readFile(file, 'utf8')]);
promises.push(promise);
}
return q.all(promises)
.then(function (list) {
list.forEach(function (item) {
var name = item[0];
var content = item[1];
partials[name] = content;
});
return partials;
});
}
/**
* Find all `.handlebars` or `.hbs` files from partials directory and build
* an object, then pass it to `readPartialsObject`.
*/
function readPartialsDirectory(src, partials) {
src = path.resolve(src);
partials = path.resolve(src, partials);
return glob(partials + '/**/*.+(handlebars|hbs)')
.then(function (files) {
var object = {};
files.forEach(function (file) {
// Strip partials directory
var partial = file.substr(partials.length + 1);
// Strip extension
partial = partial.substr(0, partial.lastIndexOf('.'));
object[partial] = file;
});
return object;
})
.then(function (partials) {
return readPartialsObject(src, partials);
});
}
/**
* If partials is a string, it will be treated as a directory
* with `readPartialsDirectory`, otherwise as an object with
* `readPartialsObject`.
*
* @param {String} src
* @param {String|Object} partials
* @return {Promise}
*/
function readPartials(src, partials) {
if (!partials) {
return null;
}
if (typeof partials === 'string') {
return readPartialsDirectory(src, partials);
}
return readPartialsObject(src, partials);
}
module.exports = function (handlebars) {
handlebars = handlebars || require('handlebars');
var writeFile = q.denodeify(fs.writeFile);
return {
handlebars: d.srcDest(function (src, dest, partials) {
var promise = q.all([
readFile(src, 'utf8'),
readPartials(this.src, partials)
])
.spread(function (template, partials) {
for (var name in partials) {
handlebars.registerPartial(name, partials[name]);
}
return handlebars.compile(template)(this.ctx);
}.bind(this))
.then(ap([dest], writeFile));
this.push(promise);
}),
};
};