electron-incremental-update 3.0.0-beta.5 → 3.0.0

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.
@@ -0,0 +1,165 @@
1
+ const require_crypto = require("./crypto-BSky88mL.cjs");
2
+ let electron = require("electron");
3
+ //#region src/utils/download.ts
4
+ /**
5
+ * Safe get value from header
6
+ * @param headers response header
7
+ * @param key target header key
8
+ */
9
+ function getHeader(headers, key) {
10
+ const value = headers[key];
11
+ if (Array.isArray(value)) return value.length === 0 ? null : value[value.length - 1];
12
+ else return value;
13
+ }
14
+ async function downloadUtil(url, headers, onResponse, signal) {
15
+ await electron.app.whenReady();
16
+ return new Promise((resolve, reject) => {
17
+ const rejectUnexpectedStatus = (resp) => {
18
+ const statusCode = resp.statusCode ?? 0;
19
+ if (statusCode >= 200 && statusCode < 300) return false;
20
+ let data = "";
21
+ resp.on("data", (chunk) => {
22
+ data += chunk;
23
+ data = trimData(data);
24
+ });
25
+ resp.on("end", () => {
26
+ reject(/* @__PURE__ */ new Error(`Unexpected response status ${statusCode}${resp.statusMessage ? ` ${resp.statusMessage}` : ""} from ${url}: "${data}"`));
27
+ });
28
+ return true;
29
+ };
30
+ const request = electron.net.request({
31
+ cache: "no-cache",
32
+ headers,
33
+ method: "GET",
34
+ redirect: "follow",
35
+ url
36
+ });
37
+ if (signal) {
38
+ if (signal.aborted) {
39
+ reject(/* @__PURE__ */ new Error("Aborted"));
40
+ return;
41
+ }
42
+ signal.addEventListener("abort", () => request.abort(), { once: true });
43
+ }
44
+ request.on("response", (resp) => {
45
+ resp.on("aborted", () => reject(/* @__PURE__ */ new Error("Aborted")));
46
+ resp.on("error", reject);
47
+ if (rejectUnexpectedStatus(resp)) return;
48
+ onResponse(request, resp, resolve, reject);
49
+ });
50
+ request.on("error", reject);
51
+ request.end();
52
+ });
53
+ }
54
+ /**
55
+ * trim length to 5000
56
+ */
57
+ function trimData(data) {
58
+ return data.trim().slice(0, 5e3).replace(/\s+/g, " ");
59
+ }
60
+ const resolveJson = (data, resolve, reject) => {
61
+ try {
62
+ resolve(JSON.parse(data));
63
+ } catch {
64
+ reject(/* @__PURE__ */ new Error(`Invalid json, "${trimData(data)}"`));
65
+ }
66
+ };
67
+ /**
68
+ * Default function to download json and parse to UpdateJson
69
+ * @param url target url
70
+ * @param headers extra headers
71
+ * @param signal abort signal
72
+ * @param resolveData on resolve
73
+ */
74
+ async function defaultDownloadText(url, headers, signal, resolveData) {
75
+ return await downloadUtil(url, headers, (request, resp, resolve, reject) => {
76
+ let data = "";
77
+ resp.on("data", (chunk) => data += chunk);
78
+ resp.on("end", () => resolveData(data, resolve, reject));
79
+ }, signal);
80
+ }
81
+ /**
82
+ * Default function to download json and parse to UpdateJson
83
+ * @param url target url
84
+ * @param headers extra headers
85
+ * @param signal abort signal
86
+ */
87
+ async function defaultDownloadUpdateJSON(url, headers, signal) {
88
+ return await defaultDownloadText(url, headers, signal, (data, resolve, reject) => {
89
+ try {
90
+ const json = JSON.parse(data);
91
+ if (require_crypto.isUpdateJSON(json)) resolve(json);
92
+ else throw Error;
93
+ } catch {
94
+ reject(/* @__PURE__ */ new Error(`Invalid update json, "${trimData(data)}"`));
95
+ }
96
+ });
97
+ }
98
+ /**
99
+ * Default function to download asar buffer,
100
+ * get total size from `Content-Length` header
101
+ * @param url target url
102
+ * @param headers extra headers
103
+ * @param signal abort signal
104
+ * @param onDownloading on downloading callback
105
+ */
106
+ async function defaultDownloadAsar(url, headers, signal, onDownloading) {
107
+ let transferred = 0;
108
+ let time = Date.now();
109
+ return await downloadUtil(url, headers, (_request, resp, resolve) => {
110
+ const total = +getHeader(resp.headers, "content-length") || -1;
111
+ let data = [];
112
+ resp.on("data", (chunk) => {
113
+ const delta = chunk.length;
114
+ transferred += delta;
115
+ const current = Date.now();
116
+ onDownloading?.({
117
+ bps: delta / Math.max(current - time, 1) * 1e3,
118
+ delta,
119
+ percent: total > 0 ? +(transferred / total).toFixed(2) * 100 : -1,
120
+ total,
121
+ transferred
122
+ });
123
+ time = current;
124
+ data.push(chunk);
125
+ });
126
+ resp.on("end", () => resolve(Buffer.concat(data)));
127
+ }, signal);
128
+ }
129
+ //#endregion
130
+ Object.defineProperty(exports, "defaultDownloadAsar", {
131
+ enumerable: true,
132
+ get: function() {
133
+ return defaultDownloadAsar;
134
+ }
135
+ });
136
+ Object.defineProperty(exports, "defaultDownloadText", {
137
+ enumerable: true,
138
+ get: function() {
139
+ return defaultDownloadText;
140
+ }
141
+ });
142
+ Object.defineProperty(exports, "defaultDownloadUpdateJSON", {
143
+ enumerable: true,
144
+ get: function() {
145
+ return defaultDownloadUpdateJSON;
146
+ }
147
+ });
148
+ Object.defineProperty(exports, "downloadUtil", {
149
+ enumerable: true,
150
+ get: function() {
151
+ return downloadUtil;
152
+ }
153
+ });
154
+ Object.defineProperty(exports, "getHeader", {
155
+ enumerable: true,
156
+ get: function() {
157
+ return getHeader;
158
+ }
159
+ });
160
+ Object.defineProperty(exports, "resolveJson", {
161
+ enumerable: true,
162
+ get: function() {
163
+ return resolveJson;
164
+ }
165
+ });
@@ -1,7 +1,16 @@
1
- import { app } from "electron";
2
1
  import fs from "node:fs";
