190 lines
7.4 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-28 11:24:10 +01:00
let adbRun
2020-04-25 23:07:43 +01:00
2020-04-24 21:40:03 +01:00
module.exports = {
2020-04-27 12:40:59 +01:00
removeCompatibleApps: async () => {
let installedAppList
common.header('Remove Installed Apps')
await shellExec(adbRun + ' shell pm list packages -3').then(async function (result) {
2020-04-28 13:02:35 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
installedAppList = result.stdout.split('\r\n'); // split string on comma space
} else {
installedAppList = result.stdout.split('\n'); // split string on comma space
}
installedAppList.splice(-1, 1)
2020-04-27 12:40:59 +01:00
});
const value = await inquirer.installedApps(installedAppList);
2020-04-27 12:41:12 +01:00
for (let element of value.removeAppsList) {
console.log('Removing ' + element)
const package = element.substring(8)
await shellExec(adbRun + ' uninstall ' + package).then(async function (result) {
2020-04-28 11:24:10 +01:00
console.log(element + ' - ' + result.stdout);
});
2020-04-27 12:40:59 +01:00
}
console.log(chalk.green('Removed Selected User Apps'))
2020-04-27 12:40:59 +01:00
await common.pause(2000)
module.exports.mainMenu()
},
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-28 11:24:10 +01:00
await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
console.log(element + ' - ' + result.stdout);
if (element === "data\\apps\\MoreLocale.apk") {
await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(function (result) {
console.log('moreLocale Activated On Watch');
});
}
});
2020-04-27 12:24:32 +01:00
}
console.log(chalk.green('Compatible Apps Installed'))
await common.pause(2000)
module.exports.mainMenu()
},
removeApps: async () => {
2020-04-25 10:44:12 +01:00
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-28 11:24:10 +01:00
await shellExec(adbRun + ' 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-27 12:24:32 +01:00
common.header('Restore Apps')
const value = await inquirer.removeAppsList();
for (let element of value.removeAppsList) {
2020-04-28 11:24:10 +01:00
await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
console.log('Restoring ' + element + ' - ' + result.stdout);
});
2020-04-27 12:24:32 +01:00
}
console.log(chalk.green('Restore Complete'))
await common.pause(2000)
module.exports.mainMenu()
},
connectWifi: async () => {
const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
common.header('Connect Wifi')
if (miwatchData.ipAddress !== "") {
console.log('Trying to connect with stored ipAddress')
2020-04-28 11:24:10 +01:00
shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
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 {
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-27 12:24:32 +01:00
} else {
const value = await inquirer.connectWifi();
const miWatchIpaddress = value.connectWifi
2020-04-28 11:24:10 +01:00
shellExec(adbRun + ' 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 {
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-27 12:24:32 +01:00
}
},
mainMenu: async () => {
common.header('Main Menu')
2020-04-28 11:24:10 +01:00
if (process.platform === 'win32' || process.platform === 'win64') {
adbRun = 'adb'
} else {
adbRun = './adb'
}
2020-04-27 12:24:32 +01:00
const mainMenuSelection = await inquirer.mainMenu();
switch (mainMenuSelection.mainMenu) {
case 'connect to miwatch via wifi':
module.exports.connectWifi()
break;
case 'remove xiaomi apps':
module.exports.removeApps()
break;
case 'restore xiaomi apps':
module.exports.restoreApps()
break;
case 'install compatible apps':
module.exports.compatibleApps()
break;
case 'remove installed apps':
2020-04-27 12:40:59 +01:00
module.exports.removeCompatibleApps()
2020-04-28 11:24:10 +01:00
break;
2020-04-27 12:24:32 +01:00
case 'quit':
break;
default:
2020-04-28 13:02:35 +01:00
// code block
2020-04-27 12:24:32 +01:00
}
}
2020-04-24 21:40:03 +01:00
};