pages.js 7.1 KB

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