Skip to content

Commit

Permalink
Merge pull request #193 from jpbackman/refactor-styleguide-main
Browse files Browse the repository at this point in the history
Refactor: use promises in styleguide.js, use named functions instead of comments, general readability improvements
  • Loading branch information
hannu committed Nov 17, 2014
2 parents dd47806 + 5e52dcd commit 76b9b6d
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 169 deletions.
5 changes: 3 additions & 2 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ gulp.task('styleguide', function() {
if (!fs.existsSync(__dirname + distPath)) {
process.stderr.write(chalk.red.bold('Error:') + ' Directory ' + distPath + ' does not exist. You probably installed library by cloning repository directly instead of NPM repository.\n');
process.stderr.write('You need to run ' + chalk.green.bold('gulp build') + ' first\n');
process.exit(1)
process.exit(1);
return 1;
}
return gulp.src([sourcePath + '/**/*.scss'])
Expand Down Expand Up @@ -102,7 +102,8 @@ gulp.task('sass', function() {
.pipe(plumber())
.pipe(sass({
// Include bourbon & neat
includePaths: neat.includePaths
includePaths: neat.includePaths,
errLogToConsole: true
}))
.pipe(sourcemaps.init())
.pipe(please({
Expand Down
28 changes: 11 additions & 17 deletions lib/modules/io.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ module.exports = function(ioServer, options) {

var loadVariables,
saveVariables,
reloadStyles,
currentSocket,
emitProgressStart,
emitProgressEnd,
emitCompileError,
emitCompileSuccess,
io = ioServer;

if (options.styleVariables && !fs.existsSync(options.styleVariables)) {
Expand All @@ -21,9 +15,8 @@ module.exports = function(ioServer, options) {

loadVariables = function(socket) {
return function() {
var syntax = path.extname(options.styleVariables).substring(1);
var values, syntax = path.extname(options.styleVariables).substring(1);
fs.readFile(options.styleVariables, {encoding: 'utf8'}, function(err, data) {
var values = {};
if (err) {
return console.error(err);
}
Expand All @@ -50,21 +43,23 @@ module.exports = function(ioServer, options) {
};
};

emitProgressStart = function() {
function emitProgressStart() {
io.sockets.emit('styleguide progress start');
};
}

emitProgressEnd = function() {
function emitProgressEnd() {
io.sockets.emit('styleguide progress end');
};
}

emitCompileError = function(err) {
function emitCompileError(err) {
io.sockets.emit('styleguide compile error', err);
};
emitProgressEnd();
}

emitCompileSuccess = function() {
function emitCompileSuccess() {
io.sockets.emit('styleguide compile success');
};
emitProgressEnd();
}

io.on('connection', function(socket) {
console.log('Socket connection established (id:', socket.conn.id + ')');
Expand All @@ -74,7 +69,6 @@ module.exports = function(ioServer, options) {

return {
emitProgressStart: emitProgressStart,
emitProgressEnd: emitProgressEnd,
emitCompileError: emitCompileError,
emitCompileSuccess: emitCompileSuccess
}
Expand Down
36 changes: 8 additions & 28 deletions lib/modules/preprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,24 @@ var concat = require('gulp-concat'),
filter = require('gulp-filter'),
less = require('gulp-less'),
sass = require('gulp-sass'),
vfs = require('vinyl-fs'),
_ = require('lodash'),
proxyOnce = function(obj, key, fn) {
var original = obj[key];
obj[key] = function proxy() {
var args = Array.prototype.slice.call(arguments);
if (_.isFunction(fn)) {
fn.apply(undefined, args);
}
if (_.isFunction(original)) {
original.apply(undefined, args);
}
obj[key] = original;
}
};
vfs = require('vinyl-fs');

module.exports = {
// Processes all SASS/LESS/CSS files and combine to a single file
getStream: function(files, opt) {
var sassStream,
lessStream,
cssStream;

proxyOnce(opt.sass, 'onError', opt.onCompileError);
proxyOnce(opt.sass, 'onSuccess', opt.onCompileSuccess);
/**
* Process all SASS/LESS/CSS files and combine them into a single stream
*/
getStream: function(files, opt, errback) {
var sassStream, lessStream, cssStream;

sassStream = vfs.src(opt.sass.src || files)
.pipe(filter('**/*.scss'))
.pipe(sass(opt.sass))
.pipe(sass(opt.sass).on('error', errback))
.pipe(concat('sass.css'));

lessStream = vfs.src(opt.less.src || files)
.pipe(filter('**/*.less'))
.pipe(less(opt.less))
.on('error', function(err) {
console.error('An error occurred when compiling LESS');
console.error(err.toString());
})
.pipe(less(opt.less).on('error', errback))
.pipe(concat('less.css'));

cssStream = vfs.src(files)
Expand Down
Loading

0 comments on commit 76b9b6d

Please sign in to comment.