@@ -181,10 +181,17 @@ app.get('/tags', function (req, res) {
181181    res . send ( data ) ; 
182182  } ) ; 
183183} ) ; 
184+ app . get ( '/branches' ,  function  ( req ,  res )  { 
185+   git . Branches ( ) . then ( data => { 
186+     res . send ( data ) ; 
187+   } ) ; 
188+ } ) ; 
184189app . get ( '/checkout/:branch' ,  function  ( req ,  res )  { 
185-   git . Checkout ( req . params . branch ) 
190+   return  Promise . resolve ( atob ( decodeURI ( req . params . branch ) ) . toString ( ) ) 
191+   . then ( a => ( console . log ( a ) , a ) ) 
192+   . then ( git . Checkout ) 
186193  . then ( data => { 
187-     res . send ( data ) ; 
194+     res . send ( data ) 
188195  } ) 
189196  . catch ( a => res . status ( 403 ) . send ( a ) ) 
190197} ) ; 
@@ -194,15 +201,16 @@ var get_cfg=()=>{
194201  . map ( f => { 
195202    return  base 
196203      . then ( p => 
197-           git . Show ( p [ 1 ] , path . join ( baseCfg , f ) ) 
204+           git . Show ( p [ 1 ] ,   path . join ( baseCfg ,   f ) ) . catch ( e   =>   git . Show ( p [ 1 ] ,   path . join ( 'Marlin' ,   f ) ) ) 
198205          . then ( file => mctool . getJson ( p [ 0 ] , file , p [ 1 ] ) ( path . join ( p [ 0 ] , 'Marlin' , f ) ) ) 
199206      ) 
200207      . then ( o => ( o . names . filter ( n => hints . d2i ( n . name ) , 1 ) . map ( n => o . defs [ n . name ] . hint = ! 0 ) , o ) ) 
201208      . then ( a => ( a . names = undefined , type = 'file' , a ) ) 
202209      . then ( a => 
203210        ( ! a . defs [ 'MOTHERBOARD' ] && a ) || 
204-         base 
205-           . then ( p => mctool . getBoards ( path . join ( p [ 0 ] , 'Marlin' , 'boards.h' ) ) ) 
211+         seek4File ( 'boards.h' ,  [  'Marlin' ,  path . join ( 'Marlin' ,  'src' ,  'core' ) ] ) 
212+           . then ( mctool . getBoards ) 
213+           . catch ( e  =>  ''  ) 
206214          . then ( boards => ( Object . assign ( a . defs [ 'MOTHERBOARD' ] , { select :boards , type :"select" } ) , a ) ) 
207215      ) 
208216  } ) ; 
@@ -213,40 +221,39 @@ app.get('/now/', function (req, res) { //???
213221  get_cfg ( ) . then ( a => res . send ( JSON . stringify ( a , null , 2 ) ) ) 
214222} ) ; 
215223var  unique = a => a . filter ( ( elem ,  index ,  self ) => index  ==  self . indexOf ( elem ) ) 
224+ var  ex_dir  =  ( rel )  =>  seek4File ( '' ,  [ path . join ( 'Marlin' ,  'example_configurations' ) ,  path . join ( 'Marlin' ,  'src' ,  'config' ,  'examples' ) ] ,  rel ) 
216225app . get ( '/examples' ,  function  ( req ,  res )  { 
217-   git . root ( ) 
218-   . then ( root => { 
219-     var   ex = path . join ( root , 'Marlin' , 'example_configurations' ) 
220-     return   walk ( ex ) 
226+      var   ex ; 
227+      return   ex_dir ( ) 
228+     . then ( dir   =>   ( ex   =   dir ) ) 
229+     . then ( walk ) 
221230    . then ( a => a . filter ( i => / C o n f i g u r a t i o n ( _ a d v ) ? \. h / . test ( i ) ) ) 
222231    . then ( a => a . map ( i => path . parse ( path . relative ( ex , i ) ) . dir ) ) 
223232    . then ( unique ) 
233+     . catch ( e  =>  [ ] ) 
224234    . then ( a => ( a . unshift ( 'Marlin' ) , a ) ) 
225235    . then ( a => res . send ( { current :baseCfg , list :a } ) ) 
226-   } ) 
227236} ) ; 
228237app . get ( '/set-base/:path' ,  function  ( req ,  res )  { 
229-   baseCfg = atob ( decodeURI ( req . params . path ) ) . toString ( ) ; 
230-   if  ( baseCfg != 'Marlin' ) 
231-     baseCfg = path . join ( 'Marlin' , 'example_configurations' , baseCfg ) ; 
232-   res . end ( ) ; 
238+   return  Promise . resolve ( atob ( decodeURI ( req . params . path ) ) . toString ( ) ) 
239+     . then ( base  =>  base  ==  'Marlin'  &&  base  ||  ex_dir ( 1 ) . then ( ex  =>  path . join ( ex ,  base ) )  ) 
240+     . then ( base  =>  res . send ( baseCfg  =  base ) ) 
233241} ) ; 
234242app . get ( '/status' ,  function  ( req ,  res )  { 
235243  git . Status ( ) . then ( a => res . send ( a ) ) 
236244} ) ; 
237245app . get ( '/checkout-force' ,  function  ( req ,  res )  { 
238-   var  cp = baseCfg == 'Marlin' ?a => a :a => 
239-     git . root ( ) 
240-     . then ( root => 
241-       [ 'Configuration.h' , 'Configuration_adv.h' ] 
246+   var  cp  =  ( )  =>  git . root ( ) 
247+     . then ( root  =>  Promise . all ( 
248+       [ 'Configuration.h' ,  'Configuration_adv.h' ,  '_Bootscreen.h' ] 
242249      . map ( f => new  Promise ( ( done , fail ) => 
243-           fs . createReadStream ( path . join ( root , baseCfg , f ) ) 
244-           . pipe ( fs . createWriteStream ( path . join ( root , 'Marlin' , f ) ) . on ( 'finish' , done ) ) 
245-         ) 
250+           fs . createReadStream ( path . join ( root ,   baseCfg ,   f ) ) . on ( 'error' ,   fail ) 
251+           . pipe ( fs . createWriteStream ( path . join ( root ,   'Marlin' ,   f ) ) . on ( 'finish' ,   done ) ) 
252+         ) . catch ( e   =>   'not found' ) 
246253      ) 
247-     ) 
254+     ) ) 
248255  git . Checkout ( '--force' ) 
249-   . then ( cp ) 
256+   . then ( a   =>   baseCfg   ==   'Marlin'   &&   a   ||   cp ( ) ) 
250257  . then ( a => res . send ( a ) ) ; 
251258} ) ; 
252259app . get ( '/fetch' ,  function  ( req ,  res )  { 
@@ -373,18 +380,19 @@ app.get('/version', function (req, res) {
373380  res . set ( 'Content-Type' ,  'image/svg+xml' ) ; 
374381  var  badge = { 
375382    text :       {  name :pjson . name ,  version :pjson . version  } , 
376-     width :      {  text :83 ,  version :39 ,   total : 122  } , 
383+     width :      {  text :  83 ,  version :  45  } , 
377384    position :   {  version :88  } 
378385  } ; 
386+   badge . width . total  =  badge . width . text  +  badge . width . version ; 
379387
380388  var  file = path . join ( __dirname , '..' , 'views' , 'version.html' ) ; 
381389  return  promisify ( fs . readFile ) ( file , 'utf8' ) 
382390  . then ( v => { 
383391    res . end ( v . replace ( / { { ( [ \w . ] + ) } } / g, ( m , r ) => r . split ( '.' ) . reduce ( ( p , o ) => ( p = p && p [ o ] , p ) , badge ) ) ) ; 
384392  } ) ; 
385393} ) ; 
386- var  pioEnv = ( dir ) => 
387-   promisify ( fs . readFile ) ( path . join ( dir , 'platformio.ini' ) , 'utf8' ) 
394+ var  pioEnv   =   ( file )   => 
395+   promisify ( fs . readFile ) ( file ,   'utf8' ) 
388396  . then ( a => a . split ( / \r \n ? | \n / ) ) 
389397  . then ( a => a . map ( i => i . match ( / \[ e n v \: ( .* ) \] / ) ) . filter ( i => i ) . map ( i => i [ 1 ] ) ) 
390398app . get ( '/version/:screen' ,  function  ( req ,  res )  { 
@@ -396,7 +404,7 @@ app.get('/version/:screen', function (req, res) {
396404      ua :req . headers [ 'user-agent' ] , 
397405      ul :req . headers [ 'accept-language' ] . split ( ',' ) [ 0 ] , 
398406    } ) . send ( ) 
399-   Promise . all ( [ pio . isPIO ( ) . catch ( ( ) => false ) , git . root ( ) , git . root ( ) . then ( pioEnv ) ] ) 
407+   Promise . all ( [ pio . isPIO ( ) . catch ( ( )   =>   false ) ,   git . root ( ) ,   pioRoot ( ) . then ( pioEnv ) . catch ( e   =>   [ ] ) ] ) 
400408  . then ( pp => { 
401409    //console.log(a) 
402410    var  cfg = { pio :pp [ 0 ] , version :pjson . version , root :pp [ 1 ] , base :baseCfg , env :pp [ 2 ] } ; 
@@ -408,13 +416,7 @@ app.get('/version/:screen', function (req, res) {
408416/* PIO */ 
409417
410418function  pioRoot ( ) { 
411-   return  git . root ( ) 
412-   . then ( root => 
413-     promisify ( fs . stat ) ( path . join ( root , 'Marlin' , 'platformio.ini' ) ) 
414-     . then ( a => path . join ( root , 'Marlin' ) ) 
415-     . catch ( a => root ) 
416-     . then ( root => ( process . chdir ( root ) , root ) ) 
417-   ) ; 
419+   return  seek4File ( 'platformio.ini' ,  [ '' ,  'Marlin' ] ) 
418420} 
419421app . get ( '/pio/:env' ,  function  ( req ,  res )  { 
420422  params = [ 'run' ] ; 
@@ -481,8 +483,7 @@ function getMatch(reg,data,second){
481483  return  m [ 1 ] ; 
482484} 
483485app . get ( '/bs/default' ,  function  ( req ,  res )  { 
484-   git . root ( ) 
485-   . then ( f => path . join ( f , 'Marlin' , 'dogm_bitmaps.h' ) ) 
486+   return  seek4File ( 'dogm_bitmaps.h' ,  [ 'Marlin' ,  path . join ( 'Marlin' ,  'src' ,  'lcd' ,  'dogm' ) ] ) 
486487  . then ( file => promisify ( fs . readFile ) ( file , 'utf8' ) ) 
487488  . then ( data => { 
488489    var  second = / \/ \/ \s * # d e f i n e \s + S T A R T _ B M P H I G H / g. test ( data ) ; 
@@ -539,6 +540,18 @@ app.get('/gcodes', function (req, res) {
539540
540541/* MAIN */ 
541542
543+ function  getFirstFile ( paths )  { 
544+   if  ( ! paths  ||  paths . length  ==  0 ) 
545+     return  Promise . reject ( ) ; 
546+   var  filePath  =  paths . shift ( ) ; 
547+   return  promisify ( fs . access ) ( filePath ,  fs . constants . R_OK ) 
548+     . then ( a  =>  filePath ) 
549+     . catch ( e  =>  getFirstFile ( paths )  ) ; 
550+ } 
551+ function  seek4File ( file ,  paths ,  rel )  { 
552+   return  git . root ( ) . then ( root  =>  getFirstFile ( paths . map ( i  =>  path . join ( root ,  i ,  file ) ) ) . then ( res  =>  rel  &&  path . relative ( root ,  res )  ||  res ) ) 
553+ } 
554+ 
542555app . get ( '/json/' ,  function  ( req ,  res )  { 
543556  res . set ( 'Content-Type' ,  'application/json' ) ; 
544557  get_cfg ( ) . then ( a => res . send ( a ) ) 
0 commit comments