electron-incremental-update 0.7.10 → 0.8.1

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/dist/index.d.mts CHANGED
@@ -12,7 +12,7 @@ declare class VerifyFailedError extends Error {
12
12
  constructor(signature: string, cert: string);
13
13
  }
14
14
  declare class DownloadError extends Error {
15
- constructor();
15
+ constructor(msg: string);
16
16
  }
17
17
  declare class IncrementalUpdater implements Updater {
18
18
  private info?;
@@ -111,7 +111,7 @@ type UpdaterOverrideFunctions = {
111
111
  * @param cert certificate
112
112
  * @returns if signature is valid, returns the version or `true` , otherwise returns `false`
113
113
  */
114
- verifySignaure?: (buffer: Buffer, signature: string, cert: string) => string | boolean | Promise<string | boolean>;
114
+ verifySignaure?: (buffer: Buffer, signature: string, cert: string) => string | false | Promise<string | false>;
115
115
  /**
116
116
  * custom download JSON function
117
117
  * @param url download url
@@ -190,6 +190,7 @@ interface UpdaterOption {
190
190
  downloadConfig?: UpdaterDownloadConfig;
191
191
  }
192
192
 
193
+ type Promisable<T> = T | Promise<T>;
193
194
  type AppOption = {
194
195
  /**
195
196
  * path of electron output dist when in development
@@ -201,31 +202,39 @@ type AppOption = {
201
202
  * @default 'main/index.js'
202
203
  */
203
204
  mainPath?: string;
204
- /**
205
- * hooks for start up
206
- */
207
- onStart?: (productAsarPath: string) => void;
208
- /**
209
- * hooks for start up error
210
- */
211
- onStartError?: (err: unknown) => void;
205
+ hooks?: {
206
+ /**
207
+ * hooks before replace the old asar is replaced by the new asar
208
+ *
209
+ * @param oldAsarPath old asar path
210
+ * @param updateTempAsarPath new asar path, end with .tmp
211
+ */
212
+ beforeDoUpdate?: (oldAsarPath: string, updateTempAsarPath: string) => Promisable<void>;
213
+ /**
214
+ * hooks before start up
215
+ */
216
+ beforeStart?: (productAsarPath: string) => Promisable<void>;
217
+ /**
218
+ * hooks on start up error
219
+ */
220
+ onStartError?: (err: unknown) => void;
221
+ };
212
222
  };
213
223
  type StartupWithUpdater = (updater: Updater) => void;
214
224
  type SetUpdater = {
215
225
  /**
216
226
  * set updater option or create function
217
227
  */
218
- setUpdater: (updater: (() => Updater | Promise<Updater>) | UpdaterOption) => void;
228
+ setUpdater: (updater: (() => Promisable<Updater>) | UpdaterOption) => void;
219
229
  };
220
230
  /**
221
- * create updater manually
231
+ * initialize app
222
232
  * @example
223
233
  * ```ts
224
234
  * import { getGithubReleaseCdnGroup, initApp, parseGithubCdnURL } from 'electron-incremental-update'
225
235
  * import { name, repository } from '../package.json'
226
236
  *
227
237
  * const SIGNATURE_CERT = '' // auto generate certificate when start app
228
- *
229
238
  * const { cdnPrefix: asarPrefix } = getGithubReleaseCdnGroup()[0]
230
239
  * const { cdnPrefix: jsonPrefix } = getGithubFileCdnGroup()[0]
231
240
  * initApp({ onStart: console.log })
package/dist/index.d.ts CHANGED
@@ -12,7 +12,7 @@ declare class VerifyFailedError extends Error {
12
12
  constructor(signature: string, cert: string);
13
13
  }
14
14
  declare class DownloadError extends Error {
15
- constructor();
15
+ constructor(msg: string);
16
16
  }
17
17
  declare class IncrementalUpdater implements Updater {
18
18
  private info?;
@@ -111,7 +111,7 @@ type UpdaterOverrideFunctions = {
111
111
  * @param cert certificate
112
112
  * @returns if signature is valid, returns the version or `true` , otherwise returns `false`
113
113
  */
114
- verifySignaure?: (buffer: Buffer, signature: string, cert: string) => string | boolean | Promise<string | boolean>;
114
+ verifySignaure?: (buffer: Buffer, signature: string, cert: string) => string | false | Promise<string | false>;
115
115
  /**
116
116
  * custom download JSON function
117
117
  * @param url download url
@@ -190,6 +190,7 @@ interface UpdaterOption {
190
190
  downloadConfig?: UpdaterDownloadConfig;
191
191
  }
192
192
 
193
+ type Promisable<T> = T | Promise<T>;
193
194
  type AppOption = {
194
195
  /**
195
196
  * path of electron output dist when in development
@@ -201,31 +202,39 @@ type AppOption = {
201
202
  * @default 'main/index.js'
202
203
  */
203
204
  mainPath?: string;
204
- /**
205
- * hooks for start up
206
- */
207
- onStart?: (productAsarPath: string) => void;
208
- /**
209
- * hooks for start up error
210
- */
211
- onStartError?: (err: unknown) => void;
205
+ hooks?: {
206
+ /**
207
+ * hooks before replace the old asar is replaced by the new asar
208
+ *
209
+ * @param oldAsarPath old asar path
210
+ * @param updateTempAsarPath new asar path, end with .tmp
211
+ */
212
+ beforeDoUpdate?: (oldAsarPath: string, updateTempAsarPath: string) => Promisable<void>;
213
+ /**
214
+ * hooks before start up
215
+ */
216
+ beforeStart?: (productAsarPath: string) => Promisable<void>;
217
+ /**
218
+ * hooks on start up error
219
+ */
220
+ onStartError?: (err: unknown) => void;
221
+ };
212
222
  };
