Skip to content

Commit

Permalink
chore(*) add options port & disable-watch to gulp serve
Browse files Browse the repository at this point in the history
You can now specify a port on which you want to launch your server:
gulp serve --port 9002
You can also disable all the watchers if not necessary:
gulp serve --disable-watch
* Possible to mix & match
* Works on any env gulp serve:dev/test/prod
  • Loading branch information
topheman committed Aug 21, 2015
1 parent 30672f9 commit 952e831
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 11 deletions.
2 changes: 2 additions & 0 deletions gulp/const.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import moment from 'moment';
import pkg from '../package.json';
import gitRev from 'git-rev-sync';

export const DEFAULT_SERVER_PORT = 9000;

var infos = {
file: '',
pkg: pkg,
Expand Down
39 changes: 28 additions & 11 deletions gulp/tasks/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,19 @@ import browserSync from 'browser-sync';

import jspmOverride from '../../test/jspm.override.json';

import {COLORS,LOG} from '../utils.js';

import {DEFAULT_SERVER_PORT} from '../const.js';
import path from '../paths';

//launch your task with `--port 9002` for example
var serverPort = util.env.port;

function infos(env) {
LOG(COLORS.yellow('[INFOS] call `gulp serve:' + env + ' --port 9002` (for example) to launch on another port'));
LOG(COLORS.yellow('[INFOS] call `gulp serve:' + env + ' --disable-watch` if you don\'t need it'));
}

//=============================================
// PROXY CONFIGURATION
//=============================================
Expand All @@ -18,17 +29,20 @@ import path from '../paths';
* Injecting `env` as a global variable + overriding jspm.config.js if in test
* @param env dev/test/prod
* @param baseDir
* @param files
* @param browser
* @param [options]
* @param [options.files='default']
* @param [options.browser='default']
* @param [options.port=DEFAULT_SERVER_PORT]
*/
function startBrowserSync(env, baseDir, files, browser) {
function startBrowserSync(env, baseDir, options = {}) {
env = env.toLowerCase();
browser = browser === undefined ? 'default' : browser;
files = files === undefined ? 'default' : files;
options.browser = options.browser === undefined ? 'default' : options.browser;
options.files = options.files === undefined ? 'default' : options.files;
options.port = options.port === undefined ? DEFAULT_SERVER_PORT : options.port;

var config = {
files: files,
port: 9000,
files: options.files,
port: options.port,
notify: false,
server: {
baseDir: baseDir,
Expand All @@ -46,7 +60,7 @@ function startBrowserSync(env, baseDir, files, browser) {
}
]
},
browser: browser
browser: options.browser
};

/**
Expand Down Expand Up @@ -95,7 +109,8 @@ function startBrowserSync(env, baseDir, files, browser) {
* The 'serve' task serve the dev environment.
*/
gulp.task('serve:dev', ['sass', 'watch:dev'], () => {
startBrowserSync('dev', ['.tmp', 'src', 'jspm_packages', './']);
infos('dev');
startBrowserSync('dev', ['.tmp', 'src', 'jspm_packages', './'], {port: serverPort});
});
gulp.task('serve', ['serve:dev']);

Expand All @@ -105,12 +120,14 @@ gulp.task('serve', ['serve:dev']);
* This is for dev purpose of the tests. Launch the tests with `npm test` or `npm test-unit`
*/
gulp.task('serve:test', ['sass', 'watch:test'], () => {
startBrowserSync('test', ['.tmp', 'src', 'jspm_packages', './']);
infos('test');
startBrowserSync('test', ['.tmp', 'src', 'jspm_packages', './'], {port: serverPort});
});

/**
* The 'serve' task serve the prod environment (you need to build before)
*/
gulp.task('serve:prod', () => {
startBrowserSync('prod', ['./build/dist']);
infos('prod');
startBrowserSync('prod', ['./build/dist'], {port: serverPort});
});
14 changes: 14 additions & 0 deletions gulp/tasks/watch.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
'use strict';

import gulp from 'gulp';
import util from 'gulp-util';
import browserSync from 'browser-sync';

import {COLORS,LOG} from '../utils.js';

import paths from '../paths';

//launch your task with `--disable-watch` for example
var disableWatch = util.env['disable-watch'];

function setupBasicGulpWatch() {
// Watch images and fonts files
gulp.watch([paths.app.images, paths.app.fonts], [browserSync.reload]);
Expand Down Expand Up @@ -46,11 +52,19 @@ function getGulpWatchHtml(env = 'dev') {
* According to the env it's launched to, it won't watch the same files (in test, will watch more files)
*/
gulp.task('watch:dev', () => {
if(disableWatch){
LOG(COLORS.yellow('[INFOS] watch is disabled'));
return;
}
setupBasicGulpWatch();
getGulpWatchJs('dev');
getGulpWatchHtml('dev');
});
gulp.task('watch:test', () => {
if(disableWatch){
LOG(COLORS.yellow('[INFOS] watch is disabled'));
return;
}
setupBasicGulpWatch();
getGulpWatchJs('test');
getGulpWatchHtml('test');
Expand Down

0 comments on commit 952e831

Please sign in to comment.