graceful-electron-updater 1.0.1 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,24 +1,24 @@
1
- # electron-updator
1
+ # graceful-electron-updater
2
2
 
3
3
  [![NPM version][npm-image]][npm-url]
4
4
  [![CI][ci-image]][ci-url]
5
5
  [![node version][node-image]][node-url]
6
6
  [![npm download][download-image]][download-url]
7
7
 
8
- [npm-image]: https://img.shields.io/npm/v/electron-updator.svg
9
- [npm-url]: https://npmjs.org/package/electron-updator
10
- [ci-image]: https://github.com/electron-modules/electron-updator/actions/workflows/ci.yml/badge.svg
11
- [ci-url]: https://github.com/electron-modules/electron-updator/actions/workflows/ci.yml
8
+ [npm-image]: https://img.shields.io/npm/v/graceful-electron-updater.svg
9
+ [npm-url]: https://npmjs.org/package/graceful-electron-updater
10
+ [ci-image]: https://github.com/electron-modules/graceful-electron-updater/actions/workflows/ci.yml/badge.svg
11
+ [ci-url]: https://github.com/electron-modules/graceful-electron-updater/actions/workflows/ci.yml
12
12
  [node-image]: https://img.shields.io/badge/node.js-%3E=_16-green.svg
13
13
  [node-url]: http://nodejs.org/download/
14
- [download-image]: https://img.shields.io/npm/dm/electron-updator.svg
15
- [download-url]: https://npmjs.org/package/electron-updator
14
+ [download-image]: https://img.shields.io/npm/dm/graceful-electron-updater.svg
15
+ [download-url]: https://npmjs.org/package/graceful-electron-updater
16
16
 
17
17
  > electron updator
18
18
  ## Installment
19
19
 
20
20
  ```bash
21
- $ npm i electron-updator --save
21
+ $ npm i graceful-electron-updater --save
22
22
  ```
23
23
 
24
24
  <!-- GITCONTRIBUTOR_START -->
@@ -1,33 +1,43 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.AppUpdator = void 0;
4
7
  const eventemitter3_1 = require("eventemitter3");
8
+ const nanoid_1 = require("nanoid");
9
+ const path_1 = __importDefault(require("path"));
5
10
  const constants_1 = require("./common/constants");
6
11
  const download_file_1 = require("./utils/download-file");
7
12
  const utils_1 = require("./utils");
8
13
  const elelctron_app_adapter_1 = require("./elelctron-app-adapter");
