-
Notifications
You must be signed in to change notification settings - Fork 0
/
packageRoutes.js
55 lines (48 loc) · 1.52 KB
/
packageRoutes.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
const { exec } = require('child_process');
function searchPackages(req, res) {
const appName = req.params.appName;
exec('adb shell pm list packages -f', (error, stdout, stderr) => {
if (error) {
res.status(500).json({ error: 'Failed to retrieve packages' });
return;
}
if (stderr) {
res.status(500).json({ error: stderr });
return;
}
const packageLines = stdout.split('\n');
const packages = packageLines
.map(line => line.trim())
.filter(line => line.length > 0)
.map(line => {
const match = line.match(/^package:(.*)=(.*)$/);
if (match) {
return {
name: match[2],
};
}
return null;
})
.filter(pkg => pkg !== null && pkg.name.toLowerCase().includes(appName.toLowerCase()));
res.json({ packages });
});
}
function checkAdbConnection(callback) {
exec('adb devices', (error, stdout, stderr) => {
if (error) {
callback(error, false);
return;
}
if (stderr) {
callback(new Error(stderr), false);
return;
}
const devicesOutput = stdout.trim().split('\n');
const connected = devicesOutput.length >= 2 && devicesOutput[1].endsWith('\tdevice');
callback(null, connected);
});
}
module.exports = {
searchPackages,
checkAdbConnection
};