3
2
  import path from "node:path";
4
-
3
+ import { app } from "electron";
4
+ import { pathToFileURL } from "node:url";
5
+ //#region src/utils/devtools/font.css?inline
6
+ var font_default = ":root,body{--source-code-font-family:var(--mono)!important;--source-code-font-size:12px!important;--monospace-font-family:var(--mono)!important;--monospace-font-size:12px!important;--default-font-family:var(--sans), sans-serif!important;--default-font-size:12px!important}button,input,select,.undisplayable-text,.expandable-inline-button{font-family:var(--sans)!important}\n";
7
+ //#endregion
8
+ //#region src/utils/devtools/js.ts?inject
9
+ var js_default = "function e(e){let t=document.createElement(`style`);t.innerHTML=e,document.body.append(t),[`platform-windows`,`platform-mac`,`platform-linux`].forEach(e=>document.querySelectorAll(`.${e}`).forEach(t=>t.classList.remove(e))),r();let n=new MutationObserver(e=>{for(let t of e)if(t.type===`childList`)for(let e=0;e<t.addedNodes.length;e++)t.addedNodes[e].classList.contains(`editor-tooltip-host`)&&r()});n.observe(document.body,{childList:!0});function r(){document.querySelectorAll(`.editor-tooltip-host`).forEach(t=>{if(t?.shadowRoot?.querySelectorAll(`[data-key=\"overridden-dev-tools-font\"]`).length===0){let n=document.createElement(`style`);n.dataset.key=`overridden-dev-tools-font`,n.innerHTML=`${e}.cm-tooltip-autocomplete ul[role=listbox]{font-family:var(--mono)!important;}`,t.shadowRoot.append(n)}})}window.onbeforeunload=()=>n.disconnect()}e(__CSS__);";
10
+ //#endregion
11
+ //#region src/utils/devtools/scrollbar.css?inline
12
+ var scrollbar_default = ":root{--scrollbar-width:max(.85vw, 10px)}@media (prefers-color-scheme:light){:root{--scrollbar-color-rgb:0, 0, 0}}@media (prefers-color-scheme:dark){:root{--scrollbar-color-rgb:255, 255, 255}}::-webkit-scrollbar{width:var(--scrollbar-width)!important;height:var(--scrollbar-width)!important}::-webkit-scrollbar-track{border-radius:var(--scrollbar-width)!important;box-shadow:none!important;background-color:#0000!important}::-webkit-scrollbar-thumb{background-clip:content-box;box-shadow:inset 0 0 0 var(--scrollbar-width)!important;border-radius:var(--scrollbar-width)!important;border:calc(var(--scrollbar-width) * 2 / 9) solid transparent!important;color:rgba(var(--scrollbar-color-rgb), 30%)!important;background-color:#0000!important}::-webkit-scrollbar-thumb:hover{color:rgba(var(--scrollbar-color-rgb), 45%)!important}::-webkit-scrollbar-thumb:active{color:rgba(var(--scrollbar-color-rgb), 60%)!important}@supports not selector(::-webkit-scrollbar){html{scrollbar-color:rgb(var(--scrollbar-color-rgb));scrollbar-width:thin}}\n";
13
+ //#endregion
5
14
  //#region src/utils/electron.ts
