1

Correction du forçage de mise à jour de la météo

This commit is contained in:
2025-12-21 23:31:26 +01:00
parent 9485e467ba
commit fa16ad9da8
3 changed files with 42 additions and 16 deletions

View File

@@ -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}`);