@harryisfish/gitt
Version:
A command-line tool to help you manage Git repositories and remote repositories, such as keeping in sync, pushing, pulling, etc.
135 lines (134 loc) • 5.05 kB
JavaScript
;
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 () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.getProjectRoot = getProjectRoot;
exports.readConfigFile = readConfigFile;
exports.writeConfigFile = writeConfigFile;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const simple_git_1 = require("simple-git");
const minimatch_1 = require("minimatch");
const CONFIG_FILE_NAME = '.gitt';
/**
* Validate configuration values.
*/
function validateConfig(config) {
const validated = {};
// Validate mainBranch
if (config.mainBranch !== undefined) {
if (typeof config.mainBranch !== 'string' || config.mainBranch.trim() === '') {
throw new Error('Invalid config: mainBranch must be a non-empty string');
}
validated.mainBranch = config.mainBranch.trim();
}
// Validate ignoreBranches
if (config.ignoreBranches !== undefined) {
if (!Array.isArray(config.ignoreBranches)) {
throw new Error('Invalid config: ignoreBranches must be an array');
}
const validPatterns = [];
for (const pattern of config.ignoreBranches) {
if (typeof pattern !== 'string' || pattern.trim() === '') {
throw new Error('Invalid config: ignoreBranches must contain non-empty strings');
}
// Test if it's a valid glob pattern by trying to use it
try {
(0, minimatch_1.minimatch)('test', pattern);
validPatterns.push(pattern);
}
catch (e) {
throw new Error(`Invalid config: "${pattern}" is not a valid glob pattern`);
}
}
validated.ignoreBranches = validPatterns;
}
// Validate staleDays
if (config.staleDays !== undefined) {
if (typeof config.staleDays !== 'number' || config.staleDays < 1 || config.staleDays > 365 || !Number.isInteger(config.staleDays)) {
throw new Error('Invalid config: staleDays must be an integer between 1 and 365');
}
validated.staleDays = config.staleDays;
}
return validated;
}
/**
* Get the project root directory (where .git is located).
*/
async function getProjectRoot() {
const git = (0, simple_git_1.simpleGit)();
try {
const root = await git.revparse(['--show-toplevel']);
return root.trim();
}
catch (e) {
return process.cwd();
}
}
/**
* Read the .gitt configuration file.
*/
async function readConfigFile() {
try {
const root = await getProjectRoot();
const configPath = path.join(root, CONFIG_FILE_NAME);
if (!fs.existsSync(configPath)) {
return {};
}
const content = fs.readFileSync(configPath, 'utf-8');
const parsedConfig = JSON.parse(content);
return validateConfig(parsedConfig);
}
catch (error) {
// If validation fails, throw the error instead of returning empty config
if (error instanceof Error && error.message.startsWith('Invalid config:')) {
throw error;
}
// For other errors (file read, JSON parse), return empty config
return {};
}
}
/**
* Write to the .gitt configuration file.
*/
async function writeConfigFile(config) {
// Validate new config before writing
const validatedConfig = validateConfig(config);
const root = await getProjectRoot();
const configPath = path.join(root, CONFIG_FILE_NAME);
// Read existing config to merge
const currentConfig = await readConfigFile();
const newConfig = { ...currentConfig, ...validatedConfig };
fs.writeFileSync(configPath, JSON.stringify(newConfig, null, 2));
}