diff --git a/app.js b/app.js index 0ddd93d..8fa6c87 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,3 @@ const pages = require('./pages/pages') -pages.mainMenu(); +pages.connectWatch(); diff --git a/data/MiWatch.json b/data/MiWatch.json deleted file mode 100644 index beaa1e9..0000000 --- a/data/MiWatch.json +++ /dev/null @@ -1 +0,0 @@ -{"ipAddress":""} \ No newline at end of file diff --git a/data/options.json b/data/options.json new file mode 100644 index 0000000..0a6cb1b --- /dev/null +++ b/data/options.json @@ -0,0 +1,4 @@ +{ + "wifi": "X", + "ipAddress":"" +} \ No newline at end of file diff --git a/lib/common.js b/lib/common.js index 60c79b7..75ba3fd 100644 --- a/lib/common.js +++ b/lib/common.js @@ -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 }, } \ No newline at end of file diff --git a/lib/files.js b/lib/files.js index b3210f6..159b98b 100644 --- a/lib/files.js +++ b/lib/files.js @@ -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) } diff --git a/lib/globalVars.js b/lib/globalVars.js new file mode 100644 index 0000000..5cce1ae --- /dev/null +++ b/lib/globalVars.js @@ -0,0 +1,7 @@ +class globalVariables { + localUSB = ""; + miWatchIpaddress = ""; + usersList=[]; + } + +module.exports = new globalVariables(); \ No newline at end of file diff --git a/lib/inquirer.js b/lib/inquirer.js index 2c54989..4c9422e 100644 --- a/lib/inquirer.js +++ b/lib/inquirer.js @@ -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", diff --git a/package.json b/package.json index 7fdd06e..db374d7 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pages/pages.js b/pages/pages.js index 4cf2027..4c69bbd 100644 --- a/pages/pages.js +++ b/pages/pages.js @@ -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()