@@ -21,6 +21,7 @@ var os = require('os');
2121var  ifaces  =  os . networkInterfaces ( ) ; 
2222var  natUpnp  =  require ( 'nat-upnp' ) ; 
2323var  moment  =  require ( 'moment' ) ; 
24+ var  mkdirp  =  require ( 'mkdirp' ) ; 
2425
2526var  natClient ; 
2627
@@ -252,8 +253,13 @@ app.get('/checkout-force', function (req, res) {
252253        ) . catch ( e  =>  'not found' ) 
253254      ) 
254255    ) ) 
256+   var  rm  =  ( )  => 
257+     seek4File ( '_Bootscreen.h' ,  [ path . join ( 'Marlin' ,  'src' ,  'config' ) ,  'Marlin' ] ) 
258+     . then ( file  =>  file  &&  promisify ( fs . unlink ) ( file ) ) ; 
259+ 
255260  git . Checkout ( '--force' ) 
256-   . then ( a  =>  baseCfg  ==  'Marlin'  &&  a  ||  cp ( ) ) 
261+   . then ( rm ) 
262+   . then ( a  =>  baseCfg  ==  'Marlin'  ? a  : cp ( ) ) 
257263  . then ( a => res . send ( a ) ) ; 
258264} ) ; 
259265app . get ( '/fetch' ,  function  ( req ,  res )  { 
@@ -275,26 +281,19 @@ var copyFile=(from,to)=>
275281  ) ; 
276282app . get ( '/save' ,  function  ( req ,  res )  { 
277283  var  dt = moment ( ) . format ( 'YYYY-MM-DD kk-mm-ss' ) ; 
278-     git . root ( ) 
279-     . then ( root => path . join ( root , store ) ) 
280-     . then ( dir => promisify ( fs . stat ) ( dir ) . catch ( a => promisify ( fs . mkdir ) ( dir ) ) . then ( a => dir ) ) 
281-     . then ( dir => git . Tag ( ) . then ( tag => path . join ( dir , tag ) ) ) 
282-     . then ( dir => promisify ( fs . stat ) ( dir ) . catch ( a => promisify ( fs . mkdir ) ( dir ) ) . then ( a => dir ) ) 
283-     . then ( dir => path . join ( dir , dt ) ) 
284-     . then ( dir => promisify ( fs . stat ) ( dir ) . catch ( a => promisify ( fs . mkdir ) ( dir ) ) . then ( a => dir ) ) 
285-     . then ( dir => git . root ( ) . then ( root => ( { dir :dir , root :root } ) ) ) 
284+     Promise . all ( [ git . root ( ) ,  git . Tag ( ) ] ) 
285+     . then ( p  =>  { 
286+       var  dir  =  path . join ( p [ 0 ] ,  store ,  p [ 1 ] . replace ( '/' ,  path . sep ) ,  dt ) ; 
287+       return  promisify ( mkdirp ) ( dir ) . then ( a  =>  ( { to : dir ,  root : p [ 0 ] } ) ) ; 
288+     } ) 
286289    . then ( dirs => Promise . all ( 
287290        [ 'Configuration.h' , 'Configuration_adv.h' , '_Bootscreen.h' ] 
288-         . map ( file => promisify ( fs . stat ) ( path . join ( dirs . root , 'Marlin' , file ) ) . then ( ( ) => file ) . catch ( ( ) => null ) ) 
291+         . map ( file   =>   seek4File ( file ,   [ 'Marlin' ,   path . join ( 'Marlin' ,   'src' ,   'config' ) ] ) . catch ( ( ) => null ) ) 
289292      ) 
290-       . then ( files => ( { root : dirs . root , files :files . filter ( a => a ) , to : dirs . dir , message :req . query . message } ) ) 
293+       . then ( files   =>   Object . assign ( dirs ,   { files :  files . filter ( a   =>   a ) ,   message :req . query . message } ) ) 
291294    ) 
292295    . then ( dirs => 
293-       Promise . all ( 
294-         dirs . files . map ( f => 
295-           copyFile ( path . join ( dirs . root , 'Marlin' , f ) ,  path . join ( dirs . to , f )  ) 
296-         ) 
297-       ) 
296+       Promise . all ( dirs . files . map ( f  =>  copyFile ( f ,  path . join ( dirs . to ,  path . basename ( f ) ) ) ) ) 
298297      . then ( ( ) => dirs ) 
299298    ) 
300299    . then ( dirs => promisify ( fs . writeFile ) ( path . join ( dirs . to , 'contents.json' ) , JSON . stringify ( dirs , null , 2 ) ) . then ( ( ) => dirs ) ) 
@@ -326,8 +325,9 @@ app.get('/restore/:path', function (req, res) {
326325    . map ( f => ( { path :f , name :path . parse ( f ) . base } ) ) 
327326    var  cp = files 
328327    . filter ( i => / _ B o o t s c r e e n \. h / . test ( i ) ) 
329-     . map ( f => git . root ( ) 
330-       . then ( root => copyFile ( f , path . join ( root , 'Marlin' , path . parse ( f ) . base ) ) ) 
328+     . map ( f  => 
329+       seek4File ( '' ,  [ path . join ( 'Marlin' ,  'src' ,  'config' ) ,  'Marlin' ] ) 
330+       . then ( dir  =>  copyFile ( f ,  path . join ( dir ,  path . basename ( f )  ) ) ) 
331331    ) 
332332    console . log ( cp ) ; 
333333    //return (uploadFiles(up)); 
@@ -489,18 +489,18 @@ app.get('/bs/default', function (req, res) {
489489} ) ; 
490490app . post ( '/bs/custom' ,  function  ( req ,  res )  { 
491491  var  name = '_Bootscreen.h' ; 
492-   Promise 
493-   . resolve ( path . join ( __dirname , '..' , 'views' , name ) ) 
494-   . then ( file => promisify ( fs . readFile ) ( file , 'utf8' ) ) 
495-   . then ( text => text . replace ( / { { ( [ \w . ] + ) } } / g, ( m , r ) => r . split ( '.' ) . reduce ( ( p , o ) => ( p = p && p [ o ] , p ) , req . body ) ) ) 
496-   . then ( file => git . root ( ) . then ( p => promisify ( fs . writeFile ) ( path . join ( p , 'Marlin' , name ) , file ) ) ) 
492+   Promise . all ( [ 
493+     seek4File ( '' ,  [ path . join ( 'Marlin' ,  'src' ,  'config' ) ,  'Marlin' ] ) , 
494+     promisify ( fs . readFile ) ( path . join ( __dirname ,  '..' ,  'views' ,  name ) ,  'utf8' ) 
495+     . then ( text  =>  text . replace ( / { { ( [ \w . ] + ) } } / g,  ( m ,  r )  =>  r . split ( '.' ) . reduce ( ( p ,  o )  =>  ( p  =  p  &&  p [ o ] ,  p ) ,  req . body ) ) ) 
496+   ] ) 
497+   . then ( p  =>  promisify ( fs . writeFile ) ( path . join ( p [ 0 ] ,  name ) ,  p [ 1 ] ) ) 
497498  . then ( a => res . end ( 'writed' ) ) 
498499  . catch ( e => res . status ( 403 ) . send ( e ) ) 
499500} ) ; 
500501app . get ( '/bs/custom' ,  function  ( req ,  res )  { 
501-   git . root ( ) 
502-   . then ( f => path . join ( f , 'Marlin' , '_Bootscreen.h' ) ) 
503-   . then ( file => promisify ( fs . readFile ) ( file , 'utf8' ) ) 
502+   seek4File ( '_Bootscreen.h' ,  [  'Marlin' ,  path . join ( 'Marlin' ,  'src' ,  'config' ) ] ) 
503+   . then ( file  =>  promisify ( fs . readFile ) ( file ,  'utf8' ) ) 
504504  . then ( data => { 
505505    var  d = getMatch ( / { ( ( [ ^ } ] | \r \n ? | \n ) * ) } / g, data ) ; 
506506    d = d . replace ( / \n / g, '' ) . replace ( /   / g, '' ) ; 
0 commit comments