Skip to content

Commit

Permalink
同时将配置文件保存在 程序目录 和 userData (AppData) 下,方便升级和在电脑之间迁移
Browse files Browse the repository at this point in the history
实际只使用保存在 userData (AppData) 下的配置文件
  • Loading branch information
Xmader committed Dec 23, 2019
1 parent d936954 commit 8414c1c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 39 deletions.
48 changes: 10 additions & 38 deletions app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,14 @@ const { app, BrowserWindow, Menu, ipcMain, shell } = require("electron")
const edit_conf = require("./edit_conf.js")
const { buildMenu } = require("./menu.js")
const { displayTray, destroyTray } = require("./tray.js")
const { syncConfigFiles, confPath } = require("./conf.js")

/** @type {Electron.BrowserWindow} */
let mainWindow = null

const icon = path.join(__dirname, "assets", "AriaNg.png")
const trayIcon = path.join(__dirname, "assets", "tray-icon.png")

/**
* @param {string} src
* @param {string} dest
*/
const moveFileSync = (src, dest) => {
// fs.rename() 不能跨驱动器移动文件
fs.copyFileSync(src, dest)
fs.unlinkSync(src)
}

/**
* @param {string} src
* @param {string} dest
*/
const moveConfigFileSync = (src, dest) => {
// 优雅升级,迁移旧版本的配置文件
if (fs.existsSync(src)) {
if (!fs.existsSync(dest)) {
moveFileSync(src, dest)
} else {
fs.unlinkSync(src)
}
}
}

/**
* @param {string} p
*/
Expand All @@ -65,6 +41,10 @@ app.on("window-all-closed", () => {
app.quit()
})

app.on("quit", () => {
syncConfigFiles()
})

app.on("ready", () => {

// 只允许运行单一实例 (进程)
Expand Down Expand Up @@ -117,19 +97,11 @@ app.on("ready", () => {
const aria2c_bin = (platform == "linux" || platform == "darwin") ? "aria2c" : "aria2c.exe"
const aria2c_path = getAria2cPath(platform, arch)

const base_path_old = path.join(__dirname, "aria2")
const conf_path_old = path.join(base_path_old, "aria2.conf")
const session_path_old = path.join(base_path_old, "aria2.session")

const base_path = app.getPath("userData")
const conf_path = path.join(base_path, "aria2.conf")
const session_path = path.join(base_path, "aria2.session")

// 优雅升级,迁移旧版本的配置文件
moveConfigFileSync(conf_path_old, conf_path)
moveConfigFileSync(session_path_old, session_path)
// 将配置文件保存在两处,方便升级和在电脑之间迁移,实际只使用保存在 userData (AppData) 下的配置文件
// 同步 在程序目录 和 在 userData (AppData) 下的 配置文件
syncConfigFiles()

edit_conf(conf_path) // 根据用户的操作系统动态编辑aria2的配置文件
edit_conf(confPath) // 根据用户的操作系统动态编辑aria2的配置文件

//打开主程序
fs.chmodSync(aria2c_path, 0o777)
Expand All @@ -140,7 +112,7 @@ app.on("ready", () => {
function runAria2() {
killAria2()

aria2c = require("child_process").spawn(aria2c_path, [`--conf-path=${conf_path}`], {
aria2c = require("child_process").spawn(aria2c_path, [`--conf-path=${confPath}`], {
stdio: "pipe"
})
aria2c.stdout.pipe(process.stdout, { end: false })
Expand Down
64 changes: 64 additions & 0 deletions app/conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*!
* AriaNg GUI
*
* Copyright (c) 2018-2019 Xmader
* Released under the MIT license
*
* Source Code: https://github.com/Xmader/aria-ng-gui
*
* 同时将配置文件保存在 程序目录 和 userData (AppData) 下
* 方便升级和在电脑之间迁移,实际只使用保存在 userData (AppData) 下的配置文件
*
* conf.js - 同步在两处的配置文件
*
*/

// @ts-check

const path = require("path")
const fs = require("fs")
const { app } = require("electron")

// 在程序目录下的配置文件路径
const basePathOld = path.join(__dirname, "aria2")
const confPathOld = path.join(basePathOld, "aria2.conf")
const sessionPathOld = path.join(basePathOld, "aria2.session")

// 在 userData (AppData) 下的配置文件路径 (实际使用)
const basePath = app.getPath("userData")
const confPath = path.join(basePath, "aria2.conf")
const sessionPath = path.join(basePath, "aria2.session")

/**
* 同步在两处的配置文件
* @param {string} path0
* @param {string} path1 (实际使用)
*/
const syncConfigFile = (path0, path1) => {
if (fs.existsSync(path0)) {
if (!fs.existsSync(path1)) { // 实际使用的文件不存在
fs.copyFileSync(path0, path1)
return
}

if (fs.statSync(path0).mtimeMs > fs.statSync(path1).mtimeMs) { // 在程序目录下的配置文件比实际使用的文件新
fs.copyFileSync(path0, path1)
return
}
}

fs.copyFileSync(path1, path0)
}

const syncConfigFiles = () => {
syncConfigFile(confPathOld, confPath)
syncConfigFile(sessionPathOld, sessionPath)
}

module.exports = {
confPathOld,
sessionPathOld,
confPath,
sessionPath,
syncConfigFiles,
}
7 changes: 6 additions & 1 deletion app/plugins/save-local-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@

// @ts-check

const { remote: { app } } = require("electron")
const { remote: { app, require: remoteRequire } } = require("electron")
const { EOL } = require("os")

/** @type {import("../conf")} */
const { syncConfigFiles } = remoteRequire("./conf.js")

/**
* @typedef {import("./index").Plugin} Plugin
* @type {Plugin}
Expand Down Expand Up @@ -47,6 +50,8 @@ module.exports = {

await fs.writeFile(conf_path, conf)

syncConfigFiles()

})

}
Expand Down

0 comments on commit 8414c1c

Please sign in to comment.