Skip to content

Commit

Permalink
plugin implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
doowb committed Dec 10, 2014
1 parent 206f5e3 commit 651ea5b
Show file tree
Hide file tree
Showing 4 changed files with 284 additions and 5 deletions.
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,4 +65,4 @@ Released under the MIT license

***

_This file was generated by [verb](https://github.com/assemble/verb) on December 09, 2014._
_This file was generated by [verb](https://github.com/assemble/verb) on December 10, 2014._
68 changes: 65 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,68 @@

'use strict';

module.exports = function () {
// do stuff
};
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();
}
});
};
};

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
187 changes: 187 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* Assemble <http://assemble.io>
*
* 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();
}
});


});

0 comments on commit 651ea5b

Please sign in to comment.