@@ -3,45 +3,72 @@ const { join } = require("path")
33const chalk = require ( "kleur" )
44const { init } = require ( "./src/adb" )
55const { existsSync } = require ( "fs" )
6- const { countdown } = require ( "./src/countdown" )
76const { cli } = require ( "./src/cli" )
87const { normalize } = require ( "path" )
9- const { setVisibleTouches, getVisibleTouches } = require ( "./src/visibleTouches" )
108const { recordVideo } = require ( "./src/recordVideo" )
9+ const { takeScreenshot } = require ( "./src/takeScreenshot" )
10+ const open = require ( "open" )
1111
1212const version = require ( join ( __dirname , "./package.json" ) ) . version
1313
14- console . log ( chalk . bold ( "record-android-screen" ) , version , "\n" )
14+ console . log ( chalk . bold ( "android-capture" ) , version , "\n" )
15+
16+ /**
17+ * @type {Record<string, boolean> }
18+ */
19+ const videoTypes = {
20+ video : true ,
21+ movie : true ,
22+ mp4 : true ,
23+ }
24+
25+ /**
26+ * @type {Record<string, boolean> }
27+ */
28+ const imageTypes = {
29+ image : true ,
30+ screenshot : true ,
31+ still : true ,
32+ picture : true ,
33+ png : true ,
34+ }
1535
1636async function run ( ) {
1737 try {
18- const [ filename , ...others ] = cli . input
38+ const [ type = "" , filename , ...others ] = cli . input
39+
40+ const mode =
41+ type in imageTypes ? "image" : type in videoTypes ? "video" : null
42+
43+ if ( ! mode ) {
44+ cli . showHelp ( 1 )
45+ }
1946
2047 if ( others . length ) {
2148 console . error ( "Unexpected arguements: " , others . join ( " " ) )
2249 cli . showHelp ( 1 )
2350 }
2451
25- const outPath = normalize ( sanitizeFilename ( filename ) ?? generateFilename ( ) )
26-
27- await init ( )
28- await countdown ( )
52+ const extension = mode === "image" ? "png" : "mp4"
53+ const defaultFilename =
54+ mode === "image" ? "android-screenshot" : "android-video"
2955
30- const hasVisibleTouchesByDefault = getVisibleTouches ( )
56+ const outPath = normalize (
57+ sanitizeFilename ( filename , extension ) ??
58+ generateFilename ( defaultFilename , extension ) ,
59+ )
3160
32- setVisibleTouches ( true )
61+ await init ( )
3362
34- try {
35- const result = await recordVideo ( outPath )
36- if ( ! result . escaped ) {
37- console . log ( "That's a wrap!" , chalk . bold ( outPath ) )
38- }
39- console . log ( )
40- } catch ( e ) {
41- console . error ( "failed in main loop" , e )
63+ if ( mode === "video" ) {
64+ await recordVideo ( outPath )
65+ } else {
66+ await takeScreenshot ( outPath )
4267 }
4368
44- setVisibleTouches ( hasVisibleTouchesByDefault )
69+ if ( cli . flags . open ) {
70+ open ( outPath )
71+ }
4572
4673 process . exit ( 0 )
4774 } catch ( e ) {
@@ -50,18 +77,22 @@ async function run() {
5077 }
5178}
5279
53- function generateFilename ( ) {
80+ /**
81+ * @param {string } name
82+ * @param {string } extension
83+ */
84+ function generateFilename ( name , extension ) {
5485 const date = new Date ( )
5586 const year = date . getFullYear ( )
5687 const month = date . getMonth ( ) + 1
5788 const day = date . getDate ( )
5889
59- let filename = `./recording .${ year } -${ month } -${ day } .00.mp4 `
90+ let filename = `./${ name } .${ year } -${ month } -${ day } .00.${ extension } `
6091 let i = 1
6192 while ( existsSync ( filename ) ) {
62- filename = `./recording .${ year } -${ month } -${ day } .${ i
93+ filename = `./${ name } .${ year } -${ month } -${ day } .${ i
6394 . toString ( )
64- . padStart ( 2 , "0" ) } .mp4 `
95+ . padStart ( 2 , "0" ) } .${ extension } `
6596 i ++
6697 }
6798 return filename
@@ -70,13 +101,14 @@ function generateFilename() {
70101/**
71102 *
72103 * @param {string | null | undefined } filename
104+ * @param {string } extension
73105 */
74- function sanitizeFilename ( filename ) {
106+ function sanitizeFilename ( filename , extension ) {
75107 if ( ! filename ) {
76108 return null
77109 }
78- if ( ! filename . endsWith ( ".mp4" ) ) {
79- return filename + ".mp4"
110+ if ( ! filename . endsWith ( "." + extension ) ) {
111+ return filename + "." + extension
80112 } else {
81113 return filename
82114 }
0 commit comments