2026-04-13 17:32:58 +08:00
|
|
|
|
const path = require('path');
|
2026-06-11 10:53:02 +08:00
|
|
|
|
const fs = require('fs');
|
2026-04-13 17:32:58 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 判断路径中是否包含非 ASCII 字符。
|
|
|
|
|
|
* 这里不只判断中文,其他非 ASCII 字符同样视为不安全路径。
|
|
|
|
|
|
*/
|
|
|
|
|
|
function hasNonAscii(targetPath = '') {
|
|
|
|
|
|
return /[^\x00-\x7F]/.test(targetPath);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取当前路径所在盘符根目录,例如 D:\
|
|
|
|
|
|
*/
|
|
|
|
|
|
function getDriveRoot(targetPath = '') {
|
|
|
|
|
|
const resolvedPath = path.resolve(targetPath || process.cwd());
|
|
|
|
|
|
return path.parse(resolvedPath).root;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-11 10:53:02 +08:00
|
|
|
|
function resolvePackagedRuntime() {
|
|
|
|
|
|
const resourcesPath = process.resourcesPath;
|
|
|
|
|
|
const packagedBaseDir = resourcesPath ? path.dirname(resourcesPath) : '';
|
|
|
|
|
|
const hasPackagedResources = resourcesPath
|
|
|
|
|
|
&& fs.existsSync(path.join(resourcesPath, 'extraResources'))
|
|
|
|
|
|
&& fs.existsSync(path.join(packagedBaseDir, 'mysql'));
|
|
|
|
|
|
|
|
|
|
|
|
if (hasPackagedResources) {
|
|
|
|
|
|
return {
|
|
|
|
|
|
isPackaged: true,
|
|
|
|
|
|
baseDir: packagedBaseDir,
|
|
|
|
|
|
resourcesPath
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const baseDir = path.join(__dirname, '..');
|
|
|
|
|
|
return {
|
|
|
|
|
|
isPackaged: false,
|
|
|
|
|
|
baseDir,
|
|
|
|
|
|
resourcesPath: path.join(baseDir, 'build', 'extraResources')
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-13 17:32:58 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 解析运行期路径策略。
|
|
|
|
|
|
* - 安全路径:继续直接使用应用目录
|
|
|
|
|
|
* - 非 ASCII 路径:切到英文安全路径
|
|
|
|
|
|
*/
|
|
|
|
|
|
function resolveRuntimeStrategy(baseDir) {
|
|
|
|
|
|
const normalizedBaseDir = path.resolve(baseDir);
|
|
|
|
|
|
const driveRoot = getDriveRoot(normalizedBaseDir);
|
|
|
|
|
|
const usesSafePaths = hasNonAscii(normalizedBaseDir);
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
|
baseDir: normalizedBaseDir,
|
|
|
|
|
|
driveRoot,
|
|
|
|
|
|
usesSafePaths,
|
|
|
|
|
|
// 与历史客户端隔离,避免继续复用旧品牌运行目录。
|
|
|
|
|
|
safeRuntimeRoot: path.join(driveRoot, 'CN_Tool_Runtime')
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
|
hasNonAscii,
|
|
|
|
|
|
getDriveRoot,
|
2026-06-11 10:53:02 +08:00
|
|
|
|
resolvePackagedRuntime,
|
2026-04-13 17:32:58 +08:00
|
|
|
|
resolveRuntimeStrategy
|
|
|
|
|
|
};
|