| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- "use strict";
- var __importDefault = (this && this.__importDefault) || function (mod) {
- return (mod && mod.__esModule) ? mod : { "default": mod };
- };
- Object.defineProperty(exports, "__esModule", { value: true });
- const electron_1 = require("electron");
- const http_1 = require("http");
- const next_1 = __importDefault(require("next"));
- const path_1 = __importDefault(require("path"));
- const fs_1 = __importDefault(require("fs"));
- const net_1 = __importDefault(require("net"));
- // ─── Globals ─────────────────────────────────────────────────────────────────
- const isDev = process.env.NODE_ENV === 'development' || !electron_1.app.isPackaged;
- // 开发模式下直接连接已运行的 Next.js dev server,不自己启动
- let nextApp = null;
- let handle = null;
- if (!isDev) {
- nextApp = (0, next_1.default)({ dev: false, dir: path_1.default.join(__dirname, '..') });
- handle = nextApp.getRequestHandler();
- }
- let httpServer = null;
- let mainWindow = null;
- // ─── Port Discovery ───────────────────────────────────────────────────────────
- function findAvailablePort(startPort) {
- return new Promise((resolve, reject) => {
- const server = net_1.default.createServer();
- server.listen(startPort, '127.0.0.1', () => {
- const address = server.address();
- const port = typeof address === 'object' && address ? address.port : startPort;
- server.close(() => resolve(port));
- });
- server.on('error', () => {
- // Port in use, try next one
- findAvailablePort(startPort + 1).then(resolve).catch(reject);
- });
- });
- }
- // ─── App Config (userData) ────────────────────────────────────────────────────
- function getAppConfigPath() {
- return path_1.default.join(electron_1.app.getPath('userData'), 'app-config.json');
- }
- function readAppConfig() {
- const configPath = getAppConfigPath();
- try {
- if (fs_1.default.existsSync(configPath)) {
- const raw = fs_1.default.readFileSync(configPath, 'utf-8');
- return JSON.parse(raw);
- }
- }
- catch {
- // Return default config if file is missing or malformed
- }
- return { recentProjects: [] };
- }
- function writeAppConfig(config) {
- const configPath = getAppConfigPath();
- const dir = path_1.default.dirname(configPath);
- if (!fs_1.default.existsSync(dir)) {
- fs_1.default.mkdirSync(dir, { recursive: true });
- }
- // Atomic write: write to temp file then rename
- const tmpPath = configPath + '.tmp';
- fs_1.default.writeFileSync(tmpPath, JSON.stringify(config, null, 2), 'utf-8');
- fs_1.default.renameSync(tmpPath, configPath);
- }
- // ─── IPC Handlers ─────────────────────────────────────────────────────────────
- // Select a folder using the native OS dialog
- electron_1.ipcMain.handle('select-folder', async () => {
- const result = await electron_1.dialog.showOpenDialog({
- properties: ['openDirectory'],
- });
- return result.canceled ? null : result.filePaths[0];
- });
- // Read global app config from userData
- electron_1.ipcMain.handle('get-app-config', () => {
- return readAppConfig();
- });
- // Write global app config to userData
- electron_1.ipcMain.handle('save-app-config', (_event, config) => {
- writeAppConfig(config);
- return { success: true };
- });
- // ─── App Startup ──────────────────────────────────────────────────────────────
- async function startApp() {
- let appPort = 3000;
- if (isDev) {
- // 开发模式:直接连接已运行的 Next.js dev server (localhost:3000)
- appPort = 3000;
- }
- else {
- // 生产模式:自己启动内嵌 Next.js 服务器
- await nextApp.prepare();
- appPort = await findAvailablePort(3000);
- httpServer = (0, http_1.createServer)((req, res) => {
- handle(req, res);
- });
- await new Promise((resolve) => {
- httpServer.listen(appPort, '127.0.0.1', () => {
- resolve();
- });
- });
- }
- mainWindow = new electron_1.BrowserWindow({
- width: 1400,
- height: 900,
- webPreferences: {
- contextIsolation: true,
- nodeIntegration: false,
- preload: path_1.default.join(__dirname, 'preload.js'),
- },
- });
- mainWindow.loadURL(`http://localhost:${appPort}/features/novel-writer`);
- mainWindow.on('closed', () => {
- mainWindow = null;
- });
- }
- // ─── Lifecycle ────────────────────────────────────────────────────────────────
- electron_1.app.whenReady().then(startApp).catch((err) => {
- console.error('Failed to start app:', err);
- electron_1.app.quit();
- });
- electron_1.app.on('window-all-closed', () => {
- if (process.platform !== 'darwin') {
- electron_1.app.quit();
- }
- });
- electron_1.app.on('activate', () => {
- if (mainWindow === null) {
- startApp().catch((err) => {
- console.error('Failed to restart app:', err);
- });
- }
- });
- electron_1.app.on('before-quit', () => {
- if (httpServer) {
- httpServer.close();
- httpServer = null;
- }
- });
- //# sourceMappingURL=main.js.map
|