Files
CN_Tool_client/scripts/path-utils.js
2026-04-13 17:32:58 +08:00

43 lines
1.1 KiB
JavaScript

const path = require('path');
/**
* 判断路径中是否包含非 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;
}
/**
* 解析运行期路径策略。
* - 安全路径:继续直接使用应用目录
* - 非 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,
resolveRuntimeStrategy
};