├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org/ 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | end_of_line = lf 7 | indent_size = 2 8 | indent_style = space 9 | insert_final_newline = true 10 | trim_trailing_whitespace = true 11 | 12 | [{**/{actual,fixtures,expected,templates}/**,*.md}] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "env": { 3 | "browser": false, 4 | "es6": true, 5 | "node": true, 6 | "mocha": true 7 | }, 8 | 9 | "globals": { 10 | "document": false, 11 | "navigator": false, 12 | "window": false 13 | }, 14 | 15 | "rules": { 16 | "accessor-pairs": 2, 17 | "arrow-spacing": [2, { "before": true, "after": true }], 18 | "block-spacing": [2, "always"], 19 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 20 | "comma-dangle": [2, "never"], 21 | "comma-spacing": [2, { "before": false, "after": true }], 22 | "comma-style": [2, "last"], 23 | "constructor-super": 2, 24 | "curly": [2, "multi-line"], 25 | "dot-location": [2, "property"], 26 | "eol-last": 2, 27 | "eqeqeq": [2, "allow-null"], 28 | "generator-star-spacing": [2, { "before": true, "after": true }], 29 | "handle-callback-err": [2, "^(err|error)$" ], 30 | "indent": [2, 2, { "SwitchCase": 1 }], 31 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 32 | "keyword-spacing": [2, { "before": true, "after": true }], 33 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 34 | "new-parens": 2, 35 | "no-array-constructor": 2, 36 | "no-caller": 2, 37 | "no-class-assign": 2, 38 | "no-cond-assign": 2, 39 | "no-const-assign": 2, 40 | "no-control-regex": 2, 41 | "no-debugger": 2, 42 | "no-delete-var": 2, 43 | "no-dupe-args": 2, 44 | "no-dupe-class-members": 2, 45 | "no-dupe-keys": 2, 46 | "no-duplicate-case": 2, 47 | "no-empty-character-class": 2, 48 | "no-eval": 2, 49 | "no-ex-assign": 2, 50 | "no-extend-native": 2, 51 | "no-extra-bind": 2, 52 | "no-extra-boolean-cast": 2, 53 | "no-extra-parens": [2, "functions"], 54 | "no-fallthrough": 2, 55 | "no-floating-decimal": 2, 56 | "no-func-assign": 2, 57 | "no-implied-eval": 2, 58 | "no-inner-declarations": [2, "functions"], 59 | "no-invalid-regexp": 2, 60 | "no-irregular-whitespace": 2, 61 | "no-iterator": 2, 62 | "no-label-var": 2, 63 | "no-labels": 2, 64 | "no-lone-blocks": 2, 65 | "no-mixed-spaces-and-tabs": 2, 66 | "no-multi-spaces": 2, 67 | "no-multi-str": 2, 68 | "no-multiple-empty-lines": [2, { "max": 1 }], 69 | "no-native-reassign": 0, 70 | "no-negated-in-lhs": 2, 71 | "no-new": 2, 72 | "no-new-func": 2, 73 | "no-new-object": 2, 74 | "no-new-require": 2, 75 | "no-new-wrappers": 2, 76 | "no-obj-calls": 2, 77 | "no-octal": 2, 78 | "no-octal-escape": 2, 79 | "no-proto": 0, 80 | "no-redeclare": 2, 81 | "no-regex-spaces": 2, 82 | "no-return-assign": 2, 83 | "no-self-compare": 2, 84 | "no-sequences": 2, 85 | "no-shadow-restricted-names": 2, 86 | "no-spaced-func": 2, 87 | "no-sparse-arrays": 2, 88 | "no-this-before-super": 2, 89 | "no-throw-literal": 2, 90 | "no-trailing-spaces": 0, 91 | "no-undef": 2, 92 | "no-undef-init": 2, 93 | "no-unexpected-multiline": 2, 94 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 95 | "no-unreachable": 2, 96 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 97 | "no-useless-call": 0, 98 | "no-with": 2, 99 | "one-var": [0, { "initialized": "never" }], 100 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 101 | "padded-blocks": [0, "never"], 102 | "quotes": [2, "single", "avoid-escape"], 103 | "radix": 2, 104 | "semi": [2, "always"], 105 | "semi-spacing": [2, { "before": false, "after": true }], 106 | "space-before-blocks": [2, "always"], 107 | "space-before-function-paren": [2, "never"], 108 | "space-in-parens": [2, "never"], 109 | "space-infix-ops": 2, 110 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 111 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 112 | "use-isnan": 2, 113 | "valid-typeof": 2, 114 | "wrap-iife": [2, "any"], 115 | "yoda": [2, "never"] 116 | } 117 | } 118 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | .nyc* 10 | 11 | # npm 12 | node_modules 13 | npm-debug.log 14 | 15 | # yarn 16 | yarn.lock 17 | yarn-error.log 18 | 19 | # misc 20 | _gh_pages 21 | _draft 22 | _drafts 23 | bower_components 24 | vendor 25 | temp 26 | tmp 27 | TODO.md 28 | package-lock.json -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | os: 3 | - linux 4 | - osx 5 | language: node_js 6 | node_js: 7 | - node 8 | - '8' 9 | - '7' 10 | - '6' 11 | - '5' 12 | - '4' 13 | git: 14 | depth: 10 15 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | {%= apidocs("index.js") %} 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2017, Brian Woodward. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-routes [![NPM version](https://img.shields.io/npm/v/gulp-routes.svg?style=flat)](https://www.npmjs.com/package/gulp-routes) [![NPM monthly downloads](https://img.shields.io/npm/dm/gulp-routes.svg?style=flat)](https://npmjs.org/package/gulp-routes) [![NPM total downloads](https://img.shields.io/npm/dt/gulp-routes.svg?style=flat)](https://npmjs.org/package/gulp-routes) [![Linux Build Status](https://img.shields.io/travis/assemble/gulp-routes.svg?style=flat&label=Travis)](https://travis-ci.org/assemble/gulp-routes) 2 | 3 | > Add middleware to run for specified routes in your gulp pipeline. 4 | 5 | ## Install 6 | 7 | Install with [npm](https://www.npmjs.com/): 8 | 9 | ```sh 10 | $ npm install --save gulp-routes 11 | ``` 12 | 13 | ## Usage 14 | 15 | ### [routes](index.js#L43) 16 | 17 | The main export is a function that takes an instance of [en-route](https://github.com/jonschlinkert/en-route) and returns a [gulp](http://gulpjs.com) plugin function. 18 | 19 | **Params** 20 | 21 | * `enRoute` **{Object}**: Instance of [en-route](https://github.com/jonschlinkert/en-route). 22 | * `returns` **{Function}** 23 | 24 | **Example** 25 | 26 | ```js 27 | var routes = require('gulp-routes'); 28 | var Router = require('en-route').Router; 29 | var router = new Router(); 30 | 31 | // define middleware 32 | router.all(/\.hbs/, function (file, next) { 33 | var str = file.contents.toString(); 34 | // do anything to `file` that can be done 35 | // in a gulp plugin 36 | file.contents = new Buffer(str); 37 | next(); 38 | }); 39 | 40 | // pass the router to `gulp-routes` 41 | var route = routes(router); 42 | 43 | gulp.src('*.hbs') 44 | .pipe(route()) 45 | .pipe(gulp.dest('_gh_pages/')); 46 | ``` 47 | 48 | ### [route](index.js#L66) 49 | 50 | Create a router stream to run middleware for the specified method. 51 | 52 | **Params** 53 | 54 | * `method` **{String}**: Method to run middleware. 55 | * `returns` **{Stream}**: Returns a stream for piping files through. 56 | 57 | **Example** 58 | 59 | ```js 60 | gulp.src('*.hbs') 61 | .pipe(route('before')) 62 | .pipe(otherPlugin()) 63 | .pipe(route('after')) 64 | .pipe(gulp.dest('dist/')); 65 | ``` 66 | 67 | ## About 68 | 69 | ### Related projects 70 | 71 | You might also be interested in these projects: 72 | 73 | * [base-routes](https://www.npmjs.com/package/base-routes): Plugin for adding routes support to your `base` application. Requires templates support to work. | [homepage](https://github.com/node-base/base-routes "Plugin for adding routes support to your `base` application. Requires templates support to work.") 74 | * [base](https://www.npmjs.com/package/base): Framework for rapidly creating high quality node.js applications, using plugins like building blocks | [homepage](https://github.com/node-base/base "Framework for rapidly creating high quality node.js applications, using plugins like building blocks") 75 | * [en-route](https://www.npmjs.com/package/en-route): Routing for static site generators, build systems and task runners, heavily based on express.js routes… [more](https://github.com/jonschlinkert/en-route) | [homepage](https://github.com/jonschlinkert/en-route "Routing for static site generators, build systems and task runners, heavily based on express.js routes but works with file objects. Used by Assemble, Verb, and Template.") 76 | 77 | ### Contributing 78 | 79 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 80 | 81 | ### Contributors 82 | 83 | | **Commits** | **Contributor** | 84 | | --- | --- | 85 | | 6 | [jonschlinkert](https://github.com/jonschlinkert) | 86 | | 5 | [doowb](https://github.com/doowb) | 87 | 88 | ### Building docs 89 | 90 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ 91 | 92 | To generate the readme, run the following command: 93 | 94 | ```sh 95 | $ npm install -g verbose/verb#dev verb-generate-readme && verb 96 | ``` 97 | 98 | ### Running tests 99 | 100 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: 101 | 102 | ```sh 103 | $ npm install && npm test 104 | ``` 105 | 106 | ### Author 107 | 108 | **Brian Woodward** 109 | 110 | * [github/doowb](https://github.com/doowb) 111 | * [twitter/doowb](https://twitter.com/doowb) 112 | 113 | ### License 114 | 115 | Copyright © 2017, [Brian Woodward](https://github.com/doowb). 116 | Released under the [MIT License](LICENSE). 117 | 118 | *** 119 | 120 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on August 07, 2017._ -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * gulp-routes 3 | * 4 | * Copyright (c) 2014-2017, Brian Woodward. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var PluginError = require('plugin-error'); 11 | var through = require('through2'); 12 | 13 | /** 14 | * The main export is a function that takes an instance of 15 | * [en-route][] and returns a [gulp][] plugin function. 16 | * 17 | * ```js 18 | * var routes = require('gulp-routes'); 19 | * var Router = require('en-route').Router; 20 | * var router = new Router(); 21 | * 22 | * // define middleware 23 | * router.all(/\.hbs/, function (file, next) { 24 | * var str = file.contents.toString(); 25 | * // do anything to `file` that can be done 26 | * // in a gulp plugin 27 | * file.contents = new Buffer(str); 28 | * next(); 29 | * }); 30 | * 31 | * // pass the router to `gulp-routes` 32 | * var route = routes(router); 33 | * 34 | * gulp.src('*.hbs') 35 | * .pipe(route()) 36 | * .pipe(gulp.dest('_gh_pages/')); 37 | * ``` 38 | * @param {Object} `enRoute` Instance of [en-route][]. 39 | * @return {Function} 40 | * @api public 41 | */ 42 | 43 | function routes(enRoute) { 44 | enRoute = enRoute || (this && this.router); 45 | 46 | if (!enRoute) { 47 | throw error(new Error('expected an instance of en-route')); 48 | } 49 | 50 | /** 51 | * Create a router stream to run middleware for the specified method. 52 | * 53 | * ```js 54 | * gulp.src('*.hbs') 55 | * .pipe(route('before')) 56 | * .pipe(otherPlugin()) 57 | * .pipe(route('after')) 58 | * .pipe(gulp.dest('dist/')); 59 | * ``` 60 | * @name route 61 | * @param {String} `method` Method to run middleware. 62 | * @return {Stream} Returns a stream for piping files through. 63 | * @api public 64 | */ 65 | 66 | return function(method) { 67 | method = method || 'all'; 68 | 69 | return through.obj(function(file, enc, next) { 70 | if (file.isNull()) { 71 | next(null, file); 72 | return; 73 | } 74 | 75 | if (file.isStream()) { 76 | this.emit('error', error('streaming is not supported')); 77 | return; 78 | } 79 | 80 | file.data = file.data || {}; 81 | file.options = file.options || {}; 82 | file.options.method = method; 83 | var stream = this; 84 | 85 | try { 86 | enRoute.handle(file, function(err) { 87 | if (err) { 88 | stream.emit('error', error(err)); 89 | return; 90 | } 91 | next(null, file); 92 | }); 93 | } catch (err) { 94 | stream.emit('error', error(err)); 95 | next(); 96 | return; 97 | } 98 | }); 99 | }; 100 | }; 101 | 102 | function error(err) { 103 | return new PluginError('gulp-routes', err, {showStack: true}); 104 | } 105 | 106 | /** 107 | * Expose `routes` 108 | */ 109 | 110 | module.exports = routes; 111 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-routes", 3 | "description": "Add middleware to run for specified routes in your gulp pipeline.", 4 | "version": "1.0.0", 5 | "homepage": "https://github.com/assemble/gulp-routes", 6 | "author": "Brian Woodward (https://github.com/doowb)", 7 | "contributors": [ 8 | "Brian Woodward (https://twitter.com/doowb)", 9 | "Jon Schlinkert (http://twitter.com/jonschlinkert)" 10 | ], 11 | "repository": "assemble/gulp-routes", 12 | "bugs": { 13 | "url": "https://github.com/assemble/gulp-routes/issues" 14 | }, 15 | "license": "MIT", 16 | "files": [ 17 | "index.js" 18 | ], 19 | "main": "index.js", 20 | "engines": { 21 | "node": ">=4" 22 | }, 23 | "scripts": { 24 | "test": "mocha" 25 | }, 26 | "dependencies": { 27 | "plugin-error": "^0.1.2", 28 | "through2": "^2.0.3" 29 | }, 30 | "devDependencies": { 31 | "en-route": "^1.0.0", 32 | "gulp-format-md": "^1.0.0", 33 | "mocha": "^3.5.0", 34 | "vinyl": "^2.1.0" 35 | }, 36 | "keywords": [ 37 | "gulp", 38 | "gulpplugin", 39 | "routes" 40 | ], 41 | "verb": { 42 | "toc": false, 43 | "layout": "default", 44 | "tasks": [ 45 | "readme" 46 | ], 47 | "plugins": [ 48 | "gulp-format-md" 49 | ], 50 | "lint": { 51 | "reflinks": true 52 | }, 53 | "related": { 54 | "list": ["en-route", "base", "base-routes"] 55 | }, 56 | "reflinks": [ 57 | "en-route", 58 | "gulp" 59 | ] 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * gulp-routes 3 | * 4 | * Copyright (c) 2014-2017, Brian Woodward. 5 | * Released under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var File = require('vinyl'); 11 | var assert = require('assert'); 12 | var through = require('through2'); 13 | var Router = require('en-route').Router; 14 | var gulpRoutes = require('./'); 15 | 16 | describe('gulp-routes', function() { 17 | it('should route files', function(cb) { 18 | var fakeFile = new File({ 19 | cwd: 'fixtures', 20 | base: 'fixtures', 21 | path: 'fixtures/index.js', 22 | contents: new Buffer('var foo = function() {};') 23 | }); 24 | 25 | var router = new Router(); 26 | router.all(/\.js/, function(file, next) { 27 | file.data = file.data || {}; 28 | file.data.middleware = true; 29 | next(); 30 | }); 31 | 32 | var routes = gulpRoutes(router); 33 | var stream = routes(); 34 | stream.on('data', function(file) { 35 | assert(file.data.middleware); 36 | }); 37 | 38 | stream.on('end', cb); 39 | stream.write(fakeFile); 40 | stream.end(); 41 | }); 42 | 43 | it('should route files with a router from `this`', function(cb) { 44 | var fakeFile = new File({ 45 | cwd: 'fixtures', 46 | base: 'fixtures', 47 | path: 'fixtures/index.js', 48 | contents: new Buffer('var foo = function() {};') 49 | }); 50 | 51 | var app = {}; 52 | app.router = new Router(); 53 | app.router.all(/\.js/, function(file, next) { 54 | file.data = file.data || {}; 55 | file.data.middleware = true; 56 | next(); 57 | }); 58 | 59 | var routes = gulpRoutes.call(app); 60 | var stream = routes(); 61 | stream.on('data', function(file) { 62 | assert(file.data.middleware); 63 | }); 64 | 65 | stream.on('end', cb); 66 | stream.write(fakeFile); 67 | stream.end(); 68 | }); 69 | 70 | it('should route files to different routes', function(cb) { 71 | var file1 = new File({ 72 | cwd: 'fixtures', 73 | base: 'fixtures', 74 | path: 'fixtures/one.js', 75 | contents: new Buffer('one') 76 | }); 77 | 78 | var file2 = new File({ 79 | cwd: 'fixtures', 80 | base: 'fixtures', 81 | path: 'fixtures/two.js', 82 | contents: new Buffer('two') 83 | }); 84 | 85 | var router = new Router(); 86 | router.use(function(file, next) { 87 | file.data = file.data || {}; 88 | next(); 89 | }); 90 | 91 | router.all(/one/, function(file, next) { 92 | file.data.one = true; 93 | next(); 94 | }); 95 | 96 | router.all(/two/, function(file, next) { 97 | file.data.two = true; 98 | next(); 99 | }); 100 | 101 | var routes = gulpRoutes(router); 102 | var stream = routes(); 103 | 104 | stream.on('data', function(file) { 105 | switch (file.contents.toString()) { 106 | case 'one': 107 | assert(file.data.one); 108 | assert(!file.data.two); 109 | break; 110 | case 'two': 111 | assert(!file.data.one); 112 | assert(file.data.two); 113 | break; 114 | } 115 | }); 116 | 117 | stream.on('error', cb); 118 | stream.on('end', cb); 119 | stream.write(file1); 120 | stream.write(file2); 121 | stream.end(); 122 | }); 123 | 124 | it('should route on multiple methods', function(cb) { 125 | var fakeFile = new File({ 126 | cwd: 'fixtures', 127 | base: 'fixtures', 128 | path: 'fixtures/one.js', 129 | contents: new Buffer('one') 130 | }); 131 | 132 | var router = new Router({ 133 | methods: ['before', 'after', 'whatever'] 134 | }); 135 | 136 | router.use(function(file, next) { 137 | file.data = file.data || {}; 138 | next(); 139 | }); 140 | 141 | router.before(/./, function(file, next) { 142 | file.data.before = true; 143 | next(); 144 | }); 145 | 146 | router.whatever(/./, function(file, next) { 147 | file.data.whatever = true; 148 | next(); 149 | }); 150 | 151 | router.after(/./, function(file, next) { 152 | file.data.after = true; 153 | next(); 154 | }); 155 | 156 | var routes = gulpRoutes(router); 157 | var whateverStream = routes('whatever'); 158 | var beforeStream = routes('before'); 159 | var afterStream = routes('after'); 160 | 161 | whateverStream 162 | .pipe(through.obj(function(file, enc, cb) { 163 | assert(file.data.whatever); 164 | assert(!file.data.after); 165 | this.push(file); 166 | cb(); 167 | })); 168 | 169 | beforeStream 170 | .pipe(through.obj(function(file, enc, cb) { 171 | assert(file.data.before); 172 | assert(!file.data.after); 173 | this.push(file); 174 | cb(); 175 | })) 176 | .pipe(whateverStream) 177 | .pipe(afterStream); 178 | 179 | afterStream.on('data', function(file) { 180 | assert(file.data.before); 181 | assert(file.data.whatever); 182 | assert(file.data.after); 183 | }); 184 | 185 | afterStream.on('end', cb); 186 | 187 | beforeStream.write(fakeFile); 188 | beforeStream.end(); 189 | }); 190 | 191 | it('should throw an error when a router is\'t provided.', function() { 192 | assert.throws(function() { 193 | gulpRoutes(); 194 | }); 195 | }); 196 | }); 197 | --------------------------------------------------------------------------------