lost of deduplication and code cleanup

This commit is contained in:
karl.hudgell 2020-08-28 15:29:38 +01:00
parent 73ee55d913
commit 1c47cbcd02
5 changed files with 143 additions and 151 deletions

View File

@ -1,7 +1,6 @@
{
"apps": [
"com.android.messaging",
"com.eg.android.AlipayGphone",
"com.google.android.clockwork.flashlight",
"com.google.android.clockwork.gestures.tutorial",
"com.google.android.clockwork.lesetup",
@ -12,7 +11,6 @@
"com.google.android.wearable.overlay.common.baiji",
"com.google.android.wearable.overlay.helium.baiji",
"com.google.android.wearable.overlay.home.baiji",
"com.google.android.wearable.overlay.home.baiji",
"com.google.android.wearable.overlay.home.baiji.tiles",
"com.gotokeep.keep",
"com.onetrack.watch",

View File

@ -22,6 +22,7 @@
"remove_installed_apps_failed": "Remove Installed Apps Failed",
"removing": "Removing",
"installing": "Installing",
"restoring": "Restoring",
"remove_selected_user_apps": "Removed Selected User Apps",
"install_compatible_apps_header": "Install Compatible Apps",
"simple_weather_activated_on_watch": "simpleWeather Activated On Watch",

View File

@ -4,7 +4,9 @@ const shellExec = require('shell-exec')
const Language = require("@shypes/language-translator");
const inquirer = require('../lib/inquirer');
const common = require('./common')
const chalk = require('chalk');
const globalVariables = require('../lib/globalVars');
const { removeAppsList } = require('../lib/inquirer');
if (process.platform === 'win32' || process.platform === 'win64') {
adbRun = 'adb'
@ -13,10 +15,6 @@ if (process.platform === 'win32' || process.platform === 'win64') {
}
module.exports = {
getListOfAPk: () => {
const result = gfin('./data/apps', matchFiletypes = ["apk"], checkSubDirectories = false)
return result
},
installApk: async (element) => {
result = await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
if (result.stderr != '') {
@ -52,8 +50,46 @@ module.exports = {
},
removeApk: async (package) => {
result = await shellExec(adbRun + ' uninstall ' + package)
console.log(package + ' - ' + result.stdout);
logger.info(package + ' - ' + result.stdout);
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Removing ' + package + ' - ' + result.stdout);
console.log('Removing ' + package + ' - ' + result.stdout);
}
},
removeXiaomiApk: async (package) => {
result = await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + package)
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Removing ' + package + ' - ' + result.stdout);
console.log('Removing ' + package + ' - ' + result.stdout);
}
},
restoreXiaomiApk: async (package) => {
result = await shellExec(adbRun + ' shell cmd package install-existing ' + package)
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Removing ' + package + ' - ' + result.stdout);
console.log('Removing ' + package + ' - ' + result.stdout);
}
},
restoreAnyApk: async (package) => {
result = await shellExec(adbRun + ' shell cmd package install-existing ' + package.restoreAnyApp)
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else if (result.stdout.includes('doesn\'t exist')) {
logger.info(result.stdout);
console.log(chalk.redBright(result.stdout));
} else {
logger.info('Restoring ' + package.restoreAnyApp + ' - ' + result.stdout);
console.log('Restoring ' + package.restoreAnyApp + ' - ' + result.stdout);
}
},
getInstalledPacakges: async () => {
result = await shellExec(adbRun + ' shell pm list packages -3')
@ -82,4 +118,57 @@ module.exports = {
return value
}
},
killAdbServer: async () => {
let result = await shellExec(adbRun + ' kill-server')
logger.info('Restarting ADB')
logger.info(result.stdout)
},
watchConnection: async (value) => {
if (value.connection === "usb") {
await module.exports.killAdbServer()
result = await shellExec(adbRun + ' devices')
console.log(result.stdout)
if (result.stdout.includes('device', 15)) {
console.log(chalk.green('MiWatch Connected via USB'))
await common.pause(3000)
logger.info("MiWatch connected")
globalVariables.localUSB = "X"
return true
} else {
console.log(chalk.red('MiWatch not found'))
logger.info("MiWatch not found")
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
return false
}
}
if (value.connection === "wifi") {
const value = await inquirer.connectWifi();
await module.exports.killAdbServer()
result = await shellExec(adbRun + ' connect ' + value.connectWifi)
logger.info("Connect Wifi Result " + result.stdout)
if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
console.log(chalk.green('MiWatch Connected'))
globalVariables.localUSB = ""
globalVariables.miWatchIpaddress = value.connectWifi
await common.pause(3000)
logger.info("Connect Wifi Complete")
return true
} else {
if (result.stdout.includes('failed to authenticate')) {
console.log(chalk.redBright('MiWatch not authenticated'))
logger.info('MiWatch not authenticated')
return false
} else {
console.log(chalk.red(result.stdout))
logger.info(result.stdout)
}
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
return false
}
}
}
};

