Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
d76ee92f
authored
Oct 05, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: set data directory path in main process and update preload.js to use it
parent
4675ea50
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
10 additions
and
17 deletions
+10
-17
electron/main.js
+6
-2
electron/preload.js
+4
-15
No files found.
electron/main.js
View file @
d76ee92f
...
...
@@ -222,6 +222,12 @@ function checkServerAvailability(port, maxRetries = 30, retryDelay = 1000) {
function
startServer
()
{
return
new
Promise
((
resolve
,
reject
)
=>
{
const
isDev
=
process
.
env
.
NODE_ENV
===
'development'
;
const
userDataPath
=
app
.
getPath
(
'userData'
);
const
dataDir
=
path
.
join
(
userDataPath
,
'data'
);
// 设置环境变量供 preload.js 使用
process
.
env
.
ELECTRON_DATA_DIR
=
dataDir
;
if
(
isDev
)
{
// 开发模式:假设开发者手动启动了 Go 后端和前端开发服务器
...
...
@@ -250,8 +256,6 @@ function startServer() {
// 生产模式:启动二进制服务器
const
env
=
{
...
process
.
env
,
PORT
:
PORT
.
toString
()
};
const
userDataPath
=
app
.
getPath
(
'userData'
);
const
dataDir
=
path
.
join
(
userDataPath
,
'data'
);
if
(
!
fs
.
existsSync
(
dataDir
))
{
fs
.
mkdirSync
(
dataDir
,
{
recursive
:
true
});
...
...
electron/preload.js
View file @
d76ee92f
const
{
contextBridge
}
=
require
(
'electron'
);
// 获取数据目录路径(用于显示给用户)
//
使用字符串拼接而不是 path.join 避免模块依赖问题
//
优先使用主进程设置的真实路径,如果没有则回退到手动拼接
function
getDataDirPath
()
{
const
platform
=
process
.
platform
;
const
homeDir
=
process
.
env
.
HOME
||
process
.
env
.
USERPROFILE
||
''
;
switch
(
platform
)
{
case
'darwin'
:
return
`
${
homeDir
}
/Library/Application Support/New API/data`
;
case
'win32'
:
{
const
appData
=
process
.
env
.
APPDATA
||
`
${
homeDir
}
\\AppData\\Roaming`
;
return
`
${
appData
}
\\New API\\data`
;
}
case
'linux'
:
return
`
${
homeDir
}
/.config/New API/data`
;
default
:
return
`
${
homeDir
}
/.new-api/data`
;
// 如果主进程已设置真实路径,直接使用
if
(
process
.
env
.
ELECTRON_DATA_DIR
)
{
return
process
.
env
.
ELECTRON_DATA_DIR
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment