├── .editorconfig ├── .gitattributes ├── .gitignore ├── .jshintrc ├── .travis.yml ├── LICENSE ├── README.md ├── _config.yml ├── contributors.md ├── examples ├── test1.html ├── test1.html.json ├── test2.html └── test3.html ├── index.js ├── package.json └── test └── main.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = spaces 6 | indent_size = 2 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | 15 | [test/fixtures/*] 16 | insert_final_newline = false 17 | trim_trailing_whitespace = false 18 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules/ 3 | temp/ 4 | coverage 5 | -------------------------------------------------------------------------------- /.jshintrc: -------------------------------------------------------------------------------- 1 | { 2 | "node": true, 3 | "esnext": true, 4 | "bitwise": true, 5 | "camelcase": true, 6 | "curly": true, 7 | "eqeqeq": true, 8 | "immed": true, 9 | "indent": 2, 10 | "latedef": true, 11 | "newcap": true, 12 | "noarg": true, 13 | "quotmark": "single", 14 | "regexp": true, 15 | "undef": true, 16 | "unused": true, 17 | "strict": false 18 | } 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "8" 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 Colyn Brown 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # gulp-data 2 | 3 | [![Build Status](https://travis-ci.org/colynb/gulp-data.svg?branch=master)](https://travis-ci.org/colynb/gulp-data) 4 | [![Dependencies](https://david-dm.org/colynb/gulp-data.svg)](https://david-dm.org/colynb/gulp-data) 5 | 6 | [![NPM](https://nodei.co/npm/gulp-data.svg?stars&downloads)](https://www.npmjs.com/package/gulp-data) 7 | 8 | [Learn more about gulp.js, the streaming build system](http://gulpjs.com) 9 | 10 | Checkout https://colyn.dev/docs for documentation 11 | 12 | ## Introduction 13 | 14 | Gulp-data proposes a common API for attaching data to the file object for other plugins to consume. With gulp-data you can generate a data object from a variety of sources: json, front-matter, database, anything... and set it to the file object for other plugins to consume. 15 | 16 | Many plugins, such as `gulp-swig` or `gulp-pug` allow for JSON data to be passed via their respective options parameter. However, frequently what you want is the ability to dynamically set the data based off the file name or some other attribute of the file. Without using another plugin, this becomes problematic - as the number of ways of getting at data (via JSON files, front-matter, data bases, promises, etc) increases, the more plugin authors have to update their APIs to support these sources. The `gulp-data` plugin aims to standardize a method that is generic enough to encapsulate these data sources into a single `data` property attached to the file object. It's really up to you as to where your data comes from, a JSON file, from a front-matter section of the file, or even a database, `gulp-data` doesn't really care. 17 | 18 | However, for this to be effective, I'm asking plugin devs that receive data through the options parameter to make a small change to additionally accept this data through the `file.data` property. (See below) 19 | 20 | ## Important Update 21 | 22 | Thanks to the help of [@izaakschroeder](http://www.github.com/izaakschroeder) we've reached version 1.0 (now 1.0.1). Some important changes have been added, primarily support for promises, and error handling. The examples below have been updated to reflect these changes. 23 | 24 | ## Usage 25 | 26 | First, install `gulp-data` as a development dependency: 27 | 28 | ```shell 29 | npm install --save-dev gulp-data 30 | ``` 31 | 32 | Then, add it to your `gulpfile.js`: 33 | 34 | ```javascript 35 | var gulp = require('gulp'); 36 | var swig = require('gulp-swig'); 37 | var data = require('gulp-data'); 38 | var fm = require('front-matter'); 39 | var path = require('path'); 40 | var MongoClient = require('mongodb').MongoClient; 41 | var fs = require('fs'); 42 | 43 | /* 44 | Get data via JSON file, keyed on filename. 45 | */ 46 | gulp.task('json-test', function() { 47 | return gulp.src('./examples/test1.html') 48 | .pipe(data(function(file) { 49 | return JSON.parse(fs.readFileSync('./examples/' + path.basename(file.path) + '.json')); 50 | })) 51 | .pipe(swig()) 52 | .pipe(gulp.dest('build')); 53 | }); 54 | 55 | /* 56 | Get data via front matter 57 | */ 58 | gulp.task('fm-test', function() { 59 | return gulp.src('./examples/test2.html') 60 | .pipe(data(function(file) { 61 | var content = fm(String(file.contents)); 62 | file.contents = new Buffer(content.body); 63 | return content.attributes; 64 | })) 65 | .pipe(swig()) 66 | .pipe(gulp.dest('build')); 67 | }); 68 | 69 | /* 70 | Get data via database, keyed on filename. 71 | */ 72 | gulp.task('db-test', function() { 73 | return gulp.src('./examples/test3.html') 74 | .pipe(data(function(file, cb) { 75 | MongoClient.connect('mongodb://127.0.0.1:27017/gulp-data-test', function(err, db) { 76 | if(err) return cb(err); 77 | cb(undefined, db.collection('file-data-test').findOne({filename: path.basename(file.path)})); 78 | }); 79 | })) 80 | .pipe(swig()) 81 | .pipe(gulp.dest('build')); 82 | }); 83 | 84 | ``` 85 | 86 | ## API 87 | 88 | ### data(dataFunction) 89 | 90 | #### dataFunction 91 | 92 | Type: `Function` 93 | 94 | Define a function that returns a data object via a callback function. Could return JSON from a file, or an object returned from a database. 95 | 96 | You can return the data object: 97 | 98 | ```javascript 99 | data(function(file) { 100 | return { 'foo': file.path } 101 | }) 102 | ``` 103 | 104 | You can return a promise: 105 | 106 | ```javascript 107 | data(function(file) { 108 | return promise; 109 | }) 110 | ``` 111 | 112 | You can feed a result object through the callback: 113 | 114 | ```javascript 115 | data(function(file, callback) { 116 | return callback(undefined, { 'foo': 'bar' }); 117 | }) 118 | ``` 119 | 120 | You can feed a promise object through the callback: 121 | 122 | ```javascript 123 | data(function(file, callback) { 124 | return callback(undefined, promise); 125 | }) 126 | ``` 127 | 128 | You can throw an error: 129 | 130 | ```javascript 131 | data(function(file) { 132 | throw new Error('my-error'); 133 | }) 134 | ``` 135 | 136 | You can raise an error via the callback: 137 | 138 | ```javascript 139 | data(function(file, callback) { 140 | return callback('error'); 141 | }) 142 | ``` 143 | 144 | ## Note to gulp plugin authors 145 | 146 | If your plugin needs a data object, one that normally gets passed in via your options parameter, I'm asking if you could please update the plugin to accept data from the `file.data` property. Here's how you can do it: 147 | 148 | `gulp-swig` usually accepts data via its `options.data` parameter, but with a small change, it checks to see if there's a `file.data` property and if so, merges it into the data object. 149 | 150 | ```javascript 151 | var data = opts.data || {}; 152 | if (file.data) { 153 | data = _.extend(file.data, data); 154 | // or just data = file.data if you don't care to merge. Up to you. 155 | } 156 | ``` 157 | 158 | ## Author 159 | 160 | - [@colynb](https://github.com/colynb) 161 | 162 | ## Contributors 163 | 164 | - [@izaakschroeder](https://github.com/izaakschroeder) 165 | - [@stevelacy](https://github.com/stevelacy) 166 | - [@shinnn](https://github.com/shinnn) 167 | - [@phillipgreenii](https://github.com/phillipgreenii) 168 | 169 | ## Libraries Using `gulp-data` 170 | 171 | - [Gulp Starter (a delicious blend of tasks and build tools)](https://github.com/vigetlabs/gulp-starter) 172 | 173 | ## License 174 | 175 | [MIT License](http://en.wikipedia.org/wiki/MIT_License) 176 | 177 | 178 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman -------------------------------------------------------------------------------- /contributors.md: -------------------------------------------------------------------------------- 1 | ## Author 2 | 3 | - [@colynb](https://github.com/colynb) 4 | 5 | ## Contributors 6 | 7 | - [@izaakschroeder](https://github.com/izaakschroeder) 8 | - [@stevelacy](https://github.com/stevelacy) 9 | - [@shinnn](https://github.com/shinnn) 10 | - [@phillipgreenii](https://github.com/phillipgreenii) 11 | -------------------------------------------------------------------------------- /examples/test1.html: -------------------------------------------------------------------------------- 1 | Hello {{ message }} 2 | -------------------------------------------------------------------------------- /examples/test1.html.json: -------------------------------------------------------------------------------- 1 | { 2 | "message" : "World" 3 | } 4 | -------------------------------------------------------------------------------- /examples/test2.html: -------------------------------------------------------------------------------- 1 | --- 2 | message: World 3 | --- 4 | Hello {{ message }} 5 | -------------------------------------------------------------------------------- /examples/test3.html: -------------------------------------------------------------------------------- 1 | Hello {{ message }} 2 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var extend = require('util-extend'); 2 | var through = require('through2'); 3 | var PluginError = require('plugin-error'); 4 | 5 | module.exports = function(data) { 6 | 'use strict'; 7 | 8 | // if necessary check for required param(s), e.g. options hash, etc. 9 | if (!data) { 10 | throw new PluginError('gulp-data', 'No data supplied'); 11 | } 12 | 13 | // see 'Writing a plugin' 14 | // https://github.com/gulpjs/gulp/blob/master/docs/writing-a-plugin/README.md 15 | 16 | function gulpData(file, enc, callback) { 17 | /*jshint validthis:true*/ 18 | var self = this, called = false; 19 | 20 | function handle(err, result){ 21 | // extra guard in case 22 | if (called) { 23 | return; 24 | } 25 | called = true; 26 | if (err) { 27 | self.emit('error', new PluginError('gulp-data', { message: err })); 28 | return callback(); 29 | } 30 | file.data = extend(file.data || {}, result); 31 | self.push(file); 32 | callback(); 33 | } 34 | 35 | function local(data) { 36 | if (data && typeof data.then === 'function') { 37 | data.then(function(data){ 38 | return handle(undefined, data); 39 | }, function(err) { return handle(err); }); 40 | } 41 | else { 42 | handle(undefined, data); 43 | } 44 | } 45 | 46 | var res = null; 47 | if (typeof data === 'function') { 48 | try { 49 | res = data(file, handle); 50 | } catch(e) { 51 | handle(e); 52 | } 53 | 54 | if (data.length <= 1) { 55 | local(res); 56 | } 57 | 58 | return; 59 | } 60 | 61 | local(data); 62 | } 63 | 64 | return through.obj(gulpData); 65 | }; 66 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-data", 3 | "version": "1.3.1", 4 | "description": 5 | "Generate a data object from a variety of sources: json, front-matter, databases, promises, anything... and set it to the file object for other plugins to consume.", 6 | "keywords": ["gulpplugin", "data", "json", "gulp"], 7 | "author": { 8 | "name": "Colyn Brown", 9 | "email": "colyn.brown@gmail.com", 10 | "url": "https://github.com/colynb" 11 | }, 12 | "contributors": [ 13 | { 14 | "name": "Izaak Schroeder", 15 | "email": "izaak.schroeder@gmail.com", 16 | "url": "http://www.github.com/izaakschroeder" 17 | } 18 | ], 19 | "repository": "colynb/gulp-data", 20 | "files": ["index.js"], 21 | "scripts": { 22 | "pretest": "jshint *.js test/*.js", 23 | "test": "istanbul test _mocha --report html -- test/*.js" 24 | }, 25 | "dependencies": { 26 | "plugin-error": "^0.1.2", 27 | "through2": "^2.0.0", 28 | "util-extend": "^1.0.1" 29 | }, 30 | "devDependencies": { 31 | "coveralls": "*", 32 | "istanbul": "*", 33 | "jshint": "^2.6.0", 34 | "mocha": "*", 35 | "mocha-lcov-reporter": "*", 36 | "q": "^1.0.1", 37 | "should": "^7.1.1", 38 | "vinyl": "^1.1.0" 39 | }, 40 | "engines": { 41 | "node": ">=0.9.0", 42 | "npm": ">=1.2.10" 43 | }, 44 | "license": "MIT" 45 | } 46 | -------------------------------------------------------------------------------- /test/main.js: -------------------------------------------------------------------------------- 1 | /*global describe, it*/ 2 | 'use strict'; 3 | 4 | var should = require('should'); 5 | var through = require('through2'); 6 | var Q = require('q'); 7 | 8 | require('mocha'); 9 | 10 | delete require.cache[require.resolve('../')]; 11 | 12 | var data = require('../'); 13 | var File = require('vinyl'); 14 | 15 | describe('gulp-data', function() { 16 | 17 | 18 | it('should handle exceptions with the callback present', function(done) { 19 | data(function() { 20 | throw new Error('potato'); 21 | }) 22 | .on('error', function(err) { 23 | should.exist(err); 24 | should.exist(err.message); 25 | done(); 26 | }) 27 | .on('end', function() { 28 | done('fail'); 29 | }) 30 | .end(new File({contents: new Buffer('')})); 31 | }); 32 | 33 | it('should handle exceptions without the callback present', function(done) { 34 | data(function() { 35 | throw new Error('potato'); 36 | }) 37 | .on('error', function(err) { 38 | should.exist(err); 39 | should.exist(err.message); 40 | done(); 41 | }) 42 | .on('end', function() { 43 | done('fail'); 44 | }) 45 | .end(new File({contents: new Buffer('')})); 46 | }); 47 | 48 | it('should produce errors when data handler has error', function(done) { 49 | data(function(file, cb) { 50 | cb({ type: 'test-error' }); 51 | }) 52 | .on('error', function(err) { 53 | should.exist(err); 54 | should.exist(err.message); 55 | should.exist(err.message.type); 56 | err.message.should.have.property('type', 'test-error'); 57 | done(); 58 | }) 59 | .on('end', function() { 60 | done('fail'); 61 | }) 62 | .end(new File({contents: new Buffer('')})); 63 | }); 64 | 65 | it('should produce errors when promises are rejected', function(done) { 66 | var deferred = Q.defer(); 67 | 68 | setTimeout(function() { 69 | deferred.reject({ type: 'test-error' }); 70 | }, 20); 71 | 72 | data(deferred.promise) 73 | .on('error', function(err) { 74 | should.exist(err); 75 | should.exist(err.message); 76 | should.exist(err.message.type); 77 | err.message.should.have.property('type', 'test-error'); 78 | done(); 79 | }) 80 | .on('end', function() { 81 | done('fail'); 82 | }) 83 | .end(new File({contents: new Buffer('')})); 84 | }); 85 | 86 | it('should work with returned values', function(done) { 87 | data(function() { 88 | return { message: 'Hello' }; 89 | }) 90 | .on('error', function(err) { 91 | should.exist(err); 92 | done(err); 93 | }) 94 | .on('data', function(newFile) { 95 | should.exist(newFile); 96 | should.exist(newFile.data); 97 | newFile.data.should.have.property('message', 'Hello'); 98 | done(); 99 | }) 100 | .end(new File({contents: new Buffer('')})); 101 | }); 102 | 103 | it('should support empty files', function(done) { 104 | data(function() { 105 | return { message: 'Hello' }; 106 | }) 107 | .on('error', function(err) { 108 | should.exist(err); 109 | done(err); 110 | }) 111 | .on('data', function(newFile) { 112 | should.exist(newFile); 113 | should.exist(newFile.data); 114 | newFile.data.should.have.property('message', 'Hello'); 115 | done(); 116 | }) 117 | .end(new File()); 118 | }); 119 | 120 | it('should support streams', function(done) { 121 | data(function() { 122 | return { message: 'Hello' }; 123 | }) 124 | .on('error', function(err) { 125 | should.exist(err); 126 | done(err); 127 | }) 128 | .on('data', function(newFile) { 129 | should.exist(newFile); 130 | should.exist(newFile.data); 131 | newFile.data.should.have.property('message', 'Hello'); 132 | done(); 133 | }) 134 | .end(new File({contents: through()})); 135 | }); 136 | 137 | it('should work with promises that resolve', function(done) { 138 | var deferred = Q.defer(); 139 | 140 | setTimeout(function() { 141 | deferred.resolve({ message: 'Hello' }); 142 | }, 20); 143 | 144 | data(deferred.promise) 145 | .on('error', function(err) { 146 | should.exist(err); 147 | done(err); 148 | }) 149 | .on('data', function(newFile) { 150 | should.exist(newFile); 151 | should.exist(newFile.data); 152 | newFile.data.should.have.property('message', 'Hello'); 153 | done(); 154 | }) 155 | .end(new File({contents: new Buffer('')})); 156 | }); 157 | 158 | it('should work with mapped promises', function(done) { 159 | data(function(file) { 160 | var deferred = Q.defer(); 161 | setTimeout(function() { 162 | deferred.resolve({ message: file.path }); 163 | }, 20); 164 | return deferred.promise; 165 | }) 166 | .on('error', function(err) { 167 | should.exist(err); 168 | done(err); 169 | }) 170 | .on('data', function(newFile) { 171 | should.exist(newFile); 172 | should.exist(newFile.data); 173 | newFile.data.should.have.property('message', newFile.path); 174 | done(); 175 | }) 176 | .end(new File({ 177 | path: 'test/fixtures/hello.txt', 178 | contents: new Buffer('') 179 | })); 180 | }); 181 | 182 | it('should produce expected file data property', function(done) { 183 | data(function(file, cb) { 184 | cb(undefined, { 185 | message: 'Hello' 186 | }); 187 | }) 188 | .on('error', function(err) { 189 | should.exist(err); 190 | done(err); 191 | }) 192 | .on('data', function(newFile) { 193 | should.exist(newFile); 194 | should.exist(newFile.data); 195 | newFile.data.should.have.property('message', 'Hello'); 196 | done(); 197 | }) 198 | .end(new File({contents: new Buffer('')})); 199 | }); 200 | 201 | }); 202 | --------------------------------------------------------------------------------