pages.js 5.4 KB

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