204 lines
8.6 KiB
JavaScript
Raw Normal View History

2020-04-24 21:40:03 +01:00
const chalk = require('chalk');
const common = require('../lib/common');
const inquirer = require('../lib/inquirer');
2020-04-24 22:20:41 +01:00
const shellExec = require('shell-exec')
2020-04-25 10:27:17 +01:00
const files = require('../lib/files')
2020-04-25 14:54:31 +01:00
const fs = require('fs')
2020-04-25 23:07:43 +01:00
const {
DownloaderHelper
} = require('node-downloader-helper');
2020-04-26 09:39:33 +01:00
const getFilesIn = require('get-files-in')
2020-04-26 10:10:29 +01:00
const http = require('http')
2020-04-26 11:08:29 +01:00
var shell = require('shelljs');
2020-04-25 23:07:43 +01:00
2020-04-24 21:40:03 +01:00
module.exports = {
2020-04-25 15:35:15 +01:00
compatibleApps: async () => {
common.header('Install Compatible Apps')
2020-04-26 10:10:29 +01:00
let compatibleApps
let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
http.get(url, (res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
compatibleApps = JSON.parse(body);
// do something with JSON
} catch (error) {
console.error(error.message);
};
});
2020-04-25 21:28:13 +01:00
2020-04-26 10:10:29 +01:00
}).on("error", (error) => {
console.error(error.message);
2020-04-26 09:39:33 +01:00
});
2020-04-26 10:10:29 +01:00
const value = await inquirer.compatibleApps();
2020-04-26 11:08:29 +01:00
await shell.rm('-rf', './data/apps/*.apk');
2020-04-26 10:10:29 +01:00
2020-04-25 16:26:34 +01:00
for (let element of value.removeAppsList) {
2020-04-25 16:56:23 +01:00
for (let element2 of compatibleApps) {
if (element === element2.name) {
2020-04-25 23:07:43 +01:00
const options = {
override: true,
}
const dl = new DownloaderHelper(element2.url, './data/apps/', options);
dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'))
await dl.start();
2020-04-25 16:56:23 +01:00
}
}
2020-04-25 16:26:34 +01:00
}
2020-04-26 09:39:33 +01:00
const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
for (let element of apkList) {
2020-04-26 10:10:29 +01:00
console.log('Installing ' + element)
2020-04-26 12:24:50 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
await shellExec('adb install -r ' + element).then(function (result) {
console.log(element + ' - ' + result.stdout);
});
} else {
await shellExec('./adb install -r ' + element).then(function (result) {
console.log(element + ' - ' + result.stdout);
});
}
2020-04-26 09:39:33 +01:00
}
console.log(chalk.green('Compatible Apps Installed'))
await common.pause(2000)
module.exports.mainMenu()
2020-04-25 15:35:15 +01:00
},
2020-04-25 10:44:12 +01:00
removeApps: async () => {
common.header('Remove Apps')
2020-04-25 14:21:45 +01:00
const value = await inquirer.removeAppsList();
for (let element of value.removeAppsList) {
2020-04-26 12:24:50 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
await shellExec('adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
console.log('Removing ' + element + ' - ' + result.stdout);
});
} else {
await shellExec('./adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
console.log('Removing ' + element + ' - ' + result.stdout);
});
}
2020-04-25 14:21:45 +01:00
}
console.log(chalk.green('Removal Complete'))
await common.pause(2000)
module.exports.mainMenu()
2020-04-25 11:06:05 +01:00
},
restoreApps: async () => {
2020-04-25 15:37:31 +01:00
common.header('Restore Apps')
2020-04-25 11:06:05 +01:00
const value = await inquirer.removeAppsList();
2020-04-25 14:21:45 +01:00
for (let element of value.removeAppsList) {
2020-04-26 12:24:50 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
await shellExec('adb shell cmd package install-existing ' + element).then(function (result) {
console.log('Restoring ' + element + ' - ' + result.stdout);
});
} else {
await shellExec('./adb shell cmd package install-existing ' + element).then(function (result) {
console.log('Restoring ' + element + ' - ' + result.stdout);
});
}
2020-04-25 14:21:45 +01:00
}
console.log(chalk.green('Restore Complete'))
await common.pause(2000)
module.exports.mainMenu()
2020-04-25 10:44:12 +01:00
},
2020-04-24 21:40:03 +01:00
connectWifi: async () => {
2020-04-25 14:54:31 +01:00
const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
2020-04-24 22:55:00 +01:00
common.header('Connect Wifi')
2020-04-25 14:54:31 +01:00
if (miwatchData.ipAddress !== "") {
2020-04-25 15:09:41 +01:00
console.log('Trying to connect with stored ipAddress')
2020-04-26 12:24:50 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
shellExec('adb connect ' + miwatchData.ipAddress).then(async function (result) {
2020-04-27 11:52:09 +01:00
if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
console.log(chalk.green('MiWatch Connected'))
await common.pause(3000)
module.exports.mainMenu()
} else {
2020-04-26 12:24:50 +01:00
console.log(chalk.red('MiWatch not found'))
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWifi()
}
}).catch()
} else {
shellExec('./adb connect ' + miwatchData.ipAddress).then(async function (result) {
2020-04-27 11:52:09 +01:00
if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
console.log(chalk.green('MiWatch Connected'))
files.writeIpAddress(miWatchIpaddress)
await common.pause(3000)
module.exports.mainMenu()
} else {
2020-04-26 12:24:50 +01:00
console.log(chalk.red('MiWatch not found'))
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWifi()
}
}).catch()
}
2020-04-25 14:54:31 +01:00
} else {
const value = await inquirer.connectWifi();
const miWatchIpaddress = value.connectWifi
2020-04-26 12:24:50 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
shellExec('adb connect ' + miWatchIpaddress).then(async function (result) {
2020-04-27 11:52:09 +01:00
if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
2020-04-26 12:24:50 +01:00
console.log(chalk.green('MiWatch Connected'))
files.writeIpAddress(miWatchIpaddress)
await common.pause(3000)
module.exports.mainMenu()
2020-04-27 11:52:09 +01:00
} else {
2020-04-26 12:24:50 +01:00
console.log(chalk.red('MiWatch not found'))
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWifi()
2020-04-27 11:52:09 +01:00
}
}).catch()
} else {
shellExec('./adb connect ' + miWatchIpaddress).then(async function (result) {
if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
console.log(chalk.green('MiWatch Connected'))
files.writeIpAddress(miWatchIpaddress)
await common.pause(3000)
module.exports.mainMenu()
} else {
2020-04-26 12:24:50 +01:00
console.log(chalk.red('MiWatch not found'))
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWifi()
}
}).catch()
}
2020-04-25 14:54:31 +01:00
}
2020-04-24 22:20:41 +01:00
},
mainMenu: async () => {
2020-04-24 22:55:00 +01:00
common.header('Main Menu')
2020-04-24 22:20:41 +01:00
const mainMenuSelection = await inquirer.mainMenu();
switch (mainMenuSelection.mainMenu) {
2020-04-24 22:55:00 +01:00
case 'connect to miwatch via wifi':
module.exports.connectWifi()
break;
2020-04-25 11:11:10 +01:00
case 'remove xiaomi apps':
2020-04-25 10:44:12 +01:00
module.exports.removeApps()
2020-04-24 22:55:00 +01:00
break;
2020-04-25 11:11:10 +01:00
case 'restore xiaomi apps':
2020-04-25 11:06:05 +01:00
module.exports.restoreApps()
break;
2020-04-25 15:35:15 +01:00
case 'install compatible apps':
module.exports.compatibleApps()
break;
2020-04-25 14:54:31 +01:00
case 'quit':
break;
2020-04-24 22:55:00 +01:00
default:
// code block
2020-04-24 22:20:41 +01:00
}
2020-04-24 22:55:00 +01:00
}
2020-04-24 21:40:03 +01:00
};