diff --git a/tools/add_weather.js b/tools/add_weather.js index e086c8ec..0299f13a 100644 --- a/tools/add_weather.js +++ b/tools/add_weather.js @@ -16,9 +16,12 @@ async function processFile(filePath, config, { force = false } = {}) { return { status: "no-frontmatter" }; } - const existingWeather = frontmatter.doc.has("weather") ? frontmatter.doc.get("weather") : null; + let existingWeather = null; + if (frontmatter.doc.has("weather")) { + existingWeather = frontmatter.doc.get("weather"); + } - if (existingWeather && !force) { + if (!force && existingWeather) { return { status: "already-set" }; } @@ -39,32 +42,51 @@ async function processFile(filePath, config, { force = false } = {}) { let finalWeather = {}; if (force) { - finalWeather = weather || {}; + if (weather) { + finalWeather = weather; + } } else if (existingWeather && typeof existingWeather === "object") { finalWeather = JSON.parse(JSON.stringify(existingWeather)); if (weather) { - const added = mergeWeather(finalWeather, weather, weather.source?.[0]); + let providerName = null; + if (weather.source && weather.source.length > 0) { + providerName = weather.source[0]; + } + const added = mergeWeather(finalWeather, weather, providerName); if (!added && Object.keys(finalWeather).length === 0) { finalWeather = weather; } } - } else { - finalWeather = weather || {}; + } else if (weather) { + finalWeather = weather; } frontmatter.doc.set("weather", finalWeather); await writeFrontmatter(filePath, frontmatter.doc, frontmatter.body); + let status = "empty"; + if (weather) { + status = "updated"; + } + + let sources = []; + if (weather && weather.source) { + sources = weather.source; + } + return { - status: weather ? "updated" : "empty", - sources: weather?.source || [], + status, + sources, }; } async function main() { const cliArgs = process.argv.slice(2); - const force = cliArgs.includes("--force") || cliArgs.includes("-f"); + let force = false; + if (cliArgs.includes("--force") || cliArgs.includes("-f")) { + force = true; + } const pathArgs = cliArgs.filter((arg) => arg !== "--force" && arg !== "-f"); const config = loadWeatherConfig(); @@ -89,10 +111,15 @@ async function main() { const result = await processFile(file, config, { force }); switch (result.status) { - case "updated": + case "updated": { updated += 1; - console.log(`✔ Added weather to ${relativePath} (${result.sources.join(", ") || "unknown source"})`); + let sourcesLabel = "unknown source"; + if (result.sources.length > 0) { + sourcesLabel = result.sources.join(", "); + } + console.log(`✔ Added weather to ${relativePath} (${sourcesLabel})`); break; + } case "empty": updated += 1; console.log(`• Added empty weather to ${relativePath}`); diff --git a/tools/lib/datetime.js b/tools/lib/datetime.js index 8f766b47..bb9a4609 100644 --- a/tools/lib/datetime.js +++ b/tools/lib/datetime.js @@ -187,4 +187,5 @@ module.exports = { getHugoTimeZone, toHugoDateTime, parseFrontmatterDate, + parseHugoDateString, }; diff --git a/tools/lib/weather/time.js b/tools/lib/weather/time.js index 7e45bd95..88b04951 100644 --- a/tools/lib/weather/time.js +++ b/tools/lib/weather/time.js @@ -1,4 +1,5 @@ const { DateTime } = require("luxon"); +const { parseHugoDateString } = require("../datetime"); function hasExplicitTime(rawDate) { if (!rawDate) return false; @@ -13,11 +14,8 @@ function resolveArticleDate(dateValue, rawDate, { timezone = "Europe/Paris", def let parsed; if (typeof dateValue === "string") { - parsed = DateTime.fromISO(dateValue, { zone }); - - if (!parsed.isValid) { - parsed = DateTime.fromRFC2822(dateValue, { zone }); - } + const source = rawDate || dateValue; + parsed = parseHugoDateString(source, zone, defaultHour, defaultMinute); } else if (dateValue instanceof Date) { parsed = DateTime.fromJSDate(dateValue, { zone }); }