/
main.js
89 lines (72 loc) · 1.99 KB
/
main.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
const path = require('path');
const assetsDirectory = path.join(__dirname, 'assets');
const {app, Tray, Menu, dialog} = require('electron');
const notifier = require('node-notifier');
const log = console.log;
const exec = require('child_process').execFile;
let tray = undefined;
app.dock.hide();
app.on('ready', () => {
createTray();
});
const createTray = () => {
tray = new Tray(path.join(assetsDirectory, 'briefcase.png'));
tray.setToolTip('Drag APK to install');
var contextMenu = Menu.buildFromTemplate([
{
label: 'quit', click: () => {
app.isQuiting = true;
app.quit();
}
}]);
tray.setContextMenu(contextMenu);
tray.on('drag-enter', () => {
tray.setImage(path.join(assetsDirectory, 'briefcase-yellow.png'));
});
tray.on('drag-leave', () => {
tray.setImage(path.join(assetsDirectory, 'briefcase.png'));
});
tray.on('right-click', () => {
notification('Cesar loves you ❤');
});
tray.on('drop-files', (event, files) => {
tray.setImage(path.join(assetsDirectory, 'briefcase.png'))
files.forEach(item => {
handleFileItem(item);
});
});
}
function handleFileItem(item) {
if(!isAPK(item)) {
notification('This is not an APK');
return;
}
log('Starting installation...');
const options = { timeout: 4000 };
exec('adb', ['install', '-r', item], options, (err, stdout) => {
if(err) {
var errorMessage = 'Something went wrong while installing';
const sanitizedError = String(`${err}`);
log(sanitizedError);
if(sanitizedError.indexOf('no devices/emulators found') > -1) {
errorMessage = 'No devices/emulators found';
}
notification(errorMessage);
log('Finished installation');
return;
}
notification(`${path.basename(item)} - Installed successfully`);
log('Finished installation');
});
}
function isAPK(file) {
return path.extname(file) === '.apk';
}
function notification(message) {
log('Notifying: '+message);
notifier.notify({
title: 'APK installer',
message,
timeout: 3,
});
}