编排添加解析路径功能

This commit is contained in:
fofolee
2025-01-05 13:08:22 +08:00
parent 00ddba20ec
commit 923fc9e4de
16 changed files with 684 additions and 34 deletions

View File

@@ -1,5 +1,5 @@
const quickcomposer = {
textProcessor: require("./quickcomposer/textProcessor"),
text: require("./quickcomposer/text"),
simulate: require("./quickcomposer/simulate"),
file: require("./quickcomposer/file"),
system: require("./quickcomposer/system"),

View File

@@ -1,13 +1,17 @@
const { execSync } = require("child_process");
const { exec: execAsync } = require("child_process");
const iconv = require("iconv-lite");
const os = require("os");
const util = require("util");
// 将 exec 转换为 Promise 版本
const execPromise = util.promisify(execAsync);
function getSystemEncoding() {
// Windows 默认使用 GBK/GB2312其他系统默认 UTF-8
return os.platform() === "win32" ? "gbk" : "utf8";
}
function exec(command, options = {}) {
async function exec(command, options = {}) {
try {
const {
autoEncoding = true,
@@ -17,7 +21,7 @@ function exec(command, options = {}) {
} = options;
// 执行命令,总是使用 buffer 获取原始输出
const output = execSync(command, {
const { stdout: output } = await execPromise(command, {
...execOptions,
encoding: "buffer",
windowsHide,

View File

@@ -1,7 +1,9 @@
const exec = require("./exec");
const os = require("./os");
const path = require("./path");
module.exports = {
exec,
os,
path,
};

View File

@@ -0,0 +1,150 @@
const path = require("path");
/**
* 规范化路径
* @param {string} p 要规范化的路径
* @returns {string} 规范化后的路径
*/
async function normalize(p) {
try {
return path.normalize(p);
} catch (error) {
throw new Error(`路径规范化失败: ${error.message}`);
}
}
/**
* 连接路径片段
* @param {...string} paths 路径片段
* @returns {string} 连接后的路径
*/
async function join(...paths) {
try {
return path.join(...paths);
} catch (error) {
throw new Error(`路径连接失败: ${error.message}`);
}
}
/**
* 解析路径
* @param {string} p 要解析的路径
* @returns {Object} 解析结果,包含 root, dir, base, ext, name
*/
async function parse(p) {
try {
return path.parse(p);
} catch (error) {
throw new Error(`路径解析失败: ${error.message}`);
}
}
/**
* 获取路径的目录名
* @param {string} p 路径
* @returns {string} 目录名
*/
async function dirname(p) {
try {
return path.dirname(p);
} catch (error) {
throw new Error(`获取目录名失败: ${error.message}`);
}
}
/**
* 获取路径的文件名
* @param {string} p 路径
* @param {string} [ext] 可选的扩展名,如果提供则从结果中移除
* @returns {string} 文件名
*/
async function basename(p, ext) {
try {
return path.basename(p, ext);
} catch (error) {
throw new Error(`获取文件名失败: ${error.message}`);
}
}
/**
* 获取路径的扩展名
* @param {string} p 路径
* @returns {string} 扩展名
*/
async function extname(p) {
try {
return path.extname(p);
} catch (error) {
throw new Error(`获取扩展名失败: ${error.message}`);
}
}
/**
* 判断路径是否为绝对路径
* @param {string} p 路径
* @returns {boolean} 是否为绝对路径
*/
async function isAbsolute(p) {
try {
return path.isAbsolute(p);
} catch (error) {
throw new Error(`判断绝对路径失败: ${error.message}`);
}
}
/**
* 计算相对路径
* @param {string} from 起始路径
* @param {string} to 目标路径
* @returns {string} 相对路径
*/
async function relative(from, to) {
try {
return path.relative(from, to);
} catch (error) {
throw new Error(`计算相对路径失败: ${error.message}`);
}
}
/**
* 将路径解析为绝对路径
* @param {...string} paths 路径片段
* @returns {string} 解析后的绝对路径
*/
async function resolve(...paths) {
try {
return path.resolve(...paths);
} catch (error) {
throw new Error(`解析绝对路径失败: ${error.message}`);
}
}
/**
* 格式化路径对象为路径字符串
* @param {Object} pathObject 路径对象,包含 dir, root, base, name, ext
* @returns {string} 格式化后的路径
*/
async function format(pathObject) {
try {
return path.format(pathObject);
} catch (error) {
throw new Error(`格式化路径失败: ${error.message}`);
}
}
module.exports = {
normalize,
join,
parse,
dirname,
basename,
extname,
isAbsolute,
relative,
resolve,
format,
sep: path.sep,
delimiter: path.delimiter,
win32: path.win32,
posix: path.posix,
};