codeplay-common 4.3.3 → 4.3.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/files/take-screenshot.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
const fs = require('node:fs');
|
|
2
|
-
const path = require('node:path');
|
|
3
|
-
const readline = require('node:readline');
|
|
4
|
-
const { spawnSync } = require('node:child_process');
|
|
5
|
-
|
|
6
|
-
const screenshotDirectory = path.join(__dirname, 'Auto-Screenshot');
|
|
7
|
-
|
|
8
|
-
function getNextNumberedPath() {
|
|
9
|
-
const highestNumber = fs.readdirSync(screenshotDirectory, { withFileTypes: true })
|
|
10
|
-
.filter((entry) => entry.isFile())
|
|
11
|
-
.reduce((highest, entry) => {
|
|
12
|
-
const match = entry.name.match(/^(\d+)\.png$/i);
|
|
13
|
-
return match ? Math.max(highest, Number(match[1])) : highest;
|
|
14
|
-
}, 0);
|
|
15
|
-
|
|
16
|
-
return path.join(screenshotDirectory, `${highestNumber + 1}.png`);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
function getCustomPath(input) {
|
|
20
|
-
let fileName = input.trim().replace(/\.png$/i, '');
|
|
21
|
-
fileName = fileName
|
|
22
|
-
.replace(/[<>:"/\\|?*\u0000-\u001F]/g, '_')
|
|
23
|
-
.replace(/[. ]+$/g, '');
|
|
24
|
-
|
|
25
|
-
if (!fileName) {
|
|
26
|
-
return getNextNumberedPath();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (/^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i.test(fileName)) {
|
|
30
|
-
fileName = `${fileName}_`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let screenshotPath = path.join(screenshotDirectory, `${fileName}.png`);
|
|
34
|
-
let duplicateNumber = 2;
|
|
35
|
-
|
|
36
|
-
while (fs.existsSync(screenshotPath)) {
|
|
37
|
-
screenshotPath = path.join(
|
|
38
|
-
screenshotDirectory,
|
|
39
|
-
`${fileName}-${duplicateNumber}.png`,
|
|
40
|
-
);
|
|
41
|
-
duplicateNumber += 1;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
return screenshotPath;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function takeScreenshot(fileNameInput) {
|
|
48
|
-
const result = spawnSync('adb', ['exec-out', 'screencap', '-p'], {
|
|
49
|
-
encoding: null,
|
|
50
|
-
maxBuffer: 100 * 1024 * 1024,
|
|
51
|
-
timeout: 30000,
|
|
52
|
-
windowsHide: true,
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
if (result.error) {
|
|
56
|
-
if (result.error.code === 'ENOENT') {
|
|
57
|
-
throw new Error('ADB was not found. Add Android platform-tools to your PATH.');
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (result.error.code === 'ETIMEDOUT') {
|
|
61
|
-
throw new Error('ADB did not respond within 30 seconds. Check the device connection.');
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
throw result.error;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
if (result.status !== 0) {
|
|
68
|
-
const adbMessage = result.stderr.toString().trim();
|
|
69
|
-
throw new Error(adbMessage || `ADB exited with code ${result.status}.`);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (!result.stdout.length) {
|
|
73
|
-
throw new Error('ADB returned an empty screenshot. Check the connected device.');
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
const screenshotPath = fileNameInput.trim()
|
|
77
|
-
? getCustomPath(fileNameInput)
|
|
78
|
-
: getNextNumberedPath();
|
|
79
|
-
|
|
80
|
-
fs.writeFileSync(screenshotPath, result.stdout, { flag: 'wx' });
|
|
81
|
-
return screenshotPath;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
fs.mkdirSync(screenshotDirectory, { recursive: true });
|
|
85
|
-
|
|
86
|
-
const terminal = readline.createInterface({
|
|
87
|
-
input: process.stdin,
|
|
88
|
-
output: process.stdout,
|
|
89
|
-
prompt: 'Enter a filename, or press Enter for the next number: ',
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
console.log(`Screenshots will be saved in: ${screenshotDirectory}`);
|
|
93
|
-
console.log('Press Ctrl+C to exit.');
|
|
94
|
-
terminal.prompt();
|
|
95
|
-
|
|
96
|
-
terminal.on('line', (fileNameInput) => {
|
|
97
|
-
try {
|
|
98
|
-
const screenshotPath = takeScreenshot(fileNameInput);
|
|
99
|
-
console.log(`Screenshot saved: ${screenshotPath}`);
|
|
100
|
-
} catch (error) {
|
|
101
|
-
console.error(`Screenshot failed: ${error.message}`);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
terminal.prompt();
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
terminal.on('SIGINT', () => {
|
|
108
|
-
console.log('\nScreenshot tool closed.');
|
|
109
|
-
terminal.close();
|
|
110
|
-
});
|