common.js 3.9 KB

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