electron-incremental-update 0.7.6 → 0.7.8

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/vite.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Plugin } from 'vite';
2
2
  import { Buffer } from 'node:buffer';
3
+ import { U as UpdateJSON } from './updateJson-7e45d9e1.js';
3
4
 
4
5
  type DistinguishedName = {
5
6
  countryName?: string;
@@ -14,7 +15,8 @@ type DistinguishedName = {
14
15
  businessCategory?: string;
15
16
  emailAddress?: string;
16
17
  };
17
- type FunctionGenerateSignature = (buffer: Buffer, privateKey: string, cert: string, version: string) => string;
18
+ type FunctionGenerateSignature = (buffer: Buffer, privateKey: string, cert: string, version: string) => string | Promise<string>;
19
+ type FunctionGenerateVersionJson = (existingJson: UpdateJSON, buffer: Buffer, signature: string, version: string, minVersion: string) => UpdateJSON | Promise<UpdateJSON>;
18
20
  type Options = {
19
21
  /**
20
22
  * whether is in build mode
@@ -32,6 +34,11 @@ type Options = {
32
34
  * you can set as 'version' in `package.json`
33
35
  */
34
36
  version: string;
37
+ /**
38
+ * mini version of entry
39
+ * @default version
40
+ */
41
+ minimumVersion?: string;
35
42
  /**
36
43
  * Whether to minify entry file
37
44
  */
@@ -55,6 +62,11 @@ type Options = {
55
62
  * @default `release/${productName}.asar`
56
63
  */
57
64
  asarOutputPath?: string;
65
+ /**
66
+ * Path to version info output, content is {@link UpdateJSON}
67
+ * @default `version.json`
68
+ */
69
+ versionPath?: string;
58
70
  /**
59
71
  * Path to gzipped asar file
60
72
  * @default `release/${productName}-${version}.asar.gz`
@@ -70,11 +82,6 @@ type Options = {
70
82
  * @default `dist`
71
83
  */
72
84
  rendererDistPath?: string;
73
- /**
74
- * Path to version info output
75
- * @default `version.json`
76
- */
77
- versionPath?: string;
78
85
  };
79
86
  /**
80
87
  * signature config
@@ -124,6 +131,13 @@ type Options = {
124
131
  * @param cert certificate
125
132
  */
126
133
  generateSignature?: FunctionGenerateSignature;
134
+ /**
135
+ * custom signature generate function {@link FunctionGenerateVersionJson}
136
+ * @param signature generated signature
137
+ * @param version currentVersion
138
+ * @param cert certificate
139
+ */
140
+ generateVersionJson?: FunctionGenerateVersionJson;
127
141
  };
128
142
  };
129
143
  };
package/dist/vite.js CHANGED
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
6
  var __export = (target, all) => {
9
7
  for (var name in all)
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
17
15
  }
18
16
  return to;
19
17
  };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
- mod
27
- ));
28
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
19
 
30
20
  // src/vite.ts
@@ -37,6 +27,8 @@ var import_vite = require("vite");
37
27
 
38
28
  // src/build-plugins/build.ts
39
29
  var import_promises = require("fs/promises");
30
+ var import_node_fs2 = require("fs");
31
+ var import_asar = require("@electron/asar");
40
32
  var import_esbuild = require("esbuild");
41
33
 
42
34
  // src/crypto.ts
@@ -77,25 +69,26 @@ async function zipFile(filePath, targetFilePath = `${filePath}.gz`) {
77
69
  });
78
70
  });
79
71
  }
