pages.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. let logger = require('perfect-logger');
  14. let adbRun
  15. logger.initialize('RunTIme', {
  16. logLevelFile: 0, // Log level for file
  17. logLevelConsole: -1, // Log level for STDOUT/STDERR
  18. logDirectory: 'data/', // Log directory
  19. });
  20. module.exports = {
  21. removeCompatibleApps: async () => {
  22. let installedAppList
  23. common.header('Remove Installed Apps')
  24. logger.info('Remove Installed Apps')
  25. await shellExec(adbRun + ' shell pm list packages -3').then(async function (result) {
  26. logger.info('Packages recieved from watch')
  27. if (result.stderr.includes('error')) {
  28. logger.info(result.stderr)
  29. console.log(chalk.red('Device not authorised'))
  30. common.pause(3000)
  31. await shellExec(adbRun + ' kill-server').then(async function (result) {
  32. logger.info('Restarting ADB')
  33. logger.info(result.stdout)
  34. console.log('Please reconnect to watch')
  35. common.pause(3000)
  36. logger.info('Remove Installed Apps Failed')
  37. module.exports.mainMenu()
  38. })
  39. } else {
  40. if (process.platform === 'win32' || process.platform === 'win64') {
  41. installedAppList = result.stdout.split('\r\n'); // split string on comma space
  42. installedAppList.splice(-1, 1)
  43. } else {
  44. installedAppList = result.stdout.split('\n'); // split string on comma space
  45. installedAppList.splice(-1, 1)
  46. }
  47. const value = await inquirer.installedApps(installedAppList);
  48. for (let element of value.removeAppsList) {
  49. console.log('Removing ' + element)
  50. logger.info('Removing ' + element)
  51. const package = element.substring(8)
  52. await shellExec(adbRun + ' uninstall ' + package).then(async function (result) {
  53. console.log(element + ' - ' + result.stdout);
  54. logger.info(element + ' - ' + result.stdout);
  55. });
  56. }
  57. console.log(chalk.green('Removed Selected User Apps'))
  58. logger.info('Removed Selected User Apps')
  59. await common.pause(2000)
  60. module.exports.mainMenu()
  61. }
  62. })
  63. },
  64. compatibleApps: async () => {
  65. logger.info("Compatible Apps")
  66. common.header('Install Compatible Apps')
  67. let compatibleApps
  68. let url = "http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json";
  69. http.get(url, (res) => {
  70. let body = "";
  71. res.on("data", (chunk) => {
  72. body += chunk;
  73. });
  74. res.on("end", () => {
  75. try {
  76. compatibleApps = JSON.parse(body);
  77. logger.info("Compatible Apps found online")
  78. // do something with JSON
  79. } catch (error) {
  80. console.error(error.message);
  81. };
  82. });
  83. }).on("error", (error) => {
  84. console.error(error.message);
  85. });
  86. const value = await inquirer.compatibleApps();
  87. await shell.rm('-rf', './data/apps/*.apk');
  88. for (let element of value.removeAppsList) {
  89. for (let element2 of compatibleApps) {
  90. if (element === element2.name) {
  91. const options = {
  92. override: true,
  93. }
  94. const dl = new DownloaderHelper(element2.url, './data/apps/', options);
  95. dl.on('end', () => console.log('Downloading Latest ' + element2.name + ' Complete'),
  96. logger.info('Downloading Latest ' + element2.name + ' Complete')
  97. )
  98. await dl.start();
  99. }
  100. }
  101. }
  102. const apkList = await getFilesIn('./data/apps', matchFiletypes = ['apk'], checkSubDirectories = false)
  103. for (let element of apkList) {
  104. console.log('Installing ' + element)
  105. logger.info('Installing ' + element)
  106. await shellExec(adbRun + ' install -r ' + element).then(async function (result) {
  107. if (result.stderr != '') {
  108. logger.info('Error ' + result.stderr);
  109. console.log(chalk.redBright('Error - Device not authorised'));
  110. }
  111. console.log(element + ' - ' + result.stdout);
  112. logger.info(element + ' - ' + result.stdout);
  113. if (element === "data\\apps\\MoreLocale.apk") {
  114. await shellExec(adbRun + ' shell pm grant jp.co.c_lis.ccl.morelocale android.permission.CHANGE_CONFIGURATION').then(function (result) {
  115. console.log('moreLocale Activated On Watch');
  116. logger.info('moreLocale Activated On Watch');
  117. });
  118. }
  119. });
  120. }
  121. console.log(chalk.green('Compatible Apps Installed'))
  122. logger.info('Compatible Apps Installed')
  123. await common.pause(2000)
  124. module.exports.mainMenu()
  125. },
  126. removeApps: async () => {
  127. logger.info("Remove Apps")
  128. common.header('Remove Apps')
  129. const value = await inquirer.removeAppsList();
  130. for (let element of value.removeAppsList) {
  131. await shellExec(adbRun + ' shell pm uninstall -k --user 0 ' + element).then(function (result) {
  132. if (result.stderr != '') {
  133. logger.info('Error ' + result.stderr);
  134. console.log(chalk.redBright('Error - Device not authorised'));
  135. } else {
  136. logger.info('Removing ' + element + ' - ' + result.stdout);
  137. console.log('Removing ' + element + ' - ' + result.stdout);
  138. }
  139. });
  140. }
  141. console.log(chalk.green('Removal Complete'))
  142. await common.pause(2000)
  143. logger.info("Remove Complete")
  144. module.exports.mainMenu()
  145. },
  146. restoreApps: async () => {
  147. logger.info("Restore Apps")
  148. common.header('Restore Apps')
  149. const value = await inquirer.removeAppsList();
  150. for (let element of value.removeAppsList) {
  151. await shellExec(adbRun + ' shell cmd package install-existing ' + element).then(function (result) {
  152. if (result.stderr != '') {
  153. logger.info('Error ' + result.stderr);
  154. console.log(chalk.redBright('Error - Device not authorised'));
  155. } else {
  156. logger.info('Restoring ' + element + ' - ' + result.stdout);
  157. console.log('Restoring ' + element + ' - ' + result.stdout);
  158. }
  159. });
  160. }
  161. console.log(chalk.green('Restore Complete'))
  162. await common.pause(2000)
  163. logger.info("Restore Apps Complete")
  164. module.exports.mainMenu()
  165. },
  166. connectWifi: async () => {
  167. logger.info("Connect Wifi")
  168. const miwatchData = JSON.parse(fs.readFileSync('./data/MiWatch.json', 'utf8'));
  169. common.header('Connect Wifi')
  170. if (miwatchData.ipAddress !== "") {
  171. await shellExec(adbRun + ' kill-server')
  172. console.log('Trying to connect with stored ipAddress')
  173. shellExec(adbRun + ' connect ' + miwatchData.ipAddress).then(async function (result) {
  174. logger.info("Connect Wifi Result " + result.stdout)
  175. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  176. console.log(chalk.green('MiWatch Connected'))
  177. await common.pause(3000)
  178. logger.info("Connect Wifi Complete")
  179. module.exports.mainMenu()
  180. } else {
  181. console.log(chalk.red('MiWatch not found'))
  182. await common.pause(2000)
  183. files.writeIpAddress('')
  184. console.log(chalk.white('Try Again'))
  185. await common.pause(1000)
  186. module.exports.connectWifi()
  187. }
  188. }).catch()
  189. } else {
  190. await shellExec(adbRun + ' kill-server')
  191. const value = await inquirer.connectWifi();
  192. const miWatchIpaddress = value.connectWifi
  193. shellExec(adbRun + ' connect ' + miWatchIpaddress).then(async function (result) {
  194. logger.info("Connect Wifi Result " + result.stdout)
  195. if (result.stdout.includes('already connected') || result.stdout.includes('connected to ')) {
  196. console.log(chalk.green('MiWatch Connected'))
  197. files.writeIpAddress(miWatchIpaddress)
  198. await common.pause(3000)
  199. logger.info("Connect Wifi Complete")
  200. module.exports.mainMenu()
  201. } else {
  202. if (result.stdout.includes('failed to authenticate')) {
  203. console.log(chalk.redBright('MiWatch not authenticated'))
  204. } else {
  205. console.log(chalk.red('MiWatch not found'))
  206. }
  207. await common.pause(2000)
  208. console.log(chalk.white('Try Again'))
  209. await common.pause(1000)
  210. module.exports.connectWifi()
  211. }
  212. }).catch()
  213. }
  214. },
  215. mainMenu: async () => {
  216. common.header('Main Menu')
  217. if (process.platform === 'win32' || process.platform === 'win64') {
  218. adbRun = 'adb'
  219. } else {
  220. adbRun = './adb'
  221. }
  222. logger.info(process.platform + " detected")
  223. const mainMenuSelection = await inquirer.mainMenu();
  224. switch (mainMenuSelection.mainMenu) {
  225. case 'connect to miwatch via wifi':
  226. module.exports.connectWifi()
  227. break;
  228. case 'remove xiaomi apps':
  229. module.exports.removeApps()
  230. break;
  231. case 'restore xiaomi apps':
  232. module.exports.restoreApps()
  233. break;
  234. case 'install compatible apps':
  235. module.exports.compatibleApps()
  236. break;
  237. case 'remove installed apps':
  238. module.exports.removeCompatibleApps()
  239. break;
  240. case 'quit':
  241. break;
  242. default:
  243. // code block
  244. }
  245. }
  246. };