UNPKG

@codecovevienna/gittt-cli

Version:

Tracking time with CLI into a git repository

130 lines (129 loc) 6.22 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.GitHelper = void 0; const promise_1 = __importDefault(require("simple-git/promise")); const _1 = require("./"); const shelljs_1 = __importDefault(require("shelljs")); class GitHelper { constructor(configDir, fileHelper) { this.logChanges = () => __awaiter(this, void 0, void 0, function* () { const listLogSummery = yield this.git.log({ from: "HEAD", to: "origin/master", }); return listLogSummery.all; }); this.pushChanges = () => __awaiter(this, void 0, void 0, function* () { _1.LogHelper.debug("Pushing changes origin master"); yield this.git.pull("origin", "master"); yield this.git.push("origin", "master"); }); this.commitChanges = (message) => __awaiter(this, void 0, void 0, function* () { _1.LogHelper.debug("Committing changes"); yield this.git.pull("origin", "master"); yield this.git.add("./*"); yield this.git.commit(message ? message : "Did some changes"); }); this.initRepo = (gitUrl) => __awaiter(this, void 0, void 0, function* () { const repoInitialized = yield this.git.checkIsRepo(); if (!repoInitialized) { _1.LogHelper.debug("Initializing repo"); yield this.git.init(); yield this.git.addRemote("origin", gitUrl); } }); this.pullRepo = (reset = false) => __awaiter(this, void 0, void 0, function* () { try { if (reset) { _1.LogHelper.debug("Resetting to origin/master"); yield this.git.reset(["--hard", "origin/master"]); } yield this.git.pull("origin", "master"); _1.LogHelper.info("Pulled repo successfully"); } catch (err) { let override = 255; if (err.message === "fatal: couldn't find remote ref master\n") { yield this.fileHelper.initReadme(); override = 1; } else { override = yield _1.QuestionHelper.chooseOverrideLocalChanges(); } switch (override) { case 0: yield this.git.reset(["--hard", "origin/master"]); yield this.git.pull("origin", "master"); break; case 1: try { yield this.git.add("./*"); _1.LogHelper.info("Added initial config file"); yield this.git.commit("Setup commit"); _1.LogHelper.info("Committed initial config file"); yield this.git.raw(["push", "origin", "master", "--force"]); _1.LogHelper.info("Pushed to repo"); const status = yield this.git.status(); _1.LogHelper.debug(status); } catch (err) { _1.LogHelper.debug("Unable to fetch repo", err); throw new Error("Initialize repo failed"); } break; case 2: // TODO helper? process.exit(0); break; default: throw new Error(`Unknown option ${override}`); } // Force fileHelper to load config file from disk this.fileHelper.invalidateCache(); } }); /** * Gets the current branch of the git repo the app is currently in, NOT the branch of the main repo in ~/.gittt-cli * * @returns Promise with either a string with the branch or undefined */ this.getCurrentBranch = () => __awaiter(this, void 0, void 0, function* () { try { _1.LogHelper.debug("Getting current branch"); // this.git is configured to look into ~/.gittt-cli, so we execute a shell command in the current directory instead const gitBranchExec = shelljs_1.default.exec("git branch --show-current", { silent: true, }); if (gitBranchExec.code !== 0) { if (gitBranchExec.code === 128) { _1.LogHelper.debug(`"git branch --show-current" returned with exit code 128`); return undefined; } _1.LogHelper.debug("Error executing git branch --show-current", new Error(gitBranchExec.stderr)); return undefined; } return gitBranchExec.stdout; } catch (err) { _1.LogHelper.debug("Unable to get current branch", err); _1.LogHelper.error("Unable to get current branch"); return undefined; } }); this.git = (0, promise_1.default)(configDir); this.fileHelper = fileHelper; } } exports.GitHelper = GitHelper;