@harryisfish/gitt
Version:
这是一个命令行工具,用于帮助你管理 Git 仓库与远端仓库,如保持同步、推送、拉取等。
51 lines (50 loc) • 1.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UserCancelError = exports.GitError = void 0;
exports.handleError = handleError;
exports.printSuccess = printSuccess;
exports.printError = printError;
// 自定义错误类型
class GitError extends Error {
constructor(message) {
super(message);
this.name = 'GitError';
}
}
exports.GitError = GitError;
class UserCancelError extends Error {
constructor(message = '操作已取消') {
super(message);
this.name = 'UserCancelError';
}
}
exports.UserCancelError = UserCancelError;
// 错误消息颜色
const ERROR_COLOR = '\x1b[31m';
const SUCCESS_COLOR = '\x1b[32m';
const RESET_COLOR = '\x1b[0m';
// 统一错误处理函数
function handleError(error) {
if (error instanceof UserCancelError) {
console.log(error.message);
process.exit(0);
}
if (error instanceof GitError) {
console.error(`${ERROR_COLOR}错误:${RESET_COLOR}${error.message}`);
process.exit(1);
}
if (error instanceof Error) {
console.error(`${ERROR_COLOR}程序错误:${RESET_COLOR}${error.message}`);
process.exit(1);
}
console.error(`${ERROR_COLOR}发生未知错误${RESET_COLOR}`);
process.exit(1);
}
// 成功消息处理
function printSuccess(message) {
console.log(`${SUCCESS_COLOR}✓ ${message}${RESET_COLOR}`);
}
// 错误消息处理
function printError(message) {
console.error(`${ERROR_COLOR}${message}${RESET_COLOR}`);
}