assemble-streams
Advanced tools
Comparing version 0.4.2 to 0.5.0
207
index.js
@@ -16,55 +16,147 @@ /*! | ||
module.exports = function(options) { | ||
return function fn(app) { | ||
if (this.isRegistered('assemble-streams')) return; | ||
return function appPlugin(app) { | ||
if (!isValidInstance(this, 'isApp')) { | ||
return appPlugin; | ||
} | ||
app.mixin('toStream', appStream(app)); | ||
/** | ||
* Push a view collection into a vinyl stream. | ||
* | ||
* ```js | ||
* app.toStream('posts', function(file) { | ||
* return file.path !== 'index.hbs'; | ||
* }) | ||
* ``` | ||
* @name .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 | ||
*/ | ||
app.mixin('toStream', function(name, filterFn) { | ||
var stream = through.obj(); | ||
stream.setMaxListeners(0); | ||
if (typeof name === 'undefined' && !this.isCollection) { | ||
process.nextTick(stream.end.bind(stream)); | ||
return src(stream); | ||
return function collectionPlugin(collection) { | ||
if (!isValidInstance(this, 'isCollection')) { | ||
return collectionPlugin; | ||
} | ||
collection.mixin('toStream', collectionStream(app, this)); | ||
var views; | ||
if (this.isApp && name) { | ||
views = this.getViews(name); | ||
} else { | ||
views = this.views; | ||
return function viewPlugin(view) { | ||
if (!isValidInstance(this, ['isView', 'isItem'])) { | ||
return viewPlugin; | ||
} | ||
this.define('toStream', viewStream(app)); | ||
} | ||
} | ||
}; | ||
}; | ||
/** | ||
* 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) { | ||
initHandlers(app); | ||
return function(name, filterFn) { | ||
var stream = through.obj(); | ||
stream.setMaxListeners(0); | ||
if (typeof name === 'undefined') { | ||
process.nextTick(stream.end.bind(stream)); | ||
return src(stream); | ||
} | ||
var write = writeStream(stream); | ||
var views = tryGetViews(this, name); | ||
if (!views && typeof name !== 'undefined') { | ||
filterFn = name; | ||
setImmediate(function() { | ||
for (var key in views) { | ||
if (!filter(key, views[key], filterFn)) { | ||
continue; | ||
} | ||
stream.write(views[key]); | ||
} | ||
Object.keys(this.views).forEach(function(key) { | ||
views = this.views[key]; | ||
write(views, filterFn); | ||
}, this); | ||
stream.end(); | ||
}); | ||
}.bind(this)); | ||
return src(stream.pipe(handle(this, 'onStream'))); | ||
} | ||
setImmediate(function() { | ||
write(views, filterFn); | ||
stream.end(); | ||
}); | ||
if (app.isApp) { | ||
return fn; | ||
} | ||
return src(stream.pipe(handle(this, 'onStream'))); | ||
}; | ||
}; | ||
} | ||
/** | ||
* 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(app, collection) { | ||
initHandlers(collection); | ||
return function(filterFn) { | ||
var stream = through.obj(); | ||
stream.setMaxListeners(0); | ||
var views = this.views; | ||
var write = writeStream(stream); | ||
setImmediate(function() { | ||
write(views, filterFn); | ||
stream.end(); | ||
}); | ||
return src(stream.pipe(handle(app, 'onStream'))); | ||
}; | ||
} | ||
/** | ||
* 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(app) { | ||
return function() { | ||
var stream = through.obj(); | ||
stream.setMaxListeners(0); | ||
setImmediate(function(view) { | ||
stream.write(view); | ||
stream.end(); | ||
}, this); | ||
return src(stream.pipe(handle(app, 'onStream'))); | ||
}; | ||
} | ||
function tryGetViews(app, name) { | ||
try { | ||
return app.getViews(name); | ||
} catch (err) { | ||
return; | ||
} | ||
} | ||
function filter(key, view, fn) { | ||
@@ -85,3 +177,38 @@ if (Array.isArray(fn)) { | ||
} | ||
if (typeof fn === 'string') { | ||
return match(fn, view); | ||
} | ||
return true; | ||
} | ||
function writeStream(stream) { | ||
return function(views, filterFn) { | ||
for (var key in views) { | ||
if (!filter(key, views[key], filterFn)) { | ||
continue; | ||
} | ||
stream.write(views[key]); | ||
} | ||
}; | ||
} | ||
function initHandlers(app) { | ||
if (typeof app.handler === 'function' && typeof app.onStream !== 'function') { | ||
app.handler('onStream'); | ||
} | ||
} | ||
function isValidInstance(app, types) { | ||
if (app.isRegistered('assemble-streams')) { | ||
return false; | ||
} | ||
types = Array.isArray(types) ? types : [types]; | ||
var valid = false; | ||
types.forEach(function(type) { | ||
if (!valid && app[type]) { | ||
valid = true; | ||
} | ||
}); | ||
return valid; | ||
} |
{ | ||
"name": "assemble-streams", | ||
"description": "Assemble pipeline plugin for pushing a view collection into a vinyl stream.", | ||
"version": "0.4.2", | ||
"version": "0.5.0", | ||
"homepage": "https://github.com/assemble/assemble-streams", | ||
@@ -26,8 +26,9 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
"src-stream": "^0.1.1", | ||
"through2": "^2.0.0" | ||
"through2": "^2.0.1" | ||
}, | ||
"devDependencies": { | ||
"assemble-core": "^0.8.3", | ||
"gulp-format-md": "^0.1.5", | ||
"mocha": "*" | ||
"assemble-fs": "^0.4.6", | ||
"gulp-format-md": "^0.1.7", | ||
"mocha": "^2.4.5", | ||
"templates": "^0.15.12" | ||
}, | ||
@@ -34,0 +35,0 @@ "keywords": [ |
@@ -24,7 +24,15 @@ # assemble-streams [![NPM version](https://img.shields.io/npm/v/assemble-streams.svg)](https://www.npmjs.com/package/assemble-streams) [![Build Status](https://img.shields.io/travis/assemble/assemble-streams.svg)](https://travis-ci.org/assemble/assemble-streams) | ||
// register the plugin | ||
app.use(stream); | ||
app.use(stream()); | ||
// use the plugin | ||
// use the plugin on app | ||
app.toStream('pages') | ||
.pipe(app.dest('site/')) | ||
.pipe(app.dest('site/')); | ||
// use the plugin on a collection | ||
app.pages.toStream() | ||
.pipe(app.dest('site/')); | ||
// use the plugin on a view | ||
app.pages.getView('home').toStream() | ||
.pipe(app.dest('site/')); | ||
``` | ||
@@ -34,2 +42,55 @@ | ||
## API | ||
### [app.toStream](index.js#L54) | ||
Push a view collection into a vinyl stream. | ||
**Params** | ||
* `collection` **{String}**: Name of the collection to push into the stream. | ||
* **{Function}**: Optionally pass a filter function to use for filtering views. | ||
* `returns` **{Stream}** | ||
**Example** | ||
```js | ||
app.toStream('posts', function(file) { | ||
return file.path !== 'index.hbs'; | ||
}) | ||
``` | ||
### [collection.toStream](index.js#L106) | ||
Push a view collection into a vinyl stream. | ||
**Params** | ||
* **{Function}**: Optionally pass a filter function to use for filtering views. | ||
* `returns` **{Stream}** | ||
**Example** | ||
```js | ||
app.posts.toStream(function(file) { | ||
return file.path !== 'index.hbs'; | ||
}) | ||
``` | ||
### [view.toStream](index.js#L141) | ||
Push the current view into a vinyl stream. | ||
* `returns` **{Stream}** | ||
**Example** | ||
```js | ||
app.pages.getView('a.html').toStream() | ||
.on('data', function(file) { | ||
console.log(file); | ||
//=> <Page "a.html" <Buffer 2e 2e 2e>> | ||
}); | ||
``` | ||
## Related projects | ||
@@ -81,2 +142,2 @@ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on March 06, 2016._ | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on March 21, 2016._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10856
182
140
4
Updatedthrough2@^2.0.1