|
| 1 | +const ejs = require("ejs"); |
| 2 | +const fs = require("fs"); |
| 3 | +const { resolveIcon } = require("./resolve-icon"); |
| 4 | +const { PROJECTS_MD_TEMPLATE } = require("../constants"); |
| 5 | + |
| 6 | +class ProjectsContentBuilder { |
| 7 | + constructor({ templatePath } = {}) { |
| 8 | + this.templatePath = templatePath || PROJECTS_MD_TEMPLATE; |
| 9 | + this.template = fs.readFileSync(this.templatePath, "utf8"); |
| 10 | + } |
| 11 | + |
| 12 | + build({ categories, repositories, generatedAt }) { |
| 13 | + const stats = this.buildStats(repositories, generatedAt); |
| 14 | + const categoryModels = this.buildCategoryModels(categories); |
| 15 | + |
| 16 | + return ejs.render( |
| 17 | + this.template, |
| 18 | + { |
| 19 | + stats, |
| 20 | + categories: categoryModels, |
| 21 | + }, |
| 22 | + { filename: this.templatePath } |
| 23 | + ); |
| 24 | + } |
| 25 | + |
| 26 | + buildStats(repositories, generatedAt) { |
| 27 | + const totalRepos = repositories.length; |
| 28 | + const totalStars = repositories.reduce( |
| 29 | + (sum, repository) => sum + (repository.stargazers_count || 0), |
| 30 | + 0 |
| 31 | + ); |
| 32 | + const languages = [ |
| 33 | + ...new Set( |
| 34 | + repositories.map((repository) => repository.language).filter(Boolean) |
| 35 | + ), |
| 36 | + ]; |
| 37 | + const primaryLanguages = languages.slice(0, 5); |
| 38 | + |
| 39 | + return { |
| 40 | + totalRepos, |
| 41 | + totalStars, |
| 42 | + languageCount: languages.length, |
| 43 | + languagesSummary: |
| 44 | + primaryLanguages.length > 0 |
| 45 | + ? primaryLanguages.join(", ") |
| 46 | + : "various stacks", |
| 47 | + generatedDate: generatedAt.toISOString().split("T")[0], |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + buildCategoryModels(categories) { |
| 52 | + return Object.entries(categories) |
| 53 | + .filter(([, repositories]) => repositories.length > 0) |
| 54 | + .map(([name, repositories]) => ({ |
| 55 | + name, |
| 56 | + repositories: repositories.map((repository) => |
| 57 | + this.buildRepositoryModel(repository) |
| 58 | + ), |
| 59 | + })); |
| 60 | + } |
| 61 | + |
| 62 | + buildRepositoryModel(repository) { |
| 63 | + return { |
| 64 | + name: repository.name, |
| 65 | + htmlUrl: repository.html_url, |
| 66 | + icon: resolveIcon(repository), |
| 67 | + language: repository.language || "Unknown", |
| 68 | + stars: repository.stargazers_count || 0, |
| 69 | + lastUpdated: this.formatDate(repository.updated_at), |
| 70 | + description: repository.description || "No description available.", |
| 71 | + topics: (repository.topics || []).slice(0, 5), |
| 72 | + homepage: repository.homepage, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + formatDate(dateString) { |
| 77 | + if (!dateString) { |
| 78 | + return "Unknown"; |
| 79 | + } |
| 80 | + |
| 81 | + const date = new Date(dateString); |
| 82 | + |
| 83 | + if (Number.isNaN(date.getTime())) { |
| 84 | + return "Unknown"; |
| 85 | + } |
| 86 | + |
| 87 | + return date.toLocaleDateString("en-US", { |
| 88 | + year: "numeric", |
| 89 | + month: "short", |
| 90 | + day: "numeric", |
| 91 | + }); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +module.exports = { |
| 96 | + ProjectsContentBuilder, |
| 97 | +}; |
0 commit comments