Correction du forçage de mise à jour de la météo
This commit is contained in:
@@ -16,9 +16,12 @@ async function processFile(filePath, config, { force = false } = {}) {
|
|||||||
return { status: "no-frontmatter" };
|
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" };
|
return { status: "already-set" };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,32 +42,51 @@ async function processFile(filePath, config, { force = false } = {}) {
|
|||||||
let finalWeather = {};
|
let finalWeather = {};
|
||||||
|
|
||||||
if (force) {
|
if (force) {
|
||||||
finalWeather = weather || {};
|
if (weather) {
|
||||||
|
finalWeather = weather;
|
||||||
|
}
|
||||||
} else if (existingWeather && typeof existingWeather === "object") {
|
} else if (existingWeather && typeof existingWeather === "object") {
|
||||||
finalWeather = JSON.parse(JSON.stringify(existingWeather));
|
finalWeather = JSON.parse(JSON.stringify(existingWeather));
|
||||||
if (weather) {
|
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) {
|
if (!added && Object.keys(finalWeather).length === 0) {
|
||||||
finalWeather = weather;
|
finalWeather = weather;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if (weather) {
|
||||||
finalWeather = weather || {};
|
finalWeather = weather;
|
||||||
}
|
}
|
||||||
|
|
||||||
frontmatter.doc.set("weather", finalWeather);
|
frontmatter.doc.set("weather", finalWeather);
|
||||||
|
|
||||||
await writeFrontmatter(filePath, frontmatter.doc, frontmatter.body);
|
await writeFrontmatter(filePath, frontmatter.doc, frontmatter.body);
|
||||||
|
|
||||||
|
let status = "empty";
|
||||||
|
if (weather) {
|
||||||
|
status = "updated";
|
||||||
|
}
|
||||||
|
|
||||||
|
let sources = [];
|
||||||
|
if (weather && weather.source) {
|
||||||
|
sources = weather.source;
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: weather ? "updated" : "empty",
|
status,
|
||||||
sources: weather?.source || [],
|
sources,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const cliArgs = process.argv.slice(2);
|
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 pathArgs = cliArgs.filter((arg) => arg !== "--force" && arg !== "-f");
|
||||||
|
|
||||||
const config = loadWeatherConfig();
|
const config = loadWeatherConfig();
|
||||||
@@ -89,10 +111,15 @@ async function main() {
|
|||||||
const result = await processFile(file, config, { force });
|
const result = await processFile(file, config, { force });
|
||||||
|
|
||||||
switch (result.status) {
|
switch (result.status) {
|
||||||
case "updated":
|
case "updated": {
|
||||||
updated += 1;
|
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;
|
break;
|
||||||
|
}
|
||||||
case "empty":
|
case "empty":
|
||||||
updated += 1;
|
updated += 1;
|
||||||
console.log(`• Added empty weather to ${relativePath}`);
|
console.log(`• Added empty weather to ${relativePath}`);
|
||||||
|
|||||||
@@ -187,4 +187,5 @@ module.exports = {
|
|||||||
getHugoTimeZone,
|
getHugoTimeZone,
|
||||||
toHugoDateTime,
|
toHugoDateTime,
|
||||||
parseFrontmatterDate,
|
parseFrontmatterDate,
|
||||||
|
parseHugoDateString,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const { DateTime } = require("luxon");
|
const { DateTime } = require("luxon");
|
||||||
|
const { parseHugoDateString } = require("../datetime");
|
||||||
|
|
||||||
function hasExplicitTime(rawDate) {
|
function hasExplicitTime(rawDate) {
|
||||||
if (!rawDate) return false;
|
if (!rawDate) return false;
|
||||||
@@ -13,11 +14,8 @@ function resolveArticleDate(dateValue, rawDate, { timezone = "Europe/Paris", def
|
|||||||
let parsed;
|
let parsed;
|
||||||
|
|
||||||
if (typeof dateValue === "string") {
|
if (typeof dateValue === "string") {
|
||||||
parsed = DateTime.fromISO(dateValue, { zone });
|
const source = rawDate || dateValue;
|
||||||
|
parsed = parseHugoDateString(source, zone, defaultHour, defaultMinute);
|
||||||
if (!parsed.isValid) {
|
|
||||||
parsed = DateTime.fromRFC2822(dateValue, { zone });
|
|
||||||
}
|
|
||||||
} else if (dateValue instanceof Date) {
|
} else if (dateValue instanceof Date) {
|
||||||
parsed = DateTime.fromJSDate(dateValue, { zone });
|
parsed = DateTime.fromJSDate(dateValue, { zone });
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user