UNPKG

@lexmin0412/gcm

Version:
133 lines (132 loc) 5.74 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProjectConfig = exports.getConfigByAlias = exports.getAllUserConfigs = exports.readConfigs = exports.isConfigJsonExists = exports.removeConfig = exports.addConfig = exports.getCurrentConfig = exports.getPkgJson = exports.createEmptyJsonWhenNeeds = exports.configJsonPath = exports.rootPath = void 0; var path = __importStar(require("path")); var fs = __importStar(require("fs")); var os = __importStar(require("os")); var child_process_1 = require("child_process"); var picocolors_1 = __importDefault(require("picocolors")); __exportStar(require("./fs"), exports); var USER_HOME = os.homedir(); exports.rootPath = path.resolve(USER_HOME, '.gcm'); if (!fs.existsSync(exports.rootPath)) { fs.mkdirSync(exports.rootPath); } exports.configJsonPath = path.resolve(exports.rootPath, 'config.json'); var createEmptyJsonWhenNeeds = function () { if (!fs.existsSync(exports.configJsonPath)) { var users = []; fs.writeFileSync(exports.configJsonPath, JSON.stringify({ users: users }, null, 2)); } }; exports.createEmptyJsonWhenNeeds = createEmptyJsonWhenNeeds; var getPkgJson = function () { return require(path.resolve(__dirname, '..', '..', 'package.json')); }; exports.getPkgJson = getPkgJson; var getCurrentConfig = function () { try { var currentUserName = (0, child_process_1.execSync)('git config --get user.name').toString().trim(); var currentUserEmail = (0, child_process_1.execSync)('git config --get user.email').toString().trim(); if (!currentUserName || !currentUserEmail) { return null; } return { name: currentUserName, email: currentUserEmail }; } catch (_a) { return null; } }; exports.getCurrentConfig = getCurrentConfig; var addConfig = function (config) { (0, exports.createEmptyJsonWhenNeeds)(); var configJson = require(exports.configJsonPath); configJson.users.push(config); fs.writeFileSync(exports.configJsonPath, JSON.stringify(configJson, null, 2)); console.log(picocolors_1.default.green('添加成功✅')); }; exports.addConfig = addConfig; var removeConfig = function (alias) { if (!(0, exports.isConfigJsonExists)()) { console.error(picocolors_1.default.red('配置文件不存在')); process.exit(1); } var configJson = require(exports.configJsonPath); var targetUserConfigIndex = configJson.users.findIndex((function (config) { return config.alias === alias; })); if (targetUserConfigIndex === -1) { console.error(picocolors_1.default.red("\u4E0D\u5B58\u5728\u522B\u540D\u4E3A ".concat(alias, " \u7684\u914D\u7F6E"))); process.exit(1); } configJson.users.splice(targetUserConfigIndex, 1); fs.writeFileSync(exports.configJsonPath, JSON.stringify(configJson, null, 2)); console.log(picocolors_1.default.green("\u914D\u7F6E ".concat(alias, " \u5220\u9664\u6210\u529F"))); }; exports.removeConfig = removeConfig; var isConfigJsonExists = function () { var isExists = fs.existsSync(exports.configJsonPath); return isExists; }; exports.isConfigJsonExists = isConfigJsonExists; var readConfigs = function () { (0, exports.createEmptyJsonWhenNeeds)(); var configs = JSON.parse(fs.readFileSync(exports.configJsonPath, 'utf8')); return configs; }; exports.readConfigs = readConfigs; var getAllUserConfigs = function () { var configs = (0, exports.readConfigs)(); return configs.users; }; exports.getAllUserConfigs = getAllUserConfigs; var getConfigByAlias = function (alias) { var configs = (0, exports.readConfigs)(); var config = configs.users.find(function (config) { return config.alias === alias; }); return config || null; }; exports.getConfigByAlias = getConfigByAlias; var getProjectConfig = function (projectPath) { if (projectPath === void 0) { projectPath = process.cwd(); } var currentUserName = (0, child_process_1.execSync)('git config --get user.name', { cwd: projectPath }).toString().trim(); var currentUserEmail = (0, child_process_1.execSync)('git config --get user.email', { cwd: projectPath }).toString().trim(); return { name: currentUserName, email: currentUserEmail }; }; exports.getProjectConfig = getProjectConfig;