schickling / gulp-webserver

Streaming gulp plugin to run a local webserver with LiveReload
https://www.npmjs.org/package/gulp-webserver
MIT License
356 stars 84 forks source link

Single run mode #48

Open demisx opened 9 years ago

demisx commented 9 years ago

Hi. I currently start the connect server from protractor task like this:

gulp.task('connect', function() {
  gulp.src('.')
    .pipe(webserver({
      port: 9001
    }));
});

gulp.task('protractor', ['connect'], function() {
  gulp.src(paths.e2e)
    .pipe((protractor({
      configFile: 'build/protractor.conf.js'
    })).on('error', function(e) {
      throw e;
    }));
});

The problem is that the protractor task never ends. Is it possible to enable a single run mode, so it exits the task when gulp protractor task is finished?

schickling commented 9 years ago

As a workaround just fire the kill event to the webserver to end it.

kennethlynne commented 9 years ago

How does one fire the kill event?

demisx commented 9 years ago

You can use gulp-exit plugin for that.

mickeyvip commented 9 years ago

It is difficult to fire kill event if the setup runs several tasks in a row, I don't know how I can get hold of the stream:

var runSequence = require('run-sequence');

gulp.task('test-e2e', function(cb) {
  runSequence(
    'build-site',
    'webserver-start', // how can I get this task's stream so I can call 'kill' on it?
    'protractor-test',
    cb);
});

Thank you @demisx for pointing out the gulp-exit. It does the work, although I am not sure I use it as intended:

var runSequence = require('run-sequence');

gulp.task('test-e2e', function(cb) {
  runSequence(
    'build-site',
    'webserver-start', // how can I get this task's stream so I can call 'kill' on it?
    'protractor-test',
    function() {
      gulp.src("").pipe(exit());
      cb();
    );
});
atais commented 7 years ago

@mickeyvip had one flaw for me - it sometimes did not close the webserver when protractor failed some tests. On dev machine it is not a problem but it resulted in very long builds on CI (travis), since the build basically had to time-out and get killed.

Instead, I have found a completely different approach based on https://github.com/angular/angular-seed/.

SO answer: http://stackoverflow.com/a/41983565/1549135

Let npm start a webserver for you package.json

"scripts": {
    "pretest": "npm install",
    "test": "(npm start > /dev/null &) && (gulp protractor)",

    "start": "http-server -a localhost -p 8000 -c-1 ./"
  },

Use gulp-angular-protractor gulpfile.js

gulp.task('protractor', function (callback) {
    gulp
        .src('tests/*.js')
        .pipe(gulpProtractorAngular({
            'configFile': 'protractor.conf.js',
            'debug': false,
            'autoStartStopServer': true,
            'verbose': false,
            'webDriverUpdate': {
                'browsers': ['ie', 'chrome']
            }
        }))
        .on('error', function (e) {
            console.log(e);
        })
        .on('end', callback);
});

And it works all good :smiley:

See the above in action in GitHub project: https://github.com/atais/angular-eonasdan-datetimepicker