pages.js 5.6 KB

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