initial refactor

This commit is contained in:
karl.hudgell 2020-08-27 16:45:49 +01:00
parent ff9f27e896
commit b918ff5f3a
9 changed files with 93 additions and 53 deletions

2
app.js
View File

@ -1,3 +1,3 @@
const pages = require('./pages/pages')
pages.mainMenu();
pages.connectWatch();

View File

@ -1 +0,0 @@
{"ipAddress":""}

4
data/options.json Normal file
View File

@ -0,0 +1,4 @@
{
"wifi": "X",
"ipAddress":""
}

View File

@ -5,6 +5,8 @@ const fs = require('fs')
var pjson = require('../package.json');
const fetch = require('node-fetch');
const globalVariables = require('../lib/globalVars');
module.exports = {
header: (page) => {
@ -25,37 +27,40 @@ module.exports = {
)
)
console.log(chalk.blue(page))
module.exports.ipCheck()
module.exports.connectionCheck()
console.log(chalk.red('----------'))
},
pause: async (time) => {
await new Promise(resolve => setTimeout(resolve, time));
},
ipCheck: async () => {
const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
if (miwatchData.ipAddress === "") {
console.log(chalk.white('MiWatch IP: ') + chalk.red('Not Connected'))
} else {
console.log(chalk.white('MiWatch IP: ' + chalk.green(miwatchData.ipAddress)))
connectionCheck: async () => {
if (globalVariables.localUSB === "X") {
console.log(chalk.white('MiWatch: ') + chalk.green('Connected via USB'))
}
if (globalVariables.miWatchIpaddress != "") {
console.log(chalk.white('MiWatch: ') + chalk.green('Connected via Wifi - ' + chalk.white(globalVariables.miWatchIpaddress)))
}
if (globalVariables.localUSB === "" && globalVariables.miWatchIpaddress === "") {
console.log(chalk.white('MiWatch: ') + chalk.red('Not Connected'))
}
},
downloadFile: async (url, path) => {
const res = await fetch(url);
await new Promise((resolve, reject) => {
const fileStream = fs.createWriteStream(path);
res.body.pipe(fileStream);
res.body.on("error", (err) => {
reject(err);
});
fileStream.on("finish", function() {
resolve();
});
const fileStream = fs.createWriteStream(path);
res.body.pipe(fileStream);
res.body.on("error", (err) => {
reject(err);
});
fileStream.on("finish", function () {
resolve();
});
});
},
getCompatibleAppsList: async () => {
let settings = { method: "Get" };
const response = await fetch("http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json", settings)
.then(res => res.json())
return response
.then(res => res.json())
return response
},
}

View File

@ -14,7 +14,7 @@ module.exports = {
ipAddress: value
}
try {
fs.writeFileSync('./data/MiWatch.json', JSON.stringify(data))
fs.writeFileSync('./data/options.json', JSON.stringify(data))
} catch (err) {
console.log(err)
}

7
lib/globalVars.js Normal file
View File

@ -0,0 +1,7 @@
class globalVariables {
localUSB = "";
miWatchIpaddress = "";
usersList=[];
}
module.exports = new globalVariables();

View File

@ -11,7 +11,7 @@ module.exports = {
name: "mainMenu",
message: "What do you want to do?",
choices: [
"Connect to MiWatch via Wifi",
// "Connect to MiWatch",
"1-Click Karl0ss Klean",
"Remove Xiaomi Apps",
"Restore Xiaomi Apps",
@ -26,6 +26,21 @@ module.exports = {
}, ];
return inquirer.prompt(questions);
},
connectionType: () => {
const questions = [{
type: "list",
name: "connection",
message: "How do you want to connect?",
choices: [
"USB",
"Wifi"
],
filter: function (val) {
return val.toLowerCase();
},
}, ];
return inquirer.prompt(questions);
},
connectWifi: () => {
const questions = [{
type: "input",

View File

@ -1,6 +1,6 @@
{
"name": "miwatchkleaner2.0",
"version": "2.0.10",
"name": "miwatchkleaner",
"version": "3.0.0",
"description": "MiWatch Cleaning Tool",
"main": "app.js",
"bin": "app.js",

View File

@ -7,8 +7,15 @@ const fs = require('fs')
const getFilesIn = require('get-files-in')
var shell = require('shelljs');
let logger = require('perfect-logger');
const globalVariables = require('../lib/globalVars');
let adbRun
logger.info(process.platform + " detected")
if (process.platform === 'win32' || process.platform === 'win64') {
adbRun = 'adb'
} else {
adbRun = './adb'
}
logger.initialize('RunTIme', {
logLevelFile: 0, // Log level for file
@ -162,46 +169,55 @@ module.exports = {
logger.info("Restore Apps Complete")
module.exports.mainMenu()
},
connectWifi: async () => {
logger.info("Connect Wifi")
const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
common.header('Connect Wifi')
if (miwatchData.ipAddress !== "") {
await shellExec(adbRun + ' kill-server')
console.log('Trying to connect with stored ipAddress')
shellExec(adbRun + ' connect ' + miwatchData.ipAddress).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'))
connectWatch: async () => {
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("Connect Wifi Complete")
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)
files.writeIpAddress('')
console.log(chalk.white('Try Again'))
await common.pause(1000)
module.exports.connectWifi()
module.exports.connectWatch()
}
}).catch()
} else {
await shellExec(adbRun + ' kill-server')
})
}
if (value.connection === "wifi") {
const value = await inquirer.connectWifi();
const miWatchIpaddress = value.connectWifi
shellExec(adbRun + ' connect ' + miWatchIpaddress).then(async function (result) {
globalVariables.miWatchIpaddress = value.connectWifi
await shellExec(adbRun + ' kill-server').then(async function (result) {
logger.info('Restarting ADB')
logger.info(result.stdout)
})
await shellExec(adbRun + ' connect ' + globalVariables.miWatchIpaddress).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'))
files.writeIpAddress(miWatchIpaddress)
globalVariables.localUSB = ""
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('MiWatch not found'))
console.log(chalk.red(result.stdout))
logger.info(result.stdout)
}
await common.pause(2000)
console.log(chalk.white('Try Again'))
@ -287,16 +303,10 @@ module.exports = {
},
mainMenu: async () => {
common.header('Main Menu')
if (process.platform === 'win32' || process.platform === 'win64') {
adbRun = 'adb'
} else {
adbRun = './adb'
}
logger.info(process.platform + " detected")
const mainMenuSelection = await inquirer.mainMenu();
switch (mainMenuSelection.mainMenu) {
case 'connect to miwatch via wifi':
module.exports.connectWifi()
case 'connect to miwatch':
module.exports.connectWatch()
break;
case '1-click karl0ss klean':
module.exports.oneClick()