pages.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. const chalk = require('chalk');
  2. const common = require('../lib/common');
  3. const inquirer = require('../lib/inquirer');
  4. const shellExec = require('shell-exec')
  5. const files = require('../lib/files')
  6. const fs = require('fs')
  7. const {
  8. DownloaderHelper
  9. } = require('node-downloader-helper');
  10. const getFilesIn = require('get-files-in')
  11. const http = require('http')
  12. var shell = require('shelljs');
  13. module.exports = {
  14. compatibleApps: async () => {
  15. common.header('Install Compatible Apps')
  16. let compatibleApps
  17. let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
  18. http.get(url, (res) => {
  19. let body = "";
  20. res.on("data", (chunk) => {
  21. body += chunk;
  22. });
  23. res.on("end", () => {
  24. try {
  25. compatibleApps = JSON.parse(body);
  26. // do something with JSON
  27. } catch (error) {
  28. console.error(error.message);
  29. };
  30. });
  31. }).on("error", (error) => {
  32. console.error(error.message);
  33. });
  34. const value = await inquirer.compatibleApps();
  35. await shell.rm('-rf', './data/apps/*.apk');
  36. for (let element of value.removeAppsList) {
  37. for (let element2 of compatibleApps) {
  38. if (element === element2.name) {
  39. const options = {
  40. override: true,
  41. }
  42. const dl = new DownloaderHelper(element2.url, './data/apps/', options);
  43. dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'))
  44. await dl.start();
  45. }
  46. }
  47. }
  48. const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
  49. for (let element of apkList) {
  50. console.log('Installing ' + element)
  51. await shellExec('adb install -r ' + element).then(function (result) {
  52. console.log(element + ' - ' + result.stdout);
  53. });
  54. }
  55. console.log(chalk.green('Compatible Apps Installed'))
  56. await common.pause(2000)
  57. module.exports.mainMenu()
  58. },
  59. removeApps: async () => {
  60. common.header('Remove Apps')
  61. const value = await inquirer.removeAppsList();
  62. for (let element of value.removeAppsList) {
  63. await shellExec('adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
  64. console.log('Removing ' + element + ' - ' + result.stdout);
  65. });
  66. }
  67. console.log(chalk.green('Removal Complete'))
  68. await common.pause(2000)
  69. module.exports.mainMenu()
  70. },
  71. restoreApps: async () => {
  72. common.header('Restore Apps')
  73. const value = await inquirer.removeAppsList();
  74. for (let element of value.removeAppsList) {
  75. await shellExec('adb shell cmd package install-existing ' + element).then(function (result) {
  76. console.log('Restoring ' + element + ' - ' + result.stdout);
  77. });
  78. }
  79. console.log(chalk.green('Restore Complete'))
  80. await common.pause(2000)
  81. module.exports.mainMenu()
  82. },
  83. connectWifi: async () => {
  84. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  85. common.header('Connect Wifi')
  86. if (miwatchData.ipAddress !== "") {
  87. console.log('Trying to connect with stored ipAddress')
  88. shellExec('adb connect ' + miwatchData.ipAddress).then(async function (result) {
  89. if (result.stdout.includes('unable to connect')) {
  90. console.log(chalk.red('MiWatch not found'))
  91. await common.pause(2000)
  92. console.log(chalk.white('Try Again'))
  93. files.writeIpAddress('')
  94. await common.pause(1000)
  95. module.exports.connectWifi()
  96. } else if (result.stdout.includes('cannot connect')) {
  97. console.log(chalk.red('MiWatch not found'))
  98. await common.pause(2000)
  99. console.log(chalk.white('Try Again'))
  100. files.writeIpAddress('')
  101. await common.pause(1000)
  102. module.exports.connectWifi()
  103. } else if (result.stdout.includes('cannot resolve host')) {
  104. console.log(chalk.red('MiWatch not found'))
  105. await common.pause(2000)
  106. console.log(chalk.white('Try Again'))
  107. files.writeIpAddress('')
  108. await common.pause(1000)
  109. module.exports.connectWifi()
  110. } else {
  111. console.log(chalk.green('MiWatch Connected'))
  112. await common.pause(3000)
  113. module.exports.mainMenu()
  114. }
  115. }).catch()
  116. } else {
  117. const value = await inquirer.connectWifi();
  118. const miWatchIpaddress = value.connectWifi
  119. shellExec('adb connect ' + miWatchIpaddress).then(async function (result) {
  120. if (result.stdout.includes('unable to connect')) {
  121. console.log(chalk.red('MiWatch not found'))
  122. await common.pause(2000)
  123. console.log(chalk.white('Try Again'))
  124. await common.pause(1000)
  125. module.exports.connectWifi()
  126. } else if (result.stdout.includes('cannot connect')) {
  127. console.log(chalk.red('MiWatch not found'))
  128. await common.pause(2000)
  129. console.log(chalk.white('Try Again'))
  130. await common.pause(1000)
  131. module.exports.connectWifi()
  132. } else if (result.stdout.includes('cannot resolve host')) {
  133. console.log(chalk.red('MiWatch not found'))
  134. await common.pause(2000)
  135. console.log(chalk.white('Try Again'))
  136. await common.pause(1000)
  137. module.exports.connectWifi()
  138. } else {
  139. console.log(chalk.green('MiWatch Connected'))
  140. files.writeIpAddress(miWatchIpaddress)
  141. await common.pause(3000)
  142. module.exports.mainMenu()
  143. }
  144. }).catch()
  145. }
  146. },
  147. mainMenu: async () => {
  148. common.header('Main Menu')
  149. const mainMenuSelection = await inquirer.mainMenu();
  150. switch (mainMenuSelection.mainMenu) {
  151. case 'connect to miwatch via wifi':
  152. module.exports.connectWifi()
  153. break;
  154. case 'remove xiaomi apps':
  155. module.exports.removeApps()
  156. break;
  157. case 'restore xiaomi apps':
  158. module.exports.restoreApps()
  159. break;
  160. case 'install compatible apps':
  161. module.exports.compatibleApps()
  162. break;
  163. case 'quit':
  164. break;
  165. default:
  166. // code block
  167. }
  168. }
  169. };