pages.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. if (process.platform === 'win32' || process.platform === 'win64') {
  52. await shellExec('adb install -r ' + element).then(function (result) {
  53. console.log(element + ' - ' + result.stdout);
  54. });
  55. } else {
  56. await shellExec('./adb install -r ' + element).then(function (result) {
  57. console.log(element + ' - ' + result.stdout);
  58. });
  59. }
  60. }
  61. console.log(chalk.green('Compatible Apps Installed'))
  62. await common.pause(2000)
  63. module.exports.mainMenu()
  64. },
  65. removeApps: async () => {
  66. common.header('Remove Apps')
  67. const value = await inquirer.removeAppsList();
  68. for (let element of value.removeAppsList) {
  69. if (process.platform === 'win32' || process.platform === 'win64') {
  70. await shellExec('adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
  71. console.log('Removing ' + element + ' - ' + result.stdout);
  72. });
  73. } else {
  74. await shellExec('./adb shell pm uninstall -k --user 0 ' + element).then(function (result) {
  75. console.log('Removing ' + element + ' - ' + result.stdout);
  76. });
  77. }
  78. }
  79. console.log(chalk.green('Removal Complete'))
  80. await common.pause(2000)
  81. module.exports.mainMenu()
  82. },
  83. restoreApps: async () => {
  84. common.header('Restore Apps')
  85. const value = await inquirer.removeAppsList();
  86. for (let element of value.removeAppsList) {
  87. if (process.platform === 'win32' || process.platform === 'win64') {
  88. await shellExec('adb shell cmd package install-existing ' + element).then(function (result) {
  89. console.log('Restoring ' + element + ' - ' + result.stdout);
  90. });
  91. } else {
  92. await shellExec('./adb shell cmd package install-existing ' + element).then(function (result) {
  93. console.log('Restoring ' + element + ' - ' + result.stdout);
  94. });
  95. }
  96. }
  97. console.log(chalk.green('Restore Complete'))
  98. await common.pause(2000)
  99. module.exports.mainMenu()
  100. },
  101. connectWifi: async () => {
  102. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  103. common.header('Connect Wifi')
  104. if (miwatchData.ipAddress !== "") {
  105. console.log('Trying to connect with stored ipAddress')
  106. if (process.platform === 'win32' || process.platform === 'win64') {
  107. shellExec('adb connect ' + miwatchData.ipAddress).then(async function (result) {
  108. if (result.stdout.includes('unable to connect')) {
  109. console.log(chalk.red('MiWatch not found'))
  110. await common.pause(2000)
  111. console.log(chalk.white('Try Again'))
  112. files.writeIpAddress('')
  113. await common.pause(1000)
  114. module.exports.connectWifi()
  115. } else if (result.stdout.includes('cannot connect')) {
  116. console.log(chalk.red('MiWatch not found'))
  117. await common.pause(2000)
  118. console.log(chalk.white('Try Again'))
  119. files.writeIpAddress('')
  120. await common.pause(1000)
  121. module.exports.connectWifi()
  122. } else if (result.stdout.includes('cannot resolve host')) {
  123. console.log(chalk.red('MiWatch not found'))
  124. await common.pause(2000)
  125. console.log(chalk.white('Try Again'))
  126. files.writeIpAddress('')
  127. await common.pause(1000)
  128. module.exports.connectWifi()
  129. } else {
  130. console.log(chalk.green('MiWatch Connected'))
  131. await common.pause(3000)
  132. module.exports.mainMenu()
  133. }
  134. }).catch()
  135. } else {
  136. shellExec('./adb connect ' + miwatchData.ipAddress).then(async function (result) {
  137. if (result.stdout.includes('unable to connect')) {
  138. console.log(chalk.red('MiWatch not found'))
  139. await common.pause(2000)
  140. console.log(chalk.white('Try Again'))
  141. files.writeIpAddress('')
  142. await common.pause(1000)
  143. module.exports.connectWifi()
  144. } else if (result.stdout.includes('cannot connect')) {
  145. console.log(chalk.red('MiWatch not found'))
  146. await common.pause(2000)
  147. console.log(chalk.white('Try Again'))
  148. files.writeIpAddress('')
  149. await common.pause(1000)
  150. module.exports.connectWifi()
  151. } else if (result.stdout.includes('cannot resolve host')) {
  152. console.log(chalk.red('MiWatch not found'))
  153. await common.pause(2000)
  154. console.log(chalk.white('Try Again'))
  155. files.writeIpAddress('')
  156. await common.pause(1000)
  157. module.exports.connectWifi()
  158. } else {
  159. console.log(chalk.green('MiWatch Connected'))
  160. await common.pause(3000)
  161. module.exports.mainMenu()
  162. }
  163. }).catch()
  164. }
  165. } else {
  166. const value = await inquirer.connectWifi();
  167. const miWatchIpaddress = value.connectWifi
  168. if (process.platform === 'win32' || process.platform === 'win64') {
  169. shellExec('adb connect ' + miWatchIpaddress).then(async function (result) {
  170. if (result.stdout.includes('unable to connect')) {
  171. console.log(chalk.red('MiWatch not found'))
  172. await common.pause(2000)
  173. console.log(chalk.white('Try Again'))
  174. await common.pause(1000)
  175. module.exports.connectWifi()
  176. } else if (result.stdout.includes('cannot connect')) {
  177. console.log(chalk.red('MiWatch not found'))
  178. await common.pause(2000)
  179. console.log(chalk.white('Try Again'))
  180. await common.pause(1000)
  181. module.exports.connectWifi()
  182. } else if (result.stdout.includes('cannot resolve host')) {
  183. console.log(chalk.red('MiWatch not found'))
  184. await common.pause(2000)
  185. console.log(chalk.white('Try Again'))
  186. await common.pause(1000)
  187. module.exports.connectWifi()
  188. } else {
  189. console.log(chalk.green('MiWatch Connected'))
  190. files.writeIpAddress(miWatchIpaddress)
  191. await common.pause(3000)
  192. module.exports.mainMenu()
  193. }
  194. }).catch()
  195. } else {
  196. shellExec('./adb connect ' + miWatchIpaddress).then(async function (result) {
  197. if (result.stdout.includes('unable to connect')) {
  198. console.log(chalk.red('MiWatch not found'))
  199. await common.pause(2000)
  200. console.log(chalk.white('Try Again'))
  201. await common.pause(1000)
  202. module.exports.connectWifi()
  203. } else if (result.stdout.includes('cannot connect')) {
  204. console.log(chalk.red('MiWatch not found'))
  205. await common.pause(2000)
  206. console.log(chalk.white('Try Again'))
  207. await common.pause(1000)
  208. module.exports.connectWifi()
  209. } else if (result.stdout.includes('cannot resolve host')) {
  210. console.log(chalk.red('MiWatch not found'))
  211. await common.pause(2000)
  212. console.log(chalk.white('Try Again'))
  213. await common.pause(1000)
  214. module.exports.connectWifi()
  215. } else {
  216. console.log(chalk.green('MiWatch Connected'))
  217. files.writeIpAddress(miWatchIpaddress)
  218. await common.pause(3000)
  219. module.exports.mainMenu()
  220. }
  221. }).catch()
  222. }
  223. }
  224. },
  225. mainMenu: async () => {
  226. common.header('Main Menu')
  227. const mainMenuSelection = await inquirer.mainMenu();
  228. switch (mainMenuSelection.mainMenu) {
  229. case 'connect to miwatch via wifi':
  230. module.exports.connectWifi()
  231. break;
  232. case 'remove xiaomi apps':
  233. module.exports.removeApps()
  234. break;
  235. case 'restore xiaomi apps':
  236. module.exports.restoreApps()
  237. break;
  238. case 'install compatible apps':
  239. module.exports.compatibleApps()
  240. break;
  241. case 'quit':
  242. break;
  243. default:
  244. // code block
  245. }
  246. }
  247. };