mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-10-12 20:49:53 +08:00
refactor(electron): remove the configuration-preset system and read .env with dotenv
- delete electron/settings, the settings preload and window, the config-presets IPC channels, the Configuration menu and the preset watcher in scripts/electron-dev.mjs; config-manager.ts keeps only the user locale (still read from config-presets.json so the saved language survives) - electron/main/env-loader.ts parses .env with dotenv instead of a hand-written parser; electron:compile bundles dependencies - drop the unreachable development branches in port-manager.ts and next-server.ts, the duplicated locale normalisation in menu-i18n.ts, the unused electronAPI members, resources/entitlements.mac.plist - scripts/copy-dereferenced.cjs shared by afterPack and prepare-electron-build - CI: delete ci.yml, move type check and concurrency into test.yml, Node 24 everywhere, docker-build only on main - remove unused dependencies (@xmldom/xmldom, remark-gfm, @anthropic-ai/tokenizer, @testing-library/user-event, concurrently, wait-on, eslint, eslint-config-next, @vitest/coverage-v8, shx), .eslintrc.json and the vitest coverage block
This commit is contained in:
+2
-41
@@ -4,49 +4,10 @@
|
||||
* and ad-hoc signs macOS apps for offline draw.io bundle compatibility
|
||||
*/
|
||||
|
||||
const {
|
||||
copyFileSync,
|
||||
existsSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
readdirSync,
|
||||
statSync,
|
||||
} = require("fs")
|
||||
const { existsSync } = require("fs")
|
||||
const path = require("path")
|
||||
const { execSync } = require("child_process")
|
||||
|
||||
/**
|
||||
* Copy directory recursively, converting symlinks to regular files/directories.
|
||||
* This is needed because cpSync with dereference:true does NOT convert symlinks.
|
||||
* macOS codesign fails if bundle contains symlinks pointing outside the bundle.
|
||||
*/
|
||||
function copyDereferenced(src, dst) {
|
||||
const lstat = lstatSync(src)
|
||||
|
||||
if (lstat.isSymbolicLink()) {
|
||||
// Follow symlink and check what it points to
|
||||
const stat = statSync(src)
|
||||
if (stat.isDirectory()) {
|
||||
// Symlink to directory: recursively copy the directory contents
|
||||
mkdirSync(dst, { recursive: true })
|
||||
for (const entry of readdirSync(src)) {
|
||||
copyDereferenced(path.join(src, entry), path.join(dst, entry))
|
||||
}
|
||||
} else {
|
||||
// Symlink to file: copy the actual file content
|
||||
mkdirSync(path.join(dst, ".."), { recursive: true })
|
||||
copyFileSync(src, dst)
|
||||
}
|
||||
} else if (lstat.isDirectory()) {
|
||||
mkdirSync(dst, { recursive: true })
|
||||
for (const entry of readdirSync(src)) {
|
||||
copyDereferenced(path.join(src, entry), path.join(dst, entry))
|
||||
}
|
||||
} else {
|
||||
mkdirSync(path.join(dst, ".."), { recursive: true })
|
||||
copyFileSync(src, dst)
|
||||
}
|
||||
}
|
||||
const copyDereferenced = require("./copy-dereferenced.cjs")
|
||||
|
||||
module.exports = async (context) => {
|
||||
const appOutDir = context.appOutDir
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
const {
|
||||
copyFileSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
readdirSync,
|
||||
statSync,
|
||||
} = require("fs")
|
||||
const path = require("path")
|
||||
|
||||
/**
|
||||
* Copy directory recursively, converting symlinks to regular files/directories.
|
||||
* This is needed because cpSync with dereference:true does NOT convert symlinks.
|
||||
* macOS codesign fails if bundle contains symlinks pointing outside the bundle.
|
||||
*/
|
||||
function copyDereferenced(src, dst) {
|
||||
const lstat = lstatSync(src)
|
||||
|
||||
if (lstat.isSymbolicLink()) {
|
||||
// Follow symlink and check what it points to
|
||||
const stat = statSync(src)
|
||||
if (stat.isDirectory()) {
|
||||
// Symlink to directory: recursively copy the directory contents
|
||||
mkdirSync(dst, { recursive: true })
|
||||
for (const entry of readdirSync(src)) {
|
||||
copyDereferenced(path.join(src, entry), path.join(dst, entry))
|
||||
}
|
||||
} else {
|
||||
// Symlink to file: copy the actual file content
|
||||
mkdirSync(path.join(dst, ".."), { recursive: true })
|
||||
copyFileSync(src, dst)
|
||||
}
|
||||
} else if (lstat.isDirectory()) {
|
||||
mkdirSync(dst, { recursive: true })
|
||||
for (const entry of readdirSync(src)) {
|
||||
copyDereferenced(path.join(src, entry), path.join(dst, entry))
|
||||
}
|
||||
} else {
|
||||
mkdirSync(path.join(dst, ".."), { recursive: true })
|
||||
copyFileSync(src, dst)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = copyDereferenced
|
||||
+9
-155
@@ -2,17 +2,13 @@
|
||||
|
||||
/**
|
||||
* Development script for running Electron with Next.js
|
||||
* 1. Reads the active preset's env vars (if any)
|
||||
* 2. Starts Next.js dev server with preset env vars
|
||||
* 3. Waits for it to be ready
|
||||
* 4. Compiles Electron TypeScript
|
||||
* 5. Launches Electron
|
||||
* 6. Watches for preset changes and restarts Next.js
|
||||
* 1. Starts Next.js dev server
|
||||
* 2. Waits for it to be ready
|
||||
* 3. Compiles Electron TypeScript
|
||||
* 4. Launches Electron
|
||||
*/
|
||||
|
||||
import { spawn } from "node:child_process"
|
||||
import { existsSync, readFileSync, watch } from "node:fs"
|
||||
import os from "node:os"
|
||||
import path from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
|
||||
@@ -22,66 +18,6 @@ const rootDir = path.join(__dirname, "..")
|
||||
const NEXT_PORT = 6002
|
||||
const NEXT_URL = `http://localhost:${NEXT_PORT}`
|
||||
|
||||
/**
|
||||
* Get the user data path (same as Electron's app.getPath("userData"))
|
||||
*/
|
||||
function getUserDataPath() {
|
||||
const appName = "next-ai-draw-io"
|
||||
switch (process.platform) {
|
||||
case "darwin":
|
||||
return path.join(
|
||||
os.homedir(),
|
||||
"Library",
|
||||
"Application Support",
|
||||
appName,
|
||||
)
|
||||
case "win32":
|
||||
return path.join(
|
||||
process.env.APPDATA ||
|
||||
path.join(os.homedir(), "AppData", "Roaming"),
|
||||
appName,
|
||||
)
|
||||
default:
|
||||
return path.join(os.homedir(), ".config", appName)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* File where the Electron main process (in development) writes the active
|
||||
* preset's env vars, already decrypted and mapped to provider-specific keys
|
||||
* (see writeDevPresetEnv in electron/main/config-manager.ts)
|
||||
*/
|
||||
const PRESET_ENV_FILE = "dev-preset-env.json"
|
||||
|
||||
/**
|
||||
* Read the active preset's env vars as JSON text (null if not available)
|
||||
*/
|
||||
function readPresetEnvFile() {
|
||||
try {
|
||||
const content = readFileSync(
|
||||
path.join(getUserDataPath(), PRESET_ENV_FILE),
|
||||
"utf-8",
|
||||
)
|
||||
JSON.parse(content) // Ignore a half-written file
|
||||
return content
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the active preset's env vars
|
||||
*/
|
||||
function loadPresetEnv(content) {
|
||||
const env = content ? JSON.parse(content) : {}
|
||||
if (Object.keys(env).length === 0) {
|
||||
console.log("📋 No active preset, using .env.local")
|
||||
return null
|
||||
}
|
||||
console.log(`📋 Using preset env: ${Object.keys(env).join(", ")}`)
|
||||
return env
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for the Next.js server to be ready
|
||||
*/
|
||||
@@ -143,21 +79,12 @@ function killProcess(proc) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Start Next.js dev server with preset environment
|
||||
* Start Next.js dev server
|
||||
*/
|
||||
function startNextServer(presetEnv) {
|
||||
// The preset keys are the user's own, not a server's
|
||||
function startNextServer() {
|
||||
// The local server's env keys are the user's own, not a shared server's
|
||||
const env = { ...process.env, NEXT_AI_DRAWIO_DESKTOP: "1" }
|
||||
|
||||
// Apply preset environment variables
|
||||
if (presetEnv) {
|
||||
for (const [key, value] of Object.entries(presetEnv)) {
|
||||
if (value !== undefined && value !== "") {
|
||||
env[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const nextProcess = spawn("npm", ["run", "dev"], {
|
||||
cwd: rootDir,
|
||||
stdio: "inherit",
|
||||
@@ -178,13 +105,9 @@ function startNextServer(presetEnv) {
|
||||
async function main() {
|
||||
console.log("🚀 Starting Electron development environment...\n")
|
||||
|
||||
// Load preset configuration
|
||||
let presetEnvContent = readPresetEnvFile()
|
||||
const presetEnv = loadPresetEnv(presetEnvContent)
|
||||
|
||||
// Start Next.js dev server with preset env
|
||||
// Start Next.js dev server
|
||||
console.log("1. Starting Next.js development server...")
|
||||
let nextProcess = startNextServer(presetEnv)
|
||||
const nextProcess = startNextServer()
|
||||
|
||||
// Wait for Next.js to be ready
|
||||
try {
|
||||
@@ -219,82 +142,14 @@ async function main() {
|
||||
},
|
||||
})
|
||||
|
||||
// Watch for preset env changes
|
||||
const userDataPath = getUserDataPath()
|
||||
let configWatcher = null
|
||||
let restartPending = false
|
||||
|
||||
// Restart Next.js when the preset env vars really changed
|
||||
async function applyPresetChange() {
|
||||
if (restartPending) return
|
||||
const newContent = readPresetEnvFile()
|
||||
if (newContent === null || newContent === presetEnvContent) return
|
||||
|
||||
restartPending = true
|
||||
presetEnvContent = newContent
|
||||
console.log(
|
||||
"\n🔄 Preset configuration changed, restarting Next.js server...",
|
||||
)
|
||||
|
||||
// Kill current Next.js process
|
||||
killProcess(nextProcess)
|
||||
|
||||
// Wait a bit for process to die
|
||||
await new Promise((r) => setTimeout(r, 1000))
|
||||
|
||||
// Reload preset and restart
|
||||
nextProcess = startNextServer(loadPresetEnv(newContent))
|
||||
|
||||
try {
|
||||
await waitForServer(NEXT_URL)
|
||||
console.log("✅ Next.js server restarted with new configuration\n")
|
||||
} catch (err) {
|
||||
console.error("❌ Failed to restart Next.js:", err.message)
|
||||
}
|
||||
|
||||
restartPending = false
|
||||
// A change written during the restart was skipped above
|
||||
applyPresetChange()
|
||||
}
|
||||
|
||||
function setupConfigWatcher() {
|
||||
if (!existsSync(userDataPath)) {
|
||||
// Directory doesn't exist yet, check again later
|
||||
setTimeout(setupConfigWatcher, 5000)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
// Watch the directory, since the file may not exist yet
|
||||
configWatcher = watch(
|
||||
userDataPath,
|
||||
{ persistent: false },
|
||||
(_eventType, filename) => {
|
||||
if (filename === PRESET_ENV_FILE) applyPresetChange()
|
||||
},
|
||||
)
|
||||
console.log("👀 Watching for preset configuration changes...")
|
||||
// Electron may have written its preset before the watch started
|
||||
applyPresetChange()
|
||||
} catch (_err) {
|
||||
// Directory might not be ready yet, try again later
|
||||
setTimeout(setupConfigWatcher, 5000)
|
||||
}
|
||||
}
|
||||
|
||||
// Start watching after a delay (user data directory might not exist yet)
|
||||
setTimeout(setupConfigWatcher, 2000)
|
||||
|
||||
electronProcess.on("close", (code) => {
|
||||
console.log(`\nElectron exited with code ${code}`)
|
||||
if (configWatcher) configWatcher.close()
|
||||
killProcess(nextProcess)
|
||||
process.exit(code || 0)
|
||||
})
|
||||
|
||||
electronProcess.on("error", (err) => {
|
||||
console.error("Electron error:", err)
|
||||
if (configWatcher) configWatcher.close()
|
||||
killProcess(nextProcess)
|
||||
process.exit(1)
|
||||
})
|
||||
@@ -302,7 +157,6 @@ async function main() {
|
||||
// Handle termination signals
|
||||
const cleanup = () => {
|
||||
console.log("\n🛑 Shutting down...")
|
||||
if (configWatcher) configWatcher.close()
|
||||
killProcess(electronProcess)
|
||||
killProcess(nextProcess)
|
||||
process.exit(0)
|
||||
|
||||
@@ -6,54 +6,14 @@
|
||||
* that electron-builder can properly include
|
||||
*/
|
||||
|
||||
import {
|
||||
copyFileSync,
|
||||
existsSync,
|
||||
lstatSync,
|
||||
mkdirSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
statSync,
|
||||
} from "node:fs"
|
||||
import { existsSync, mkdirSync, readdirSync, rmSync } from "node:fs"
|
||||
import { join } from "node:path"
|
||||
import { fileURLToPath } from "node:url"
|
||||
import copyDereferenced from "./copy-dereferenced.cjs"
|
||||
|
||||
const __dirname = fileURLToPath(new URL(".", import.meta.url))
|
||||
const rootDir = join(__dirname, "..")
|
||||
|
||||
/**
|
||||
* Copy directory recursively, converting symlinks to regular files/directories.
|
||||
* This is needed because cpSync with dereference:true does NOT convert symlinks.
|
||||
* macOS codesign fails if bundle contains symlinks pointing outside the bundle.
|
||||
*/
|
||||
function copyDereferenced(src, dst) {
|
||||
const lstat = lstatSync(src)
|
||||
|
||||
if (lstat.isSymbolicLink()) {
|
||||
// Follow symlink and check what it points to
|
||||
const stat = statSync(src)
|
||||
if (stat.isDirectory()) {
|
||||
// Symlink to directory: recursively copy the directory contents
|
||||
mkdirSync(dst, { recursive: true })
|
||||
for (const entry of readdirSync(src)) {
|
||||
copyDereferenced(join(src, entry), join(dst, entry))
|
||||
}
|
||||
} else {
|
||||
// Symlink to file: copy the actual file content
|
||||
mkdirSync(join(dst, ".."), { recursive: true })
|
||||
copyFileSync(src, dst)
|
||||
}
|
||||
} else if (lstat.isDirectory()) {
|
||||
mkdirSync(dst, { recursive: true })
|
||||
for (const entry of readdirSync(src)) {
|
||||
copyDereferenced(join(src, entry), join(dst, entry))
|
||||
}
|
||||
} else {
|
||||
mkdirSync(join(dst, ".."), { recursive: true })
|
||||
copyFileSync(src, dst)
|
||||
}
|
||||
}
|
||||
|
||||
const standaloneDir = join(rootDir, ".next", "standalone")
|
||||
const staticDir = join(rootDir, ".next", "static")
|
||||
const targetDir = join(rootDir, "electron-standalone")
|
||||
|
||||
Reference in New Issue
Block a user