Skip to content

Commit

Permalink
Merge pull request #3 from izaakschroeder/master
Browse files Browse the repository at this point in the history
Support for error handling and promises
  • Loading branch information
colynb committed Jul 30, 2014
2 parents 0440572 + c406a2c commit 61a8b15
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 40 deletions.
83 changes: 57 additions & 26 deletions README.md
Expand Up @@ -36,51 +36,40 @@ var MongoClient = require('mongodb').MongoClient;
/*
Get data via JSON file, keyed on filename.
*/
var getJsonData = function(file, cb) {
var jsonPath = './examples/' + path.basename(file.path) + '.json';
cb(require(jsonPath));
};

gulp.task('json-test', function() {
return gulp.src('./examples/test1.html')
.pipe(data(getJsonData))
.pipe(data(function(file) {
return require('./examples/' + path.basename(file.path) + '.json');
}))
.pipe(swig())
.pipe(gulp.dest('build'));
});

var getFrontMatter = function(file, cb) {
var content = fm(String(file.contents));
file.contents = new Buffer(content.body);
cb(content.attributes);
};

/*
Get data via front matter
*/
gulp.task('fm-test', function() {
return gulp.src('./examples/test2.html')
.pipe(data(getFrontMatter))
.pipe(data(function(file) {
var content = fm(String(file.contents));
file.contents = new Buffer(content.body);
return content.attributes;
}))
.pipe(swig())
.pipe(gulp.dest('build'));
});

/*
Get data via database, keyed on filename.
*/
var getMongoData = function(file, cb) {
MongoClient.connect('mongodb://127.0.0.1:27017/gulp-data-test', function(err, db) {
if(err) throw err;
var collection = db.collection('file-data-test');
collection.findOne({filename: path.basename(file.path)}, function(err, doc) {
db.close();
cb(doc);
});
});
};

gulp.task('db-test', function() {
return gulp.src('./examples/test3.html')
.pipe(data(getMongoData))
.pipe(data(function(file, cb) {
MongoClient.connect('mongodb://127.0.0.1:27017/gulp-data-test', function(err, db) {
if(err) return cb(err);
cb(undefined, db.collection('file-data-test').findOne({filename: path.basename(file.path)}));
});
}))
.pipe(swig())
.pipe(gulp.dest('build'));
});
Expand All @@ -92,10 +81,52 @@ gulp.task('db-test', function() {
### data(dataFunction)

#### dataFunction
Type: `Function`
Type: `Function`

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.

You can return the data object:
```javascript
data(function(file) {
return { 'foo': file.path }
})
```

You can return a promise:
```javascript
data(function(file) {
return promise;
})
```

You can feed a result object through the callback:
```javascript
data(function(file, callback) {
return callback(undefined, { 'foo': 'bar' });
})
```

You can feed a promise object through the callback:
```javascript
data(function(file, callback) {
return callback(undefined, promise);
})
```

You can throw an error:
```javascript
data(function(file) {
throw new Error('my-error');
})
```

You can raise an error via the callback:
```javascript
data(function(file, callback) {
return callback('error');
})
```

## Note to gulp plugin authors

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:
Expand Down
43 changes: 33 additions & 10 deletions index.js
Expand Up @@ -14,6 +14,27 @@ module.exports = function(data) {

function gulpData(file, enc, callback) {
/*jshint validthis:true*/
var self = this, called = false;

function handle(err, result){
// extra guard in case
if (called) return;
called = true;
if (err) {
self.emit("error", new gutil.PluginError("gulp-data", { message: err }));
return callback();
}
file.data = result;
self.push(file);
callback();
}

function local(data) {
if (data && typeof data.then === 'function')
data.then(function(data){ return handle(undefined, data) }, function(err) { return handle(err); });
else
handle(undefined, data);
}

// Do nothing if no contents
if (file.isNull()) {
Expand All @@ -28,21 +49,23 @@ module.exports = function(data) {
// https://github.com/dominictarr/event-stream

// accepting streams is optional
this.emit("error",
new gutil.PluginError("gulp-data", "Stream content is not supported"));
this.emit("error", new gutil.PluginError("gulp-data", "Stream content is not supported"));
return callback();
}

// check if file.contents is a `Buffer`
if (file.isBuffer()) {
var self = this;
if (typeof data === 'function') {
data(file, function(result){
file.data = result;
self.push(file);
callback();
});
}
var res = null;
if (typeof data === 'function')
try {
res = data(file, handle);
if (data.length <= 1)
return local(res)
} catch(e) {
return handle(e);
}
else
local(data);
}
}

Expand Down
12 changes: 9 additions & 3 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "gulp-data",
"version": "0.1.0",
"description": "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.",
"version": "1.0.1",
"description": "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.",
"keywords": [
"gulpplugin", "data", "json", "gulp"
],
Expand All @@ -10,6 +10,11 @@
"email": "colyn.brown@gmail.com",
"url": "https://github.com/colynb"
},
"contributors": [{
"name": "Izaak Schroeder",
"email": "izaak.schroeder@gmail.com",
"url": "http://www.github.com/izaakschroeder"
}],
"repository": "colynb/gulp-data",
"scripts": {
"test": "istanbul test _mocha --report html -- test/*.js --reporter spec"
Expand All @@ -24,7 +29,8 @@
"mocha-lcov-reporter": "*",
"istanbul": "*",
"event-stream": "*",
"should": "~2.1.0"
"should": "~2.1.0",
"q": "~1.0.1"
},
"engines": {
"node": ">=0.8.0",
Expand Down

0 comments on commit 61a8b15

Please sign in to comment.