@@ -35,7 +35,7 @@ function SSEsend(event,data){
3535  } ) ; 
3636} 
3737function  serial_init ( ) { 
38-   serial . changes ( ) . then ( monitor => { 
38+   return   serial . changes ( ) . then ( monitor => { 
3939    monitor . on ( "created" ,  function  ( f ,  stat )  { 
4040      SSEsend ( 'created' , f ) 
4141//      serial.list().then(a=>SSEsend('list',a)); 
@@ -134,7 +134,7 @@ app.get('/version/:screen', function (req, res) {
134134  . then ( a => { 
135135    //console.log(a) 
136136    var  s = JSON . stringify ( a ) ; 
137-     res . write ( `var config={pio:${ s } ${ pjson . version }  ) ; 
137+     res . write ( `var config={pio:${ s } ${ pjson . version } ; ` ) ; 
138138    res . end ( ) ; 
139139  } ) 
140140} ) ; 
@@ -146,7 +146,7 @@ app.get('/pio', function (req, res) {
146146  } ) ; 
147147} ) ; 
148148function  atob ( b64string ) { 
149-   if  ( typeof   Buffer . from   ===   "function" ) 
149+   if  (   process . version < "v6.0.0"   ) 
150150    // Node 5.10+ 
151151    return  Buffer . from ( b64string ,  'base64' ) ; 
152152  else 
@@ -257,23 +257,31 @@ app.post('/set/:file/:name/:prop/:value', function (req, res) {
257257  . then ( a => res . send ( req . params ) ) 
258258  . catch ( a => res . status ( 403 ) . send ( a ) ) 
259259} ) 
260- function  main ( ) { 
261-   serial_init ( ) ; 
262-   hints . init ( 1 ) ; 
263-   git . root ( ) 
264-   . then ( root => { 
265-     fs . stat ( path . join ( root , 'Marlin' ) , ( e , a ) => { 
266-     if ( ! a ) 
267-       console . log ( 'this git not look like Marlin repository' ) ; 
268-     else 
269-       getPort ( 3000 ) . then ( port  =>  { 
270-         server . listen ( port ,  function  ( )  { 
271-           console . log ( 'Marlin config tool started on port http://localhost:' + port ) ; 
272-         } ) ; 
273-         opn ( 'http://localhost:' + port + '/' ) ; 
274-       } ) ; 
275-     } ) 
260+ function  main ( noOpn ) { 
261+   return  Promise . resolve ( ) 
262+   . then ( serial_init ) 
263+   . catch ( a => console . error ( 'serial failed' ) ) 
264+   . then ( ( ) => hints . init ( 1 ) ) 
265+   . catch ( a => console . error ( 'hints failed' ) ) 
266+   . then ( ( ) => git . root ( ) ) 
267+   . then ( root => promisify ( fs . stat ) ( path . join ( root , 'Marlin' ) ) ) 
268+   . catch ( a => { 
269+     var  e = ( 'this git not look like Marlin repository' ) ; 
270+     console . log ( e ) ; 
271+     throw  e ; 
276272  } ) 
273+   . then ( ( ) => getPort ( 3000 ) ) 
274+   . then ( port  => new  Promise ( ( done , fail ) => { 
275+       server . on ( 'error' , function ( e ) { 
276+         fail ( e ) 
277+       } ) 
278+       server . listen ( port ,  function  ( )  { 
279+         var  url = 'http://localhost:' + port + '/' ; 
280+         console . log ( 'Marlin config tool started on ' + url ) ; 
281+         done ( url ) ; 
282+       } ) ; 
283+   } ) ) 
284+   . then ( url => ( ! noOpn && opn ( url ) , url ) ) ; 
277285} 
278286module . exports . main = main ; 
279287require . main === module  &&  main ( ) ; 
0 commit comments