electron-incremental-update 0.1.5 → 0.1.6

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
@@ -50,6 +50,8 @@ src
50
50
 
51
51
  ### setup app
52
52
 
53
+ more example see comment on `initApp()`
54
+
53
55
  ```ts
54
56
  // electron/app.ts
55
57
  import { createUpdater, initApp } from 'electron-incremental-update'
@@ -143,55 +145,31 @@ db.close()
143
145
  ### setup vite.config.ts
144
146
 
145
147
  ```ts
146
- import { rmSync } from 'node:fs'
147
- import { defineConfig } from 'vite'
148
- import electron from 'vite-plugin-electron'
149
- import updater from 'electron-incremental-update/vite'
150
- import pkg from './package.json'
151
-
152
- // https://vitejs.dev/config/
153
-
154
148
  export default defineConfig(({ command }) => {
155
- rmSync('dist-electron', { recursive: true, force: true })
156
149
 
157
- const isServe = command === 'serve'
158
150
  const isBuild = command === 'build'
159
- const sourcemap = isServe || !!process.env.VSCODE_DEBUG
151
+ // ...
160
152
 
161
153
  return {
162
154
  plugins: [
163
155
  electron([
156
+ // main
164
157
  {
165
- // Main-Process entry file of the Electron App.
166
- entry: ['electron/main/index.ts'],
167
- onstart(options) {
168
- if (process.env.VSCODE_DEBUG) {
169
- console.log(/* For `.vscode/.debug.script.mjs` */'[startup] Electron App')
170
- } else {
171
- options.startup()
172
- }
173
- },
158
+ // ...
159
+ },
160
+ // preload
161
+ {
162
+ // ...
174
163
  vite: {
175
164
  plugins: [
176
- updater({ // options see below
165
+ updater({ // !make sure the plugin run pack asar after all build finish
177
166
  productName: pkg.name,
178
167
  version: pkg.version,
179
168
  isBuild,
180
169
  }),
181
170
  ],
182
- build: {
183
- sourcemap,
184
- minify: false,
185
- outDir: 'dist-electron/main',
186
- rollupOptions: {
187
- external: Object.keys('dependencies' in pkg ? pkg.dependencies : {}),
188
- treeshake: true,
189
- },
190
- },
191
- },
192
- },
193
- {
194
- // ...preload
171
+ // ...
172
+ }
195
173
  },
196
174
  ]),
197
175
  // ... other plugins
@@ -201,7 +179,7 @@ export default defineConfig(({ command }) => {
201
179
  })
202
180
  ```
203
181
 
204
- #### option
182
+ #### plugin options
205
183
 
206
184
  ```ts
207
185
  type Options = {
package/dist/index.cjs CHANGED
@@ -34,7 +34,7 @@ __export(src_exports, {
34
34
  getAppAsarPath: () => getAppAsarPath,
35
35
  getAppVersion: () => getAppVersion,
36
36
  getElectronVersion: () => getElectronVersion,
37
- getReleaseDnsPrefix: () => getReleaseDnsPrefix,
37
+ getReleaseCdnLink: () => getReleaseCdnLink,
38
38
  initApp: () => initApp,
39
39
  requireNative: () => requireNative
40
40
  });
@@ -114,24 +114,31 @@ function requireNative(packageName) {
114
114
  const path = import_electron.app.isPackaged ? (0, import_node_path.join)(import_electron.app.getAppPath(), "node_modules", packageName) : packageName;
115
115
  return require(path);
116
116
  }
117
- function getReleaseDnsPrefix() {
118
- const hub = "https://github.com";
117
+ function getReleaseCdnLink(url) {
118
+ const hub = "https://github.com/";
119
+ if (!url.startsWith(hub)) {
120
+ throw new Error("URL must start with 'https://github.com/'");
121
+ }
122
+ if (url.endsWith("/")) {
123
+ url = url.slice(0, -1);
124
+ }
125
+ const _url = url.replace(hub, "");
119
126
  return [
120
- { urlPrefix: `https://gh.gh2233.ml/${hub}`, maintainer: "@X.I.U/XIU2" },
121
- { urlPrefix: `https://ghproxy.com/${hub}`, maintainer: "gh-proxy" },
122
- { urlPrefix: `https://gh.ddlc.top/${hub}`, maintainer: "@mtr-static-official" },
123
- { urlPrefix: `https://ghdl.feizhuqwq.cf/${hub}`, maintainer: "feizhuqwq.com" },
124
- { urlPrefix: `https://slink.ltd/${hub}`, maintainer: "\u77E5\u4E86\u5C0F\u7AD9" },
125
- { urlPrefix: `https://git.xfj0.cn/${hub}`, maintainer: "anonymous1" },
126
- { urlPrefix: `https://gh.con.sh/${hub}`, maintainer: "anonymous2" },
127
- { urlPrefix: `https://ghps.cc/${hub}`, maintainer: "anonymous3" },
128
- { urlPrefix: "https://cors.isteed.cc/github.com", maintainer: "Lufs's" },
129
- { urlPrefix: `https://hub.gitmirror.com/${hub}`, maintainer: "GitMirror" },
130
- { urlPrefix: `https://js.xxooo.ml/${hub}`, maintainer: "\u996D\u592A\u786C" },
131
- { urlPrefix: `https://proxy.freecdn.ml/?url=${hub}`, maintainer: "anonymous4" },
132
- { urlPrefix: "https://download.njuu.cf", maintainer: "LibraryCloud-njuu" },
133
- { urlPrefix: "https://download.yzuu.cf", maintainer: "LibraryCloud-yzuu" },
134
- { urlPrefix: "https://download.nuaa.cf", maintainer: "LibraryCloud-nuaa" }
127
+ { urlPrefix: `https://gh.gh2233.ml/${url}`, maintainer: "@X.I.U/XIU2" },
128
+ { urlPrefix: `https://ghproxy.com/${url}`, maintainer: "gh-proxy" },
129
+ { urlPrefix: `https://gh.ddlc.top/${url}`, maintainer: "@mtr-static-official" },
130
+ { urlPrefix: `https://ghdl.feizhuqwq.cf/${url}`, maintainer: "feizhuqwq.com" },
131
+ { urlPrefix: `https://slink.ltd/${url}`, maintainer: "\u77E5\u4E86\u5C0F\u7AD9" },
132
+ { urlPrefix: `https://git.xfj0.cn/${url}`, maintainer: "anonymous1" },
133
+ { urlPrefix: `https://gh.con.sh/${url}`, maintainer: "anonymous2" },
134
+ { urlPrefix: `https://ghps.cc/${url}`, maintainer: "anonymous3" },
135
+ { urlPrefix: `https://cors.isteed.cc/github.com/${_url}`, maintainer: "Lufs's" },
136
+ { urlPrefix: `https://hub.gitmirror.com/${url}`, maintainer: "GitMirror" },
137
+ { urlPrefix: `https://js.xxooo.ml/${url}`, maintainer: "\u996D\u592A\u786C" },
138
+ { urlPrefix: `https://proxy.freecdn.ml/?url=${url}`, maintainer: "anonymous4" },
139
+ { urlPrefix: `https://download.njuu.cf/${_url}`, maintainer: "LibraryCloud-njuu" },
140
+ { urlPrefix: `https://download.yzuu.cf/${_url}`, maintainer: "LibraryCloud-yzuu" },
141
+ { urlPrefix: `https://download.nuaa.cf/${_url}`, maintainer: "LibraryCloud-nuaa" }
135
142
  ];
136
143
  }
137
144
 
@@ -245,7 +252,14 @@ function initApp(productName, updater, option) {
245
252
  } = option ?? {};
246
253
  const mainDir = import_electron3.app.isPackaged ? `../${productName}.asar` : electronDistPath;
247
254
  const entry = (0, import_node_path2.resolve)(__dirname, mainDir, mainPath);
248
- return require(entry)(updater);
255
+ let _updater;
256
+ if ("SIGNATURE_PUB" in updater) {
257
+ const _option = updater.productName ? updater : { ...updater, productName };
258
+ _updater = createUpdater(_option);
259
+ } else {
260
+ _updater = updater;
261
+ }
262
+ return require(entry)(_updater);
249
263
  }
250
264
  // Annotate the CommonJS export names for ESM import in node:
251
265
  0 && (module.exports = {
@@ -253,7 +267,7 @@ function initApp(productName, updater, option) {
253
267
  getAppAsarPath,
254
268
  getAppVersion,
255
269
  getElectronVersion,
256
- getReleaseDnsPrefix,
270
+ getReleaseCdnLink,
257
271
  initApp,
258
272
  requireNative
259
273
  });
package/dist/index.d.ts CHANGED
@@ -15,7 +15,7 @@ type UpdateJSON = {
15
15
  size: number;
16
16
  };
17
17
  type MaybeArray<T> = T extends undefined | null | never ? [] : T extends any[] ? T['length'] extends 1 ? [data: T[0]] : T : [data: T];
18
- interface UpdateOption {
18
+ interface BaseOption {
19
19
  /**
20
20
  * URL of version info json
21
21
  * @default `${repository.replace('github.com', 'raw.githubusercontent.com')}/version.json`
@@ -37,10 +37,10 @@ interface TypedUpdater<T extends Record<string | symbol, MaybeArray<any>>, Event
37
37
  once<E extends Event>(eventName: E, listener: (...args: MaybeArray<T[E]>) => void): this;
38
38
  emit<E extends Event>(eventName: E, ...args: MaybeArray<T[E]>): boolean;
39
39
  off<E extends Event>(eventName: E, listener: (...args: MaybeArray<T[E]>) => void): this;
40
- checkUpdate(options?: UpdateOption): Promise<void>;
40
+ checkUpdate(options?: BaseOption): Promise<void>;
41
41
  }
42
42
  type Updater = TypedUpdater<UpdateEvents>;
43
- interface Options extends UpdateOption {
43
+ interface UpdaterOption extends BaseOption {
44
44
  /**
45
45
  * public key of signature
46
46
  *
@@ -101,18 +101,36 @@ interface Options extends UpdateOption {
101
101
  };
102
102
  }
103
103
 
104
+ /**
105
+ * get the application asar absolute path
106
+ * @param name The name of the application
107
+ */
104
108
  declare function getAppAsarPath(name: string): string;
109
+ /**
110
+ * get the version of electron
111
+ */
105
112
  declare function getElectronVersion(): string;
113
+ /**
114
+ * get the version of application
115
+ * @param name - The name of the application
116
+ */
106
117
  declare function getAppVersion(name: string): string;
118
+ /**
119
+ * require native package from app.asar
120
+ * @param packageName native package name
121
+ */
107
122
  declare function requireNative<T = any>(packageName: string): T;
108
- declare function getReleaseDnsPrefix(): {
123
+ /**
124
+ * get group of parsed github release CDN links for accelerating the speed of downloading release
125
+ */
126
+ declare function getReleaseCdnLink(url: string): {
109
127
  urlPrefix: string;
110
128
  maintainer: string;
111
129
  }[];
112
130
 
113
- declare function createUpdater({ SIGNATURE_PUB, repository, productName, releaseAsarURL: _release, updateJsonURL: _update, downloadConfig, }: Options): Updater;
131
+ declare function createUpdater({ SIGNATURE_PUB, repository, productName, releaseAsarURL: _release, updateJsonURL: _update, downloadConfig, }: UpdaterOption): Updater;
114
132
 
115
- interface PathConfig {
133
+ interface AppOption {
116
134
  /**
117
135
  * path of electron output dist
118
136
  * @default 'dist-electron'
@@ -127,10 +145,46 @@ interface PathConfig {
127
145
  /**
128
146
  * Initialize application
129
147
  * @param productName name of your application
130
- * @param updater updater
131
- * @param option options for entry, will be used to generate electron main path, default: `dist-electron/main/index.js`
148
+ * @param updater updater instance or updater options
149
+ * @param option options for entry, will be used to generate electron main path, default target path: `dist-electron/main/index.js`
132
150
  * @returns a function to init your application with a updater
133
- */
134
- declare function initApp(productName: string, updater: Updater, option?: PathConfig): any;
151
+ *
152
+ * @example
153
+ * **manual** generate updater
154
+ * ```ts
155
+ * import { initApp } from 'electron-incremental-updater'
156
+ * import { name, repository } from '../package.json'
157
+ *
158
+ * const SIGNATURE_PUB = '' // auto generate RSA public key when start app
159
+ * const updater = createUpdater({
160
+ * SIGNATURE_PUB,
161
+ * productName: name,
162
+ * repository,
163
+ * })
164
+ * initApp(name, updater)
165
+ * ```
166
+ * @example
167
+ * **auto** generate updater and set update URL
168
+ *
169
+ * ```ts
170
+ * import { getReleaseDnsPrefix, initApp } from 'electron-incremental-update'
171
+ * import { name, repository } from '../package.json'
172
+ *
173
+ * const SIGNATURE_PUB = '' // auto generate RSA public key when start app
174
+ *
175
+ * const { urlPrefix } = getReleaseCdnPrefix()[0]
176
+ * initApp(name, {
177
+ * SIGNATURE_PUB,
178
+ * repository,
179
+ * updateJsonURL: `https://cdn.jsdelivr.net/gh/${repository.replace('https://github.com', '')}/version.json`,
180
+ * releaseAsarURL: `${urlPrefix}/download/latest/${name}.asar.gz`,
181
+ * }, {
182
+ * // options for main entry
183
+ * })
184
+ * ```
185
+ */
186
+ declare function initApp(productName: string, updater: Updater | Omit<UpdaterOption, 'productName'> & {
187
+ productName?: string;
188
+ }, option?: AppOption): any;
135
189
 
136
- export { CheckResultType, Options, UpdateJSON, UpdateOption, Updater, createUpdater, getAppAsarPath, getAppVersion, getElectronVersion, getReleaseDnsPrefix, initApp, requireNative };
190
+ export { BaseOption, CheckResultType, UpdateJSON, Updater, UpdaterOption, createUpdater, getAppAsarPath, getAppVersion, getElectronVersion, getReleaseCdnLink, initApp, requireNative };
package/dist/index.mjs CHANGED
@@ -78,24 +78,31 @@ function requireNative(packageName) {
78
78
  const path = app.isPackaged ? join(app.getAppPath(), "node_modules", packageName) : packageName;
79
79
  return __require(path);
80
80
  }
81
- function getReleaseDnsPrefix() {
82
- const hub = "https://github.com";
81
+ function getReleaseCdnLink(url) {
82
+ const hub = "https://github.com/";
83
+ if (!url.startsWith(hub)) {
84
+ throw new Error("URL must start with 'https://github.com/'");
85
+ }
86
+ if (url.endsWith("/")) {
87
+ url = url.slice(0, -1);
88
+ }
89
+ const _url = url.replace(hub, "");
83
90
  return [
84
- { urlPrefix: `https://gh.gh2233.ml/${hub}`, maintainer: "@X.I.U/XIU2" },
85
- { urlPrefix: `https://ghproxy.com/${hub}`, maintainer: "gh-proxy" },
86
- { urlPrefix: `https://gh.ddlc.top/${hub}`, maintainer: "@mtr-static-official" },
87
- { urlPrefix: `https://ghdl.feizhuqwq.cf/${hub}`, maintainer: "feizhuqwq.com" },
88
- { urlPrefix: `https://slink.ltd/${hub}`, maintainer: "\u77E5\u4E86\u5C0F\u7AD9" },
89
- { urlPrefix: `https://git.xfj0.cn/${hub}`, maintainer: "anonymous1" },
90
- { urlPrefix: `https://gh.con.sh/${hub}`, maintainer: "anonymous2" },
91
- { urlPrefix: `https://ghps.cc/${hub}`, maintainer: "anonymous3" },
92
- { urlPrefix: "https://cors.isteed.cc/github.com", maintainer: "Lufs's" },
93
- { urlPrefix: `https://hub.gitmirror.com/${hub}`, maintainer: "GitMirror" },
94
- { urlPrefix: `https://js.xxooo.ml/${hub}`, maintainer: "\u996D\u592A\u786C" },
95
- { urlPrefix: `https://proxy.freecdn.ml/?url=${hub}`, maintainer: "anonymous4" },
96
- { urlPrefix: "https://download.njuu.cf", maintainer: "LibraryCloud-njuu" },
97
- { urlPrefix: "https://download.yzuu.cf", maintainer: "LibraryCloud-yzuu" },
98
- { urlPrefix: "https://download.nuaa.cf", maintainer: "LibraryCloud-nuaa" }
91
+ { urlPrefix: `https://gh.gh2233.ml/${url}`, maintainer: "@X.I.U/XIU2" },
92
+ { urlPrefix: `https://ghproxy.com/${url}`, maintainer: "gh-proxy" },
93
+ { urlPrefix: `https://gh.ddlc.top/${url}`, maintainer: "@mtr-static-official" },
94
+ { urlPrefix: `https://ghdl.feizhuqwq.cf/${url}`, maintainer: "feizhuqwq.com" },
95
+ { urlPrefix: `https://slink.ltd/${url}`, maintainer: "\u77E5\u4E86\u5C0F\u7AD9" },
96
+ { urlPrefix: `https://git.xfj0.cn/${url}`, maintainer: "anonymous1" },
97
+ { urlPrefix: `https://gh.con.sh/${url}`, maintainer: "anonymous2" },
98
+ { urlPrefix: `https://ghps.cc/${url}`, maintainer: "anonymous3" },
99
+ { urlPrefix: `https://cors.isteed.cc/github.com/${_url}`, maintainer: "Lufs's" },
100
+ { urlPrefix: `https://hub.gitmirror.com/${url}`, maintainer: "GitMirror" },
101
+ { urlPrefix: `https://js.xxooo.ml/${url}`, maintainer: "\u996D\u592A\u786C" },
102
+ { urlPrefix: `https://proxy.freecdn.ml/?url=${url}`, maintainer: "anonymous4" },
103
+ { urlPrefix: `https://download.njuu.cf/${_url}`, maintainer: "LibraryCloud-njuu" },
104
+ { urlPrefix: `https://download.yzuu.cf/${_url}`, maintainer: "LibraryCloud-yzuu" },
105
+ { urlPrefix: `https://download.nuaa.cf/${_url}`, maintainer: "LibraryCloud-nuaa" }
99
106
  ];
100
107
  }
101
108
 
@@ -209,14 +216,21 @@ function initApp(productName, updater, option) {
209
216
  } = option ?? {};
210
217
  const mainDir = app3.isPackaged ? `../${productName}.asar` : electronDistPath;
211
218
  const entry = resolve(__dirname, mainDir, mainPath);
212
- return __require(entry)(updater);
219
+ let _updater;
220
+ if ("SIGNATURE_PUB" in updater) {
221
+ const _option = updater.productName ? updater : { ...updater, productName };
222
+ _updater = createUpdater(_option);
223
+ } else {
224
+ _updater = updater;
225
+ }
226
+ return __require(entry)(_updater);
213
227
  }
214
228
  export {
215
229
  createUpdater,
216
230
  getAppAsarPath,
217
231
  getAppVersion,
218
232
  getElectronVersion,
219
- getReleaseDnsPrefix,
233
+ getReleaseCdnLink,
220
234
  initApp,
221
235
  requireNative
222
236
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "electron-incremental-update",
3
- "version": "0.1.5",
3
+ "version": "0.1.6",
4
4
  "description": "electron incremental update tools, powered by vite",
5
5
  "scripts": {
6
6
  "build": "tsup",