UNPKG

projex

Version:
82 lines (81 loc) 3.03 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOriginUrl = exports.getTheLastTag = exports.getGitCommits = exports.gitStatus = exports.pushCommand = exports.tag = exports.checkIfInGitRepo = exports.checkGit = void 0; const _api_1 = require("../../api/index"); const logger_1 = require("../logger"); const chalk_1 = __importDefault(require("chalk")); const runCommand_1 = require("./runCommand"); const cp = require('child-process-es6-promise'); /** * The function `checkGit` checks if Git is available in the system and throws an error if it is not. */ const checkGit = () => { try { cp.execSync('git --version'); } catch (e) { logger_1.log.error(_api_1.Colors.ERROR(`${chalk_1.default.bold(`git`)} is not available in your system. \ Please install ${chalk_1.default.bold(`git`)} and try again.`)); throw e; } }; exports.checkGit = checkGit; /** * The function `checkIfInGitRepo` checks if the current working directory is a git repository in * TypeScript. */ const checkIfInGitRepo = () => { try { cp.execSync('git rev-parse --git-dir'); } catch (e) { logger_1.log.error(_api_1.Colors.ERROR(`the current working directory is not a git repository, please run this command in a git repository`)); logger_1.log.verbose(e); process.exit(1); } }; exports.checkIfInGitRepo = checkIfInGitRepo; const tag = (tagName, root) => { const tagMessage = `Release ${tagName}`; return (0, runCommand_1.runCommand)(`git tag ${tagName} -m "${tagMessage}"`, root, `Tag created: ${tagName}`, true); }; exports.tag = tag; const pushCommand = (tagName, noTag) => { return `git push --force ${noTag ? '' : `&& git push origin ${tagName} --force`}`; }; exports.pushCommand = pushCommand; const gitStatus = (root) => { return (0, runCommand_1.runCommand)('git status --porcelain', root, '', true); }; exports.gitStatus = gitStatus; const getGitCommits = (root) => { return (0, runCommand_1.runCommand)(`git rev-list HEAD --pretty=oneline`, root, '', true, 0, true); }; exports.getGitCommits = getGitCommits; const getTheLastTag = (root) => { try { const tags = (0, runCommand_1.runCommand)('git describe --tags --abbrev=0', root, '', true, 0, true, false); if (tags) { return tags.toString(); } else { return null; } } catch (e) { logger_1.log.error(_api_1.Colors.ERROR('no tags found')); return null; } }; exports.getTheLastTag = getTheLastTag; const getOriginUrl = (root) => { const url = (0, runCommand_1.runCommand)(`git config --get remote.origin.url`, root, '', true, 0, true) ?.toString() .replace('\n', '') .replace('.git', ''); return url.replace(/https?:\/\/[^@]*@/, 'https://'); }; exports.getOriginUrl = getOriginUrl;