80
-
81
- // src/build-plugins/build.ts
82
- async function pack(dir, target) {
83
- let asar = null;
84
- try {
85
- asar = await import("asar");
86
- } catch (ignore) {
87
- }
88
- if (!asar) {
89
- try {
90
- asar = await import("@electron/asar");
91
- } catch (ignore) {
92
- }
72
+ function parseVersion(version) {
73
+ const semver = /^(\d+)\.(\d+)\.(\d+)(?:-([a-zA-Z0-9\.-]+))?/i;
74
+ const match = semver.exec(version);
75
+ if (!match) {
76
+ throw new TypeError(`invalid version: ${version}`);
93
77
  }
94
- if (!asar) {
95
- throw new Error("no asar, please install @electron/asar");
78
+ const [major, minor, patch] = match.slice(1, 4).map(Number);
79
+ if (isNaN(major) || isNaN(minor) || isNaN(patch)) {
80
+ throw new TypeError(`invalid version: ${version}`);
96
81
  }
97
- await asar.createPackage(dir, target);
82
+ return { major, minor, patch, stage: match[4] };
98
83
  }
84
+
85
+ // src/updateJson.ts
86
+ function isUpdateJSON(json) {
87
+ const is = (j) => "signature" in j && "version" in j && "size" in j && "minimumVersion" in j;
88
+ return is(json) && "beta" in json && is(json.beta);
89
+ }
90
+
91
+ // src/build-plugins/build.ts
99
92
  async function buildAsar({
100
93
  version,
101
94
  asarOutputPath,
@@ -105,7 +98,7 @@ async function buildAsar({
105
98
  }) {
106
99
  await (0, import_promises.rename)(rendererDistPath, `${electronDistPath}/renderer`);
107
100
  await (0, import_promises.writeFile)(`${electronDistPath}/version`, version);
108
- await pack(electronDistPath, asarOutputPath);
101
+ await (0, import_asar.createPackage)(electronDistPath, asarOutputPath);
109
102
  await zipFile(asarOutputPath, gzipPath);
110
103
  }
111
104
  async function buildVersion({
@@ -114,15 +107,51 @@ async function buildVersion({
114
107
  privateKey,
115
108
  cert,
116
109
  version,
117
- generateSignature
110
+ minimumVersion,
111
+ generateSignature,
112
+ generateVersionJson
118
113
  }) {
114
+ let _json = {
115
+ beta: {
116
+ minimumVersion: version,
117
+ signature: "",
118
+ size: 0,
119
+ version
120
+ },
121
+ minimumVersion: version,
122
+ signature: "",
123
+ size: 0,
124
+ version
125
+ };
126
+ if ((0, import_node_fs2.existsSync)(versionPath)) {
127
+ try {
128
+ _json = JSON.parse(await (0, import_promises.readFile)(versionPath, "utf-8"));
129
+ } catch (error) {
130
+ }
131
+ }
132
+ if (!isUpdateJSON(_json)) {
133
+ throw new Error("invalid version file");
134
+ }
119
135
  const buffer = await (0, import_promises.readFile)(gzipPath);
120
- const _func = generateSignature ?? signature;
121
- await (0, import_promises.writeFile)(versionPath, JSON.stringify({
122
- signature: _func(buffer, privateKey, cert, version),
123
- version,
124
- size: buffer.length
125
- }, null, 2));
136
+ const sig = await (generateSignature ?? signature)(buffer, privateKey, cert, version);
137
+ if (generateVersionJson) {
138
+ _json = await generateVersionJson(_json, buffer, sig, version, minimumVersion);
139
+ if (!isUpdateJSON(_json)) {
140
+ throw new Error("invalid version info");
141
+ }
142
+ } else {
143
+ _json.beta.version = version;
144
+ _json.beta.minimumVersion = minimumVersion;
145
+ _json.beta.signature = sig;
146
+ _json.beta.size = buffer.length;
147
+ if (!parseVersion(version).stage) {
148
+ _json.version = version;
149
+ _json.minimumVersion = minimumVersion;
150
+ _json.signature = sig;
151
+ _json.size = buffer.length;
152
+ }
153
+ }
154
+ await (0, import_promises.writeFile)(versionPath, JSON.stringify(_json, null, 2));
126
155
  }
127
156
  async function buildEntry({
128
157
  entryPath,
@@ -143,25 +172,25 @@ async function buildEntry({
143
172
  var import_ci_info = require("ci-info");
144
173
 
145
174
  // src/build-plugins/key.ts
146
- var import_node_fs2 = require("fs");
175
+ var import_node_fs3 = require("fs");
147
176
  var import_node_path2 = require("path");
148
177
  var import_node_os = require("os");
149
178
  var import_selfsigned = require("selfsigned");
150
179
  function generateKeyPair(keyLength, subject, days, privateKeyPath, certPath) {
151
180
  const privateKeyDir = (0, import_node_path2.dirname)(privateKeyPath);
152
- (0, import_node_fs2.existsSync)(privateKeyDir) || (0, import_node_fs2.mkdirSync)(privateKeyDir, { recursive: true });
181
+ (0, import_node_fs3.existsSync)(privateKeyDir) || (0, import_node_fs3.mkdirSync)(privateKeyDir, { recursive: true });
153
182
  const certDir = (0, import_node_path2.dirname)(certPath);
154
- (0, import_node_fs2.existsSync)(certDir) || (0, import_node_fs2.mkdirSync)(certDir, { recursive: true });
183
+ (0, import_node_fs3.existsSync)(certDir) || (0, import_node_fs3.mkdirSync)(certDir, { recursive: true });
155
184
  const { cert, private: privateKey } = (0, import_selfsigned.generate)(subject, {
156
185
  keySize: keyLength,
157
186
  algorithm: "sha256",
158
187
  days
159
188
  });
160
- (0, import_node_fs2.writeFileSync)(privateKeyPath, privateKey.replace(/\r\n?/g, "\n"));
161
- (0, import_node_fs2.writeFileSync)(certPath, cert.replace(/\r\n?/g, "\n"));
189
+ (0, import_node_fs3.writeFileSync)(privateKeyPath, privateKey.replace(/\r\n?/g, "\n"));
190
+ (0, import_node_fs3.writeFileSync)(certPath, cert.replace(/\r\n?/g, "\n"));
162
191
  }
163
192
  function writeCertToMain(entryPath, cert) {
164
- const file = (0, import_node_fs2.readFileSync)(entryPath, "utf-8");
193
+ const file = (0, import_node_fs3.readFileSync)(entryPath, "utf-8");
165
194
  const regex = /const SIGNATURE_CERT = ['`][\s\S]*?['`]/;
166
195
  const replacement = `const SIGNATURE_CERT = \`${cert}\``;
167
196
  let replaced = file;
@@ -183,7 +212,7 @@ function writeCertToMain(entryPath, cert) {
183
212
  !isMatched && lines.push(r);
184
213
  replaced = lines.join(import_node_os.EOL);
185
214
  }
186
- (0, import_node_fs2.writeFileSync)(entryPath, replaced.replace(/\r\n?/g, "\n"));
215
+ (0, import_node_fs3.writeFileSync)(entryPath, replaced.replace(/\r\n?/g, "\n"));
187
216
  }
188
217
  function parseKeys({
189
218
  keyLength,
@@ -194,12 +223,12 @@ function parseKeys({
194
223
  days
195
224
  }) {
196
225
  const keysDir = (0, import_node_path2.dirname)(privateKeyPath);
197
- !(0, import_node_fs2.existsSync)(keysDir) && (0, import_node_fs2.mkdirSync)(keysDir);
198
- if (!(0, import_node_fs2.existsSync)(privateKeyPath) || !(0, import_node_fs2.existsSync)(certPath)) {
226
+ !(0, import_node_fs3.existsSync)(keysDir) && (0, import_node_fs3.mkdirSync)(keysDir);
227
+ if (!(0, import_node_fs3.existsSync)(privateKeyPath) || !(0, import_node_fs3.existsSync)(certPath)) {
199
228
  generateKeyPair(keyLength, parseSubjects(subject), days, privateKeyPath, certPath);
200
229
  }
201
- const privateKey = (0, import_node_fs2.readFileSync)(privateKeyPath, "utf-8");
202
- const cert = (0, import_node_fs2.readFileSync)(certPath, "utf-8");
230
+ const privateKey = (0, import_node_fs3.readFileSync)(privateKeyPath, "utf-8");
231
+ const cert = (0, import_node_fs3.readFileSync)(certPath, "utf-8");
203
232
  writeCertToMain(entryPath, cert);
204
233
  return {
205
234
  privateKey,
@@ -221,6 +250,7 @@ function parseOptions(options) {
221
250
  isBuild,
222
251
  productName,
223
252
  version,
253
+ minimumVersion = version,
224
254
  minify = false,
225
255
  paths: {
226
256
  entryPath = "electron/app.ts",
@@ -239,7 +269,7 @@ function parseOptions(options) {
239
269
  overrideFunctions = {}
240
270
  } = {}
241
271
  } = options;
242
- const { generateSignature } = overrideFunctions;
272
+ const { generateSignature, generateVersionJson } = overrideFunctions;
243
273
  let {
244
274
  subject = {
245
275
  commonName: productName,
@@ -271,11 +301,13 @@ function parseOptions(options) {
271
301
  });
272
302
  buildVersionOption = {
273
303
  version,
304
+ minimumVersion,
274
305
  gzipPath,
275
306
  privateKey,
276
307
  cert,
277
308
  versionPath,
278
- generateSignature
309
+ generateSignature,
310
+ generateVersionJson
279
311
  };
280
312
  }
281
313
  return { isBuild, buildAsarOption, buildEntryOption, buildVersionOption };
package/dist/vite.mjs CHANGED
@@ -1,33 +1,20 @@
1
1
  import {
2
+ isUpdateJSON,
2
3
  signature
3
- } from "./chunk-Q2K52LOG.mjs";
4
+ } from "./chunk-2XHZMWRR.mjs";
4
5
  import {
6
+ parseVersion,
5
7
  zipFile
6
- } from "./chunk-67MCNA7W.mjs";
8
+ } from "./chunk-SWXNCK6H.mjs";
7
9
 
8
10
  // src/vite.ts
9
11
  import { createLogger } from "vite";
10
12
 
11
13
  // src/build-plugins/build.ts
12
14
  import { readFile, rename, writeFile } from "node:fs/promises";
15
+ import { existsSync } from "node:fs";
16
+ import { createPackage } from "@electron/asar";
13
17
  import { build } from "esbuild";
14
- async function pack(dir, target) {
15
- let asar = null;
16
- try {
17
- asar = await import("asar");
18
- } catch (ignore) {
19
- }
20
- if (!asar) {
21
- try {
22
- asar = await import("@electron/asar");
23
- } catch (ignore) {
24
- }
25
- }
26
- if (!asar) {
27
- throw new Error("no asar, please install @electron/asar");
28
- }
29
- await asar.createPackage(dir, target);
30
- }
31
18
  async function buildAsar({
32
19
  version,
33
20
  asarOutputPath,
@@ -37,7 +24,7 @@ async function buildAsar({
37
24
  }) {
38
25
  await rename(rendererDistPath, `${electronDistPath}/renderer`);
39
26
  await writeFile(`${electronDistPath}/version`, version);
40
- await pack(electronDistPath, asarOutputPath);
27
+ await createPackage(electronDistPath, asarOutputPath);
41
28
  await zipFile(asarOutputPath, gzipPath);
42
29
  }
43
30
  async function buildVersion({
@@ -46,15 +33,51 @@ async function buildVersion({
46
33
  privateKey,
47
34
  cert,
48
35
  version,
49
- generateSignature
36
+ minimumVersion,
37
+ generateSignature,
38
+ generateVersionJson
50
39
  }) {
40
+ let _json = {
41
+ beta: {
42
+ minimumVersion: version,
43
+ signature: "",
44
+ size: 0,
45
+ version
46
+ },
47
+ minimumVersion: version,
48
+ signature: "",
49
+ size: 0,
50
+ version
51
+ };
52
+ if (existsSync(versionPath)) {
53
+ try {
54
+ _json = JSON.parse(await readFile(versionPath, "utf-8"));
55
+ } catch (error) {
56
+ }
57
+ }
58
+ if (!isUpdateJSON(_json)) {
59
+ throw new Error("invalid version file");
60
+ }
51
61
  const buffer = await readFile(gzipPath);
52
- const _func = generateSignature ?? signature;
53
- await writeFile(versionPath, JSON.stringify({
54
- signature: _func(buffer, privateKey, cert, version),
55
- version,
56
- size: buffer.length
57
- }, null, 2));
62
+ const sig = await (generateSignature ?? signature)(buffer, privateKey, cert, version);
63
+ if (generateVersionJson) {
64
+ _json = await generateVersionJson(_json, buffer, sig, version, minimumVersion);
65
+ if (!isUpdateJSON(_json)) {
66
+ throw new Error("invalid version info");
67
+ }
68
+ } else {
69
+ _json.beta.version = version;
70
+ _json.beta.minimumVersion = minimumVersion;
71
+ _json.beta.signature = sig;
72
+ _json.beta.size = buffer.length;
73
+ if (!parseVersion(version).stage) {
74
+ _json.version = version;
75
+ _json.minimumVersion = minimumVersion;
76
+ _json.signature = sig;
77
+ _json.size = buffer.length;
78
+ }
79
+ }
80
+ await writeFile(versionPath, JSON.stringify(_json, null, 2));
58
81
  }
59
82
  async function buildEntry({
60
83
  entryPath,
@@ -75,15 +98,15 @@ async function buildEntry({
75
98
  import { isCI } from "ci-info";
76
99
 
77
100
  // src/build-plugins/key.ts
78
- import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
101
+ import { existsSync as existsSync2, mkdirSync, readFileSync, writeFileSync } from "node:fs";
79
102
  import { dirname } from "node:path";
80
103
  import { EOL } from "node:os";
81
104
  import { generate } from "selfsigned";
82
105
  function generateKeyPair(keyLength, subject, days, privateKeyPath, certPath) {
83
106
  const privateKeyDir = dirname(privateKeyPath);
84
- existsSync(privateKeyDir) || mkdirSync(privateKeyDir, { recursive: true });
107
+ existsSync2(privateKeyDir) || mkdirSync(privateKeyDir, { recursive: true });
85
108
  const certDir = dirname(certPath);
86
- existsSync(certDir) || mkdirSync(certDir, { recursive: true });
109
+ existsSync2(certDir) || mkdirSync(certDir, { recursive: true });
87
110
  const { cert, private: privateKey } = generate(subject, {
88
111
  keySize: keyLength,
89
112
  algorithm: "sha256",
@@ -126,8 +149,8 @@ function parseKeys({
126
149
  days
127
150
  }) {
128
151
  const keysDir = dirname(privateKeyPath);
129
- !existsSync(keysDir) && mkdirSync(keysDir);
130
- if (!existsSync(privateKeyPath) || !existsSync(certPath)) {
152
+ !existsSync2(keysDir) && mkdirSync(keysDir);
153
+ if (!existsSync2(privateKeyPath) || !existsSync2(certPath)) {
131
154
  generateKeyPair(keyLength, parseSubjects(subject), days, privateKeyPath, certPath);
132
155
  }
133
156
  const privateKey = readFileSync(privateKeyPath, "utf-8");
@@ -153,6 +176,7 @@ function parseOptions(options) {
153
176
  isBuild,
154
177
  productName,
155
178
  version,
179
+ minimumVersion = version,
156
180
  minify = false,
157
181
  paths: {
158
182
  entryPath = "electron/app.ts",
@@ -171,7 +195,7 @@ function parseOptions(options) {
171
195
  overrideFunctions = {}
172
196
  } = {}
173
197
  } = options;
174
- const { generateSignature } = overrideFunctions;
198
+ const { generateSignature, generateVersionJson } = overrideFunctions;
175
199
  let {
176
200
  subject = {
177
201
  commonName: productName,
@@ -203,11 +227,13 @@ function parseOptions(options) {
203
227
  });
204
228
  buildVersionOption = {
205
229
  version,
230
+ minimumVersion,
206
231
  gzipPath,
207
232
  privateKey,
208
233
  cert,
209
234
  versionPath,
210
- generateSignature
235
+ generateSignature,
236
+ generateVersionJson
211
237
  };
212
238
  }
213
239
  return { isBuild, buildAsarOption, buildEntryOption, buildVersionOption };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron-incremental-update",
3
3
  "author": "subframe7536",
4
- "version": "0.7.6",
4
+ "version": "0.7.8",
5
5
  "description": "electron incremental update tools, powered by vite",
6
6
  "scripts": {
7
7
  "build": "tsup && node fix-module.js",
@@ -43,10 +43,8 @@
43
43
  "updater"
44
44
  ],
45
45
  "devDependencies": {
46
- "@electron/asar": "^3.2.4",
47
46
  "@subframe7536/eslint-config": "^0.1.9",
48
47
  "@types/node": "^20.3.2",
49
- "asar": "^3.2.0",
50
48
  "bumpp": "^9.1.1",
51
49
  "electron": "^25.2.0",
52
50
  "eslint": "^8.43.0",
@@ -56,6 +54,7 @@
56
54
  "vitest": "^0.32.2"
57
55
  },
58
56
  "dependencies": {
57
+ "@electron/asar": "^3.2.4",
59
58
  "ci-info": "^3.8.0",
60
59
  "selfsigned": "^2.1.1"
61
60
  }