BaseUpdater.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.BaseUpdater = void 0;
  6. function _bluebirdLst() {
  7. const data = require("bluebird-lst");
  8. _bluebirdLst = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _builderUtilRuntime() {
  14. const data = require("builder-util-runtime");
  15. _builderUtilRuntime = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _fsExtraP() {
  21. const data = require("fs-extra-p");
  22. _fsExtraP = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. var path = _interopRequireWildcard(require("path"));
  28. function _AppUpdater() {
  29. const data = require("./AppUpdater");
  30. _AppUpdater = function () {
  31. return data;
  32. };
  33. return data;
  34. }
  35. function _main() {
  36. const data = require("./main");
  37. _main = function () {
  38. return data;
  39. };
  40. return data;
  41. }
  42. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  43. class BaseUpdater extends _AppUpdater().AppUpdater {
  44. constructor(options, app) {
  45. super(options, app);
  46. this.quitAndInstallCalled = false;
  47. this.quitHandlerAdded = false;
  48. }
  49. quitAndInstall(isSilent = false, isForceRunAfter = false) {
  50. var _this = this;
  51. return (0, _bluebirdLst().coroutine)(function* () {
  52. _this._logger.info(`Install on explicit quitAndInstall`);
  53. const isInstalled = yield _this.install(isSilent, isSilent ? isForceRunAfter : true);
  54. if (isInstalled) {
  55. setImmediate(() => {
  56. if (_this.app.quit !== undefined) {
  57. _this.app.quit();
  58. }
  59. });
  60. } else {
  61. _this.quitAndInstallCalled = false;
  62. }
  63. })();
  64. }
  65. executeDownload(taskOptions) {
  66. var _this2 = this;
  67. return (0, _bluebirdLst().coroutine)(function* () {
  68. if (_this2.listenerCount(_main().DOWNLOAD_PROGRESS) > 0) {
  69. taskOptions.downloadOptions.onProgress = it => _this2.emit(_main().DOWNLOAD_PROGRESS, it);
  70. }
  71. const updateInfo = taskOptions.updateInfo;
  72. const version = updateInfo.version;
  73. const fileInfo = taskOptions.fileInfo;
  74. const packageInfo = fileInfo.packageInfo;
  75. const cacheDir = _this2.downloadedUpdateHelper.cacheDir;
  76. yield (0, _fsExtraP().ensureDir)(cacheDir);
  77. const updateFileName = taskOptions.fileExtension === "AppImage" ? path.basename(updateInfo.path) : `installer-${version}.${taskOptions.fileExtension}`;
  78. const updateFile = path.join(cacheDir, updateFileName);
  79. const packageFile = packageInfo == null ? null : path.join(cacheDir, `package-${version}.${path.extname(packageInfo.path) || "7z"}`);
  80. const done = () => {
  81. _this2.downloadedUpdateHelper.setDownloadedFile(updateFile, packageFile, updateInfo, fileInfo);
  82. _this2.addQuitHandler();
  83. _this2.emit(_main().UPDATE_DOWNLOADED, updateInfo);
  84. return packageFile == null ? [updateFile] : [updateFile, packageFile];
  85. };
  86. const log = _this2._logger;
  87. if (yield _this2.downloadedUpdateHelper.validateDownloadedPath(updateFile, updateInfo, fileInfo, log)) {
  88. return done();
  89. }
  90. const removeFileIfAny = () => {
  91. _this2.downloadedUpdateHelper.clear();
  92. return (0, _fsExtraP().unlink)(updateFile).catch(() => {// ignored
  93. });
  94. }; // https://github.com/electron-userland/electron-builder/pull/2474#issuecomment-366481912
  95. let nameCounter = 0;
  96. let tempUpdateFile = path.join(cacheDir, `temp-${updateFileName}`);
  97. for (let i = 0; i < 3; i++) {
  98. try {
  99. yield (0, _fsExtraP().unlink)(tempUpdateFile);
  100. } catch (e) {
  101. if (e.code === "ENOENT") {
  102. break;
  103. }
  104. log.warn(`Error on remove temp update file: ${e}`);
  105. tempUpdateFile = path.join(cacheDir, `temp-${nameCounter++}-${updateFileName}`);
  106. }
  107. }
  108. try {
  109. yield taskOptions.task(tempUpdateFile, packageFile, removeFileIfAny);
  110. yield (0, _fsExtraP().rename)(tempUpdateFile, updateFile);
  111. } catch (e) {
  112. yield removeFileIfAny();
  113. if (e instanceof _builderUtilRuntime().CancellationError) {
  114. log.info("Cancelled");
  115. _this2.emit("update-cancelled", updateInfo);
  116. }
  117. throw e;
  118. }
  119. log.info(`New version ${version} has been downloaded to ${updateFile}`);
  120. return done();
  121. })();
  122. }
  123. install(isSilent, isRunAfter) {
  124. var _this3 = this;
  125. return (0, _bluebirdLst().coroutine)(function* () {
  126. if (_this3.quitAndInstallCalled) {
  127. _this3._logger.warn("install call ignored: quitAndInstallCalled is set to true");
  128. return false;
  129. }
  130. const installerPath = _this3.downloadedUpdateHelper.file; // todo check (for now it is ok to no check as before, cached (from previous launch) update file checked in any case)
  131. // const isValid = await this.isUpdateValid(installerPath)
  132. if (installerPath == null) {
  133. _this3.dispatchError(new Error("No valid update available, can't quit and install"));
  134. return false;
  135. } // prevent calling several times
  136. _this3.quitAndInstallCalled = true;
  137. try {
  138. _this3._logger.info(`Install: isSilent: ${isSilent}, isRunAfter: ${isRunAfter}`);
  139. return _this3.doInstall(installerPath, isSilent, isRunAfter);
  140. } catch (e) {
  141. _this3.dispatchError(e);
  142. return false;
  143. }
  144. })();
  145. }
  146. addQuitHandler() {
  147. var _this4 = this;
  148. if (this.quitHandlerAdded || !this.autoInstallOnAppQuit) {
  149. return;
  150. }
  151. this.quitHandlerAdded = true;
  152. this.app.once("quit", (0, _bluebirdLst().coroutine)(function* () {
  153. if (!_this4.quitAndInstallCalled) {
  154. _this4._logger.info("Auto install update on quit");
  155. yield _this4.install(true, false);
  156. } else {
  157. _this4._logger.info("Update installer has already been triggered. Quitting application.");
  158. }
  159. }));
  160. }
  161. } exports.BaseUpdater = BaseUpdater;
  162. //# sourceMappingURL=BaseUpdater.js.map