@@ -13,10 +13,58 @@ var os = require('os');
13
13
var bsconfig = 'bsconfig.json'
14
14
var bsb_exe = __filename + ".exe"
15
15
16
+ var LAST_SUCCESS_BUILD_STAMP = 0
16
17
18
+ // All clients of type MiniWebSocket
19
+ var wsClients = [ ]
17
20
var watch_mode = false
18
21
var verbose = false
19
22
var postBuild = undefined
23
+ var useWebSocket = false
24
+ var webSocketPort = 9999
25
+
26
+ function dlog ( str ) {
27
+ if ( verbose ) {
28
+ console . log ( str )
29
+ }
30
+ }
31
+ function notifyClients ( ) {
32
+ wsClients = wsClients . filter ( x => ! x . closed && ! x . socket . destroyed )
33
+ var wsClientsLen = wsClients . length
34
+ dlog ( `Alive sockets number: ${ wsClientsLen } ` )
35
+ for ( var i = 0 ; i < wsClientsLen ; ++ i ) {
36
+ var client = wsClients [ i ]
37
+ if ( ! client . closed ) {
38
+ client . sendText (
39
+ JSON . stringify (
40
+ {
41
+ LAST_SUCCESS_BUILD_STAMP : LAST_SUCCESS_BUILD_STAMP
42
+ }
43
+ )
44
+
45
+ )
46
+ }
47
+ }
48
+ }
49
+
50
+ function setUpWebSocket ( ) {
51
+ var WebSocket = require ( './minisocket.js' ) . MiniWebSocket
52
+ var id = setInterval ( notifyClients , 3000 )
53
+ require ( 'http' ) . createServer ( )
54
+ . on ( 'upgrade' , function ( req , socket , upgradeHead ) {
55
+ dlog ( "connection opened" ) ;
56
+ var ws = new WebSocket ( req , socket , upgradeHead ) ;
57
+ wsClients . push ( ws )
58
+ } )
59
+ . on ( 'error' , function ( err ) {
60
+ console . error ( err )
61
+ process . exit ( 2 )
62
+ } )
63
+ . listen ( webSocketPort , "localhost" ) ;
64
+ }
65
+
66
+
67
+
20
68
/**
21
69
* @type {string[] }
22
70
*/
@@ -28,17 +76,27 @@ for (var i = 2; i < process_argv.length; ++i) {
28
76
// TODO boundary safety check
29
77
// Not really needed
30
78
postBuild = process_argv [ ++ i ]
79
+ } else if ( current === "-ws" ) {
80
+ var portNumber = parseInt ( process_argv [ ++ i ] )
81
+ dlog ( `WebSocket port number: ${ portNumber } ` )
82
+ if ( ! isNaN ( portNumber ) ) {
83
+ webSocketPort = portNumber
84
+ }
85
+ useWebSocket = true
86
+
31
87
} else {
32
88
delegate_args . push ( current )
33
89
if ( current === '-w' ) {
34
90
watch_mode = true
35
91
} else if ( current === "-verbose" ) {
36
92
verbose = true
37
93
38
- }
94
+ }
39
95
}
40
96
}
41
97
98
+
99
+
42
100
try {
43
101
child_process . execFileSync ( bsb_exe , delegate_args , { stdio : 'inherit' } )
44
102
} catch ( e ) {
55
113
if ( watch_mode ) {
56
114
var fs = require ( 'fs' )
57
115
var path = require ( 'path' )
58
-
116
+ if ( useWebSocket ) {
117
+ setUpWebSocket ( )
118
+ }
59
119
// for column one based error message
60
120
process . env . BS_VSCODE = '1'
61
121
var cwd = process . cwd ( )
@@ -192,20 +252,12 @@ if (watch_mode) {
192
252
function logFinish ( code ) {
193
253
if ( std_is_tty ) {
194
254
if ( code === 0 ) {
195
- if ( postBuild ) {
196
- console . log ( "running postbuild command" , postBuild )
197
- child_process . exec ( postBuild , { shell : true } )
198
- }
199
255
console . log ( "\x1b[36m>>>> Finish compiling\x1b[0m" )
200
256
} else {
201
257
console . log ( "\x1b[1;31m>>>> Finish compiling(exit: " + code + ")\x1b[0m" )
202
258
}
203
259
} else {
204
260
if ( code === 0 ) {
205
- if ( postBuild ) {
206
- console . log ( "running postbuild command" , postBuild )
207
- child_process . exec ( postBuild , { shell : true } )
208
- }
209
261
console . log ( ">>>> Finish compiling" )
210
262
} else {
211
263
console . log ( ">>>> Finish compiling(exit: " + code + ")" )
@@ -227,6 +279,14 @@ if (watch_mode) {
227
279
* @param signal {string}
228
280
*/
229
281
function build_finished_callback ( code , signal ) {
282
+ if ( code === 0 ) {
283
+ LAST_SUCCESS_BUILD_STAMP = + new Date ( ) ;
284
+ notifyClients ( )
285
+ if ( postBuild ) {
286
+ dlog ( `running postbuild command: ${ postBuild } ` )
287
+ child_process . exec ( postBuild , { shell : true } )
288
+ }
289
+ }
230
290
logFinish ( code )
231
291
releaseBuild ( )
232
292
if ( needRebuild ( ) ) {
0 commit comments