pages.js 6.6 KB

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