/
index.js
90 lines (78 loc) · 2.16 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
/*!
* template-push <https://github.com/jonschlinkert/template-push>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var File = require('vinyl');
var through = require('through2');
var toVinyl = require('to-vinyl');
/**
* Expose `push`
*/
module.exports = push;
/**
* Returns a function that takes the current instance of `app`.
*
* ```js
* var app = require('your-app');
* var appPush = require('template-push')(app);
* ```
*
* @param {Object} `app` Instance of app.
* @return {Function} Factory function used to build a stream.
* @api public
*/
function push(app) {
if (!app || typeof app.views === 'undefined') {
throw new Error('template-push expects `app` to be an instance of Template.');
}
/**
* Returns a stream that pushes a collection of templates
* onto a stream as vinyl file objects.
*
* ```js
* // create a new arbitrary template type (collection)
* app.create('foo', {isRenderable: true});
*
* // Load `foo` templates
* app.foo('about-app.md', {content: '...'});
*
* // push the `foo` collection into the stream to be rendered
* app.task('default', function () {
* appPush('foo').pipe(app.dest('dist/'));
* });
* ```
*
* @name appPush
* @param {String} `name` Name of the collection to push into the stream.
* @return {Stream} Stream used in piping objects through.
* @api public
*/
return function(name) {
var isString = typeof name === 'string';
if (!isString && typeof name !== 'object') {
throw new TypeError('template-push expects collection `name` to be a string or object.');
}
var session = app.session;
var loaded = session.get('loaded') || [];
loaded.push(name);
session.set('loaded', loaded);
var stream = through.obj();
var pass = through.obj();
stream.pipe(pass);
var collection = isString
? (app.views[name] || {})
: name;
process.nextTick(function () {
for (var key in collection) {
if (collection.hasOwnProperty(key)) {
stream.write(toVinyl(collection[key], File));
}
}
stream.end();
});
return pass;
};
}