6
15
  /**
7
16
  * Compile time dev check
@@ -16,13 +25,19 @@ const isLinux = process.platform === "linux";
16
25
  * If is in dev, **always** return `'DEV.asar'`
17
26
  */
18
27
  function getPathFromAppNameAsar(...paths) {
19
- return isDev ? "DEV.asar" : path.join(path.dirname(app.getAppPath()), `${app.name}.asar`, ...paths);
28
+ return isDev ? path.join(__EIU_LOCAL_DEV_UPDATE_ASAR_PATH__ || "DEV.asar", ...paths) : path.join(path.dirname(app.getAppPath()), `${app.name}.asar`, ...paths);
20
29
  }
21
30
  /**
22
31
  * Get app version, if is in dev, return `getEntryVersion()`
23
32
  */
24
33
  function getAppVersion() {
25
- return isDev ? getEntryVersion() : fs.readFileSync(getPathFromAppNameAsar("version"), "utf-8");
34
+ if (!isDev) return fs.readFileSync(getPathFromAppNameAsar("version"), "utf-8");
35
+ if (!__EIU_LOCAL_DEV_UPDATE__) return getEntryVersion();
36
+ try {
37
+ return fs.readFileSync(getPathFromAppNameAsar("version"), "utf-8").trim();
38
+ } catch {
39
+ return getEntryVersion();
40
+ }
26
41
  }
27
42
  /**
28
43
  * Get entry version
@@ -38,7 +53,7 @@ function getEntryVersion() {
38
53
  */
39
54
  function requireNative(moduleName) {
40
55
  const m = getPathFromEntryAsar(moduleName);
41
- if (__EIU_IS_ESM__) throw new Error(`Cannot require "${m}", \`requireNative\` only support CommonJS, use \`importNative\` instead`);
56
+ if (__EIU_IS_ESM__) throw new Error(`Cannot require "${m}", \`requireNative\` only supports CommonJS. Use \`importNative\` instead`);
42
57
  return require(m);
43
58
  }
44
59
  /**
@@ -49,13 +64,24 @@ function requireNative(moduleName) {
49
64
  */
50
65
  async function importNative(moduleName) {
51
66
  const m = getPathFromEntryAsar(moduleName);
52
- if (!__EIU_IS_ESM__) throw new Error(`Cannot import "${m}", \`importNative\` only support ESModule, use \`requireNative\` instead`);
53
- return await import(`file://${m}.js`);
67
+ if (!__EIU_IS_ESM__) throw new Error(`Cannot import "${m}", \`importNative\` only supports ESModule. Use \`requireNative\` instead`);
68
+ return await import(pathToFileURL(path.extname(m) ? m : `${m}.mjs`).href);
54
69
  }
55
70
  /**
56
71
  * Restarts the Electron app.
57
72
  */
58
73
  function restartApp() {
74
+ if (isDev && __EIU_LOCAL_DEV_UPDATE__ && process.send) {
75
+ const forceExit = setTimeout(() => app.exit(0), 5e3);
76
+ process.once("message", (message) => {
77
+ if (message === "eiu:restart-ready") {
78
+ clearTimeout(forceExit);
79
+ app.exit(0);
80
+ }
81
+ });
82
+ process.send("eiu:restart");
83
+ return;
84
+ }
59
85
  app.relaunch();
60
86
  app.quit();
61
87
  }
