Browse Source

addded port scanner to gulp webserver

Bachir Soussi Chiadmi 4 years ago
parent
commit
b9b26866d6
2 changed files with 21 additions and 11 deletions
  1. 19 10
      gulpfile.js
  2. 2 1
      package.json

+ 19 - 10
gulpfile.js

@@ -6,15 +6,24 @@ var rename = require('gulp-rename');
 var shell = require('gulp-shell')
 var watch = require('gulp-watch');
 var webserver = require('gulp-webserver');
-
-gulp.task('webserver', function() {
-  gulp.src('.')
-    .pipe(webserver({
-      livereload: false,
-      directoryListing: false,
-      open: false,
-      fallback: 'index.html'
-    }));
+var portscanner = require('portscanner');
+
+
+gulp.task('webserver', ['build'], function() {
+  // Find the first available port. Asynchronously checks, so first port
+  // determined as available is returned.
+  portscanner.findAPortNotInUse(8000, 8020, '127.0.0.1', function(error, port) {
+    // console.log('AVAILABLE PORT AT: ' + port)
+    gulp.src('.')
+      .pipe(webserver({
+        port:port,
+        livereload: false,
+        directoryListing: false,
+        open: false,
+        fallback: 'index.html'
+      }));
+
+  })
 });
 
 gulp.task('scss', function () {
@@ -39,7 +48,7 @@ gulp.task('build', ['sync'], shell.task([
 
 
 // default gulp task
-gulp.task('default', ['build', 'scss', 'gui', 'webserver'], function() {
+gulp.task('default', ['scss', 'gui', 'webserver'], function() {
   gulp.watch('./assets/css/**/*.scss', ['scss']);
   gulp.watch('./assets/css/gui.scss', ['gui']);
   gulp.watch(['bin/build.py', './book-src/*', './templates/*.tpl.html'], ['build']);

+ 2 - 1
package.json

@@ -16,7 +16,8 @@
     "gulp-sass": "^2.0.1",
     "gulp-shell": "^0.5.2",
     "gulp-watch": "^4.2.4",
-    "gulp-webserver": "^0.9.1"
+    "gulp-webserver": "^0.9.1",
+    "portscanner": "^2.1.1"
   },
   "dependencies": {}
 }