@@ -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 ( ) ;
83+ } ) ;
84+
85+ request . on ( "error" , ( ) => {
86+ if ( Date . now ( ) - startedAt >= timeoutMs ) {
87+ resolve ( ) ;
88+ return ;
89+ }
90+
91+ setTimeout ( tryConnect , pollIntervalMs ) ;
92+ } ) ;
93+
94+ request . setTimeout ( pollIntervalMs , ( ) => {
95+ request . destroy ( ) ;
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" ,
@@ -323,17 +374,16 @@ function provideMissingData() {
323374 } ) ;
324375}
325376
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- ) ;
377+ gulp . task ( "step-templates:data" , ( ) => {
378+ return gulp
379+ . src ( "./step-templates/*.json" )
380+ . pipe ( provideMissingData ( ) )
381+ . pipe ( concat ( "step-templates.json" , { newLine : "," } ) )
382+ . pipe ( insert . wrap ( '{"items": [' , "]}" ) )
383+ . pipe ( argv . production ? gulp . dest ( `${ publishDir } /app/services` ) : gulp . dest ( `${ buildDir } /app/services` ) ) ;
384+ } ) ;
385+
386+ gulp . task ( "step-templates" , gulp . series ( "tests" , "step-templates:data" ) ) ;
337387
338388gulp . task ( "styles:vendor" , ( ) => {
339389 return gulp . src ( vendorStyles , { base : "node_modules/" } ) . pipe ( argv . production ? gulp . dest ( `${ publishDir } /public/styles/vendor` ) : gulp . dest ( `${ buildDir } /public/styles/vendor` ) ) ;
@@ -426,14 +476,28 @@ gulp.task(
426476 server . start ( ) ;
427477 process . chdir ( `../` ) ;
428478
429- browserSync . init ( null , {
430- proxy : "http://localhost:9000" ,
431- } ) ;
479+ browserSync . init (
480+ null ,
481+ {
482+ proxy : "http://localhost:9000" ,
483+ open : false ,
484+ } ,
485+ ( ) => {
486+ waitForServer ( "http://localhost:9000" ) . then ( ( ) => {
487+ openBrowser ( "http://localhost:9000" ) ;
488+ } ) ;
489+ }
490+ ) ;
491+
492+ function reloadServer ( done ) {
493+ server . start . bind ( server ) ( ) ;
494+ done ( ) ;
495+ }
432496
433497 gulp . watch ( `${ clientDir } /**/*.jade` , gulp . series ( "build:client" ) ) ;
434- gulp . watch ( `${ clientDir } /**/*.jsx` , gulp . series ( "scripts" , "copy:app" ) ) ;
498+ gulp . watch ( `${ clientDir } /**/*.jsx` , gulp . series ( "scripts" , "copy:app" , reloadServer ) ) ;
435499 gulp . watch ( `${ clientDir } /content/styles/**/*.scss` , gulp . series ( "styles:client" ) ) ;
436- gulp . watch ( "step-templates/*.json" , gulp . series ( "step-templates" ) ) ;
500+ gulp . watch ( "step-templates/*.json" , gulp . series ( "step-templates:data " ) ) ;
437501
438502 gulp . watch ( `${ buildDir } /**/*.*` ) . on ( "change" , reload ) ;
439503 } )
0 commit comments