@@ -128,10 +154,10 @@ function loadPage(win, htmlFilePath = "index.html") {
128
154
  function beautifyDevTools(win, options) {
129
155
  const { mono, sans, scrollbar = true } = options;
130
156
  win.webContents.on("devtools-opened", async () => {
131
- let css = `:root{--sans:${sans};--mono:${mono}}:root, body { --source-code-font-family: var(--mono) !important; --source-code-font-size: 12px !important; --monospace-font-family: var(--mono) !important; --monospace-font-size: 12px !important; --default-font-family: var(--sans), sans-serif !important; --default-font-size: 12px !important; } button, input, select, .undisplayable-text, .expandable-inline-button { font-family: var(--sans) !important; } `;
132
- if (scrollbar) css += ":root { --scrollbar-width: max(0.85vw, 10px); } @media (prefers-color-scheme: light) { :root { --scrollbar-color-rgb: 0, 0, 0; } } @media (prefers-color-scheme: dark) { :root { --scrollbar-color-rgb: 255, 255, 255; } } *::-webkit-scrollbar { width: var(--scrollbar-width) !important; height: var(--scrollbar-width) !important; } *::-webkit-scrollbar-track { background-color: transparent !important; border-radius: var(--scrollbar-width) !important; box-shadow: none !important; } *::-webkit-scrollbar-thumb { box-shadow: inset 0 0 0 var(--scrollbar-width) !important; border-radius: var(--scrollbar-width) !important; border: calc(var(--scrollbar-width) * 2 / 9) solid transparent !important; background-clip: content-box; background-color: transparent !important; color: rgba(var(--scrollbar-color-rgb), 30%) !important; } *::-webkit-scrollbar-thumb:hover { color: rgba(var(--scrollbar-color-rgb), 45%) !important; } *::-webkit-scrollbar-thumb:active { color: rgba(var(--scrollbar-color-rgb), 60%) !important; } @supports not selector(::-webkit-scrollbar) { html { scrollbar-color: rgb(var(--scrollbar-color-rgb)); scrollbar-width: thin; } } ";
133
- const js = `const __CSS__='${css}';function e(e){let t=document.createElement(\`style\`);t.innerHTML=e,document.body.append(t),[\`platform-windows\`,\`platform-mac\`,\`platform-linux\`].forEach(e=>document.querySelectorAll(\`.\${e}\`).forEach(t=>t.classList.remove(e))),r();let n=new MutationObserver(e=>{for(let t of e)if(t.type===\`childList\`)for(let e=0;e<t.addedNodes.length;e++)t.addedNodes[e].classList.contains(\`editor-tooltip-host\`)&&r()});n.observe(document.body,{childList:!0});function r(){document.querySelectorAll(\`.editor-tooltip-host\`).forEach(t=>{if(t?.shadowRoot?.querySelectorAll(\`[data-key="overridden-dev-tools-font"]\`).length===0){let n=document.createElement(\`style\`);n.dataset.key=\`overridden-dev-tools-font\`,n.innerHTML=\`\${e}.cm-tooltip-autocomplete ul[role=listbox]{font-family:var(--mono)!important;}\`,t.shadowRoot.append(n)}})}window.onbeforeunload=()=>n.disconnect()}e(__CSS__);`;
134
- await win?.webContents.devToolsWebContents?.executeJavaScript(js);
157
+ let css = `:root{--sans:${sans};--mono:${mono}}${font_default}`;
158
+ if (scrollbar) css += scrollbar_default;
159
+ const js = `const __CSS__=\`${css}\`;${js_default}`;
160
+ await win?.webContents.devToolsWebContents?.executeJavaScript(js).catch((e) => console.log(`Failed to execute js: ${js}.\n`, e));
135
161
  });
136
162
  }
137
163
  /**
@@ -176,6 +202,5 @@ function handleUnexpectedErrors(callback) {
176
202
  function reloadOnPreloadScriptChanged() {
177
203
  console.warn("`reloadOnPreloadScriptChange()` is no longer needed. It is embeded in `startupWithUpdater()`");
178
204
  }
179
-
180
205
  //#endregion
181
- export { singleInstance as C, setPortableDataPath as S, reloadOnPreloadScriptChanged as _, getPathFromAppNameAsar as a, setAppUserModelId as b, getPathFromPreload as c, importNative as d, isDev as f, loadPage as g, isWin as h, getEntryVersion as i, getPathFromPublic as l, isMac as m, disableHWAccForWin7 as n, getPathFromEntryAsar as o, isLinux as p, getAppVersion as r, getPathFromMain as s, beautifyDevTools as t, handleUnexpectedErrors as u, requireNative as v, setPortableAppDataPath as x, restartApp as y };
206
+ export { singleInstance as C, setPortableDataPath as S, reloadOnPreloadScriptChanged as _, getPathFromAppNameAsar as a, setAppUserModelId as b, getPathFromPreload as c, importNative as d, isDev as f, loadPage as g, isWin as h, getEntryVersion as i, getPathFromPublic as l, isMac as m, disableHWAccForWin7 as n, getPathFromEntryAsar as o, isLinux as p, getAppVersion as r, getPathFromMain as s, beautifyDevTools as t, handleUnexpectedErrors as u, requireNative as v, setPortableAppDataPath as x, restartApp as y };
@@ -0,0 +1,346 @@
1
+ const require_crypto = require("./crypto-BSky88mL.cjs");
2
+ let node_fs = require("node:fs");
3
+ node_fs = require_crypto.__toESM(node_fs, 1);
4
+ let node_path = require("node:path");
5
+ node_path = require_crypto.__toESM(node_path, 1);
6
+ let electron = require("electron");
7
+ let node_url = require("node:url");
8
+ //#region src/utils/devtools/font.css?inline
9
+ var font_default = ":root,body{--source-code-font-family:var(--mono)!important;--source-code-font-size:12px!important;--monospace-font-family:var(--mono)!important;--monospace-font-size:12px!important;--default-font-family:var(--sans), sans-serif!important;--default-font-size:12px!important}button,input,select,.undisplayable-text,.expandable-inline-button{font-family:var(--sans)!important}\n";
10
+ //#endregion
11
+ //#region src/utils/devtools/js.ts?inject
12
+ var js_default = "function e(e){let t=document.createElement(`style`);t.innerHTML=e,document.body.append(t),[`platform-windows`,`platform-mac`,`platform-linux`].forEach(e=>document.querySelectorAll(`.${e}`).forEach(t=>t.classList.remove(e))),r();let n=new MutationObserver(e=>{for(let t of e)if(t.type===`childList`)for(let e=0;e<t.addedNodes.length;e++)t.addedNodes[e].classList.contains(`editor-tooltip-host`)&&r()});n.observe(document.body,{childList:!0});function r(){document.querySelectorAll(`.editor-tooltip-host`).forEach(t=>{if(t?.shadowRoot?.querySelectorAll(`[data-key=\"overridden-dev-tools-font\"]`).length===0){let n=document.createElement(`style`);n.dataset.key=`overridden-dev-tools-font`,n.innerHTML=`${e}.cm-tooltip-autocomplete ul[role=listbox]{font-family:var(--mono)!important;}`,t.shadowRoot.append(n)}})}window.onbeforeunload=()=>n.disconnect()}e(__CSS__);";
13
+ //#endregion
14
+ //#region src/utils/devtools/scrollbar.css?inline
15
+ var scrollbar_default = ":root{--scrollbar-width:max(.85vw, 10px)}@media (prefers-color-scheme:light){:root{--scrollbar-color-rgb:0, 0, 0}}@media (prefers-color-scheme:dark){:root{--scrollbar-color-rgb:255, 255, 255}}::-webkit-scrollbar{width:var(--scrollbar-width)!important;height:var(--scrollbar-width)!important}::-webkit-scrollbar-track{border-radius:var(--scrollbar-width)!important;box-shadow:none!important;background-color:#0000!important}::-webkit-scrollbar-thumb{background-clip:content-box;box-shadow:inset 0 0 0 var(--scrollbar-width)!important;border-radius:var(--scrollbar-width)!important;border:calc(var(--scrollbar-width) * 2 / 9) solid transparent!important;color:rgba(var(--scrollbar-color-rgb), 30%)!important;background-color:#0000!important}::-webkit-scrollbar-thumb:hover{color:rgba(var(--scrollbar-color-rgb), 45%)!important}::-webkit-scrollbar-thumb:active{color:rgba(var(--scrollbar-color-rgb), 60%)!important}@supports not selector(::-webkit-scrollbar){html{scrollbar-color:rgb(var(--scrollbar-color-rgb));scrollbar-width:thin}}\n";
16
+ //#endregion
17
+ //#region src/utils/electron.ts
18
+ /**
19
+ * Compile time dev check
20
+ */
21
+ const isDev = __EIU_IS_DEV__;
22
+ const isWin = process.platform === "win32";
23
+ const isMac = process.platform === "darwin";
24
+ const isLinux = process.platform === "linux";
25
+ /**
26
+ * Get joined path of `${electron.app.name}.asar` (not `app.asar`)
27
+ *
28
+ * If is in dev, **always** return `'DEV.asar'`
29
+ */
30
+ function getPathFromAppNameAsar(...paths) {
31
+ return isDev ? node_path.default.join(__EIU_LOCAL_DEV_UPDATE_ASAR_PATH__ || "DEV.asar", ...paths) : node_path.default.join(node_path.default.dirname(electron.app.getAppPath()), `${electron.app.name}.asar`, ...paths);
32
+ }
33
+ /**
34
+ * Get app version, if is in dev, return `getEntryVersion()`
35
+ */
36
+ function getAppVersion() {
37
+ if (!isDev) return node_fs.default.readFileSync(getPathFromAppNameAsar("version"), "utf-8");
38
+ if (!__EIU_LOCAL_DEV_UPDATE__) return getEntryVersion();
39
+ try {
40
+ return node_fs.default.readFileSync(getPathFromAppNameAsar("version"), "utf-8").trim();
41
+ } catch {
42
+ return getEntryVersion();
43
+ }
44
+ }
45
+ /**
46
+ * Get entry version
47
+ */
48
+ function getEntryVersion() {
49
+ return electron.app.getVersion();
50
+ }
51
+ /**
52
+ * Use `require` to load native module from entry asar
53
+ * @param moduleName file name in entry
54
+ * @example
55
+ * requireNative<typeof import('../native/db')>('db')
56
+ */
57
+ function requireNative(moduleName) {
58
+ const m = getPathFromEntryAsar(moduleName);
59
+ if (__EIU_IS_ESM__) throw new Error(`Cannot require "${m}", \`requireNative\` only supports CommonJS. Use \`importNative\` instead`);
60
+ return require(m);
61
+ }
62
+ /**
63
+ * Use `import` to load native module from entry asar
64
+ * @param moduleName file name in entry
65
+ * @example
66
+ * await importNative<typeof import('../native/db')>('db')
67
+ */
68
+ async function importNative(moduleName) {
69
+ const m = getPathFromEntryAsar(moduleName);
70
+ if (!__EIU_IS_ESM__) throw new Error(`Cannot import "${m}", \`importNative\` only supports ESModule. Use \`requireNative\` instead`);
71
+ return await import((0, node_url.pathToFileURL)(node_path.default.extname(m) ? m : `${m}.mjs`).href);
72
+ }
73
+ /**
74
+ * Restarts the Electron app.
75
+ */
76
+ function restartApp() {
77
+ if (isDev && __EIU_LOCAL_DEV_UPDATE__ && process.send) {
78
+ const forceExit = setTimeout(() => electron.app.exit(0), 5e3);
79
+ process.once("message", (message) => {
80
+ if (message === "eiu:restart-ready") {
81
+ clearTimeout(forceExit);
82
+ electron.app.exit(0);
83
+ }
84
+ });
85
+ process.send("eiu:restart");
86
+ return;
87
+ }
88
+ electron.app.relaunch();
89
+ electron.app.quit();
90
+ }
91
+ /**
92
+ * Fix app use model id, only for Windows
93
+ * @param id app id, default is `org.${electron.app.name}`
94
+ */
95
+ function setAppUserModelId(id) {
96
+ if (isWin) electron.app.setAppUserModelId(id ?? `org.${electron.app.name}`);
97
+ }
98
+ /**
99
+ * Disable hardware acceleration for Windows 7
100
+ *
101
+ * Only support CommonJS
102
+ */
103
+ function disableHWAccForWin7() {
104
+ if (!__EIU_IS_ESM__ && require("node:os").release().startsWith("6.1")) electron.app.disableHardwareAcceleration();
105
+ }
106
+ /**
107
+ * Keep single electron instance and auto restore window on `second-instance` event
108
+ * @param window brwoser window to show
109
+ */
110
+ function singleInstance(window) {
111
+ electron.app.on("second-instance", () => {
112
+ if (window) {
113
+ window.show();
114
+ if (window.isMinimized()) window.restore();
115
+ window.focus();
116
+ }
117
+ });
118
+ }
119
+ /**
120
+ * Set `userData` dir to the dir of .exe file
121
+ *
122
+ * Useful for portable Windows app
123
+ * @param dirName dir name, default to `data`
124
+ * @param create whether to create dir, default to `true`
125
+ */
126
+ function setPortableDataPath(dirName = "data", create = true) {
127
+ if (electron.app.isReady()) throw new Error("Portable app data dir must be setup before app is ready");
128
+ const portableDir = node_path.default.join(node_path.default.dirname(electron.app.getPath("exe")), dirName);
129
+ if (create) {
130
+ if (!node_fs.default.existsSync(portableDir)) node_fs.default.mkdirSync(portableDir);
131
+ else if (!node_fs.default.statSync(portableDir).isDirectory()) {
132
+ node_fs.default.rmSync(portableDir);
133
+ node_fs.default.mkdirSync(portableDir);
134
+ }
135
+ } else if (!node_fs.default.existsSync(portableDir)) throw new Error("Portable app data dir does not exists");
136
+ electron.app.setPath("userData", portableDir);
137
+ }
138
+ /**
139
+ * @deprecated Use {@link setPortableDataPath} instead
140
+ */
141
+ const setPortableAppDataPath = setPortableDataPath;
142
+ /**
143
+ * Load `process.env.VITE_DEV_SERVER_URL` when dev, else load html file
144
+ * @param win window
145
+ * @param htmlFilePath html file path, default is `index.html`
146
+ */
147
+ function loadPage(win, htmlFilePath = "index.html") {
148
+ if (isDev) win.loadURL(process.env.VITE_DEV_SERVER_URL + htmlFilePath);
149
+ else win.loadFile(getPathFromAppNameAsar("renderer", htmlFilePath));
150
+ }
151
+ /**
152
+ * Beautify devtools' font and scrollbar
153
+ * @param win target window
154
+ * @param options sans font family, mono font family and scrollbar
155
+ * @see https://github.com/electron/electron/issues/42055
156
+ */
157
+ function beautifyDevTools(win, options) {
158
+ const { mono, sans, scrollbar = true } = options;
159
+ win.webContents.on("devtools-opened", async () => {
160
+ let css = `:root{--sans:${sans};--mono:${mono}}${font_default}`;
161
+ if (scrollbar) css += scrollbar_default;
162
+ const js = `const __CSS__=\`${css}\`;${js_default}`;
163
+ await win?.webContents.devToolsWebContents?.executeJavaScript(js).catch((e) => console.log(`Failed to execute js: ${js}.\n`, e));
164
+ });
165
+ }
166
+ /**
167
+ * Get joined path from main dir
168
+ * @param paths rest paths
169
+ */
170
+ function getPathFromMain(...paths) {
171
+ return isDev ? node_path.default.join(electron.app.getAppPath(), __EIU_ELECTRON_DIST_PATH__, "main", ...paths) : getPathFromAppNameAsar("main", ...paths);
172
+ }
173
+ /**
174
+ * Get joined path from preload dir
175
+ * @param paths rest paths
176
+ */
177
+ function getPathFromPreload(...paths) {
178
+ return isDev ? node_path.default.join(electron.app.getAppPath(), __EIU_ELECTRON_DIST_PATH__, "preload", ...paths) : getPathFromAppNameAsar("preload", ...paths);
179
+ }
180
+ /**
181
+ * Get joined path from publich dir
182
+ * @param paths rest paths
183
+ */
184
+ function getPathFromPublic(...paths) {
185
+ return isDev ? node_path.default.join(electron.app.getAppPath(), "public", ...paths) : getPathFromAppNameAsar("renderer", ...paths);
186
+ }
187
+ /**
188
+ * Get joined path from entry asar
189
+ * @param paths rest paths
190
+ */
191
+ function getPathFromEntryAsar(...paths) {
192
+ return node_path.default.join(electron.app.getAppPath(), __EIU_ENTRY_DIST_PATH__, ...paths);
193
+ }
194
+ /**
195
+ * Handle all unhandled error
196
+ * @param callback callback function
197
+ */
198
+ function handleUnexpectedErrors(callback) {
199
+ process.on("uncaughtException", callback);
200
+ process.on("unhandledRejection", callback);
201
+ }
202
+ /**
203
+ * @deprecated No longer needed. It is embeded in `startupWithUpdater()`
204
+ */
205
+ function reloadOnPreloadScriptChanged() {
206
+ console.warn("`reloadOnPreloadScriptChange()` is no longer needed. It is embeded in `startupWithUpdater()`");
207
+ }
208
+ //#endregion
209
+ Object.defineProperty(exports, "beautifyDevTools", {
210
+ enumerable: true,
211
+ get: function() {
212
+ return beautifyDevTools;
213
+ }
214
+ });
215
+ Object.defineProperty(exports, "disableHWAccForWin7", {
216
+ enumerable: true,
217
+ get: function() {
218
+ return disableHWAccForWin7;
219
+ }
220
+ });
221
+ Object.defineProperty(exports, "getAppVersion", {
222
+ enumerable: true,
223
+ get: function() {
224
+ return getAppVersion;
225
+ }
226
+ });
227
+ Object.defineProperty(exports, "getEntryVersion", {
228
+ enumerable: true,
229
+ get: function() {
230
+ return getEntryVersion;
231
+ }
232
+ });
233
+ Object.defineProperty(exports, "getPathFromAppNameAsar", {
234
+ enumerable: true,
235
+ get: function() {
236
+ return getPathFromAppNameAsar;
237
+ }
238
+ });
239
+ Object.defineProperty(exports, "getPathFromEntryAsar", {
240
+ enumerable: true,
241
+ get: function() {
242
+ return getPathFromEntryAsar;
243
+ }
244
+ });
245
+ Object.defineProperty(exports, "getPathFromMain", {
246
+ enumerable: true,
247
+ get: function() {
248
+ return getPathFromMain;
249
+ }
250
+ });
251
+ Object.defineProperty(exports, "getPathFromPreload", {
252
+ enumerable: true,
253
+ get: function() {
254
+ return getPathFromPreload;
255
+ }
256
+ });
257
+ Object.defineProperty(exports, "getPathFromPublic", {
258
+ enumerable: true,
259
+ get: function() {
260
+ return getPathFromPublic;
261
+ }
262
+ });
263
+ Object.defineProperty(exports, "handleUnexpectedErrors", {
264
+ enumerable: true,
265
+ get: function() {
266
+ return handleUnexpectedErrors;
267
+ }
268
+ });
269
+ Object.defineProperty(exports, "importNative", {
270
+ enumerable: true,
271
+ get: function() {
272
+ return importNative;
273
+ }
274
+ });
275
+ Object.defineProperty(exports, "isDev", {
276
+ enumerable: true,
277
+ get: function() {
278
+ return isDev;
279
+ }
280
+ });
281
+ Object.defineProperty(exports, "isLinux", {
282
+ enumerable: true,
283
+ get: function() {
284
+ return isLinux;
285
+ }
286
+ });
287
+ Object.defineProperty(exports, "isMac", {
288
+ enumerable: true,
289
+ get: function() {
290
+ return isMac;
291
+ }
292
+ });
293
+ Object.defineProperty(exports, "isWin", {
294
+ enumerable: true,
295
+ get: function() {
296
+ return isWin;
297
+ }
298
+ });
299
+ Object.defineProperty(exports, "loadPage", {
300
+ enumerable: true,
301
+ get: function() {
302
+ return loadPage;
303
+ }
304
+ });
305
+ Object.defineProperty(exports, "reloadOnPreloadScriptChanged", {
306
+ enumerable: true,
307
+ get: function() {
308
+ return reloadOnPreloadScriptChanged;
309
+ }
310
+ });
311
+ Object.defineProperty(exports, "requireNative", {
312
+ enumerable: true,
313
+ get: function() {
314
+ return requireNative;
315
+ }
316
+ });
317
+ Object.defineProperty(exports, "restartApp", {
318
+ enumerable: true,
319
+ get: function() {
320
+ return restartApp;
321
+ }
322
+ });
323
+ Object.defineProperty(exports, "setAppUserModelId", {
324
+ enumerable: true,
325
+ get: function() {
326
+ return setAppUserModelId;
327
+ }
328
+ });
329
+ Object.defineProperty(exports, "setPortableAppDataPath", {
330
+ enumerable: true,
331
+ get: function() {
332
+ return setPortableAppDataPath;
333
+ }
334
+ });
335
+ Object.defineProperty(exports, "setPortableDataPath", {
336
+ enumerable: true,
337
+ get: function() {
338
+ return setPortableDataPath;
339
+ }
340
+ });
341
+ Object.defineProperty(exports, "singleInstance", {
342
+ enumerable: true,
343
+ get: function() {
344
+ return singleInstance;
345
+ }
346
+ });