metacoding
Version:
Guided Development Workflow for GitHub Copilot - Transform your coding experience with AI-guided standards, structured workflows, and quality practices
127 lines • 4.49 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.BackupService = void 0;
const path = __importStar(require("path"));
const fs = __importStar(require("fs-extra"));
const crypto = __importStar(require("crypto"));
class BackupService {
constructor() {
this.backupBaseDir = '.backup';
}
async createBackup() {
const timestamp = this.generateTimestamp();
const backupPath = path.join(this.backupBaseDir, timestamp);
const githubDir = '.github';
await fs.ensureDir(backupPath);
const filesBackedUp = [];
if (await fs.pathExists(githubDir)) {
await fs.copy(githubDir, path.join(backupPath, githubDir));
const allFiles = await this.listAllFiles(githubDir);
filesBackedUp.push(...allFiles);
}
return {
backupPath,
timestamp,
filesBackedUp,
};
}
generateTimestamp() {
const now = new Date();
return now
.toISOString()
.replace(/[-:]/g, '')
.replace(/\..+/, '')
.replace('T', '_');
}
async listAllFiles(dirPath) {
const files = [];
if (!(await fs.pathExists(dirPath))) {
return files;
}
const items = await fs.readdir(dirPath);
for (const item of items) {
const itemPath = path.join(dirPath, item);
const stat = await fs.stat(itemPath);
if (stat.isDirectory()) {
const subFiles = await this.listAllFiles(itemPath);
files.push(...subFiles);
}
else {
files.push(itemPath);
}
}
return files;
}
async getFileHash(filePath) {
if (!(await fs.pathExists(filePath))) {
return null;
}
const content = await fs.readFile(filePath, 'utf8');
return crypto.createHash('md5').update(content).digest('hex');
}
async hasFileChanged(filePath, originalContent) {
if (!(await fs.pathExists(filePath))) {
return true;
}
const currentContent = await fs.readFile(filePath, 'utf8');
const currentHash = crypto
.createHash('md5')
.update(currentContent)
.digest('hex');
const originalHash = crypto
.createHash('md5')
.update(originalContent)
.digest('hex');
return currentHash !== originalHash;
}
async cleanupOldBackups() {
if (!(await fs.pathExists(this.backupBaseDir))) {
return;
}
const backupDirs = await fs.readdir(this.backupBaseDir);
const sortedDirs = backupDirs
.filter((dir) => /^\d{8}_\d{6}$/.test(dir))
.sort()
.reverse();
const dirsToDelete = sortedDirs.slice(5);
for (const dir of dirsToDelete) {
const dirPath = path.join(this.backupBaseDir, dir);
await fs.remove(dirPath);
}
}
}
exports.BackupService = BackupService;
//# sourceMappingURL=backup.js.map