1 var through = require('through2');
2 var gutil = require('gulp-util');
3 var fm = require('front-matter');
4 var PluginError = gutil.PluginError;
5 var path = require('path');
6 var fs = require('fs');
8 module.exports = function(options) {
10 var directory = options.dir || process.cwd();
12 function bufferContents(file, enc, cb) {
16 if(file.isNull()) return cb(null, file);
20 content = fm(String(file.contents));
22 return cb(new PluginError('Gulp Dynamic Routing', e));
25 if(content.attributes.name) {
26 file.contents = new Buffer(content.body);
27 config = content.attributes;
28 var relativePath = path.relative(directory + path.sep + options.root, file.path);
29 config.path = relativePath.split(path.sep).join('/');
39 function endStream(cb) {
41 var appPath = options.path;
43 configs.sort(function(a, b) {
48 fs.writeFile(appPath, 'var foundationRoutes = ' + JSON.stringify(configs) + '; \n', function(err) {
55 return through.obj(bufferContents, endStream);