electron-version-deployer-cli 0.0.1 → 0.0.2

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/cli.cjs CHANGED
@@ -493,4 +493,4 @@ async function installPrebuilt(configs) {
493
493
  }
494
494
  commander.program.description(
495
495
  "Electron 版本部署 CLI,简化你的 Electron 软件更新,让一切变得简单。"
496
- ).helpOption("-h, --help", "使用帮助").version("0.0.1", "-V, --version", "显示版本号").parse(process.argv);
496
+ ).helpOption("-h, --help", "使用帮助").version("0.0.2", "-V, --version", "显示版本号").parse(process.argv);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "electron-version-deployer-cli",
3
3
  "private": false,
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "types": "./dist/index.d.ts",
6
6
  "main": "./dist/index.cjs.js",
7
7
  "module": "./dist/index.es.js",
package/dist/main.es.js DELETED
@@ -1,306 +0,0 @@
1
- import { app, BrowserWindow, ipcMain, shell } from "electron";
2
- import { format } from "node:url";
3
- import { join, basename } from "node:path";
4
- import { get } from "node:https";
5
- import { readFileSync, appendFileSync, existsSync, rmSync, mkdirSync, createWriteStream, readdirSync, statSync, writeFileSync } from "node:fs";
6
- import extract from "extract-zip";
7
- function fetchRemoteChangelogJSON(remote_url) {
8
- return new Promise((res, rej) => {
9
- get(`${remote_url}/changelog.json`, (_res) => {
10
- let data = "";
11
- _res.on("data", (chunk) => {
12
- data += chunk;
13
- });
14
- _res.on("end", () => {
15
- try {
16
- res(JSON.parse(data));
17
- } catch (e) {
18
- res(null);
19
- }
20
- });
21
- }).on("error", (err) => {
22
- rej(`获取 changelog.json 失败:` + err.toString());
23
- });
24
- });
25
- }
26
- function fetchRemotePkgJSON(remote_url) {
27
- return new Promise((res, rej) => {
28
- get(`${remote_url}/package.json`, (_res) => {
29
- let data = "";
30
- _res.on("data", (chunk) => {
31
- data += chunk;
32
- });
33
- _res.on("end", () => {
34
- try {
35
- res(JSON.parse(data));
36
- } catch (e) {
37
- res(null);
38
- }
39
- });
40
- }).on("error", (err) => {
41
- rej(`自动更新检查请求失败:` + err.toString());
42
- });
43
- });
44
- }
45
- function versionToNum(a) {
46
- let c = a.split(".");
47
- let num_place = ["", "0", "00", "000", "0000"], r = num_place.reverse();
48
- for (let i = 0; i < c.length; i++) {
49
- let len = c[i].length;
50
- c[i] = r[len] + c[i];
51
- }
52
- let res = c.join("");
53
- return res;
54
- }
55
- function compareObjectsIsEqual(obj1, obj2) {
56
- const keys1 = Object.keys(obj1);
57
- const keys2 = Object.keys(obj2);
58
- if (keys1.length !== keys2.length) {
59
- return false;
60
- }
61
- for (let key of keys1) {
62
- if (obj1[key] !== obj2[key]) {
63
- return false;
64
- }
65
- }
66
- return true;
67
- }
68
- const CLI_NAME = "electron-version-deployer-cli";
69
- const id = `${Date.now()}-${Math.random()}`;
70
- var EVDEventEnum = /* @__PURE__ */ ((EVDEventEnum2) => {
71
- EVDEventEnum2["OPEN_LINK"] = "evd-open-link";
72
- EVDEventEnum2["UPDATE"] = "evd-update-now";
73
- EVDEventEnum2["SKIP"] = "evd-skip";
74
- EVDEventEnum2["GET_CHANGELOGS"] = "evd-get-change-logs";
75
- return EVDEventEnum2;
76
- })(EVDEventEnum || {});
77
- let globalArgs = null;
78
- let cacheCurrentPkgJSON = null;
79
- function EVDInit(props) {
80
- globalArgs = props;
81
- const { detectionFrequency, detectAtStart, onError } = getConfigs();
82
- setInterval(async () => {
83
- try {
84
- await EVDCheckUpdate();
85
- } catch (e) {
86
- onError(e);
87
- }
88
- }, 1e3 * detectionFrequency);
89
- if (!detectAtStart)
90
- return;
91
- setTimeout(async () => {
92
- try {
93
- await EVDCheckUpdate();
94
- } catch (e) {
95
- onError(e);
96
- }
97
- }, 1e3 * 2);
98
- const appPath = app.getAppPath();
99
- cacheCurrentPkgJSON = JSON.parse(
100
- readFileSync(join(appPath, "package.json"), "utf-8")
101
- );
102
- }
103
- async function EVDCheckUpdate() {
104
- const { netlifyUrl } = getConfigs();
105
- const { version } = cacheCurrentPkgJSON;
106
- const remoteJSON = await fetchRemotePkgJSON(netlifyUrl);
107
- if (!remoteJSON)
108
- throw new Error(`${netlifyUrl}package.json 文件不存在`);
109
- const localVersion = versionToNum(version);
110
- const remoteVersion = versionToNum(remoteJSON.version);
111
- if (remoteVersion > localVersion) {
112
- await showNewVersionDialog();
113
- }
114
- }
115
- async function showNewVersionDialog() {
116
- const { windowHeight, windowWidth, onError } = getConfigs();
117
- app.whenReady().then(async () => {
118
- try {
119
- let promptWindow = new BrowserWindow({
120
- frame: true,
121
- width: windowWidth,
122
- height: windowHeight,
123
- minWidth: windowWidth,
124
- minHeight: windowHeight,
125
- resizable: false,
126
- minimizable: true,
127
- fullscreenable: false,
128
- maximizable: false,
129
- skipTaskbar: true,
130
- alwaysOnTop: true,
131
- useContentSize: false,
132
- title: "有可用的更新",
133
- webPreferences: {
134
- nodeIntegration: true,
135
- contextIsolation: false
136
- },
137
- show: false
138
- });
139
- promptWindow.setMenu(null);
140
- promptWindow.setMenuBarVisibility(false);
141
- const promptUrl = format({
142
- protocol: "file",
143
- slashes: true,
144
- pathname: join(
145
- app.getAppPath(),
146
- "node_modules",
147
- CLI_NAME,
148
- "dist",
149
- "templates",
150
- "newVersionDialog.html"
151
- ),
152
- hash: id
153
- });
154
- promptWindow.loadURL(promptUrl);
155
- promptWindow.once("ready-to-show", () => {
156
- promptWindow.show();
157
- });
158
- promptWindow.once("close", () => {
159
- cleanup(promptWindow);
160
- });
161
- bindEvent(promptWindow);
162
- } catch (e) {
163
- onError(e);
164
- }
165
- });
166
- }
167
- async function installNewVersion() {
168
- const { netlifyUrl } = getConfigs();
169
- const remoteJSON = await fetchRemotePkgJSON(netlifyUrl);
170
- const needInstallFullSize = compareObjectsIsEqual(
171
- remoteJSON.dependencies,
172
- cacheCurrentPkgJSON.dependencies
173
- );
174
- try {
175
- await installPkg(needInstallFullSize ? "fullCode.zip" : "logicCode.zip");
176
- } catch (error) {
177
- appendFileSync(
178
- join(app.getPath("userData"), "evd-runtime-error.txt"),
179
- `
180
- ${(/* @__PURE__ */ new Date()).toString()}
181
-
182
- ${error.toString()}
183
-
184
- -- stack
185
-
186
- ${error.stack}
187
-
188
- ----------------------------------------------------------------
189
-
190
- `
191
- );
192
- }
193
- }
194
- async function installPkg(zipFile) {
195
- const { netlifyUrl } = getConfigs();
196
- const appPath = app.getAppPath();
197
- const unzipPath = join(appPath, "evdUnzip");
198
- if (existsSync(unzipPath)) {
199
- rmSync(unzipPath, {
200
- force: true,
201
- maxRetries: 3,
202
- recursive: true
203
- });
204
- }
205
- mkdirSync(unzipPath);
206
- const tmpZipFilePath = createWriteStream(unzipPath + ".zip");
207
- await new Promise(
208
- (res, rej) => get(`${netlifyUrl}/${zipFile}`, (response) => {
209
- response.pipe(tmpZipFilePath).on("finish", () => {
210
- res();
211
- }).on("error", (err) => {
212
- rej(err);
213
- });
214
- }).on("error", (err) => {
215
- rej(err);
216
- })
217
- );
218
- await extract(unzipPath + ".zip", { dir: unzipPath });
219
- await new Promise((res) => setTimeout(res, 1e3));
220
- copyFolderRecursiveSync(unzipPath, appPath);
221
- await new Promise((res) => setTimeout(res, 1e3));
222
- }
223
- function bindEvent(promptWindow) {
224
- ipcMain.on("evd-open-link", (_, link) => {
225
- shell.openExternal(link);
226
- });
227
- ipcMain.on("evd-skip", (_) => {
228
- promptWindow.close();
229
- });
230
- ipcMain.on("evd-update-now", (_) => {
231
- installNewVersion().then(() => {
232
- console.log(222);
233
- promptWindow.close();
234
- app.relaunch();
235
- app.exit();
236
- });
237
- });
238
- ipcMain.handle("evd-get-change-logs", async () => {
239
- const { netlifyUrl } = getConfigs();
240
- return await fetchRemoteChangelogJSON(netlifyUrl);
241
- });
242
- }
243
- function cleanup(promptWindow) {
244
- ipcMain.removeAllListeners(
245
- "evd-open-link"
246
- /* OPEN_LINK */
247
- );
248
- ipcMain.removeAllListeners(
249
- "evd-skip"
250
- /* SKIP */
251
- );
252
- ipcMain.removeAllListeners(
253
- "evd-update-now"
254
- /* UPDATE */
255
- );
256
- promptWindow == null ? void 0 : promptWindow.focus();
257
- if (promptWindow) {
258
- promptWindow.destroy();
259
- }
260
- }
261
- function getConfigs() {
262
- if (!globalArgs)
263
- throw new Error("必须先执行 EVDInit 后才能继续运行!");
264
- return {
265
- ...{
266
- onError: () => {
267
- },
268
- windowHeight: 360,
269
- windowWidth: 400,
270
- logo: void 0,
271
- // 默认六小时检测一次
272
- detectionFrequency: 60 * 60 * 6,
273
- detectAtStart: true
274
- },
275
- ...globalArgs
276
- };
277
- }
278
- function copyFileSync(source, target) {
279
- let targetFile = target;
280
- if (existsSync(target)) {
281
- if (statSync(target).isDirectory()) {
282
- targetFile = join(target, basename(source));
283
- }
284
- }
285
- writeFileSync(targetFile, readFileSync(source));
286
- }
287
- function copyFolderRecursiveSync(source, target) {
288
- if (!existsSync(target)) {
289
- mkdirSync(target);
290
- }
291
- const files = readdirSync(source);
292
- files.forEach((file) => {
293
- const sourcePath = join(source, file);
294
- const targetPath = join(target, file);
295
- if (statSync(sourcePath).isDirectory()) {
296
- copyFolderRecursiveSync(sourcePath, targetPath);
297
- } else {
298
- copyFileSync(sourcePath, targetPath);
299
- }
300
- });
301
- }
302
- export {
303
- EVDCheckUpdate,
304
- EVDEventEnum,
305
- EVDInit
306
- };
File without changes