33 lines
960 B
JavaScript
33 lines
960 B
JavaScript
#!/usr/bin/env node
|
|
|
|
const { loadArticles } = require("../lib/stats/articles");
|
|
|
|
function computeAveragePerMonth(articles) {
|
|
let first = null;
|
|
let last = null;
|
|
|
|
for (const article of articles) {
|
|
if (!article.date) continue;
|
|
if (!first || article.date < first) first = article.date;
|
|
if (!last || article.date > last) last = article.date;
|
|
}
|
|
|
|
if (!first || !last) {
|
|
return { average: 0, months: 0 };
|
|
}
|
|
|
|
const monthSpan = Math.max(1, Math.round(last.diff(first.startOf("month"), "months").months) + 1);
|
|
const total = articles.filter((a) => a.date).length;
|
|
const average = total / monthSpan;
|
|
|
|
return { average, months: monthSpan };
|
|
}
|
|
|
|
async function run({ contentDir }) {
|
|
const articles = await loadArticles(contentDir || "content");
|
|
const { average, months } = computeAveragePerMonth(articles);
|
|
return { value: average.toFixed(2), meta: { months } };
|
|
}
|
|
|
|
module.exports = { run };
|