var es = require('event-stream'),
	gutil = require('gulp-util'),
	PluginError = gutil.PluginError,
	jsesc = require('jsesc'),
	cheerio = require('cheerio'),
	concat = require('gulp-concat');

var BUNDLENAME = 'templatecache.js';

module.exports = function(options){
	options = options || {};

	var bundlename = options.filename || BUNDLENAME;

	function ngTemplateCacheFile(file, callback){
		var fileContent = file.contents.toString('utf-8'),
			$ = cheerio.load('<div id="document">' + fileContent + '</div>'),
			$content = $("#document").children();
		if ($content.length == 1 && $content[0].type == "script" && $content[0].attribs.type == "text/ng-template") {
			var newFile = file.clone();
			console.log($content[0].attribs.id);
			newFile.contents = new Buffer('$templateCache.put(\'' + $content[0].attribs.id + '\',\'' + jsesc($("#document script").eq(0).html()) + '\');');
			callback(null, newFile);
		} else {
			this.emit('error', new PluginError('gulp-ng-template-cache', 'The file is not ng-template: ' + file.path));
			return callback();
		}
	}

	function ngTemplateCacheStream(context){
		context.emit('error', new PluginError('gulp-ng-template-cache', 'The file is not ng-template: '));
		es.map(ngTemplateCacheFile);
	}

	return es.pipeline(
		ngTemplateCacheStream(this),
		concat(bundlename));
};
var es = require('event-stream'),
	gutil = require('gulp-util'),
	PluginError = gutil.PluginError,
	jsesc = require('jsesc'),
	cheerio = require('cheerio'),
	concat = require('gulp-concat');

var BUNDLENAME = 'templatecache.js';

module.exports = function(options){
	options = options || {};

	var bundlename = options.filename || BUNDLENAME;

	function ngTemplateCacheFile(file, callback){
		var fileContent = file.contents.toString('utf-8'),
			$ = cheerio.load('<div id="document">' + fileContent + '</div>'),
			$content = $("#document").children();
		if ($content.length == 1 && $content[0].type == "script" && $content[0].attribs.type == "text/ng-template") {
			var newFile = file.clone();
			console.log($content[0].attribs.id);
			newFile.contents = new Buffer('$templateCache.put(\'' + $content[0].attribs.id + '\',\'' + jsesc($("#document script").eq(0).html()) + '\');');
			callback(null, newFile);
		} else {
			callback(new PluginError('gulp-ng-template-cache', 'The file is not ng-template: ' + file.path));
		}
	}

	function ngTemplateCacheStream(){
		es.map(ngTemplateCacheFile);
	}

	return es.pipeline(
		ngTemplateCacheStream(),
		concat(bundlename));
};
Add error to callback