Skip to content

Commit

Permalink
yakit download position & custom yakit-projects position (#1461)
Browse files Browse the repository at this point in the history
* feat: yakit下载路径平台区分

* 调整自定义路径获取

* linux下载文件夹额外路基

* 容错处理

---------

Co-authored-by: Youngster_yj <907985037@qq.com>
  • Loading branch information
b1rdfree and youngster-yj authored Mar 1, 2024
1 parent 2f8e9d2 commit c4e04b4
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 75 deletions.
87 changes: 66 additions & 21 deletions app/main/filePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -38,6 +70,7 @@ const getLocalYaklangEngine = () => {
}
}

/** 生成软件根目录+参数的完成路径 */
const loadExtraFilePath = (s) => {
if (electronIsDev) {
return s
Expand All @@ -55,7 +88,7 @@ const loadExtraFilePath = (s) => {
}
}

/** 所有缓存数据文件夹 */
/** 基础缓存数据文件夹 */
const basicDir = path.join(YakitProjectPath, "base")
/** 本地缓存数据文件地址 */
const localCachePath = path.join(basicDir, "yakit-local.json")
Expand All @@ -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")
Expand All @@ -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
}
76 changes: 30 additions & 46 deletions app/main/handlers/upgradeUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
Expand Down Expand Up @@ -114,7 +97,7 @@ const isWindows = process.platform === "win32";

const saveAllSecret = (authInfos) => {
try {
fs.unlinkSync(secretFile)
fs.unlinkSync(remoteLinkFile)
} catch (e) {

}
Expand All @@ -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 = () => {
Expand All @@ -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")
}
}

Expand Down Expand Up @@ -222,7 +205,7 @@ module.exports = {
return authMeta;
})
ipcMain.handle("get-yakit-remote-auth-dir", async (e, name) => {
return secretDir;
return remoteLinkDir;
})

// asyncQueryLatestYakEngineVersion wrapper
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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())) {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)) {
Expand Down
13 changes: 9 additions & 4 deletions app/main/uiOperate/hardware.js
Original file line number Diff line number Diff line change
@@ -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瞬时使用均值
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/renderer/src/main/src/components/layout/FuncDomain.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ const UIOpUpdateYakit: React.FC<UIOpUpdateProps> = React.memo((props) => {
{isSimple ? (
<></>
) : isUpdateWait ? (
<YakitButton onClick={() => ipcRenderer.invoke("open-yakit-or-yaklang")}>{`安装 `}</YakitButton>
<YakitButton onClick={() => ipcRenderer.invoke("open-yakit-path")}>{`安装 `}</YakitButton>
) : isUpdate ? (
<div className={styles["update-btn"]} onClick={() => onDownload("yakit")}>
<UpdateSvgIcon style={{marginRight: 4}} />
Expand Down
4 changes: 2 additions & 2 deletions app/renderer/src/main/src/components/layout/UILayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1877,7 +1877,7 @@ const DownloadYakit: React.FC<DownloadYakitProps> = 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) => {
Expand Down Expand Up @@ -1928,7 +1928,7 @@ const DownloadYakit: React.FC<DownloadYakitProps> = 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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ export const UpdateYakitAndYaklang: React.FC<UpdateYakitAndYaklangProps> = React
}, 300)
})
const yakitUpdate = useMemoizedFn(() => {
ipcRenderer.invoke("open-yakit-or-yaklang")
ipcRenderer.invoke("open-yakit-path")
setTimeout(() => {
ipcRenderer.invoke("UIOperate", "close")
}, 100)
Expand Down

0 comments on commit c4e04b4

Please sign in to comment.