Просмотр исходного кода

chore(electron-packaging): disable signing for electron packaging on windows and include log-related js files and log-path configurations

dengdx 1 месяц назад
Родитель
Сommit
32291a0fc9
3 измененных файлов с 46 добавлено и 16 удалено
  1. 22 7
      electron-builder.json
  2. 1 1
      main.js
  3. 23 8
      src/log/log-path.js

+ 22 - 7
electron-builder.json

@@ -2,20 +2,30 @@
   "appId": "zskk.dros",
   "productName": "dros",
   "electronVersion": "36.2.1",
-
-  "files": ["main.js"],
+  "asar": false,
+  "compression": "store",
+  "files": [
+    "main.js",
+    "src/log/**/*"
+  ],
   "extraFiles": [
     {
       "from": "dist/h5",
       "to": "h5",
-      "filter": ["**/*"]
+      "filter": [
+        "**/*"
+      ]
     }
   ],
   "win": {
+    "signAndEditExecutable": false,
+    "forceCodeSigning": false,
     "target": [
       {
         "target": "portable",
-        "arch": ["x64"]
+        "arch": [
+          "x64"
+        ]
       }
     ]
   },
@@ -23,7 +33,10 @@
     "target": [
       {
         "target": "dmg",
-        "arch": ["x64", "arm64"]
+        "arch": [
+          "x64",
+          "arm64"
+        ]
       }
     ]
   },
@@ -31,11 +44,13 @@
     "target": [
       {
         "target": "AppImage",
-        "arch": ["arm64"]
+        "arch": [
+          "arm64"
+        ]
       }
     ]
   },
   "directories": {
     "output": "dist/electron"
   }
-}
+}

+ 1 - 1
main.js

@@ -1,4 +1,4 @@
-const { app, BrowserWindow, Menu } = require('electron');
+const { app, BrowserWindow, Menu ,ipcMain} = require('electron');
 const path = require('path');
 const {writeLog} = require('./src/log/log-writer.js')
 

+ 23 - 8
src/log/log-path.js

@@ -1,16 +1,31 @@
-import { fileURLToPath } from 'url';
 import path from 'path';
 import fs from 'fs';
+import { fileURLToPath } from 'url';
+import { createRequire } from 'module';
+const require = createRequire(import.meta.url);
+
+const { app } = require('electron');
+
+const __dirname = path.dirname(fileURLToPath(import.meta.url));
+
+/* --------------- 环境检测 --------------- */
+const IS_PKG      = process.pkg !== undefined;
+const IS_ELECTRON = process.versions && process.versions.electron;
 
-// ① 兼容 pkg:__dirname 会被重写成 /snapshot/...,需要 process.execPath
-const IS_PKG = process.pkg !== undefined;
-const EXE_DIR = IS_PKG
-  ? path.dirname(process.execPath)          // pkg 运行时
-  : path.dirname(process.argv[1]);          // 普通 node 启动
+/* --------------- 计算「可写」根目录 --------------- */
+let baseDir;
+if (IS_PKG) {
+  baseDir = path.dirname(process.execPath);
+} else if (IS_ELECTRON) {
+  // Electron 打包后:exe 旁边
+  baseDir = path.dirname(app.getPath('exe'));
+} else {
+  // 开发阶段:项目根
+  baseDir = path.resolve(__dirname, '../..');
+}
 
-export const LOG_DIR = path.join(EXE_DIR, 'logs');
+export const LOG_DIR = path.join(baseDir, 'logs');
 
-/* 确保目录存在 */
 if (!fs.existsSync(LOG_DIR)) {
   fs.mkdirSync(LOG_DIR, { recursive: true });
 }