pages.js 12 KB

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