UNPKG

git-veil

Version:

A CLI tool for synchronizing development activities to a personal GitHub repository discreetly and confidentially.

126 lines (125 loc) 6.21 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 __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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.recordActivity = void 0; const child_process_1 = require("child_process"); const fs = __importStar(require("fs")); const path = __importStar(require("path")); const fileUtils_1 = require("../utils/fileUtils"); function recordActivity(options) { return __awaiter(this, void 0, void 0, function* () { let { email, dryRun = false, clear = false, target = "./records-folder", } = options; // Always resolve records-folder relative to the project root const projectRoot = path.resolve(__dirname, "../../"); target = path.join(projectRoot, "records-folder"); // If clear option is enabled, clear the records folder and return if (clear) { console.log("> Clearing records folder..."); (0, fileUtils_1.clearRecordsFolder)(target); console.log("Records folder cleared with success"); return; } try { let authorEmail = email; let filterByEmail = true; if (!authorEmail) { try { authorEmail = (0, child_process_1.execSync)("git config user.email").toString().trim(); console.log(`Using local git user.email: ${authorEmail}`); } catch (e) { console.warn(`Failed to get local git user.email: ${e instanceof Error ? e.message : String(e)}`); try { authorEmail = (0, child_process_1.execSync)("git config user.email --global") .toString() .trim(); console.log(`Using global git user.email: ${authorEmail}`); } catch (e2) { console.warn(`Failed to get global git user.email: ${e2 instanceof Error ? e2.message : String(e2)}`); } } } if (!authorEmail) { filterByEmail = false; console.warn("No email provided and unable to retrieve from git config. All commits will be included."); } // 2. Fetch latest changes console.log(); console.log("> Fetching latest changes from origin..."); try { (0, child_process_1.execSync)("git fetch origin", { stdio: "inherit" }); } catch (e) { console.warn("Could not fetch from origin. Continuing anyway."); } // 3. Extract commit dates const branch = "origin/main"; const gitLogCmd = filterByEmail ? `git log ${branch} --author=\"${authorEmail}\" --pretty=format:%ad --date=iso8601-strict` : `git log ${branch} --pretty=format:%ad --date=iso8601-strict`; let commitsRaw = ""; try { commitsRaw = (0, child_process_1.execSync)(gitLogCmd).toString(); } catch (e) { console.error("Error running git log."); return; } const commitDates = commitsRaw.split("\n").filter(Boolean); if (dryRun) { console.log(`[DRY RUN] Commits found ${commitDates.length}`); return; } // 4. Write to JSON file const now = new Date(); const date = now.toISOString().slice(0, 10); const time = now.toISOString().slice(11, 19).replace(/:/g, ""); // HHmmss let baseName = `record_${date}_${time}`; let outputFile = path.join(target, `${baseName}.json`); if (!fs.existsSync(target)) { fs.mkdirSync(target, { recursive: true }); } fs.writeFileSync(outputFile, JSON.stringify(commitDates, null, 2), "utf-8"); console.log(`✅ Commits successfully recorded`); console.log(`🎉 You're all set — ready to sync`); console.log(); console.log(`Use the command 'gitveil push' to synchronize your records.`); } catch (error) { console.error(`Error recording activity: ${(error === null || error === void 0 ? void 0 : error.message) || error}`); } }); } exports.recordActivity = recordActivity;