diff --git a/README.md b/README.md index f0c886a..83fbaec 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,31 @@ var gulpRoutes = require('gulp-routes'); ``` ## API +### [.routesPlugin](index.js#L28) + +Create a routes plugin that runs middleware defined on a router. + +* `router` **{Object}**: Instance of an [en-route] router +* `returns` **{Function}**: New function for creating a router stream. + +```js +var gulpRoutes = require('gulp-routes'); +var router = require('en-route'); +var routes = gulpRoutes(router); +``` + +### [.routes](index.js#L49) + +Create a router stream to run middleware for the specified method. + +* `method` **{String}**: Method to run middleware for. +* `returns` **{Stream}**: Stream for piping files through + +```js +gulp.src('*.hbs') + .pipe(routes()) + .pipe(gulp.dest('_gh_pages/')); +``` ## Contributing @@ -40,4 +65,4 @@ Released under the MIT license *** -_This file was generated by [verb](https://github.com/assemble/verb) on December 09, 2014._ \ No newline at end of file +_This file was generated by [verb](https://github.com/assemble/verb) on December 10, 2014._ \ No newline at end of file diff --git a/index.js b/index.js index 360bbb6..71ac741 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,68 @@ 'use strict'; -module.exports = function () { - // do stuff -}; \ No newline at end of file +var through = require('through2'); +var gutil = require('gulp-util'); + +/** + * Create a routes plugin that runs middleware defined on a router. + * + * ```js + * var gulpRoutes = require('gulp-routes'); + * var router = require('en-route'); + * var routes = gulpRoutes(router); + * ``` + * + * @name routesPlugin + * @param {Object} `router` Instance of an [en-route] router + * @return {Function} New function for creating a router stream. + * @api public + */ + +module.exports = function routesPlugin(router) { + router = router || (this && this.router); + if (!router) { + throw new gutil.PluginError('gulp-routes', new Error('Expected a valid router object.')); + } + + /** + * Create a router stream to run middleware for the specified method. + * + * ```js + * gulp.src('*.hbs') + * .pipe(routes()) + * .pipe(gulp.dest('_gh_pages/')); + * ``` + * + * @name routes + * @param {String} `method` Method to run middleware for. + * @return {Stream} Stream for piping files through + * @api public + */ + + return function routes (method) { + method = method || 'all'; + + return through.obj(function (file, encoding, cb) { + var stream = this; + try { + file.options = file.options || {}; + file.options.method = method; + // run middleware + router.handle(file, function (err) { + if (err) { + stream.emit('error', new gutil.PluginError('gulp-routes', err)); + cb(); + } else { + stream.push(file); + cb(); + } + }); + } catch (ex) { + stream.emit('error', new gutil.PluginError('gulp-routes - handle', ex)); + cb(); + } + }); + }; +}; + diff --git a/package.json b/package.json index 2b29b2f..fc6d1f0 100644 --- a/package.json +++ b/package.json @@ -26,8 +26,13 @@ "test": "mocha -R spec" }, "devDependencies": { + "en-route": "^0.3.3", "mocha": "*", "should": "*" }, - "keywords": [] + "keywords": [], + "dependencies": { + "gulp-util": "^3.0.1", + "through2": "^0.6.3" + } } diff --git a/test.js b/test.js new file mode 100644 index 0000000..34255bd --- /dev/null +++ b/test.js @@ -0,0 +1,187 @@ +/** + * Assemble + * + * Copyright (c) 2014 Jon Schlinkert, Brian Woodward, contributors + * Licensed under the MIT License (MIT). + */ +'use strict'; + +var should = require('should'); +var through = require('through2'); +var File = require('gulp-util').File; +var Router = require('en-route').Router; +var gulpRoutes = require('./'); + +describe('gulp-routes', function() { + it('should route files', function(done) { + + var fakeFile = new File({ + cwd: 'src/templates', + base: 'src/templates', + path: 'src/templates/alert.hbs', + contents: new Buffer('---\ntitle: sup\n---\n{{upper title}}') + }); + + var router = new Router(); + router.all(/\.hbs/, function (file, next) { + file.data = file.data || {}; + file.data.middleware = true; + next(); + }); + + var routes = gulpRoutes(router); + var stream = routes(); + stream.on('data', function (file) { + file.data.middleware.should.be.true; + }); + + stream.on('end', done); + + stream.write(fakeFile); + stream.end(); + }); + + it('should route files with a router from `this`', function (done) { + var fakeFile = new File({ + cwd: 'src/templates', + base: 'src/templates', + path: 'src/templates/alert.hbs', + contents: new Buffer('---\ntitle: sup\n---\n{{upper title}}') + }); + + var app = {}; + app.router = new Router(); + app.router.all(/\.hbs/, function (file, next) { + file.data = file.data || {}; + file.data.middleware = true; + next(); + }); + + var routes = gulpRoutes.call(app); + var stream = routes(); + stream.on('data', function (file) { + file.data.middleware.should.be.true; + }); + + stream.on('end', done); + + stream.write(fakeFile); + stream.end(); + }); + + it('should route files to different routes', function (done) { + var file1 = new File({ + cwd: 'src/templates', + base: 'src/templates', + path: 'src/templates/one.hbs', + contents: new Buffer('one') + }); + + var file2 = new File({ + cwd: 'src/templates', + base: 'src/templates', + path: 'src/templates/two.hbs', + contents: new Buffer('two') + }); + + var router = new Router(); + router.use(function (file, next ) { + file.data = file.data || {}; + next(); + }); + + router.all(/one/, function (file, next) { + file.data.one = true; + next(); + }); + + router.all(/two/, function (file, next) { + file.data.two = true; + next(); + }); + + var routes = gulpRoutes(router); + var stream = routes(); + stream.on('data', function (file) { + switch (file.contents.toString()) { + case 'one': + file.data.one.should.be.true; + file.data.should.not.have.property('two'); + break; + case 'two': + file.data.should.not.have.property('one'); + file.data.two.should.be.true; + break; + } + }); + + stream.on('end', done); + + stream.write(file1); + stream.write(file2); + stream.end(); + }); + + it('should route on multiple methods', function (done) { + var fakeFile = new File({ + cwd: 'src/templates', + base: 'src/templates', + path: 'src/templates/one.hbs', + contents: new Buffer('one') + }); + + var router = new Router({ + methods: ['before', 'after'] + }); + + router.use(function (file, next) { + file.data = file.data || {}; + next(); + }); + + router.before(/\./, function (file, next) { + file.data.before = true; + next(); + }); + + router.after(/\./, function (file, next) { + file.data.after = true; + next(); + }); + + var routes = gulpRoutes(router); + var beforeStream = routes('before'); + var afterStream = routes('after'); + + beforeStream + .pipe(through.obj(function (file, enc, cb) { + file.data.before.should.be.true; + file.data.should.not.have.property('after'); + this.push(file); + cb(); + })) + .pipe(afterStream); + + afterStream.on('data', function (file) { + file.data.before.should.be.true; + file.data.after.should.be.true; + }); + + afterStream.on('end', done); + + beforeStream.write(fakeFile); + beforeStream.end(); + }); + + it('should throw an error when a router is\'t provided.', function (done) { + try { + var routes = gulpRoutes(); + return done(new Error('Expected an error to be thrown')); + } catch (err) { + if (!err) return done(new Error('Expected an error to be thrown')); + done(); + } + }); + + +});