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') 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'); var pjson = require('../package.json');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const globalVariables = require('../lib/globalVars');
module.exports = { module.exports = {
header: (page) => { header: (page) => {
@ -25,18 +27,21 @@ module.exports = {
) )
) )
console.log(chalk.blue(page)) console.log(chalk.blue(page))
module.exports.ipCheck() module.exports.connectionCheck()
console.log(chalk.red('----------')) console.log(chalk.red('----------'))
}, },
pause: async (time) => { pause: async (time) => {
await new Promise(resolve => setTimeout(resolve, time)); await new Promise(resolve => setTimeout(resolve, time));
}, },
ipCheck: async () => { connectionCheck: async () => {
const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8')); if (globalVariables.localUSB === "X") {
if (miwatchData.ipAddress === "") { console.log(chalk.white('MiWatch: ') + chalk.green('Connected via USB'))
console.log(chalk.white('MiWatch IP: ') + chalk.red('Not Connected')) }
} else { if (globalVariables.miWatchIpaddress != "") {
console.log(chalk.white('MiWatch IP: ' + chalk.green(miwatchData.ipAddress))) 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) => { downloadFile: async (url, path) => {
@ -47,7 +52,7 @@ module.exports = {
res.body.on("error", (err) => { res.body.on("error", (err) => {
reject(err); reject(err);
}); });
fileStream.on("finish", function() { fileStream.on("finish", function () {
resolve(); resolve();
}); });
}); });

View File

@ -14,7 +14,7 @@ module.exports = {
ipAddress: value ipAddress: value
} }
try { try {
fs.writeFileSync('./data/MiWatch.json', JSON.stringify(data)) fs.writeFileSync('./data/options.json', JSON.stringify(data))
} catch (err) { } catch (err) {
console.log(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", name: "mainMenu",
message: "What do you want to do?", message: "What do you want to do?",
choices: [ choices: [
"Connect to MiWatch via Wifi", // "Connect to MiWatch",
"1-Click Karl0ss Klean", "1-Click Karl0ss Klean",
"Remove Xiaomi Apps", "Remove Xiaomi Apps",
"Restore Xiaomi Apps", "Restore Xiaomi Apps",
@ -26,6 +26,21 @@ module.exports = {
}, ]; }, ];
return inquirer.prompt(questions); 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: () => { connectWifi: () => {
const questions = [{ const questions = [{
type: "input", type: "input",

View File

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

View File

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