common.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const chalk = require('chalk');
  2. const clear = require('clear');
  3. const figlet = require('figlet');
  4. const fs = require('fs')
  5. var pjson = require('../package.json');
  6. const fetch = require('node-fetch');
  7. var shell = require('shelljs');
  8. const logger = require('perfect-logger');
  9. const Language = require("@shypes/language-translator");
  10. const globalVariables = require('../lib/globalVars');
  11. const settings = require("settings-store");
  12. var chalkRainbow = require('chalk-rainbow')
  13. module.exports = {
  14. header: async (page) => {
  15. Language.setActiveLang(await settings.value("Lang"))
  16. clear();
  17. console.log(
  18. chalkRainbow(
  19. figlet.textSync('MiWatch Kleaner', {
  20. horizontalLayout: 'full'
  21. })
  22. )
  23. );
  24. console.log(chalk.red(' ' + pjson.version));
  25. console.log();
  26. console.log(
  27. chalk.red(
  28. '-------------------------------------------------------------------------------------------------------'
  29. )
  30. )
  31. console.log(chalk.blue(await Language.get(page)))
  32. module.exports.connectionCheck()
  33. console.log(chalk.red('----------'))
  34. },
  35. pause: async (time) => {
  36. await new Promise(resolve => setTimeout(resolve, time));
  37. },
  38. connectionCheck: async () => {
  39. Language.setActiveLang(await settings.value("Lang"))
  40. if (globalVariables.localUSB === "X") {
  41. console.log(chalk.white('MiWatch: ') + chalk.green(await Language.get('connected-via-usb')))
  42. }
  43. if (globalVariables.miWatchIpaddress != "") {
  44. console.log(chalk.white('MiWatch: ') + chalk.green(await Language.get('connected-via-wifi') + ' ' + chalk.white(globalVariables.miWatchIpaddress)))
  45. }
  46. if (globalVariables.localUSB === "" && globalVariables.miWatchIpaddress === "") {
  47. console.log(chalk.white('MiWatch: ') + chalk.red(await Language.get('not-connected')))
  48. }
  49. },
  50. downloadFile: async (url, path) => {
  51. Language.setActiveLang(await settings.value("Lang"))
  52. const res = await fetch(url);
  53. // await module.exports.downloadSize(url)
  54. await new Promise(async (resolve, reject) => {
  55. const fileStream = fs.createWriteStream(path);
  56. res.body.pipe(fileStream);
  57. res.body.on("error", (err) => {
  58. reject(err);
  59. });
  60. fileStream.on("finish", function () {
  61. resolve();
  62. });
  63. });
  64. },
  65. downloadSize: async (url) => {
  66. remote(url, function (err, o) {
  67. console.log(o)
  68. console.log(err)
  69. })
  70. },
  71. getCompatibleAppsList: async () => {
  72. let settings = { method: "Get" };
  73. const response = await fetch("http://kithub.cf/Karl/MiWatchKleaner-APKs/raw/master/compatibleApps.json", settings)
  74. .then(res => res.json())
  75. return response
  76. },
  77. getXiaomiAppsList: async () => {
  78. let settings = { method: "Get" };
  79. const response = await fetch("http://kithub.cf/Karl/MiWatchKleaner/raw/master/data/xiaomiPackageRemovalList.json", settings)
  80. .then(res => res.json())
  81. return response
  82. },
  83. clearApkFolder: async () => {
  84. await shell.rm('-rf', './data/apps/*.apk');
  85. },
  86. log: async (item) => {
  87. logger.info(await Language.get(item, 'en'))
  88. },
  89. print: async (item, colour) => {
  90. Language.setActiveLang(await settings.value("Lang"))
  91. switch (colour) {
  92. case 'green':
  93. console.log(chalk.green(await Language.get(item)))
  94. break;
  95. case 'red':
  96. console.log(chalk.redBright(await Language.get(item)))
  97. break;
  98. case 'whiteBright':
  99. console.log(chalk.whiteBright(await Language.get(item)))
  100. break;
  101. case 'rainbow':
  102. console.log(chalkRainbow(await Language.get(item)))
  103. break;
  104. default:
  105. console.log(chalk.white(await Language.get(item)))
  106. break;
  107. }
  108. },
  109. dualLog: async (item, colour) => {
  110. await module.exports.log(item)
  111. await module.exports.print(item, colour)
  112. }
  113. }