t-comm
Version:
专业、稳定、纯粹的工具库
130 lines (127 loc) • 4.97 kB
JavaScript
import _typeof from '@babel/runtime/helpers/typeof';
import { getChildProcess } from '../nodejs/child_process.mjs';
/**
* 在 Node.js 中调用 child_process.execSync 执行命令
* 该方法会对输出结果进行处理,默认只返回指定行的内容(默认第一行)
* @param command - 要执行的命令字符串
* @param root - 执行命令的工作目录,默认为当前工作目录
* @param options - 配置选项,可以是字符串(stdio)或对象
* @param options.stdio - 标准输入输出配置,默认为 'pipe'
* @param options.line - 返回结果的行号,默认为 0(第一行),设置为 -1 返回全部内容
* @param options.throwError - 是否在命令执行失败时抛出错误,默认为 false
* @returns 命令执行结果字符串
* @example
* ```ts
* // 获取 git 分支名(第一行)
* const branch = execCommand('git branch', '/path/to/repo');
*
* // 获取完整输出
* const output = execCommand('ls -la', './', { line: -1 });
*
* // 自定义 stdio
* const result = execCommand('npm install', './', { stdio: 'inherit' });
* ```
*/
function execCommand(command, root, options) {
var _a, _b, _c;
if (!root) {
root = process.cwd();
}
var _getChildProcess = getChildProcess(),
execSync = _getChildProcess.execSync;
var innerOptions = {};
if (typeof options === 'string') {
innerOptions = {
stdio: options
};
} else if (_typeof(options) === 'object') {
innerOptions = options;
}
var stdio = (_a = innerOptions === null || innerOptions === void 0 ? void 0 : innerOptions.stdio) !== null && _a !== void 0 ? _a : 'pipe';
var line = (_b = innerOptions === null || innerOptions === void 0 ? void 0 : innerOptions.line) !== null && _b !== void 0 ? _b : 0;
var res = '';
try {
res = execSync(command, {
cwd: root || process.cwd(),
encoding: 'utf-8',
stdio: stdio || 'pipe'
});
} catch (err) {
// 默认(throwError=false)语义即“失败静默返回空字符串”,
// 不应无条件打印堆栈,否则在 CI / detached HEAD / 未配 user.name 等
// 命令天然失败的场景会疯狂刷屏。仅在需要抛出或显式开启调试时打印。
if (innerOptions === null || innerOptions === void 0 ? void 0 : innerOptions.throwError) {
throw err;
}
if (process.env.T_COMM_DEBUG) {
console.log('[execCommand] error: ', err);
}
}
if (line > -1) {
return ((_c = res === null || res === void 0 ? void 0 : res.split('\n')[line]) === null || _c === void 0 ? void 0 : _c.trim()) || '';
}
return res;
}
/**
* execCommand 的偏函数应用版本,固定 throwError 为 true
* 当命令执行失败时会直接抛出错误,而非静默返回空字符串
* @param command - 要执行的命令字符串
* @param root - 执行命令的工作目录,默认为当前工作目录
* @param options - 配置选项,可以是字符串(stdio)或对象(不含 throwError,因为已固定为 true)
* @param options.stdio - 标准输入输出配置,默认为 'pipe'
* @param options.line - 返回结果的行号,默认为 0(第一行),设置为 -1 返回全部内容
* @returns 命令执行结果字符串
* @example
* ```ts
* // 执行失败时会抛出错误
* const branch = execCommandStrict('git branch', '/path/to/repo');
*
* // 配合 try-catch 使用
* try {
* const result = execCommandStrict('some-command');
* } catch (err) {
* console.error('命令执行失败:', err);
* }
* ```
*/
function execCommandStrict(command, root, options) {
if (typeof options === 'string') {
return execCommand(command, root, {
stdio: options,
throwError: true
});
}
return execCommand(command, root, Object.assign(Object.assign({}, options), {
throwError: true
}));
}
/**
* 在 Node.js 中调用 child_process.execSync 执行命令并返回第一行结果
* 该方法是 execCommand 的简化版本,始终返回输出的第一行
* @param command - 要执行的命令字符串
* @param root - 执行命令的工作目录,默认为当前工作目录
* @param stdio - 标准输入输出配置,默认为 'pipe'
* @returns 命令执行结果的第一行(去除首尾空格)
* @example
* ```ts
* // 获取当前 git 分支
* const branch = execCommandAll('git rev-parse --abbrev-ref HEAD');
*
* // 获取 Node 版本
* const nodeVersion = execCommandAll('node --version');
* ```
*/
function execCommandAll(command, root, stdio) {
var _a, _b;
if (!root) {
root = process.cwd();
}
var _getChildProcess2 = getChildProcess(),
execSync = _getChildProcess2.execSync;
return ((_b = (_a = execSync(command, {
cwd: root || process.cwd(),
encoding: 'utf-8',
stdio: stdio || 'pipe'
})) === null || _a === void 0 ? void 0 : _a.split('\n')[0]) === null || _b === void 0 ? void 0 : _b.trim()) || '';
}
export { execCommand, execCommandAll, execCommandStrict };