UNPKG

@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.

152 lines 5.12 kB
"use strict"; 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.ProjectFileFinder = void 0; const fs = __importStar(require("fs")); const path = __importStar(require("path")); /** * Shared utility for finding and validating projects files * Follows DRY principle by centralizing project file discovery logic */ class ProjectFileFinder { /** * Find the projects file in the specified directory * Searches for projects.yaml, projects.yml, or projects.json in order * * @param cwd - Directory to search in * @returns ProjectFileResult if found, null otherwise */ static find(cwd) { for (const fileName of this.POSSIBLE_FILES) { const filePath = path.join(cwd, fileName); if (fs.existsSync(filePath)) { return { path: filePath, format: this.detectFormat(filePath) }; } } return null; } /** * Find the projects file or throw an error if not found * * @param cwd - Directory to search in * @returns ProjectFileResult * @throws Error if no projects file is found */ static findOrThrow(cwd) { const result = this.find(cwd); if (!result) { throw new Error(`No projects file found. Expected one of: ${this.POSSIBLE_FILES.join(', ')}`); } return result; } /** * Get all possible projects file paths for a directory * Useful for error messages showing what was searched * * @param cwd - Directory to get paths for * @returns Array of possible file paths */ static getPossiblePaths(cwd) { return this.POSSIBLE_FILES.map(fileName => path.join(cwd, fileName)); } /** * Validate that a specific projects file exists * * @param filePath - Path to validate * @returns true if file exists, false otherwise */ static exists(filePath) { return fs.existsSync(filePath); } /** * Resolve a projects file path (handles relative and absolute paths) * If path is provided, resolves it relative to cwd * If path is not provided, searches for default projects files * * @param cwd - Base directory * @param providedPath - Optional path provided by user * @returns ProjectFileResult if found, null otherwise */ static resolve(cwd, providedPath) { if (providedPath) { const resolvedPath = path.isAbsolute(providedPath) ? providedPath : path.resolve(cwd, providedPath); if (fs.existsSync(resolvedPath)) { return { path: resolvedPath, format: this.detectFormat(resolvedPath) }; } return null; } return this.find(cwd); } /** * Detect file format from extension * * @param filePath - Path to detect format for * @returns 'yaml' or 'json' */ static detectFormat(filePath) { const ext = path.extname(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 list of supported file names * * @returns Array of supported file names */ static getSupportedFileNames() { return [...this.POSSIBLE_FILES]; } } exports.ProjectFileFinder = ProjectFileFinder; ProjectFileFinder.POSSIBLE_FILES = [ 'projects.yaml', 'projects.yml', 'projects.json' ]; //# sourceMappingURL=project-file-finder.js.map