42 lines
1.0 KiB
JavaScript
42 lines
1.0 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, 'NPQS9100_Runtime')
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
hasNonAscii,
|
|
getDriveRoot,
|
|
resolveRuntimeStrategy
|
|
};
|