View File

@ -1,7 +1,8 @@
const fs = require('fs');
const path = require('path');
const tiny = require("@peterpanhihi/tiny");
const { resolve } = require('path');
const getFilesIn = require('get-files-in')
module.exports = {
getCurrentDirectoryBase: () => {
@ -39,5 +40,9 @@ module.exports = {
}
resolve(x / y);
}).catch(err => NaN)
}
},
getListOfAPk: (path) => {
const result = getFilesIn(path, matchFiletypes = ["apk"], checkSubDirectories = false)
return result
},
}

View File

@ -1,11 +1,8 @@
const chalk = require('chalk');
const common = require('../lib/common');
const inquirer = require('../lib/inquirer');
const shellExec = require('shell-exec')
const files = require('../lib/files')
const getFilesIn = require('get-files-in')
let logger = require('perfect-logger');
const globalVariables = require('../lib/globalVars');
const logger = require('perfect-logger');
const Language = require("@shypes/language-translator");
const adb = require('../lib/adb');
@ -30,10 +27,10 @@ module.exports = {
value = await adb.getInstalledPacakges()
for (let element of value.removeAppsList) {
console.log(await Language.get('removing') + ' ' + element)
logger.info(await Language.get('removing') + ' ' + element)
const package = element.substring(8)
await adb.removeApk(package)
console.log(await Language.get('removing') + ' ' + element)
logger.info(await Language.get('removing') + ' ' + element)
const package = element.substring(8)
await adb.removeApk(package)
}
console.log(chalk.green(await Language.get('remove_selected_user_apps')))
logger.info(await Language.get('remove_selected_user_apps'))
@ -41,7 +38,7 @@ module.exports = {
module.exports.mainMenu()
},
compatibleApps: async () => {
installCompatibleApps: async () => {
logger.info(await Language.get('install_compatible_apps_header'))
common.header(await Language.get('install_compatible_apps_header'))
@ -59,12 +56,12 @@ module.exports = {
}
}
const apkList = await adb.getListOfAPk()
const apkList = await files.getListOfAPk('./data/apps')
for (let element of apkList) {
console.log(await Language.get('installing') + ' ' + element)
logger.info(await Language.get('installing') + ' ' + element)
await adb.installApk(element)
for (let package of apkList) {
console.log(await Language.get('installing') + ' ' + package)
logger.info(await Language.get('installing') + ' ' + package)
await adb.installApk(package)
}
console.log(chalk.green(await Language.get('compatible_apps_installed')))
logger.info(await Language.get('compatible_apps_installed'))
@ -75,17 +72,8 @@ module.exports = {
logger.info("Remove Apps")
common.header('Remove Apps')
const value = await inquirer.removeAppsList();
for (let element of value.removeAppsList) {
// await shellExec(adbRun + ' shell pm disable-user --0 ' + element).then(function (result) {
await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Removing ' + element + ' - ' + result.stdout);
console.log('Removing ' + element + ' - ' + result.stdout);
}
});
for (let package of value.removeAppsList) {
await adb.removeXiaomiApk(package)
}
console.log(chalk.green('Removal Complete'))
await common.pause(2000)
@ -96,16 +84,8 @@ module.exports = {
logger.info("Restore Apps")
common.header('Restore Apps')
const value = await inquirer.removeAppsList();
for (let element of value.removeAppsList) {
await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Restoring ' + element + ' - ' + result.stdout);
console.log('Restoring ' + element + ' - ' + result.stdout);
}
});
for (let package of value.removeAppsList) {
await adb.restoreXiaomiApk(package)
}
console.log(chalk.green('Restore Complete'))
await common.pause(2000)
@ -116,74 +96,19 @@ module.exports = {
logger.info("Connect to watch")
common.header('Connect to watch')
const value = await inquirer.connectionType()
if (value.connection === "usb") {
await shellExec(adbRun + ' kill-server').then(async function (result) {
logger.info('Restarting ADB')
logger.info(result.stdout)
})
await shellExec(adbRun + ' devices').then(async function (result) {
console.log(result.stdout)
if (result.stdout.includes('device', 15)) {
console.log(chalk.green('MiWatch Connected via USB'))
await common.pause(3000)
logger.info("MiWatch connected")
globalVariables.localUSB = "X"
module.exports.mainMenu()
} else {
console.log(chalk.red('MiWatch not found'))
logger.info("MiWatch not found")
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWatch()
}
})
}
if (value.connection === "wifi") {
const value = await inquirer.connectWifi();
await shellExec(adbRun + ' kill-server').then(async function (result) {
logger.info('Restarting ADB')
logger.info(result.stdout)
})
await shellExec(adbRun + ' connect ' + value.connectWifi).then(async function (result) {
logger.info("Connect Wifi Result " + result.stdout)
if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
console.log(chalk.green('MiWatch Connected'))
globalVariables.localUSB = ""
globalVariables.miWatchIpaddress = value.connectWifi
await common.pause(3000)
logger.info("Connect Wifi Complete")
module.exports.mainMenu()
} else {
if (result.stdout.includes('failed to authenticate')) {
console.log(chalk.redBright('MiWatch not authenticated'))
logger.info('MiWatch not authenticated')
} else {
console.log(chalk.red(result.stdout))
logger.info(result.stdout)
}
await common.pause(2000)
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWatch()
}
}).catch()
connected = await adb.watchConnection(value)
if (connected != true) {
module.exports.connectWatch()
} else {
module.exports.mainMenu()
}
},
oneClick: async () => {
logger.info("1-Click Karl0ss Klean")
common.header('1-Click Karl0ss Klean')
const removalPackagesList = files.loadPackageList()
for (let element of removalPackagesList.apps) {
await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Removing ' + element + ' - ' + result.stdout);
console.log('Removing ' + element + ' - ' + result.stdout);
}
});
for (let package of removalPackagesList.apps) {
await adb.removeXiaomiApk(package)
}
console.log(chalk.green('Removal Complete'))
await common.pause(2000)
@ -193,33 +118,24 @@ module.exports = {
await common.clearApkFolder()
const compatibleApps = await common.getCompatibleAppsList()
console.log(chalk.green('Download Compatible APKS'))
for (const element of compatibleApps) {
if (element.Klean === "X") {
for (const package of compatibleApps) {
if (package.Klean === "X") {
try {
logger.info('Downloading Latest ' + element.name + ' Complete')
newName = element.name.replace(/\s/g, '');
await common.downloadFile(element.url, './data/apps/' + newName + '.apk')
logger.info('Downloading Latest ' + element.name + ' Complete')
newPacakgeName = package.name.replace(/\s/g, '');
await common.downloadFile(package.url, './data/apps/' + newPacakgeName + '.apk')
logger.info('Downloading Latest ' + package.name + ' Complete')
console.log('Downloading Latest ' + package.name + ' Complete')
} catch (error) {
logger.info('Downloading Latest ' + element.name + ' FAILED')
logger.info('Downloading Latest ' + package.name + ' FAILED')
}
}
}
const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
const apkList = await files.getListOfAPk('./data/apps')
console.log(chalk.green('Install Apks'))
for (let element of apkList) {
console.log('Installing ' + element)
logger.info('Installing ' + element)
await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
}
console.log(element + ' - ' + result.stdout);
logger.info(element + ' - ' + result.stdout);
});
await adb.installApk(element)
}
console.log(chalk.green('Compatible Apps Installed'))
logger.info('Compatible Apps Installed')
@ -230,15 +146,7 @@ module.exports = {
logger.info("Restore Any App")
common.header('Restore Any App')
const value = await inquirer.restoreAnyApp();
await shellExec(adbRun + ' shell cmd package install-existing ' + value.restoreAnyApp).then(function (result) {
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright('Error - Device not authorised'));
} else {
logger.info('Restoring ' + value.restoreAnyApp + ' - ' + result.stdout);
console.log('Restoring ' + value.restoreAnyApp + ' - ' + result.stdout);
}
});
await adb.restoreAnyApk(value)
console.log(chalk.green('Restore Complete'))
await common.pause(2000)
logger.info("App Restore Complete")
@ -248,23 +156,14 @@ module.exports = {
logger.info("Batch Install Apks")
common.header('Batch Install Apks')
let apkList = await getFilesIn('./my_apk/', matchFiletypes = ['apk'], checkSubDirectories = false)
let apkList = await files.getListOfAPk('./my_apk/')
await files.renameLocalApk(apkList)
apkList = await getFilesIn('./my_apk/', matchFiletypes = ['apk'], checkSubDirectories = false)
apkList = await files.getListOfAPk('./my_apk/')
for (let element of apkList) {
console.log('Installing ' + element)
logger.info('Installing ' + element)
await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
if (result.stderr != '') {
logger.info('Error ' + result.stderr);
console.log(chalk.redBright(result.stderr));
}
console.log(element + ' - ' + result.stdout);
logger.info(element + ' - ' + result.stdout);
});
await adb.installApk(element)
}
console.log(chalk.green('Batch Install Apks Completed'))
logger.info('Batch Install Apks Completed')
@ -288,7 +187,7 @@ module.exports = {
module.exports.restoreApps()
break;
case 'install compatible apps':
module.exports.compatibleApps()
module.exports.installCompatibleApps()
break;
case 'batch remove installed apps':
module.exports.removeCompatibleApps()