14
+ const DEFAULT_EXEFILE_DIR = path_1.default.join(__dirname, '..', 'helper');
9
15
  class AppUpdator extends eventemitter3_1.EventEmitter {
10
16
  constructor(options, app) {
11
17
  super();
12
18
  this.state = constants_1.StateType.Idle;
13
19
  this.options = options;
20
+ this._windowHelperExeDir = this.options?.getWindowsHelperExeDir?.() || DEFAULT_EXEFILE_DIR;
14
21
  this.logger = this._wrapLogger(options.logger);
15
22
  this.app = app || new elelctron_app_adapter_1.ElectronAppAdapter();
16
- this.startTimeStamp = new Date().getTime();
17
- this.logger.info('ElectronUpdator#constructor');
23
+ this.startUuid = this._getStartUuid();
24
+ this.logger.info('constructor');
18
25
  this.availableUpdate = {
19
26
  resourcePath: '',
20
27
  latestAsarPath: '',
21
28
  downloadTargetDir: '',
22
29
  };
23
30
  }
31
+ _getStartUuid() {
32
+ return `${(0, nanoid_1.nanoid)(16)}${Date.now()}`;
33
+ }
24
34
  _wrapLogger(logger) {
25
35
  if (!logger) {
26
36
  logger = console;
27
37
  }
28
38
  const _logger = { ...logger };
29
39
  const _wrap = (message, callback) => {
30
- callback(`ElectronUpdator(${this.startTimeStamp})${message}`);
40
+ callback(`[ElectronUpdator][${this.startUuid}]#${message}`);
31
41
  };
32
42
  _logger.error = (message) => {
33
43
  _wrap(message, logger.error);
@@ -41,28 +51,28 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
41
51
  return _logger;
42
52
  }
43
53
  setState(state) {
44
- this.logger.info(`ElectronUpdator#setState${state}`);
54
+ this.logger.info(`setState:${state}`);
45
55
  this.state = state;
46
56
  }
47
57
  setFeedUrl(url) {
48
- this.logger.info(`ElectronUpdator#setFeedUrl:url is ${url}`);
58
+ this.logger.info(`setFeedUrl:url is ${url}`);
49
59
  if (url && this.options) {
50
60
  this.options.url = url;
51
61
  }
52
62
  }
53
63
  async checkForUpdates(executeType = constants_1.ExecuteType.Auto) {
54
- this.logger.info(`ElectronUpdator#checkForUpdates:state is ${this.state}`);
64
+ this.logger.info(`checkForUpdates:state is ${this.state}`);
55
65
  this.setState(constants_1.StateType.Idle);
56
66
  try {
57
- // 新一轮更新流程,更新 TimeStamp
58
- this.startTimeStamp = new Date().getTime();
67
+ // 新一轮更新流程,更新 startUuid
68
+ this.startUuid = this._getStartUuid();
59
69
  this.setState(constants_1.StateType.CheckingForUpdate);
60
70
  this.emit(constants_1.EventType.CHECKING_FOR_UPDATE);
61
71
  const updateInfoResponse = await (0, utils_1.requestUpdateInfo)(this.options);
62
72
  this.updateInfo = (this.options?.updateInfoFormatter ? this.options?.updateInfoFormatter(updateInfoResponse) : updateInfoResponse);
63
73
  const ifNeedUpdate = this.options?.ifNeedUpdate(updateInfoResponse);
64
74
  if (!ifNeedUpdate) {
65
- this.logger.info(`ElectronUpdator#updateInfo is ${JSON.stringify(this.updateInfo)},ifNeedUpdate is false`);
75
+ this.logger.info(`checkForUpdates:updateInfo is ${JSON.stringify(this.updateInfo)},ifNeedUpdate is false`);
66
76
  this.emit(constants_1.EventType.UPDATE_NOT_AVAILABLE, {
67
77
  updateInfo: this.updateInfo,
68
78
  executeType,
@@ -70,10 +80,10 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
70
80
  this.setState(constants_1.StateType.Idle);
71
81
  return;
72
82
  }
73
- this.logger.info('ElectronUpdator#checkForUpdates:ifNeedUpdate is true');
83
+ this.logger.info('checkForUpdates:ifNeedUpdate is true');
74
84
  this.availableUpdate = this.doGetAvailableUpdateInfo(this.updateInfo);
75
85
  if (!this.options?.autoDownload || executeType === constants_1.ExecuteType.User) {
76
- this.logger.info('ElectronUpdator#checkForUpdates:emit UPDATE_AVAILABLE');
86
+ this.logger.info('checkForUpdates:emit UPDATE_AVAILABLE');
77
87
  this.emit(constants_1.EventType.UPDATE_AVAILABLE, {
78
88
  updateInfo: this.updateInfo,
79
89
  executeType,
@@ -89,11 +99,11 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
89
99
  }
90
100
  }
91
101
  async downloadUpdate(executeType = constants_1.ExecuteType.User) {
92
- this.logger.info(`ElectronUpdator#downloadUpdate:executeType is ${executeType}`);
102
+ this.logger.info(`downloadUpdate:executeType is ${executeType}`);
93
103
  await this.downloadUpdateFile(this.updateInfo, executeType);
94
104
  const result = await this.preCheck(executeType);
95
105
  if (result.success) {
96
- this.logger.info('ElectronUpdator#downloadUpdate:emit UPDATE_DOWNLOADED');
106
+ this.logger.info('downloadUpdate:emit UPDATE_DOWNLOADED');
97
107
  this.emit(constants_1.EventType.UPDATE_DOWNLOADED, {
98
108
  executeType,
99
109
  });
@@ -104,7 +114,7 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
104
114
  }
105
115
  }
106
116
  async quitAndInstall() {
107
- this.logger.info(`ElectronUpdator#quitAndInstall:state is ${this.state}`);
117
+ this.logger.info(`quitAndInstall:state is ${this.state}`);
108
118
  if (this.state !== constants_1.StateType.Downloaded) {
109
119
  this.downloadUpdate();
110
120
  return;
@@ -112,17 +122,14 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
112
122
  this.setState(constants_1.StateType.Idle);
113
123
  try {
114
124
  let result = { success: false };
125
+ this.emit(constants_1.EventType.BEFORE_QUIT_FOR_UPDATE);
115
126
  if (this.updateInfo?.updateType === constants_1.UpdateType.Package) {
116
127
  result = await this.doQuitAndInstallPackage();
117
128
  }
118
129
  else {
119
130
  result = await this.doQuitAndInstallAsar();
120
131
  }
121
- if (result.success) {
122
- this.logger.warn('ElectronUpdator#quitAndInstall:install success');
123
- this.emit(constants_1.EventType.BEFORE_QUIT_FOR_UPDATE);
124
- }
125
- else {
132
+ if (!result.success) {
126
133
  result.message = `error: ${result.error?.message}`;
127
134
  this.dispatchError(result.error);
128
135
  }
@@ -132,21 +139,21 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
132
139
  }
133
140
  }
134
141
  async preCheckForAsar() {
135
- this.logger.info('ElectronUpdator#preCheckForAsar');
142
+ this.logger.info('preCheckForAsar');
136
143
  return await this.unzip();
137
144
  }
138
145
  async preCheck(executeType) {
139
- this.logger.info('ElectronUpdator#preCheck');
146
+ this.logger.info('preCheck');
140
147
  const { resourcePath } = this.availableUpdate;
141
148
  if (this.state !== constants_1.StateType.Downloaded) {
142
149
  return {
143
150
  success: false,
144
- error: new Error(`ElectronUpdator#preCheck:update status(${this.state}) error`),
151
+ error: new Error(`preCheck:update status(${this.state}) error`),
145
152
  };
146
153
  }
147
154
  // 清理老包
148
155
  try {
149
- this.logger.info('ElectronUpdator#preCheck:cleanOldArchive');
156
+ this.logger.info('preCheck:cleanOldArchive');
150
157
  await (0, utils_1.cleanOldArchive)(resourcePath);
151
158
  }
152
159
  catch (e) {
@@ -183,7 +190,7 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
183
190
  }
184
191
  async downloadUpdateFile(updateInfo, executeType) {
185
192
  if (this.state !== constants_1.StateType.CheckingForUpdate) {
186
- throw new Error(`ElectronUpdator#downloadUpdateFile:update status(${this.state}) error`);
193
+ throw new Error(`downloadUpdateFile:update status(${this.state}) error`);
187
194
  }
188
195
  const { url, signature } = updateInfo.files[0];
189
196
  const { downloadTargetDir } = this.availableUpdate;
@@ -199,7 +206,7 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
199
206
  this.emit(constants_1.EventType.UPDATE_DOWNLOAD_PROGRESS, { ...data, executeType });
200
207
  },
201
208
  });
202
- this.logger.info('ElectronUpdator#downloadUpdateFile:Downloaded');
209
+ this.logger.info('downloadUpdateFile:Downloaded');
203
210
  this.setState(constants_1.StateType.Downloaded);
204
211
  }
205
212
  catch (e) {
@@ -209,7 +216,7 @@ class AppUpdator extends eventemitter3_1.EventEmitter {
209
216
  }
210
217
  }
211
218
  async unzip() {
212
- this.logger.info('ElectronUpdator#unzip:start');
219
+ this.logger.info('unzip:start');
213
220
  try {
214
221
  const result = await this.doUnzip();
215
222
  if (!result.success) {
File without changes
File without changes
@@ -36,7 +36,7 @@ class MacUpdator extends app_updator_1.AppUpdator {
36
36
  * @return
37
37
  */
38
38
  async doUnzip() {
39
- this.logger.info('ElectronUpdator#MacUpdator#doUnzip:start');
39
+ this.logger.info('MacUpdator#doUnzip:start');
40
40
  const { resourcePath, downloadTargetDir } = this.availableUpdate;
41
41
  try {
42
42
  // 直接解压
@@ -88,13 +88,20 @@ class MacUpdator extends app_updator_1.AppUpdator {
88
88
  error,
89
89
  };
90
90
  }
91
+ this.logger.warn('quitAndInstall:install success');
92
+ this.app.relaunch();
91
93
  return {
92
94
  success: true,
93
95
  };
94
96
  }
95
97
  async doQuitAndInstallPackage() {
96
98
  this.logger.info('ElectronUpdator#doQuitAndInstallPackage:start');
97
- return await (0, install_macos_dmg_1.default)(this.options, this.logger, this.availableUpdate, this.updateInfo);
99
+ const result = await (0, install_macos_dmg_1.default)(this.options, this.logger, this.availableUpdate, this.updateInfo);
100
+ if (result.success) {
101
+ this.logger.warn('quitAndInstall:install success');
102
+ this.app.relaunch();
103
+ }
104
+ return result;
98
105
  }
99
106
  }
100
107
  exports.MacUpdator = MacUpdator;
@@ -17,7 +17,7 @@ const _1 = require(".");
17
17
  * @return
18
18
  */
19
19
  const downloadFile = async ({ logger, url, signature, targetDir, progressHandle }) => {
20
- logger.info('ElectronUpdator#downloadFile#downloadFile (start)');
20
+ logger.info('downloadFile#downloadFile (start)');
21
21
  const writeStream = (0, _1.createWriteStream)(targetDir);
22
22
  let currentLength = 0;
23
23
  let currentProgress = 0;
@@ -33,7 +33,7 @@ const downloadFile = async ({ logger, url, signature, targetDir, progressHandle
33
33
  })
34
34
  .then((res) => {
35
35
  const totalLength = res.headers['content-length'];
36
- logger.info(`ElectronUpdator#downloadFile#downloadFile (then),totalLength is ${totalLength}`);
36
+ logger.info(`downloadFile#downloadFile (then),totalLength is ${totalLength}`);
37
37
  res.res.on('data', (data) => {
38
38
  try {
39
39
  currentLength += data.length;
@@ -63,7 +63,7 @@ const downloadFile = async ({ logger, url, signature, targetDir, progressHandle
63
63
  url,
64
64
  signature,
65
65
  });
66
- logger.info('ElectronUpdator#download file success, url:%s, to %s', url, targetDir);
66
+ logger.info('downloadFile#download file success, url:%s, to %s', url, targetDir);
67
67
  resolve();
68
68
  }
69
69
  catch (e) {
@@ -115,8 +115,8 @@ const requestUpdateInfo = async (options) => {
115
115
  return res.data;
116
116
  };
117
117
  exports.requestUpdateInfo = requestUpdateInfo;
118
- const getExecuteFile = (file) => {
119
- return path_1.default.join(__dirname, '..', 'libs', file);
118
+ const getExecuteFile = (helperExeFileDir, file) => {
119
+ return path_1.default.join(helperExeFileDir, file);
120
120
  };
121
121
  exports.getExecuteFile = getExecuteFile;
122
122
  //# sourceMappingURL=index.js.map
@@ -2,20 +2,20 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.sudoPromptExec = void 0;
4
4
  const index_1 = require("./index");
5
- function sudoPromptExec(appUpdatorOptions, logger, shell) {
5
+ function sudoPromptExec(logger, shell) {
6
6
  const options = {
7
- name: appUpdatorOptions.productName,
7
+ name: 'SoftwareUpdate',
8
8
  };
9
9
  return new Promise((resolve, reject) => {
10
- logger.warn(`ElectronUpdator#update#sudoPromptExec_shell_${shell}`);
10
+ logger.warn(`sudoPromptExec#_shell_${shell}`);
11
11
  index_1.sudoPrompt.exec(shell, options, (error, stdout) => {
12
12
  if (error) {
13
13
  reject(error);
14
- logger.error(`ElectronUpdator#update#sudoPromptExec_error_${error}`);
14
+ logger.error(`sudoPromptExec#error_${error}`);
15
15
  return;
16
16
  }
17
17
  resolve(stdout);
18
- logger.warn(`ElectronUpdator#update#sudoPromptExec_stdout_${stdout}`);
18
+ logger.warn(`sudoPromptExec#stdout_${stdout}`);
19
19
  });
20
20
  });
21
21
  }
@@ -12,7 +12,7 @@ const app_updator_1 = require("./app-updator");
12
12
  const utils_1 = require("./utils");
13
13
  class WindowsUpdator extends app_updator_1.AppUpdator {
14
14
  doGetAvailableUpdateInfo(updateInfo) {
15
- this.logger.info('ElectronUpdator#WindowsUpdator#doGetAvailableUpdateInfo:start');
15
+ this.logger.info('WindowsUpdator#doGetAvailableUpdateInfo:start');
16
16
  const resourcePath = path_1.default.resolve(this.app.userDataPath);
17
17
  const latestAsarPath = path_1.default.resolve(resourcePath, 'latest.asar');
18
18
  const latestAppPath = path_1.default.resolve(resourcePath, 'latest');
@@ -27,15 +27,15 @@ class WindowsUpdator extends app_updator_1.AppUpdator {
27
27
  };
28
28
  }
29
29
  async doPreCheckForPackage() {
30
- this.logger.info('ElectronUpdator#WindowsUpdator#doPreCheckForPackage:start');
30
+ this.logger.info('WindowsUpdator#doPreCheckForPackage:start');
31
31
  // Windows 全量安装默认预检正常
32
32
  return Promise.resolve({ success: true });
33
33
  }
34
34
  async doUnzip() {
35
- this.logger.info('ElectronUpdator#WindowsUpdator#doUnzip:start');
35
+ this.logger.info('WindowsUpdator#doUnzip:start');
36
36
  try {
37
37
  const { downloadTargetDir, resourcePath } = this.availableUpdate;
38
- const unzipExe = (0, utils_1.getExecuteFile)('unzip.exe');
38
+ const unzipExe = (0, utils_1.getExecuteFile)(this._windowHelperExeDir, 'unzip.exe');
39
39
  const executeCommand = `"${unzipExe}" -o "${downloadTargetDir}" -d "${resourcePath}"`;
40
40
  await (0, utils_1.execAsync)(executeCommand);
41
41
  }
@@ -45,13 +45,13 @@ class WindowsUpdator extends app_updator_1.AppUpdator {
45
45
  error,
46
46
  };
47
47
  }
48
- this.logger.info('ElectronUpdator#WindowsUpdator#doUnzip:success');
48
+ this.logger.info('WindowsUpdator#doUnzip:success');
49
49
  return {
50
50
  success: true,
51
51
  };
52
52
  }
53
53
  async doQuitAndInstallPackage() {
54
- this.logger.info('ElectronUpdator#WindowsUpdator#doQuitAndInstallPackage:success');
54
+ this.logger.info('WindowsUpdator#doQuitAndInstallPackage:success');
55
55
  const { downloadTargetDir } = this.availableUpdate;
56
56
  try {
57
57
  // Windows 全量安装
@@ -60,6 +60,7 @@ class WindowsUpdator extends app_updator_1.AppUpdator {
60
60
  setTimeout(() => {
61
61
  this.app.exit(0);
62
62
  }, 30);
63
+ this.logger.warn('WindowsUpdator#quitAndInstall:install success');
63
64
  return Promise.resolve({ success: true });
64
65
  }
65
66
  catch (error) {
@@ -67,17 +68,16 @@ class WindowsUpdator extends app_updator_1.AppUpdator {
67
68
  }
68
69
  }
69
70
  async doQuitAndInstallAsar() {
70
- this.logger.info('ElectronUpdator#WindowsUpdator#doQuitAndInstallAsar:start');
71
+ this.logger.info('WindowsUpdator#doQuitAndInstallAsar:start');
71
72
  const productName = this.options?.productName;
72
73
  const { resourcePath } = this.availableUpdate;
73
74
  const exePath = this.app.exePath;
74
- const updateExePath = (0, utils_1.getExecuteFile)('installer.exe');
75
+ const updateExePath = (0, utils_1.getExecuteFile)(this._windowHelperExeDir, 'installer.exe');
75
76
  const targetPath = path_1.default.resolve(exePath, '..', 'resources');
76
77
  const executeCommand = `"${updateExePath}" "${targetPath}" "${resourcePath}" "${productName}.exe" "${exePath}"`;
77
78
  try {
78
- await (0, sudo_prompt_exec_1.sudoPromptExec)(this.options, this.logger, executeCommand);
79
- this.logger.warn('ElectronUpdator#quitAndInstall:install success');
80
- this.emit(constants_1.EventType.BEFORE_QUIT_FOR_UPDATE);
79
+ await (0, sudo_prompt_exec_1.sudoPromptExec)(this.logger, executeCommand);
80
+ this.logger.warn('WindowsUpdator#quitAndInstall:install success');
81
81
  return Promise.resolve({ success: true });
82
82
  }
83
83
  catch (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "graceful-electron-updater",
3
- "version": "1.0.1",
4
- "description": "electron-updator is a software updator management solution for Electron applications, It is convenient to complete full software update and dynamic update.",
3
+ "version": "1.0.4",
4
+ "description": "graceful-electron-updater is a software updator management solution for Electron applications, It is convenient to complete full software update and dynamic update.",
5
5
  "scripts": {
6
6
  "build": "sh ./build.sh",
7
7
  "test": "egg-bin test test/**/index.test.ts",
@@ -22,12 +22,13 @@
22
22
  ],
23
23
  "repository": {
24
24
  "type": "git",
25
- "url": "https://github.com/electron-modules/electron-updator"
25
+ "url": "https://github.com/electron-modules/graceful-electron-updater"
26
26
  },
27
27
  "dependencies": {
28
28
  "eventemitter3": "^4.0.0",
29
29
  "lodash": "4",
30
30
  "moment": "2",
31
+ "nanoid": "^3.3.4",
31
32
  "rimraf-alt": "*",
32
33
  "sudo-prompt-alt": "9",
33
34
  "urllib": "2"