hot-updater.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * power by biuuu
  3. */
  4. const chalk = require("chalk");
  5. const { join } = require('path')
  6. const crypto = require('crypto')
  7. const AdmZip = require('adm-zip')
  8. const packageFile = require('../package.json')
  9. const { build } = require("../config/index")
  10. const { platform } = require("os")
  11. const { ensureDir, emptyDir, copy, outputJSON, remove, stat, readFile } = require("fs-extra");
  12. const platformName = platform().includes('win32') ? 'win' : platform().includes('darwin') ? 'mac' : 'linux'
  13. const buildPath = join('.', 'build', `${platformName === 'mac' ? 'mac' : platformName + '-unpacked'}`)
  14. const hash = (data, type = 'sha256') => {
  15. const hmac = crypto.createHmac(type, 'Sky')
  16. hmac.update(data)
  17. return hmac.digest('hex')
  18. }
  19. const createZip = (filePath, dest) => {
  20. const zip = new AdmZip()
  21. zip.addLocalFolder(filePath)
  22. zip.toBuffer()
  23. zip.writeZip(dest)
  24. }
  25. const start = async () => {
  26. console.log(chalk.green.bold(`\n Start packing`))
  27. if (packageFile.build.asar) {
  28. console.log(
  29. "\n" +
  30. chalk.bgRed.white(" ERROR ") +
  31. " " +
  32. chalk.red("Please make sure the build.asar option in the Package.json file is set to false") +
  33. "\n"
  34. );
  35. return;
  36. }
  37. if (build.hotPublishConfigName === '') {
  38. console.log(
  39. "\n" +
  40. chalk.bgRed.white(" ERROR ") +
  41. " " +
  42. chalk.red("HotPublishConfigName is not set, which will cause the update to fail, please set it in the config/index.js \n")
  43. + chalk.red.bold(`\n Packing failed \n`)
  44. );
  45. process.exit(1)
  46. }
  47. stat(join(buildPath, 'resources', 'app'), async (err, stats) => {
  48. if (err) {
  49. console.log(
  50. "\n" +
  51. chalk.bgRed.white(" ERROR ") +
  52. " " +
  53. chalk.red("No resource files were found, please execute this command after the build command") +
  54. "\n"
  55. );
  56. return;
  57. }
  58. try {
  59. const packResourcesPath = join('.', 'build', 'resources', 'dist');
  60. const packPackagePath = join('.', 'build', 'resources');
  61. const resourcesPath = join('.', 'dist');
  62. const appPath = join('.', 'build', 'resources');
  63. const name = "app.zip";
  64. const outputPath = join('.', 'build', 'update');
  65. const zipPath = join(outputPath, name);
  66. await ensureDir(packResourcesPath);
  67. await emptyDir(packResourcesPath);
  68. await copy(resourcesPath, packResourcesPath);
  69. await outputJSON(join(packPackagePath, "package.json"), {
  70. name: packageFile.name,
  71. productName: packageFile.productName,
  72. version: packageFile.version,
  73. private: packageFile.private,
  74. description: packageFile.description,
  75. main: packageFile.main,
  76. author: packageFile.author,
  77. dependencies: packageFile.dependencies
  78. });
  79. await ensureDir(outputPath);
  80. await emptyDir(outputPath);
  81. createZip(appPath, zipPath);
  82. const buffer = await readFile(zipPath);
  83. const sha256 = hash(buffer);
  84. const hashName = sha256.slice(7, 12);
  85. await copy(zipPath, join(outputPath, `${hashName}.zip`));
  86. await remove(zipPath);
  87. await remove(appPath)
  88. await outputJSON(join(outputPath, `${build.hotPublishConfigName}.json`),
  89. {
  90. version: packageFile.version,
  91. name: `${hashName}.zip`,
  92. hash: sha256
  93. }
  94. );
  95. console.log(
  96. "\n" + chalk.bgGreen.white(" DONE ") + " " + "The resource file is packaged!\n"
  97. );
  98. console.log("File location: " + chalk.green(outputPath) + "\n");
  99. } catch (error) {
  100. console.log(
  101. "\n" +
  102. chalk.bgRed.white(" ERROR ") +
  103. " " +
  104. chalk.red(error.message || error) +
  105. "\n"
  106. );
  107. process.exit(1)
  108. }
  109. });
  110. }
  111. start()