@quasarbright/projection
Version:
A static site generator that creates a beautiful, interactive gallery to showcase your coding projects. Features search, filtering, tags, responsive design, and an admin UI.
172 lines • 5.64 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.FileManager = void 0;
const fs = __importStar(require("fs"));
const path = __importStar(require("path"));
const chokidar = __importStar(require("chokidar"));
const yaml_file_manager_1 = require("./yaml-file-manager");
const json_file_manager_1 = require("./json-file-manager");
/**
* Unified file manager that delegates to YAML or JSON manager
* Provides format detection, backup functionality, and file watching
*/
class FileManager {
constructor(filePath) {
this.watcher = null;
this.filePath = filePath;
this.format = this.detectFormat();
// Create appropriate manager based on format
if (this.format === 'yaml') {
this.manager = new yaml_file_manager_1.YAMLFileManager(filePath);
}
else {
this.manager = new json_file_manager_1.JSONFileManager(filePath);
}
}
/**
* Detect file format based on extension
*/
detectFormat() {
const ext = path.extname(this.filePath).toLowerCase();
if (ext === '.yaml' || ext === '.yml') {
return 'yaml';
}
else if (ext === '.json') {
return 'json';
}
// Default to YAML if extension is unclear
return 'yaml';
}
/**
* Get the detected file format
*/
getFormat() {
return this.format;
}
/**
* Read projects from file
*/
async readProjects() {
return await this.manager.readProjects();
}
/**
* Update an existing project
* Creates a backup before modification
*/
async updateProject(projectId, updatedProject) {
await this.createBackup();
await this.manager.updateProject(projectId, updatedProject);
}
/**
* Add a new project
* Creates a backup before modification
*/
async addProject(project) {
await this.createBackup();
await this.manager.addProject(project);
}
/**
* Delete a project
* Creates a backup before modification
*/
async deleteProject(projectId) {
await this.createBackup();
await this.manager.deleteProject(projectId);
}
/**
* Create a backup of the current file
* Returns the backup file path
*/
async createBackup() {
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
const ext = path.extname(this.filePath);
const basename = path.basename(this.filePath, ext);
const dirname = path.dirname(this.filePath);
// Create .backup directory if it doesn't exist
const backupDir = path.join(dirname, '.backup');
if (!fs.existsSync(backupDir)) {
await fs.promises.mkdir(backupDir, { recursive: true });
}
const backupPath = path.join(backupDir, `${basename}.backup-${timestamp}${ext}`);
try {
await fs.promises.copyFile(this.filePath, backupPath);
return backupPath;
}
catch (error) {
throw new Error(`Failed to create backup: ${error}`);
}
}
/**
* Watch file for external changes
* Calls the callback when the file is modified externally
*/
watchFile(callback) {
// Close existing watcher if any
if (this.watcher) {
this.watcher.close();
}
// Create new watcher
this.watcher = chokidar.watch(this.filePath, {
persistent: true,
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 100,
pollInterval: 100
}
});
// Handle file changes
this.watcher.on('change', async () => {
try {
const data = await this.readProjects();
callback(data);
}
catch (error) {
console.error('Error reading file after change:', error);
}
});
}
/**
* Stop watching the file
*/
stopWatching() {
if (this.watcher) {
this.watcher.close();
this.watcher = null;
}
}
}
exports.FileManager = FileManager;
//# sourceMappingURL=file-manager.js.map