@@ -30,8 +30,11 @@ import jasmineReporters from "jasmine-reporters";
3030import jasmineTerminalReporter from "jasmine-terminal-reporter" ;
3131import eventStream from "event-stream" ;
3232import fs from "fs" ;
33+ import http from "http" ;
34+ import https from "https" ;
3335import jsonlint from "gulp-jsonlint" ;
3436import path from "path" ;
37+ import { spawn } from "child_process" ;
3538
3639const sass = gulpSass ( dartSass ) ;
3740const clientDir = "app" ;
@@ -49,6 +52,54 @@ const $ = gulpLoadPlugins({
4952const reload = browserSync . reload ;
5053const argv = yargs . argv ;
5154
55+ function openBrowser ( url ) {
56+ if ( process . env . CI ) {
57+ return ;
58+ }
59+
60+ if ( process . platform === "darwin" ) {
61+ spawn ( "open" , [ url ] , { detached : true , stdio : "ignore" } ) . unref ( ) ;
62+ return ;
63+ }
64+
65+ if ( process . platform === "win32" ) {
66+ spawn ( "cmd" , [ "/c" , "start" , "" , url ] , { detached : true , stdio : "ignore" } ) . unref ( ) ;
67+ return ;
68+ }
69+
70+ spawn ( "xdg-open" , [ url ] , { detached : true , stdio : "ignore" } ) . unref ( ) ;
71+ }
72+
73+ function waitForServer ( url , { timeoutMs = 10000 , pollIntervalMs = 200 } = { } ) {
74+ const parsedUrl = new URL ( url ) ;
75+ const client = parsedUrl . protocol === "https:" ? https : http ;
76+ const startedAt = Date . now ( ) ;
77+
78+ return new Promise ( ( resolve ) => {
79+ function tryConnect ( ) {
80+ const request = client . get ( url , ( response ) => {
81+ response . resume ( ) ;
82+ resolve ( true ) ;
83+ } ) ;
84+
85+ request . on ( "error" , ( ) => {
86+ if ( Date . now ( ) - startedAt >= timeoutMs ) {
87+ resolve ( false ) ;
88+ return ;
89+ }
90+
91+ setTimeout ( tryConnect , pollIntervalMs ) ;
92+ } ) ;
93+
94+ request . setTimeout ( pollIntervalMs , ( ) => {
95+ request . destroy ( new Error ( "timeout" ) ) ;
96+ } ) ;
97+ }
98+
99+ tryConnect ( ) ;
100+ } ) ;
101+ }
102+
52103const vendorStyles = [
53104 "node_modules/font-awesome/css/font-awesome.min.css" ,
54105 "node_modules/font-awesome/css/font-awesome.css.map" ,
@@ -64,7 +115,6 @@ function lint(files, options = {}) {
64115 return ( ) => {
65116 return gulp
66117 . src ( files )
67- . pipe ( reload ( { stream : true , once : true } ) )
68118 . pipe ( $ . eslint ( options ) )
69119 . pipe ( $ . eslint . format ( "compact" ) )
70120 . pipe ( $ . if ( ! browserSync . active , $ . eslint . failOnError ( ) ) ) ;
@@ -323,17 +373,16 @@ function provideMissingData() {
323373 } ) ;
324374}
325375
326- gulp . task (
327- "step-templates" ,
328- gulp . series ( "tests" , ( ) => {
329- return gulp
330- . src ( "./step-templates/*.json" )
331- . pipe ( provideMissingData ( ) )
332- . pipe ( concat ( "step-templates.json" , { newLine : "," } ) )
333- . pipe ( insert . wrap ( '{"items": [' , "]}" ) )
334- . pipe ( argv . production ? gulp . dest ( `${ publishDir } /app/services` ) : gulp . dest ( `${ buildDir } /app/services` ) ) ;
335- } )
336- ) ;
376+ gulp . task ( "step-templates:data" , ( ) => {
377+ return gulp
378+ . src ( "./step-templates/*.json" )
379+ . pipe ( provideMissingData ( ) )
380+ . pipe ( concat ( "step-templates.json" , { newLine : "," } ) )
381+ . pipe ( insert . wrap ( '{"items": [' , "]}" ) )
382+ . pipe ( argv . production ? gulp . dest ( `${ publishDir } /app/services` ) : gulp . dest ( `${ buildDir } /app/services` ) ) ;
383+ } ) ;
384+
385+ gulp . task ( "step-templates" , gulp . series ( "tests" , "step-templates:data" ) ) ;
337386
338387gulp . task ( "styles:vendor" , ( ) => {
339388 return gulp . src ( vendorStyles , { base : "node_modules/" } ) . pipe ( argv . production ? gulp . dest ( `${ publishDir } /public/styles/vendor` ) : gulp . dest ( `${ buildDir } /public/styles/vendor` ) ) ;
@@ -426,14 +475,35 @@ gulp.task(
426475 server . start ( ) ;
427476 process . chdir ( `../` ) ;
428477
429- browserSync . init ( null , {
430- proxy : "http://localhost:9000" ,
431- } ) ;
478+ browserSync . init (
479+ null ,
480+ {
481+ proxy : "http://localhost:9000" ,
482+ open : false ,
483+ } ,
484+ ( ) => {
485+ waitForServer ( "http://localhost:9000" ) . then ( ( isReady ) => {
486+ if ( isReady ) {
487+ openBrowser ( "http://localhost:9000" ) ;
488+ return ;
489+ }
490+
491+ log . warn ( "Timed out waiting for http://localhost:9000, skipping automatic browser launch." ) ;
492+ } ) ;
493+ }
494+ ) ;
495+
496+ function reloadServer ( done ) {
497+ process . chdir ( `${ buildDir } ` ) ;
498+ server . start ( ) ;
499+ process . chdir ( `../` ) ;
500+ done ( ) ;
501+ }
432502
433503 gulp . watch ( `${ clientDir } /**/*.jade` , gulp . series ( "build:client" ) ) ;
434- gulp . watch ( `${ clientDir } /**/*.jsx` , gulp . series ( "scripts" , "copy:app" ) ) ;
504+ gulp . watch ( `${ clientDir } /**/*.jsx` , gulp . series ( "scripts" , "copy:app" , reloadServer ) ) ;
435505 gulp . watch ( `${ clientDir } /content/styles/**/*.scss` , gulp . series ( "styles:client" ) ) ;
436- gulp . watch ( "step-templates/*.json" , gulp . series ( "step-templates" ) ) ;
506+ gulp . watch ( "step-templates/*.json" , gulp . series ( "step-templates:data " ) ) ;
437507
438508 gulp . watch ( `${ buildDir } /**/*.*` ) . on ( "change" , reload ) ;
439509 } )
0 commit comments