From 877e77bb8f098c0922c530267ee3bb3439ce6049 Mon Sep 17 00:00:00 2001 From: Artem Malko Date: Thu, 20 Aug 2015 22:22:49 +0600 Subject: [PATCH] move final cb to the css file rendering and writing function --- index.js | 31 ++++++++++++------------------- test/gulpfile.js | 2 +- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index ec78776..c252ea3 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,6 @@ 'use strict'; var cheerio = require('cheerio'), - events = require('events'), fs = require('fs'), gutil = require('gulp-util'), mkdirp = require('mkdirp'), @@ -75,21 +74,17 @@ var spriteSVG = function(options) { units: options.units, width: 0 }, - eventEmitter = new events.EventEmitter(), self, x = options.x, y = options.y; - // When a template file is loaded, render it - eventEmitter.on("loadedTemplate", renderTemplate); - // Generate relative em/rem untis from pixels function pxToRelative(value) { return value / options.pixelBase; } // Load a template file and then render it - function loadTemplate(src, dest) { + function loadTemplate(src, dest, cb) { fs.readFile(src, function(err, contents) { if(err) { new gutil.PluginError(PLUGIN_NAME, err); @@ -101,7 +96,7 @@ var spriteSVG = function(options) { dest: dest }; - eventEmitter.emit("loadedTemplate", file); + renderTemplate(file, cb); }); } @@ -276,10 +271,10 @@ var spriteSVG = function(options) { } // Render our template and then save the file - function renderTemplate(file) { + function renderTemplate(file, cb) { var compiled = mustache.render(file.contents, file.data); mkdirp(path.dirname(file.dest), function(){ - fs.writeFile(file.dest, compiled); + fs.writeFile(file.dest, compiled, cb); }); } @@ -307,22 +302,20 @@ var spriteSVG = function(options) { data.width = pxToRelative(data.width); } - // Save our CSS template file - loadTemplate(options.templateSrc, options.templateDest); - - // If a demo file is required, save that too - if(options.demoDest) { - loadTemplate(options.demoSrc, options.demoDest); - } - // Create a file to pipe back to gulp var file = new gutil.File({path: './', contents: new Buffer($.xml())}); // Pipe it baby! self.push(file); - // Aaand we're done - cb(); + // cb will be executed after css file will be rendered and created + // Save our CSS template file + loadTemplate(options.templateSrc, options.templateDest, cb); + + // If a demo file is required, save that too + if(options.demoDest) { + loadTemplate(options.demoSrc, options.demoDest, cb); + } } return through2.obj(processSVG, processSprites); diff --git a/test/gulpfile.js b/test/gulpfile.js index 3628b3c..1ee4719 100644 --- a/test/gulpfile.js +++ b/test/gulpfile.js @@ -22,4 +22,4 @@ gulp.task('jshint', function() { 'node': true })) .pipe(jshint.reporter('default')); -}); \ No newline at end of file +});