From: Gustavo Martin Morcuende Date: Tue, 18 Aug 2015 17:37:14 +0000 (+0200) Subject: showcase: run HTTP server from gulp task. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=b25c9a93f64b5b7d0978b0ae4586dacc7b2693cb;p=JavaScriptForFun showcase: run HTTP server from gulp task. --- diff --git a/angularjs/showcase/gulp.config.js b/angularjs/showcase/gulp.config.js index fd98db1..9362257 100644 --- a/angularjs/showcase/gulp.config.js +++ b/angularjs/showcase/gulp.config.js @@ -26,7 +26,7 @@ module.exports = function() { jscsConfigurationFile: '.jscsrc' }; - config.getWiredepDefaultOptions = function() { + config.getWiredepOptions = function() { return { // The directory of your Bower packages. default: '.bowerrc'.directory || bower_components bowerJson: bower.json, diff --git a/angularjs/showcase/gulpfile.js b/angularjs/showcase/gulpfile.js index c9ee560..ae5f0c5 100644 --- a/angularjs/showcase/gulpfile.js +++ b/angularjs/showcase/gulpfile.js @@ -1,14 +1,8 @@ var args = require('yargs').argv; var config = require('./gulp.config')(); +var serverConfig = require('./server/server.config')(); var gulp = require('gulp'); -var plugins = require('gulp-load-plugins')({ - pattern: ['gulp-*', 'gulp.*'], // the glob(s) to search for - scope: ['dependencies', 'devDependencies', 'peerDependencies'], // which keys in the config to look within - replaceString: /^gulp(-|\.)/, // what to remove from the name of the module when adding it to the context - camelize: true, // if true, transforms hyphenated plugins names to camel case - lazy: true, // whether the plugins should be lazy loaded on demand - rename: {} // a mapping of plugins to rename -}); +var plugins = require('gulp-load-plugins')({lazy: true}); /** * Arguments: @@ -17,17 +11,23 @@ var plugins = require('gulp-load-plugins')({ * --stubs : Using stubs in index.html (for mocking services, controllers or any other stuff) */ + +/** + * Default tasks + * + */ gulp.task('help', plugins.taskListing); +gulp.task('default', ['help']); + /** - * @description - * vet (evaluate) the code and create coverage report + * vet (evaluate) the code and create coverage report. * * @return {Stream} */ gulp.task('vet', function() { - plugins.util.log(plugins.util.colors.blue('Checking source with JSHint and JSCS')); + log('Checking source files with JSHint and JSCS'); return gulp.src(config.jsAllFiles) .pipe(plugins.if(args.verbose, plugins.print())) @@ -40,12 +40,17 @@ gulp.task('vet', function() { })); }); +/** + * wire up bower dependencies and inject files in index.html + * + * @return {Stream} + */ gulp.task('wiredep', function() { - plugins.util.log('Wiring bower dependencies into html'); + log('Wiring bower dependencies into html'); var wiredep = require('wiredep').stream; - var wiredepOptions = config.getWiredepDefaultOptions(); + var wiredepOptions = config.getWiredepOptions(); var jsFiles = args.stubs ? [].concat(config.jsFilesWithoutSpecs, config.jsFilesStubs) : config.jsFilesWithoutSpecs; @@ -56,7 +61,15 @@ gulp.task('wiredep', function() { }); /** - * @description + * Runs HTTP server in development mode. + */ +gulp.task('server-dev', ['wiredep'], function() { + log('Starting server in development mode'); + + server(true); +}); + +/** * Inject files in a sorted sequence at a specified inject label. * * @param {Array} source Source files (glob patterns) @@ -72,4 +85,39 @@ function inject(source, label) { return plugins.inject( gulp.src(source) .pipe(plugins.angularFilesort(), options)); +} + +/** + * Runs HTTP server. + * + * @param {boolean} isDev - development or build mode. + */ +function server(isDev) { + var nodeOptions = { + script: serverConfig.script, + delayTime: 1, + env: { + 'NODE_ENV': isDev ? 'dev' : 'build' + }, + watch: [serverConfig.directory] + }; + + return plugins.nodemon(nodeOptions) + .on('restart', ['vet'], function(ev) { + log('HTTP server restarted'); + log('files changed:\n' + ev); + }) + .on('start', function () { + log('HTTP server started'); + }) + .on('crash', function () { + log('HTTP server crashed'); + }) + .on('exit', function () { + log('HTTP server exited'); + }); +} + +function log(message) { + plugins.util.log(plugins.util.colors.blue(message)); } \ No newline at end of file diff --git a/angularjs/showcase/package.json b/angularjs/showcase/package.json index c6282dc..236af8b 100644 --- a/angularjs/showcase/package.json +++ b/angularjs/showcase/package.json @@ -12,6 +12,7 @@ "gulp-jscs": "~2.0.0", "gulp-jshint": "~1.11.2", "gulp-load-plugins": "~1.0.0-rc.1", + "gulp-nodemon": "~2.0.3", "gulp-order": "~1.1.1", "gulp-print": "~1.1.0", "gulp-task-listing": "~1.0.1", @@ -22,6 +23,8 @@ "karma-jasmine": "~0.3.6", "karma-junit-reporter": "~0.3.3", "karma-phantomjs-launcher": "~0.2.1", + "morgan": "~1.6.1", + "serve-favicon": "~2.3.0", "wiredep": "~3.0.0-beta", "yargs": "~3.19.0" }, diff --git a/angularjs/showcase/server.js b/angularjs/showcase/server.js deleted file mode 100644 index 86be7b0..0000000 --- a/angularjs/showcase/server.js +++ /dev/null @@ -1,25 +0,0 @@ -var express = require('express'); -var proxy = require('express-http-proxy'); -var app = express(); - -// URL to backend server -var moduleName = process.env.npm_package_config_backend_server_url; - -// Backend server port: -var serverPort = process.env.npm_package_config_backend_port; - -// Backend server name: -var serverName = process.env.npm_package_config_backend_server_name; - - -app.use('/', express.static(__dirname)); -app.use('/', express.static('app')); -app.use('/' + moduleName, proxy('http://' + serverName, { - forwardPath: function(req, res) { - return require('url').parse('/' + moduleName + req.url).path; - }, - port: serverPort -})); - - -app.listen(process.env.npm_package_config_express_port); diff --git a/angularjs/showcase/server/favicon.ico b/angularjs/showcase/server/favicon.ico new file mode 100644 index 0000000..b8e4f5f Binary files /dev/null and b/angularjs/showcase/server/favicon.ico differ diff --git a/angularjs/showcase/server/server.config.js b/angularjs/showcase/server/server.config.js new file mode 100644 index 0000000..d0b022a --- /dev/null +++ b/angularjs/showcase/server/server.config.js @@ -0,0 +1,12 @@ +module.exports = function() { + var main = './src/showcase/'; + var config = { + main: main, + index: main + 'index.html', + port: 9000, + script: 'server/server.js', + directory: 'server/' + }; + + return config; +}; diff --git a/angularjs/showcase/server/server.js b/angularjs/showcase/server/server.js new file mode 100644 index 0000000..a291373 --- /dev/null +++ b/angularjs/showcase/server/server.js @@ -0,0 +1,39 @@ +'use strict'; + +var express = require('express'); +var app = express(); +var favicon = require('serve-favicon'); +var logger = require('morgan'); +var config = require('./server.config')(); +var port = config.port; +var environment = process.env.NODE_ENV; + +app.use(favicon(__dirname + '/favicon.ico')); +app.use(logger('dev')); + +switch (environment) { + case 'build': + console.log('build mode'); + + app.use(express.static('./build/')); + + // Deep linking + app.use('/*', express.static('./build/index.html')); + break; + default: + console.log('development mode'); + + app.use(express.static(config.main)); + app.use(express.static('./')); + + // Deep linking + app.use('/*', express.static(config.index)); + break; +} + +app.listen(port, function() { + console.log('Express server listening on port ' + port); + console.log('env = ' + app.get('env') + + '\n __dirname = ' + __dirname + + '\n process.cwd = ' + process.cwd()); +}); diff --git a/angularjs/showcase/src/showcase/index.html b/angularjs/showcase/src/showcase/index.html index 6db5d71..82f39d2 100644 --- a/angularjs/showcase/src/showcase/index.html +++ b/angularjs/showcase/src/showcase/index.html @@ -79,7 +79,7 @@
- Hello World!!! You should never see this if ui-router works as expected. + Hello World!!! You should never see this message if ui-router works as expected.