-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
195 lines (168 loc) · 4.28 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*!
* assemble-streams <https://github.com/assemble/assemble-streams>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var utils = require('./utils');
module.exports = function(options) {
return function plugin(app) {
if (utils.isValid(app, 'assemble-streams')) {
app.define('toStream', appStream(app));
app.on('view', function(view) {
viewPlugin.call(view, view);
});
return collectionPlugin;
}
function collectionPlugin(collection) {
if (utils.isValid(collection, 'assemble-streams', ['collection'])) {
collection.define('toStream', collectionStream(app, this));
}
return viewPlugin;
}
function viewPlugin(view) {
if (utils.isValid(this, 'assemble-streams', ['item', 'file'])) {
utils.define(this, 'toStream', viewStream(app));
}
}
return plugin;
};
};
/**
* Push a view collection into a vinyl stream.
*
* ```js
* app.toStream('posts', function(file) {
* return file.path !== 'index.hbs';
* })
* ```
* @name app.toStream
* @param {String} `collection` Name of the collection to push into the stream.
* @param {Function} Optionally pass a filter function to use for filtering views.
* @return {Stream}
* @api public
*/
function appStream(app) {
if (!hasHandler(app, 'onStream')) {
app.handler('onStream');
}
return function(name, filterFn) {
var stream = utils.through.obj();
stream.setMaxListeners(0);
if (typeof name === 'undefined') {
process.nextTick(stream.end.bind(stream));
return utils.src(stream);
}
var write = writeStream(stream);
var collection = this[name];
var views = collection && collection.views;
if (!views && typeof name !== 'undefined') {
filterFn = name;
setImmediate(function() {
Object.keys(this.views).forEach(function(key) {
views = this.views[key];
write(views, filterFn);
}, this);
stream.end();
}.bind(this));
return outStream(stream, this);
}
setImmediate(function() {
write(views, filterFn);
stream.end();
});
return outStream(stream, this);
};
}
/**
* Push a view collection into a vinyl stream.
*
* ```js
* app.posts.toStream(function(file) {
* return file.path !== 'index.hbs';
* })
* ```
* @name collection.toStream
* @param {Function} Optionally pass a filter function to use for filtering views.
* @return {Stream}
* @api public
*/
function collectionStream(collection) {
if (!hasHandler(collection, 'onStream')) {
collection.handler('onStream');
}
return function(filterFn) {
var stream = utils.through.obj();
stream.setMaxListeners(0);
var views = this.views;
var write = writeStream(stream);
setImmediate(function() {
write(views, filterFn);
stream.end();
});
return outStream(stream, collection);
};
}
/**
* Push the current view into a vinyl stream.
*
* ```js
* app.pages.getView('a.html').toStream()
* .on('data', function(file) {
* console.log(file);
* //=> <Page "a.html" <Buffer 2e 2e 2e>>
* });
* ```
*
* @name view.toStream
* @return {Stream}
* @api public
*/
function viewStream(view) {
return function() {
var stream = utils.through.obj();
stream.setMaxListeners(0);
setImmediate(function(item) {
stream.write(item);
stream.end();
}, this);
return outStream(stream, view);
};
}
function writeStream(stream) {
return function(views, filterFn) {
for (var key in views) {
if (filter(key, views[key], filterFn)) {
stream.write(views[key]);
}
}
};
}
function outStream(stream, instance) {
return utils.src(stream.pipe(utils.handle.once(instance, 'onStream')));
}
function hasHandler(app, name) {
return typeof app.handler === 'function' && typeof app[name] === 'function';
}
function filter(key, view, val) {
switch (utils.typeOf(val)) {
case 'array':
var len = val.length;
var idx = -1;
while (++idx < len) {
var name = val[idx];
if (utils.match(name, view)) {
return true;
}
}
return false;
case 'function':
return val(key, view);
case 'string':
return utils.match(val, view);
default: {
return true;
}
}
}