From c4e04b423015ba24f8a5b84d2e4814f19f74a14a Mon Sep 17 00:00:00 2001 From: nonight <378099757@qq.com> Date: Fri, 1 Mar 2024 15:54:20 +0800 Subject: [PATCH] yakit download position & custom yakit-projects position (#1461) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: yakit下载路径平台区分 * 调整自定义路径获取 * linux下载文件夹额外路基 * 容错处理 --------- Co-authored-by: Youngster_yj <907985037@qq.com> --- app/main/filePath.js | 87 ++++++++++++++----- app/main/handlers/upgradeUtil.js | 76 +++++++--------- app/main/uiOperate/hardware.js | 13 ++- .../main/src/components/layout/FuncDomain.tsx | 2 +- .../main/src/components/layout/UILayout.tsx | 4 +- .../layout/update/UpdateYakitAndYaklang.tsx | 2 +- 6 files changed, 109 insertions(+), 75 deletions(-) diff --git a/app/main/filePath.js b/app/main/filePath.js index d5b62248a6..8c554dd05f 100644 --- a/app/main/filePath.js +++ b/app/main/filePath.js @@ -4,30 +4,62 @@ const os = require("os") const path = require("path") const process = require("process") const fs = require("fs") + /** 软件根目录 */ const appPath = app.isPackaged ? path.dirname(app.getPath("exe")) : app.getAppPath() -/** 软件关联项目相关目录路径 */ -/** 优先使用家目录下的yakit-projects - * 在新版本中,windows自定义安装路径会将家目录的yakit-projects迁移到软件根目录下,则会使用该目录 */ -const defaultYakitProjectPath = path.join(os.homedir(), "yakit-projects") -const currentYakitProjectPath = path.join(appPath, "yakit-projects") -const YakitProjectPath = - ( - process.platform === "win32" - ? ( - fs.existsSync(currentYakitProjectPath) ? - currentYakitProjectPath: - process.env.YAKIT_HOME || defaultYakitProjectPath - ) - : defaultYakitProjectPath - ) - -/** 引擎和软件安装包路径 */ -const yaklangEngineDir = path.join(YakitProjectPath, "yak-engine") +/** 系统用户根路径 */ +const osHome = os.homedir() +/** 软件关联数据文件夹名 */ +const projectName = "yakit-projects" + +console.log(`software: ${appPath}\n osHome: ${osHome}\n projectName: ${projectName}`) + +/** 软件关联数据路径设置逻辑 Start */ +// 数据文件夹路径 +let project_path = "" +// os-home环境 项目文件夹路径 +const osHomeProjectPath = path.join(osHome, projectName) +// 软件环境 项目文件夹路径 +const appProjectPath = path.join(appPath, projectName) + +try { + /** + * 开发环境,win系统会优先查找环境变量的指定地址,mac和linux指定为os-home + * 发布环境,win系统指定为软件根目录下,mac和linux指定为os-home + */ + if (process.platform === "win32") { + if (electronIsDev) { + project_path = process.env.YAKIT_HOME + ? fs.existsSync(process.env.YAKIT_HOME) + ? process.env.YAKIT_HOME + : osHomeProjectPath + : osHomeProjectPath + } else { + project_path = appProjectPath + } + } else { + project_path = osHomeProjectPath + } +} catch (error) { + console.log(`读取项目数据文件夹失败,${error}`) +} +/** 软件关联数据路径设置逻辑 End */ + /** - * Yaklang引擎在本地的绝对地址 - * @returns {String} 本地绝对地址 + * @name 软件关联项目相关目录路径 + * 在新版本中,windows自定义安装路径会将os-home目录的yakit-projects迁移到软件根目录下 + * 如果获取项目关联文件夹路径错误时,将自动设置为系统用户下面(容灾处理) */ +const YakitProjectPath = project_path || osHomeProjectPath +console.log(`yakit-projects-path: ${YakitProjectPath}`) + +/** 引擎文件夹路径 */ +const yaklangEngineDir = path.join(YakitProjectPath, "yak-engine") + +/** yakit安装包路径 */ +const yakitInstallDir = path.join(os.homedir(), "Downloads") + +/** 引擎文件路径 */ const getLocalYaklangEngine = () => { switch (process.platform) { case "darwin": @@ -38,6 +70,7 @@ const getLocalYaklangEngine = () => { } } +/** 生成软件根目录+参数的完成路径 */ const loadExtraFilePath = (s) => { if (electronIsDev) { return s @@ -55,7 +88,7 @@ const loadExtraFilePath = (s) => { } } -/** 所有缓存数据文件夹 */ +/** 基础缓存数据文件夹 */ const basicDir = path.join(YakitProjectPath, "base") /** 本地缓存数据文件地址 */ const localCachePath = path.join(basicDir, "yakit-local.json") @@ -69,6 +102,9 @@ const remoteLinkDir = path.join(YakitProjectPath, "auth") /** 远程连接引擎配置数据文件地址 */ const remoteLinkFile = path.join(remoteLinkDir, "yakit-remote.json") +/** yak code文件夹路径 */ +const codeDir = path.join(YakitProjectPath, "code") + /** Html模板文件地址 */ // const yakitDir = path.join(os.homedir(), "AppData","Local","Programs","yakit") // const htmlTemplateDir = path.join(yakitDir, "report") @@ -79,13 +115,22 @@ const windowStatePatch = path.join(basicDir) module.exports = { YakitProjectPath, + yaklangEngineDir, + yakitInstallDir, getLocalYaklangEngine, + loadExtraFilePath, + + basicDir, localCachePath, extraLocalCachePath, engineLog, + remoteLinkDir, remoteLinkFile, + + codeDir, + htmlTemplateDir, windowStatePatch } diff --git a/app/main/handlers/upgradeUtil.js b/app/main/handlers/upgradeUtil.js index f9689e50d2..4657d5e4f1 100644 --- a/app/main/handlers/upgradeUtil.js +++ b/app/main/handlers/upgradeUtil.js @@ -9,50 +9,33 @@ const requestProgress = require("request-progress"); const request = require("request"); const zip = require('node-stream-zip'); -const electronIsDev = require("electron-is-dev"); -const {YakitProjectPath} = require("../filePath") +const { + YakitProjectPath, + remoteLinkDir, + yaklangEngineDir, + basicDir, + remoteLinkFile, + codeDir, + loadExtraFilePath, + yakitInstallDir +} = require("../filePath") - -const secretDir = path.join(YakitProjectPath, "auth"); - -const yakEngineDir = path.join(YakitProjectPath, "yak-engine") -const codeDir = path.join(YakitProjectPath, "code"); -const cacheDir = path.join(YakitProjectPath, "base"); const userChromeDataDir = path.join(YakitProjectPath, "chrome-profile"); -const secretFile = path.join(secretDir, "yakit-remote.json"); const authMeta = []; -const loadExtraFilePath = (s) => { - if (electronIsDev) { - return s - } - - switch (os.platform()) { - case "darwin": - return path.join(app.getAppPath(), "../..", s) - case "linux": - return path.join(app.getAppPath(), "../..", s) - case "win32": - return path.join(app.getAppPath(), "../..", s) - default: - // ..../Contents/Resources/app.asar/... - return path.join(app.getAppPath(), s) - } -} - const initMkbaseDir = async () => { return new Promise((resolve, reject) => { try { - fs.mkdirSync(secretDir, {recursive: true}) - fs.mkdirSync(cacheDir, {recursive: true}) + fs.mkdirSync(remoteLinkDir, {recursive: true}) + fs.mkdirSync(basicDir, {recursive: true}) fs.mkdirSync(userChromeDataDir, {recursive: true}) - fs.mkdirSync(yakEngineDir, {recursive: true}) + fs.mkdirSync(yaklangEngineDir, {recursive: true}) fs.mkdirSync(codeDir, {recursive: true}) try { console.info("Start checking bins/resources") const extraResources = loadExtraFilePath(path.join("bins", "resources")); - const resourceBase = cacheDir; + const resourceBase = basicDir; if (!fs.existsSync(path.join(resourceBase, "flag.txt"))) { console.info("Start to load bins/resources ...") fs.readdirSync(extraResources).forEach(value => { @@ -79,7 +62,7 @@ const initMkbaseDir = async () => { const loadSecrets = () => { authMeta.splice(0, authMeta.length) try { - const data = fs.readFileSync(path.join(secretDir, "yakit-remote.json")); + const data = fs.readFileSync(path.join(remoteLinkDir, "yakit-remote.json")); JSON.parse(data).forEach(i => { if (!(i["host"] && i["port"])) { return @@ -114,7 +97,7 @@ const isWindows = process.platform === "win32"; const saveAllSecret = (authInfos) => { try { - fs.unlinkSync(secretFile) + fs.unlinkSync(remoteLinkFile) } catch (e) { } @@ -125,7 +108,7 @@ const saveAllSecret = (authInfos) => { return arr.findIndex(origin => origin.name === v.name) === i })] ); - fs.writeFileSync(secretFile, new Buffer(authFileStr, "utf8")) + fs.writeFileSync(remoteLinkFile, new Buffer(authFileStr, "utf8")) }; const getYakDownloadUrl = () => { @@ -143,9 +126,9 @@ const getLatestYakLocalEngine = () => { switch (process.platform) { case "darwin": case "linux": - return path.join(yakEngineDir, "yak") + return path.join(yaklangEngineDir, "yak") case "win32": - return path.join(yakEngineDir, "yak.exe") + return path.join(yaklangEngineDir, "yak.exe") } } @@ -222,7 +205,7 @@ module.exports = { return authMeta; }) ipcMain.handle("get-yakit-remote-auth-dir", async (e, name) => { - return secretDir; + return remoteLinkDir; }) // asyncQueryLatestYakEngineVersion wrapper @@ -324,7 +307,7 @@ module.exports = { // asyncDownloadLatestYak wrapper const asyncDownloadLatestYak = (version) => { return new Promise((resolve, reject) => { - const dest = path.join(yakEngineDir, `yak-${version}`); + const dest = path.join(yaklangEngineDir, `yak-${version}`); try { fs.unlinkSync(dest) } catch (e) { @@ -368,13 +351,14 @@ module.exports = { } const downloadYakitByDownloadUrl = (resolve,reject,downloadUrl) => { - const dest = path.join(yakEngineDir, path.basename(downloadUrl)); + // 可能存在中文的下载文件夹,就判断下Downloads文件夹是否存在,不存在则新建一个 + if(!fs.existsSync(yakitInstallDir)) fs.mkdirSync(yakitInstallDir, {recursive: true}) + const dest = path.join(yakitInstallDir, path.basename(downloadUrl)); try { fs.unlinkSync(dest) } catch (e) { } - // https://github.com/IndigoUnited/node-request-progress // The options argument is optional so you can omit it requestProgress( @@ -449,7 +433,7 @@ module.exports = { const installYakEngine = (version) => { return new Promise((resolve, reject) => { - let origin = path.join(yakEngineDir, `yak-${version}`); + let origin = path.join(yaklangEngineDir, `yak-${version}`); origin = origin.replaceAll(`"`, `\"`); let dest = getLatestYakLocalEngine(); //;isWindows ? getWindowsInstallPath() : "/usr/local/bin/yak"; @@ -511,7 +495,7 @@ module.exports = { }) console.info("Start to Extract yak.zip: Set `ready`") zipHandler.on("ready", () => { - const buildInPath = path.join(yakEngineDir, "yak.build-in"); + const buildInPath = path.join(yaklangEngineDir, "yak.build-in"); console.log('Entries read: ' + zipHandler.entriesCount); for (const entry of Object.values(zipHandler.entries())) { @@ -546,7 +530,7 @@ module.exports = { * 复制引擎到真实地址 * */ try { - let targetEngine = path.join(yakEngineDir, isWindows ? "yak.exe" : "yak") + let targetEngine = path.join(yaklangEngineDir, isWindows ? "yak.exe" : "yak") if (!isWindows) { fs.copyFileSync(buildInPath, targetEngine) fs.chmodSync(targetEngine, 0o755) @@ -599,9 +583,9 @@ module.exports = { // asyncRestoreEngineAndPlugin wrapper ipcMain.handle("RestoreEngineAndPlugin", async (e, params) => { - const engineTarget = isWindows ? path.join(yakEngineDir, "yak.exe") : path.join(yakEngineDir, "yak") - const buidinEngine = path.join(yakEngineDir, "yak.build-in") - const cacheFlagLock = path.join(cacheDir, "flag.txt") + const engineTarget = isWindows ? path.join(yaklangEngineDir, "yak.exe") : path.join(yaklangEngineDir, "yak") + const buidinEngine = path.join(yaklangEngineDir, "yak.build-in") + const cacheFlagLock = path.join(basicDir, "flag.txt") try { // remove old engine if (fs.existsSync(buidinEngine)) { diff --git a/app/main/uiOperate/hardware.js b/app/main/uiOperate/hardware.js index 06be6de97b..c3bbe7364f 100644 --- a/app/main/uiOperate/hardware.js +++ b/app/main/uiOperate/hardware.js @@ -1,9 +1,8 @@ -const {ipcMain, clipboard, shell} = require("electron") +const {ipcMain, shell} = require("electron") const OS = require("os") -const {execFile, exec} = require("child_process") const path = require("path") const process = require("process") -const {yaklangEngineDir, remoteLinkDir} = require("../filePath") +const {yaklangEngineDir, remoteLinkDir, yakitInstallDir} = require("../filePath") module.exports = (win, getClient) => { // CPU瞬时使用均值 @@ -109,10 +108,16 @@ module.exports = (win, getClient) => { return `${process.platform}-${process.arch}` }) - /** 打开 yaklang 或 yakit 文件所在文件夹 */ + /** 打开 yaklang 或 yakit 文件所在文件夹 (ps:随着yakit下载移动至下载文件夹中,此方法仅打开yaklang)*/ ipcMain.handle("open-yakit-or-yaklang", (e) => { return shell.openPath(yaklangEngineDir) }) + + /** 打开 yakit 文件所在文件夹 */ + ipcMain.handle("open-yakit-path", (e) => { + return shell.openPath(yakitInstallDir) + }) + /** 打开远程连接配置信息文件夹 */ ipcMain.handle("open-remote-link", (e) => { return shell.openPath(remoteLinkDir) diff --git a/app/renderer/src/main/src/components/layout/FuncDomain.tsx b/app/renderer/src/main/src/components/layout/FuncDomain.tsx index 637ba7c9b9..657aade365 100644 --- a/app/renderer/src/main/src/components/layout/FuncDomain.tsx +++ b/app/renderer/src/main/src/components/layout/FuncDomain.tsx @@ -1301,7 +1301,7 @@ const UIOpUpdateYakit: React.FC = React.memo((props) => { {isSimple ? ( <> ) : isUpdateWait ? ( - ipcRenderer.invoke("open-yakit-or-yaklang")}>{`安装 `} + ipcRenderer.invoke("open-yakit-path")}>{`安装 `} ) : isUpdate ? (
onDownload("yakit")}> diff --git a/app/renderer/src/main/src/components/layout/UILayout.tsx b/app/renderer/src/main/src/components/layout/UILayout.tsx index b8d0c1b7b1..5173696e81 100644 --- a/app/renderer/src/main/src/components/layout/UILayout.tsx +++ b/app/renderer/src/main/src/components/layout/UILayout.tsx @@ -1877,7 +1877,7 @@ const DownloadYakit: React.FC = React.memo((props) => { // @ts-ignore size: getDownloadProgress().size }) - ipcRenderer.invoke("open-yakit-or-yaklang") + ipcRenderer.invoke("open-yakit-path") ipcRenderer.invoke("download-update-wait", "yakit") }) .catch((e: any) => { @@ -1928,7 +1928,7 @@ const DownloadYakit: React.FC = React.memo((props) => { // @ts-ignore size: getDownloadProgress().size }) - ipcRenderer.invoke("open-yakit-or-yaklang") + ipcRenderer.invoke("open-yakit-path") ipcRenderer.invoke("download-update-wait", "yakit") }) .catch((e: any) => { diff --git a/app/renderer/src/main/src/components/layout/update/UpdateYakitAndYaklang.tsx b/app/renderer/src/main/src/components/layout/update/UpdateYakitAndYaklang.tsx index b9c4cd5755..fc7930f173 100644 --- a/app/renderer/src/main/src/components/layout/update/UpdateYakitAndYaklang.tsx +++ b/app/renderer/src/main/src/components/layout/update/UpdateYakitAndYaklang.tsx @@ -211,7 +211,7 @@ export const UpdateYakitAndYaklang: React.FC = React }, 300) }) const yakitUpdate = useMemoizedFn(() => { - ipcRenderer.invoke("open-yakit-or-yaklang") + ipcRenderer.invoke("open-yakit-path") setTimeout(() => { ipcRenderer.invoke("UIOperate", "close") }, 100)