@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.
179 lines • 6.94 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.YAMLFileManager = void 0;
const yaml_1 = require("yaml");
const fs = __importStar(require("fs"));
/**
* Helper function to recursively quote date strings in YAML nodes
*/
function quoteDateStrings(node) {
if (!node)
return;
if ((0, yaml_1.isScalar)(node)) {
// If it's a scalar that looks like a date (YYYY-MM-DD), quote it
if (typeof node.value === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(node.value)) {
node.type = 'QUOTE_DOUBLE';
}
}
else if (node.items) {
// It's a collection (array or object)
for (const item of node.items) {
if (item && item.value) {
quoteDateStrings(item.value);
}
if (item && item.key) {
quoteDateStrings(item.key);
}
}
}
}
/**
* YAML file manager that preserves comments and formatting
* Uses the yaml package's Document API for surgical updates
*/
class YAMLFileManager {
constructor(filePath) {
this.doc = null;
this.filePath = filePath;
}
/**
* Read projects from YAML file
* Parses the file and stores the Document for future modifications
*/
async readProjects() {
const fileContent = await fs.promises.readFile(this.filePath, 'utf-8');
this.doc = (0, yaml_1.parseDocument)(fileContent);
// Convert to plain JS object for validation and use
const data = this.doc.toJS();
// Ensure projects array exists
if (!data.projects) {
data.projects = [];
}
return data;
}
/**
* Update an existing project in the YAML file
* Modifies the Document node directly to preserve comments
*/
async updateProject(projectId, updatedProject) {
if (!this.doc) {
throw new Error('Document not loaded. Call readProjects() first.');
}
const projects = this.doc.get('projects');
if (!projects || !projects.items) {
throw new Error('Projects array not found in document');
}
// Find the project index
const projectIndex = projects.items.findIndex((p) => p && p.get && p.get('id') === projectId);
if (projectIndex === -1) {
throw new Error(`Project with id "${projectId}" not found`);
}
// Ensure creationDate is a string (not a Date object)
const projectToSave = {
...updatedProject,
creationDate: typeof updatedProject.creationDate === 'string'
? updatedProject.creationDate
: updatedProject.creationDate.toISOString().split('T')[0]
};
// Replace the project node with updated data
// This preserves comments around the project
const newNode = this.doc.createNode(projectToSave);
// Force date strings to be quoted
quoteDateStrings(newNode);
projects.items[projectIndex] = newNode;
// Write back to file
await fs.promises.writeFile(this.filePath, this.doc.toString(), 'utf-8');
}
/**
* Add a new project to the YAML file
* Appends to the projects array while preserving existing structure
*/
async addProject(project) {
if (!this.doc) {
throw new Error('Document not loaded. Call readProjects() first.');
}
// Ensure creationDate is a string (not a Date object)
const projectToSave = {
...project,
creationDate: typeof project.creationDate === 'string'
? project.creationDate
: project.creationDate.toISOString().split('T')[0]
};
let projects = this.doc.get('projects');
// If projects doesn't exist, create it with the new project
if (!projects) {
this.doc.set('projects', [projectToSave]);
}
else if (projects.items) {
// If projects exists and has items, add to it
const newNode = this.doc.createNode(projectToSave);
// Force date strings to be quoted
quoteDateStrings(newNode);
projects.items.push(newNode);
}
else {
// Fallback: recreate projects array with new project
const existingProjects = this.doc.toJS().projects || [];
this.doc.set('projects', [...existingProjects, projectToSave]);
}
// Write back to file
await fs.promises.writeFile(this.filePath, this.doc.toString(), 'utf-8');
}
/**
* Delete a project from the YAML file
* Removes the project node while preserving other structure
*/
async deleteProject(projectId) {
if (!this.doc) {
throw new Error('Document not loaded. Call readProjects() first.');
}
const projects = this.doc.get('projects');
if (!projects || !projects.items) {
throw new Error('Projects array not found in document');
}
// Find the project index
const projectIndex = projects.items.findIndex((p) => p && p.get && p.get('id') === projectId);
if (projectIndex === -1) {
throw new Error(`Project with id "${projectId}" not found`);
}
// Remove the project
projects.items.splice(projectIndex, 1);
// Write back to file
await fs.promises.writeFile(this.filePath, this.doc.toString(), 'utf-8');
}
}
exports.YAMLFileManager = YAMLFileManager;
//# sourceMappingURL=yaml-file-manager.js.map