-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
executable file
·154 lines (130 loc) · 3.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#! /usr/bin/env node
const { join } = require("path")
const { init } = require("./src/adb")
const { existsSync } = require("fs")
const { cli } = require("./src/cli")
const { normalize } = require("path")
const { recordVideo } = require("./src/recordVideo")
const { takeScreenshot } = require("./src/takeScreenshot")
const open = require("open")
const { bold, gray } = require("kleur")
const { selectOption } = require("./src/selectOption")
const logUpdate = require("log-update")
const { fail } = require("./src/log")
const version = require(join(__dirname, "./package.json")).version
console.log(bold().gray("android-capture"), gray(version), "\n")
/**
* @type {Record<string, boolean>}
*/
const videoTypes = {
video: true,
movie: true,
mp4: true,
}
/**
* @type {Record<string, boolean>}
*/
const imageTypes = {
image: true,
screenshot: true,
still: true,
picture: true,
png: true,
}
/**
* @param {any} message
*/
function error(message) {
fail(message, "\nRun with", bold("--help"), "for usage information.")
}
async function run() {
try {
const [type = "", filename, ...others] = cli.input
/**
* @type {"video" | "image" | null}
*/
let mode =
type in imageTypes ? "image" : type in videoTypes ? "video" : null
if (!mode) {
const idx = await selectOption("What do you want to capture?", [
"Video",
"Screenshot",
])
mode = idx === 0 ? "video" : "image"
logUpdate(
`💡 Hint! In the future you can run ${bold(
`npx android-capture ${mode}`,
)}\n`,
)
logUpdate.done()
await new Promise((r) => setTimeout(r, 1000))
}
if (others.length) {
error("Unexpected arguements: " + others.join(" "))
}
const extension = mode === "image" ? "png" : "mp4"
const defaultFilename =
mode === "image" ? "android-screenshot" : "android-video"
const outPath = normalize(
sanitizeFilename(filename, extension) ||
generateFilename(defaultFilename, extension),
)
if (cli.flags.copy && mode === "video") {
error(`The ${bold("--copy")} option does not work with video.`)
}
const useTemporaryFile = !filename && cli.flags.copy && !cli.flags.open
await init()
if (mode === "video") {
await recordVideo(outPath)
} else {
await takeScreenshot(outPath, useTemporaryFile)
}
if (cli.flags.open) {
open(outPath)
}
process.exit(0)
} catch (e) {
console.error(e)
process.exit(1)
}
}
/**
* @param {string} name
* @param {string} extension
*/
function generateFilename(name, extension) {
const date = new Date()
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
let filename = `./${name}.${year}-${month}-${day}.00.${extension}`
let i = 1
while (existsSync(filename)) {
filename = `./${name}.${year}-${month}-${day}.${i
.toString()
.padStart(2, "0")}.${extension}`
i++
}
return filename
}
/**
*
* @param {string | null | undefined} filename
* @param {string} extension
*/
function sanitizeFilename(filename, extension) {
if (!filename) {
return null
}
if (!filename.endsWith("." + extension)) {
return filename + "." + extension
} else {
return filename
}
}
run()
/**
* TODO:
* - add screenshot capturing
* - rename to android-capture
*/