From: Gustavo Martin Morcuende Date: Fri, 21 Aug 2015 20:23:11 +0000 (+0200) Subject: showcase: files synchronization, gulp function X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=8d546dfac63bedb85c8d62970bca6a87bd8398f5;p=JavaScriptForFun showcase: files synchronization, gulp function --- diff --git a/angularjs/showcase/gulp.config.js b/angularjs/showcase/gulp.config.js index f479316..f13b331 100644 --- a/angularjs/showcase/gulp.config.js +++ b/angularjs/showcase/gulp.config.js @@ -32,7 +32,7 @@ module.exports = function() { directory: './build/' }, - templates: app + '**/*.html', + html: app + '**/*.html', templateFile: 'templates.js', temp: './.tmp/', diff --git a/angularjs/showcase/gulpfile.js b/angularjs/showcase/gulpfile.js index d81f338..16d191b 100644 --- a/angularjs/showcase/gulpfile.js +++ b/angularjs/showcase/gulpfile.js @@ -21,7 +21,6 @@ var serverConfig = require('./server/server.config')(); gulp.task('help', plugins.taskListing); gulp.task('default', ['help']); - /** * @ngdoc function * @@ -67,11 +66,10 @@ gulp.task('vet-html', function() { log('*** Checking HTML source files with HTMLHint ***'); - return gulp.src(config.templates) + return gulp.src(config.html) .pipe(plugins.if(args.verbose, plugins.print())) .pipe(plugins.htmlhint(config.htmlHintConfigurationFile)) .pipe(plugins.htmlhint.failReporter()); - }); /** @@ -82,9 +80,9 @@ gulp.task('vet-html', function() { * * @returns {Stream} */ -gulp.task('wiredep', function() { +gulp.task('wireup', function() { - log('*** Wiring bower dependencies into html ***'); + log('*** Wiring bower dependencies and injecting files into html ***'); var wiredep = require('wiredep').stream; var jsFiles = args.stubs ? [].concat(config.jsFilesWithoutSpecs, config.jsFilesStubs) : config.jsFilesWithoutSpecs; @@ -103,7 +101,7 @@ gulp.task('wiredep', function() { * * @returns {Stream} */ -gulp.task('wiredepkarma', function() { +gulp.task('wireupkarma', function() { log('*** Wiring bower dependencies into karma.conf.js ***'); @@ -120,7 +118,7 @@ gulp.task('wiredepkarma', function() { * @description * Runs HTTP server. */ -gulp.task('server', ['wiredep'], function(done) { +gulp.task('server', function(done) { var environment = 'development'; if (args.environment) { @@ -141,7 +139,7 @@ gulp.task('server', ['wiredep'], function(done) { * * @returns {Stream} */ -gulp.task('test', ['vet', 'wiredepkarma'], function(done) { +gulp.task('test', ['vet', 'wireupkarma'], function(done) { log('*** Run tests once ***'); @@ -154,7 +152,7 @@ gulp.task('test', ['vet', 'wiredepkarma'], function(done) { * @description * Run specs and wait. Watch for file changes and re-run tests on each change */ -gulp.task('autotest', ['wiredepkarma'], function(done) { +gulp.task('autotest', ['wireupkarma'], function(done) { log('*** Run tests and wait ***'); @@ -169,7 +167,7 @@ gulp.task('autotest', ['wiredepkarma'], function(done) { * * @returns {stream} The stream. */ -gulp.task('build', ['wiredep', 'test', 'templatecache'], function() { +gulp.task('build', ['wireup', 'test', 'templatecache'], function() { log('*** Building application for production - Optimizing assets - HTML,CSS,JS ***'); @@ -302,7 +300,7 @@ gulp.task('templatecache', ['clean'], function() { log('*** Creating AngularJS $templateCache ***'); return gulp - .src(config.templates) + .src(config.html) .pipe(plugins.if(args.verbose, plugins.bytediff.start())) .pipe(plugins.minifyHtml({ empty: true, @@ -347,7 +345,7 @@ function inject(source, label) { * Runs HTTP server. * * @param {string=} [environment='development'] development or production environments. - * @returns {undefined} + * @returns {stream} The stream */ function server(environment) { var nodeOptions = { @@ -366,6 +364,7 @@ function server(environment) { }) .on('start', function () { log('HTTP server started'); + synchronization(environment); }) .on('crash', function () { log('HTTP server crashed'); @@ -429,3 +428,43 @@ function byteDiffFormat(data) { var difference = (data.savings > 0) ? ' smaller.' : ' larger.'; return data.fileName + ' is ' + data.percent + '%' + difference; } + +/** + * @ngdoc function + * + * @description + * Files synchronization. + * + * @param {string=} [environment='development'] development or production environments. + * @returns {undefined} + */ +function synchronization(environment) { + + log('Files synchronization'); + + var jsFiles = args.stubs ? [].concat(config.jsFilesWithoutSpecs, config.jsFilesStubs) : config.jsFilesWithoutSpecs; + + switch (environment) { + case 'production': + jsFiles = jsFiles.concat(config.html); + plugins.watch(jsFiles, { + name: 'Files synchronization', + verbose: true, + readDelay: 200 + }, plugins.batch(function (events, done) { + // TODO: gulp.start going to be deprecated in gulp 4.0.0 version. Alternatives? + gulp.start('build', done); + })); + break; + default: + plugins.watch(jsFiles, { + name: 'Files synchronization', + verbose: true, + readDelay: 200 + }, plugins.batch(function (events, done) { + // TODO: gulp.start going to be deprecated in gulp 4.0.0 version. Alternatives? + gulp.start('wireup', done); + })); + break; + } +} diff --git a/angularjs/showcase/package.json b/angularjs/showcase/package.json index debc816..e14a7ef 100644 --- a/angularjs/showcase/package.json +++ b/angularjs/showcase/package.json @@ -16,6 +16,7 @@ "gulp": "~3.9.0", "gulp-angular-filesort": "~1.1.1", "gulp-angular-templatecache": "~1.7.0", + "gulp-batch": "~1.0.5", "gulp-bytediff": "~1.0.0", "gulp-filter": "~3.0.0", "gulp-header": "~1.2.2", @@ -37,6 +38,7 @@ "gulp-uglify": "~1.2.0", "gulp-useref": "~1.3.0", "gulp-util": "~3.0.6", + "gulp-watch": "~4.3.5", "jshint-stylish": "~2.0.1", "karma": "~0.13.9", "karma-coverage": "~0.5.0",