213
223
  type StartupWithUpdater = (updater: Updater) => void;
214
224
  type SetUpdater = {
215
225
  /**
216
226
  * set updater option or create function
217
227
  */
218
- setUpdater: (updater: (() => Updater | Promise<Updater>) | UpdaterOption) => void;
228
+ setUpdater: (updater: (() => Promisable<Updater>) | UpdaterOption) => void;
219
229
  };
220
230
  /**
221
- * create updater manually
231
+ * initialize app
222
232
  * @example
223
233
  * ```ts
224
234
  * import { getGithubReleaseCdnGroup, initApp, parseGithubCdnURL } from 'electron-incremental-update'
225
235
  * import { name, repository } from '../package.json'
226
236
  *
227
237
  * const SIGNATURE_CERT = '' // auto generate certificate when start app
228
- *
229
238
  * const { cdnPrefix: asarPrefix } = getGithubReleaseCdnGroup()[0]
230
239
  * const { cdnPrefix: jsonPrefix } = getGithubFileCdnGroup()[0]
231
240
  * initApp({ onStart: console.log })
package/dist/index.js CHANGED
@@ -241,8 +241,8 @@ var VerifyFailedError = class extends Error {
241
241
  }
242
242
  };
243
243
  var DownloadError = class extends Error {
244
- constructor() {
245
- super("download update error");
244
+ constructor(msg) {
245
+ super(`download update error, ${msg}`);
246
246
  }
247
247
  };
248
248
  var IncrementalUpdater = class {
@@ -334,7 +334,7 @@ var IncrementalUpdater = class {
334
334
  this.logger?.info(`download ${format} success${format === "buffer" ? `, file size: ${ret.length}` : ""}`);
335
335
  return ret;
336
336
  } catch (e) {
337
- throw new DownloadError();
337
+ throw new DownloadError(e.toString());
338
338
  }
339
339
  }
340
340
  async checkUpdate(data) {
@@ -383,7 +383,7 @@ var IncrementalUpdater = class {
383
383
  await (0, import_promises.writeFile)(this.gzipPath, buffer);
384
384
  this.logger?.info(`extract to ${this.tmpFilePath}`);
385
385
  await unzipFile(this.gzipPath, this.tmpFilePath);
386
- this.logger?.info(`download success${typeof _ver === "string" ? `, version: ${_ver}` : ""}`);
386
+ this.logger?.info(`download success, version: ${_ver}`);
387
387
  this.info = void 0;
388
388
  return true;
389
389
  } catch (error) {
@@ -401,22 +401,28 @@ function initApp(appOptions) {
401
401
  const {
402
402
  electronDevDistPath = "dist-electron",
403
403
  mainPath = "main/index.js",
404
- onStart,
405
- onStartError
404
+ hooks
406
405
  } = appOptions || {};
406
+ const {
407
+ beforeDoUpdate,
408
+ beforeStart,
409
+ onStartError
410
+ } = hooks || {};
407
411
  function handleError(msg) {
408
412
  onStartError?.(new Error(msg));
409
413
  import_electron3.app.quit();
410
414
  }
411
- function startup(updater) {
415
+ async function startup(updater) {
412
416
  try {
413
417
  const asarPath = getProductAsarPath(updater.productName);
414
- if ((0, import_node_fs3.existsSync)(`${asarPath}.tmp`)) {
415
- (0, import_node_fs3.renameSync)(`${asarPath}.tmp`, asarPath);
418
+ const updateAsarPath = `${asarPath}.tmp`;
419
+ if ((0, import_node_fs3.existsSync)(updateAsarPath)) {
420
+ await beforeDoUpdate?.(asarPath, updateAsarPath);
421
+ (0, import_node_fs3.renameSync)(updateAsarPath, asarPath);
416
422
  }
417
423
  const mainDir = import_electron3.app.isPackaged ? asarPath : electronDevDistPath;
418
424
  const entry = (0, import_node_path2.resolve)(__dirname, mainDir, mainPath);
419
- onStart?.(entry);
425
+ await beforeStart?.(entry);
420
426
  require(entry)(updater);
421
427
  } catch (error) {
422
428
  handleError(`failed to start app, ${error}`);
@@ -429,9 +435,9 @@ function initApp(appOptions) {
429
435
  async setUpdater(updater) {
430
436
  clearTimeout(timer);
431
437
  if (typeof updater === "object") {
432
- startup(createUpdater(updater));
438
+ await startup(createUpdater(updater));
433
439
  } else if (typeof updater === "function") {
434
- startup(await updater());
440
+ await startup(await updater());
435
441
  } else {
436
442
  handleError("invalid updater option or updater is not a function");
437
443
  }
package/dist/index.mjs CHANGED
@@ -135,8 +135,8 @@ var VerifyFailedError = class extends Error {
135
135
  }
136
136
  };
137
137
  var DownloadError = class extends Error {
138
- constructor() {
139
- super("download update error");
138
+ constructor(msg) {
139
+ super(`download update error, ${msg}`);
140
140
  }
141
141
  };
142
142
  var IncrementalUpdater = class {
@@ -228,7 +228,7 @@ var IncrementalUpdater = class {
228
228
  this.logger?.info(`download ${format} success${format === "buffer" ? `, file size: ${ret.length}` : ""}`);
229
229
  return ret;
230
230
  } catch (e) {
231
- throw new DownloadError();
231
+ throw new DownloadError(e.toString());
232
232
  }
233
233
  }
234
234
  async checkUpdate(data) {
@@ -277,7 +277,7 @@ var IncrementalUpdater = class {
277
277
  await writeFile(this.gzipPath, buffer);
278
278
  this.logger?.info(`extract to ${this.tmpFilePath}`);
279
279
  await unzipFile(this.gzipPath, this.tmpFilePath);
280
- this.logger?.info(`download success${typeof _ver === "string" ? `, version: ${_ver}` : ""}`);
280
+ this.logger?.info(`download success, version: ${_ver}`);
281
281
  this.info = void 0;
282
282
  return true;
283
283
  } catch (error) {
@@ -295,22 +295,28 @@ function initApp(appOptions) {
295
295
  const {
296
296
  electronDevDistPath = "dist-electron",
297
297
  mainPath = "main/index.js",
298
- onStart,
299
- onStartError
298
+ hooks
300
299
  } = appOptions || {};
300
+ const {
301
+ beforeDoUpdate,
302
+ beforeStart,
303
+ onStartError
304
+ } = hooks || {};
301
305
  function handleError(msg) {
302
306
  onStartError?.(new Error(msg));
303
307
  app.quit();
304
308
  }
305
- function startup(updater) {
309
+ async function startup(updater) {
306
310
  try {
307
311
  const asarPath = getProductAsarPath(updater.productName);
308
- if (existsSync2(`${asarPath}.tmp`)) {
309
- renameSync(`${asarPath}.tmp`, asarPath);
312
+ const updateAsarPath = `${asarPath}.tmp`;
313
+ if (existsSync2(updateAsarPath)) {
314
+ await beforeDoUpdate?.(asarPath, updateAsarPath);
315
+ renameSync(updateAsarPath, asarPath);
310
316
  }
311
317
  const mainDir = app.isPackaged ? asarPath : electronDevDistPath;
312
318
  const entry = resolve(__dirname, mainDir, mainPath);
313
- onStart?.(entry);
319
+ await beforeStart?.(entry);
314
320
  __require(entry)(updater);
315
321
  } catch (error) {
316
322
  handleError(`failed to start app, ${error}`);
@@ -323,9 +329,9 @@ function initApp(appOptions) {
323
329
  async setUpdater(updater) {
324
330
  clearTimeout(timer);
325
331
  if (typeof updater === "object") {
326
- startup(createUpdater(updater));
332
+ await startup(createUpdater(updater));
327
333
  } else if (typeof updater === "function") {
328
- startup(await updater());
334
+ await startup(await updater());
329
335
  } else {
330
336
  handleError("invalid updater option or updater is not a function");
331
337
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron-incremental-update",
3
3
  "author": "subframe7536",
4
- "version": "0.7.10",
4
+ "version": "0.8.1",
5
5
  "description": "electron incremental update tools, powered by vite",
6
6
  "scripts": {
7
7
  "build": "tsup && node fix-module.js",