Merge pull request #199 from Mibew/gulp_promise

Use promises to build all the styles
This commit is contained in:
Fedor A. Fetisov 2017-03-18 00:49:54 +03:00 committed by GitHub
commit 88ba0f148e

View File

@ -186,74 +186,65 @@ gulp.task('chat-styles', ['chat-styles-handlebars', 'chat-styles-js'], function(
// Compile and concatenate handlebars files for all chat styles. // Compile and concatenate handlebars files for all chat styles.
gulp.task('chat-styles-handlebars', function() { gulp.task('chat-styles-handlebars', function() {
var promises = []; return getChildDirs(config.chatStylesPath)
fs.readdir(config.chatStylesPath, function(err, list){ .then(function (dirs) {
if(err) return done(err); return Promise.all(dirs.map(function (dir) {
list.filter(function(path){ return new Promise(function(resolve, reject){
return fs.lstatSync(config.chatStylesPath + "/" + path).isDirectory(); gulp.src(config.chatStylesPath + '/' + dir + '/templates_src/client_side/**/*.handlebars')
}).map(function(dir){ .pipe(handlebars({
promises.push(new Promise(function(resolve, reject){ // Use specific version of Handlebars.js
var pipeline = gulp.src(config.chatStylesPath + '/' + dir + '/templates_src/client_side/**/*.handlebars') handlebars: handlebarsEngine
.pipe(handlebars({ }))
// Use specific version of Handlebars.js .pipe(wrapHandlebarsTemplate())
handlebars: handlebarsEngine .pipe(concat('templates.js'))
})) .pipe(uglify({preserveComments: 'some'}))
.pipe(wrapHandlebarsTemplate()) .pipe(header(config.compiledTemplatesHeader))
.pipe(concat('templates.js')) .pipe(gulp.dest(config.chatStylesPath + '/' + dir + '/templates_compiled/client_side'))
.pipe(uglify({preserveComments: 'some'})) .on('end', resolve)
.pipe(header(config.compiledTemplatesHeader)) .on('error', reject);
.pipe(gulp.dest(config.chatStylesPath + '/' + dir + '/templates_compiled/client_side')); });
pipeline.on('end', resolve);
pipeline.on('error', reject);
})); }));
}); });
});
}); });
// Compile and concatenate js files for all chat styles. // Compile and concatenate js files for all chat styles.
gulp.task('chat-styles-js', function() { gulp.task('chat-styles-js', function() {
var promises = []; return getChildDirs(config.chatStylesPath)
fs.readdir(config.chatStylesPath, function(err, list){ .then(function (dirs) {
if(err) return done(err); return Promise.all(dirs.map(function (dir) {
list.filter(function(path){ return new Promise(function(resolve, reject){
return fs.lstatSync(config.chatStylesPath + "/" + path).isDirectory(); gulp.src(config.chatStylesPath + '/' + dir + '/js/source/**/*.js')
}).map(function(dir){ .pipe(concat('scripts.js'))
promises.push(new Promise(function(resolve, reject){ .pipe(uglify({preserveComments: 'some'}))
var pipeline = gulp.src(config.chatStylesPath + '/' + dir + '/js/source/**/*.js') .pipe(gulp.dest(config.chatStylesPath + '/' + dir + '/js/compiled'))
.pipe(concat('scripts.js')) .on('end', resolve)
.pipe(uglify({preserveComments: 'some'})) .on('error', reject);
.pipe(gulp.dest(config.chatStylesPath + '/' + dir + '/js/compiled')); });
pipeline.on('end', resolve);
pipeline.on('error', reject);
})); }));
}); });
});
}); });
// Performs all job related with pages styles. // Performs all job related with pages styles.
gulp.task('page-styles', function() { gulp.task('page-styles', function() {
var promises = []; return getChildDirs(config.pageStylesPath)
fs.readdir(config.pageStylesPath, function(err, list){ .then(function (dirs) {
if(err) return done(err); return Promise.all(dirs.map(function (dir) {
list.filter(function(path){ return new Promise(function(resolve, reject){
return fs.lstatSync(config.pageStylesPath + "/" + path).isDirectory(); gulp.src(config.pageStylesPath + '/' + dir + '/templates_src/client_side/**/*.handlebars')
}).map(function(dir){ .pipe(handlebars({
promises.push(new Promise(function(resolve, reject){ // Use specific version of Handlebars.js
var pipeline = gulp.src(config.pageStylesPath + '/' + dir + '/templates_src/client_side/**/*.handlebars') handlebars: handlebarsEngine
.pipe(handlebars({ }))
// Use specific version of Handlebars.js .pipe(wrapHandlebarsTemplate())
handlebars: handlebarsEngine .pipe(concat('templates.js'))
})) .pipe(uglify({preserveComments: 'some'}))
.pipe(wrapHandlebarsTemplate()) .pipe(header(config.compiledTemplatesHeader))
.pipe(concat('templates.js')) .pipe(gulp.dest(config.pageStylesPath + '/' + dir + '/templates_compiled/client_side'))
.pipe(uglify({preserveComments: 'some'})) .on('end', resolve)
.pipe(header(config.compiledTemplatesHeader)) .on('error', reject);
.pipe(gulp.dest(config.pageStylesPath + '/' + dir + '/templates_compiled/client_side')); });
pipeline.on('end', resolve);
pipeline.on('error', reject);
})); }));
}); });
});
}); });
// Watch styles // Watch styles
@ -509,3 +500,40 @@ var xgettextHandlebars = function() {
callback(); callback();
}); });
} }
/**
* Retrieves list of all dirs which are placed in the specified one.
*
* @param {String} srcDir A dir where to search.
* @returns Promise A promise which will be resolved with list of child dirs or
* rejected with the occured error.
*/
var getChildDirs = function(srcDir) {
return (new Promise(function(resolve, reject) {
fs.readdir(srcDir, function (err, files) {
if (err) {
reject(err);
} else {
resolve(files);
}
});
})).then(function (files) {
// Replace all the files that are not a directory with nulls.
return Promise.all(files.map(function (file) {
return new Promise(function (resolve, reject) {
fs.lstat(config.chatStylesPath + '/' + file, function (err, stat) {
if (err) {
reject(err);
} else {
resolve(stat.isDirectory() ? file : null);
}
});
});
}));
}).then(function(dirs) {
// Remove all the nulls from the array.
return dirs.filter(function (dir) {
return null !== dir;
});
});
}