@cosmstack/repoeject
Version:
Interactive CLI tool to safely find and delete old or inactive GitHub repositories. Clean up your GitHub profile by removing abandoned projects, old experiments, and unused forks.
137 lines • 5.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatTimeAgo = formatTimeAgo;
exports.isInactiveRepository = isInactiveRepository;
exports.formatRepositoryInfo = formatRepositoryInfo;
exports.promptRepositorySelection = promptRepositorySelection;
exports.confirmDeletion = confirmDeletion;
exports.displayDeletionSummary = displayDeletionSummary;
const chalk_1 = __importDefault(require("chalk"));
const inquirer_1 = __importDefault(require("inquirer"));
const config_js_1 = require("../config.js");
const TIME_CONSTANTS = {
MINUTE: 60,
HOUR: 60 * 60,
DAY: 60 * 60 * 24,
WEEK: 60 * 60 * 24 * 7,
MONTH: 60 * 60 * 24 * 30,
YEAR: 60 * 60 * 24 * 365,
};
function formatTimeAgo(date) {
const now = new Date();
const then = new Date(date);
const diffInSeconds = Math.floor((now.getTime() - then.getTime()) / 1000);
if (diffInSeconds < TIME_CONSTANTS.MINUTE) {
return `${diffInSeconds} seconds ago`;
}
if (diffInSeconds < TIME_CONSTANTS.HOUR) {
const minutes = Math.floor(diffInSeconds / TIME_CONSTANTS.MINUTE);
return formatPluralUnit(minutes, "minute");
}
if (diffInSeconds < TIME_CONSTANTS.DAY) {
const hours = Math.floor(diffInSeconds / TIME_CONSTANTS.HOUR);
return formatPluralUnit(hours, "hour");
}
if (diffInSeconds < TIME_CONSTANTS.WEEK) {
const days = Math.floor(diffInSeconds / TIME_CONSTANTS.DAY);
return formatPluralUnit(days, "day");
}
if (diffInSeconds < TIME_CONSTANTS.MONTH) {
const weeks = Math.floor(diffInSeconds / TIME_CONSTANTS.WEEK);
return formatPluralUnit(weeks, "week");
}
if (diffInSeconds < TIME_CONSTANTS.YEAR) {
const months = Math.floor(diffInSeconds / TIME_CONSTANTS.MONTH);
return formatPluralUnit(months, "month");
}
const years = Math.floor(diffInSeconds / TIME_CONSTANTS.YEAR);
return formatPluralUnit(years, "year");
}
function formatPluralUnit(value, unit) {
return `${value} ${unit}${value > 1 ? "s" : ""} ago`;
}
function isInactiveRepository(lastUpdateDate) {
const now = new Date();
const lastUpdate = new Date(lastUpdateDate);
const diffInDays = Math.floor((now.getTime() - lastUpdate.getTime()) / (TIME_CONSTANTS.DAY * 1000));
return diffInDays > config_js_1.config.ui.inactiveThresholdDays;
}
function formatRepositoryInfo(repo, commitCount) {
const isInactive = isInactiveRepository(repo.updated_at);
const hasLowCommits = commitCount <= config_js_1.config.ui.lowCommitThreshold;
return {
name: repo.full_name,
description: repo.description || "(No description)",
lastUpdated: formatTimeAgo(repo.updated_at),
commitCount: commitCount.toString(),
isInactive,
hasLowCommits,
language: repo.language || "Unknown",
visibility: repo.private ? "Private" : "Public",
url: repo.html_url,
id: repo.id,
};
}
function formatRepositoryDisplayName(repo) {
let name = repo.name;
if (repo.isInactive) {
name = chalk_1.default.yellow(name);
}
if (repo.hasLowCommits) {
name = chalk_1.default.red(name);
}
return name;
}
async function promptRepositorySelection(repositories) {
const { selectedRepos } = await inquirer_1.default.prompt([
{
type: "checkbox",
name: "selectedRepos",
message: "Select repositories to delete (space to select, enter to confirm):",
choices: repositories.map((repo) => {
const displayName = formatRepositoryDisplayName(repo);
return {
name: `${displayName} (Last updated: ${repo.lastUpdated}, Commits: ${repo.commitCount})`,
value: repo,
short: repo.name,
};
}),
pageSize: 20,
},
]);
return selectedRepos;
}
async function confirmDeletion(repositories) {
displayWarningMessage(repositories);
const { confirmation } = await inquirer_1.default.prompt([
{
type: "input",
name: "confirmation",
message: `Type ${chalk_1.default.red.bold(config_js_1.config.cli.confirmationKeyword)} to confirm deletion:`,
validate: (input) => {
if (input === config_js_1.config.cli.confirmationKeyword)
return true;
return `You must type ${config_js_1.config.cli.confirmationKeyword} to proceed`;
},
},
]);
return confirmation === config_js_1.config.cli.confirmationKeyword;
}
function displayWarningMessage(repositories) {
console.log(chalk_1.default.red.bold("\n⚠️ WARNING: You are about to delete the following repositories:\n"));
for (const repo of repositories) {
console.log(` - ${chalk_1.default.yellow(repo.name)}`);
}
console.log(chalk_1.default.red("\nThis action cannot be undone!"));
}
function displayDeletionSummary(repositories) {
console.log(chalk_1.default.green.bold("\n✅ Successfully deleted repositories:\n"));
for (const repo of repositories) {
console.log(` - ${chalk_1.default.yellow(repo.name)}`);
}
console.log("");
}
//# sourceMappingURL=ui.js.map