-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
110 lines (96 loc) · 2.41 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* gulp-routes <https://github.com/assemble/gulp-routes>
*
* Copyright (c) 2014-2017, Brian Woodward.
* Released under the MIT License.
*/
'use strict';
var PluginError = require('plugin-error');
var through = require('through2');
/**
* The main export is a function that takes an instance of
* [en-route][] and returns a [gulp][] plugin function.
*
* ```js
* var routes = require('gulp-routes');
* var Router = require('en-route').Router;
* var router = new Router();
*
* // define middleware
* router.all(/\.hbs/, function (file, next) {
* var str = file.contents.toString();
* // do anything to `file` that can be done
* // in a gulp plugin
* file.contents = new Buffer(str);
* next();
* });
*
* // pass the router to `gulp-routes`
* var route = routes(router);
*
* gulp.src('*.hbs')
* .pipe(route())
* .pipe(gulp.dest('_gh_pages/'));
* ```
* @param {Object} `enRoute` Instance of [en-route][].
* @return {Function}
* @api public
*/
function routes(enRoute) {
enRoute = enRoute || (this && this.router);
if (!enRoute) {
throw error(new Error('expected an instance of en-route'));
}
/**
* Create a router stream to run middleware for the specified method.
*
* ```js
* gulp.src('*.hbs')
* .pipe(route('before'))
* .pipe(otherPlugin())
* .pipe(route('after'))
* .pipe(gulp.dest('dist/'));
* ```
* @name route
* @param {String} `method` Method to run middleware.
* @return {Stream} Returns a stream for piping files through.
* @api public
*/
return function(method) {
method = method || 'all';
return through.obj(function(file, enc, next) {
if (file.isNull()) {
next(null, file);
return;
}
if (file.isStream()) {
this.emit('error', error('streaming is not supported'));
return;
}
file.data = file.data || {};
file.options = file.options || {};
file.options.method = method;
var stream = this;
try {
enRoute.handle(file, function(err) {
if (err) {
stream.emit('error', error(err));
return;
}
next(null, file);
});
} catch (err) {
stream.emit('error', error(err));
next();
return;
}
});
};
};
function error(err) {
return new PluginError('gulp-routes', err, {showStack: true});
}
/**
* Expose `routes`
*/
module.exports = routes;