1- const  {  spawn }  =  require ( "child_process" ) 
21const  {  join }  =  require ( "path" ) 
32
43const  chalk  =  require ( "kleur" ) 
5- const  {  spawnSafeSync }  =  require ( "./src/spawnSafeSync" ) 
6- const  {  getDevice }  =  require ( "./src/getDevice" ) 
4+ const  {  init,  adb,  adbAsync }  =  require ( "./src/adb" ) 
5+ const  {  existsSync }  =  require ( "fs" ) 
6+ const  {  countdown }  =  require ( "./src/countdown" ) 
77
88const  version  =  require ( join ( __dirname ,  "./package.json" ) ) . version 
99
@@ -18,31 +18,25 @@ process.stdin.resume()
1818process . stdin . setEncoding ( "utf8" ) 
1919
2020/** 
21-  * @returns  {"1" | "0" } 
21+  * @returns  {boolean } 
2222 */ 
2323function  getVisibleTouches ( )  { 
24-   // @ts -ignore 
25-   return  spawnSafeSync ( "adb" ,  [ 
26-     "shell" , 
27-     "settings" , 
28-     "get" , 
29-     "system" , 
30-     "show_touches" , 
31-   ] ) . stdout . toString ( ) 
24+   return  ( 
25+     adb ( 
26+       "shell" , 
27+       "settings" , 
28+       "get" , 
29+       "system" , 
30+       "show_touches" , 
31+     ) . stdout . toString ( )  ===  "1" 
32+   ) 
3233} 
3334
3435/** 
3536 * @param  {boolean } visible 
3637 */ 
3738function  setVisibleTouches ( visible )  { 
38-   spawnSafeSync ( "adb" ,  [ 
39-     "shell" , 
40-     "settings" , 
41-     "put" , 
42-     "system" , 
43-     "show_touches" , 
44-     visible  ? "1"  : "0" , 
45-   ] ) 
39+   adb ( "shell" ,  "settings" ,  "put" ,  "system" ,  "show_touches" ,  visible  ? "1"  : "0" ) 
4640} 
4741
4842const  ESCAPE_KEYS  =  { 
@@ -64,12 +58,12 @@ const ENTER_KEYS = {
6458
6559/** 
6660 * @param  {string } outFile 
67-  * @returns  {Promise<void > } 
61+  * @returns  {Promise<{escaped: boolean} > } 
6862 */ 
6963function  recordVideo ( outFile )  { 
7064  return  new  Promise ( ( resolve ,  reject )  =>  { 
7165    const  internalFilePath  =  `/sdcard/android-screen-recording.mp4` 
72-     const  recordProc  =  spawn ( "adb" ,   [ " shell",  "screenrecord" ,  internalFilePath ] ) 
66+     const  recordProc  =  adbAsync ( " shell",  "screenrecord" ,  internalFilePath ) 
7367    let  err  =  "" 
7468    recordProc . stderr . on ( "data" ,  ( data )  =>  { 
7569      err  +=  data . toString ( ) 
@@ -82,13 +76,17 @@ function recordVideo(outFile) {
8276      } 
8377      process . stdin . removeListener ( "data" ,  handleKeyPress ) 
8478      console . log ( "\n         " ,  chalk . green ( "✔" ) ,  "Cut! 🎬" ,  "\n" ) 
85-       console . log ( "Transferring video from phone...\n" ) 
86-       await  new  Promise ( ( r )  =>  setTimeout ( r ,  2000 ) ) 
87-       spawnSafeSync ( "adb" ,  [ "pull" ,  internalFilePath ,  outFile ] ) 
88-       spawnSafeSync ( "adb" ,  [ "shell" ,  "rm" ,  internalFilePath ] ) 
89-       resolve ( ) 
79+       if  ( ! escaped )  { 
80+         console . log ( "Transferring video from phone...\n" ) 
81+         await  new  Promise ( ( r )  =>  setTimeout ( r ,  2000 ) ) 
82+         adb ( "pull" ,  internalFilePath ,  outFile ) 
83+       }  else  { 
84+         console . log ( "Cancelling..." ) 
85+       } 
86+       adb ( "shell" ,  "rm" ,  internalFilePath ) 
87+       resolve ( {  escaped } ) 
9088    } ) 
91-     console . log ( "\n          " ,  chalk . red ( "⦿" ) ,  "Recording..." ,  "\n" ) 
89+     console . log ( "         " ,  chalk . red ( "⦿" ) ,  "Recording..." ,  "\n" ) 
9290    printCallToAction ( ) 
9391
9492    // on any data into stdin 
@@ -134,24 +132,42 @@ function printCallToAction() {
134132} 
135133
136134async  function  run ( )  { 
137-   const  device  =  await  getDevice ( ) 
138-   console . log ( { device} ) 
139-   process . exit ( 1 ) 
140-   const  outPath  =  "./recording.mp4" 
141-   setVisibleTouches ( true ) 
142- 
143135  try  { 
144-     await  recordVideo ( outPath ) 
145-     console . log ( "That's a wrap!" ,  chalk . bold ( outPath ) ) 
146-     console . log ( ) 
136+     await  init ( ) 
137+     await  countdown ( ) 
138+ 
139+     const  date  =  new  Date ( ) 
140+     const  year  =  date . getFullYear ( ) 
141+     const  month  =  date . getMonth ( )  +  1 
142+     const  day  =  date . getDate ( ) 
143+ 
144+     let  outPath  =  `./recording.${ year }  -${ month }  -${ day }  .00.mp4` 
145+     let  i  =  1 
146+     while  ( existsSync ( outPath ) )  { 
147+       outPath  =  `./recording.${ year }  -${ month }  -${ day }  .${ i  
148+         . toString ( )  
149+         . padStart ( 2 ,  "0" ) }  .mp4`
150+       i ++ 
151+     } 
152+     setVisibleTouches ( true ) 
153+ 
154+     try  { 
155+       const  result  =  await  recordVideo ( outPath ) 
156+       if  ( ! result . escaped )  { 
157+         console . log ( "That's a wrap!" ,  chalk . bold ( outPath ) ) 
158+       } 
159+       console . log ( ) 
160+     }  catch  ( e )  { 
161+       console . error ( "failed in main loop" ,  e ) 
162+     } 
163+ 
164+     setVisibleTouches ( false ) 
165+ 
166+     process . exit ( 0 ) 
147167  }  catch  ( e )  { 
148168    console . error ( e ) 
169+     process . exit ( 1 ) 
149170  } 
150- 
151-   setVisibleTouches ( false ) 
152- 
153-   process . exit ( 0 ) 
154171} 
155172
156173run ( ) 
157- // getVisibleTouches() 
0 commit comments