diff --git a/cli.mjs b/cli.mjs index f0b4f83..db2a504 100755 --- a/cli.mjs +++ b/cli.mjs @@ -2,10 +2,6 @@ /* jshint esversion: 11,-W097, -W040, module: true, node: true, expr: true, undef: true *//* global echo, $, pipe, s, fetch, cyclicLoop */ const url_drops= "https://pagenotfound.cz/drop/"; const { version, description }= s.cat("package.json").xargs(JSON.parse); -const paths= { - sitemap: "sitemap.json", - rss: "rss.xml", -}; /** * @typedef {Object} Article * @property {string} title @@ -24,6 +20,11 @@ const paths= { * @property {Article[]} articles * @property {Drop[]} drops * */ +/** + * @typedef {Object} State + * @property {Sitemap} json + * @property {string[]} changed Changed files + * */ $.api() .version(version) @@ -31,16 +32,15 @@ $.api() .command("pull", "Update article list") .option("--git", "Update git repository") .action(async function pull({ git: is_git= false }){ - const json= await sitemap(); - toRSS(json); - if(is_git) - gitCommit(Object.values(paths)); + const { changed }= await sitemap().then(toRSS); + echo("Changed files:", changed.length ? changed.join(", ") : "—"); + if(is_git) gitCommit(changed, "pull"); $.exit(0); }) .parse(); function gitCommit(files, des= "not specified"){ - if(!s.run`git diff --numstat`.trim()) + if(!files.length || !s.run`git diff --numstat`.trim()) return echo("Nothig todo"); echo("Diff to save"); @@ -51,9 +51,14 @@ function gitCommit(files, des= "not specified"){ s.run`git push`; s.run`git config --remove-section user`; } -/** @param {Sitemap} json */ -async function toRSS(json){ - const path= paths.rss; +/** + * @param {State} state + * @returns {State} state + * */ +async function toRSS({ json, changed }){ + if(!changed.length) return { json, changed }; + + const path= "rss.xml"; const host= "https://pagenotfound.cz"; const articles= json.articles.map(function({ title, perex, author, loc, drop }){ return [ @@ -80,20 +85,22 @@ async function toRSS(json){ "", "" ].join("\n")).to(path); + return { json, changed: [...changed, path] }; } import { JSDOM } from "jsdom"; +/** @returns {Promise} */ async function sitemap(){ - const path= paths.sitemap; - + const path= "sitemap.json"; /** @type {Sitemap} */ const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] }; - const [ article_last= { drop: "" } ]= json.articles; await syncDrops(json); const [ { drop: drop_last } ]= json.drops; - if(drop_last === article_last.drop) return json; + const [ article_last= { drop: "" } ]= json.articles; + if(drop_last === article_last.drop) return { json, changed: [] }; const res= await fetch(url_drops+drop_last); - if(res.status !== 200) return; + if(res.status !== 200) return { json, changed: [] }; + const dom= new JSDOM(await res.text()); const diff= []; for(const article of dom.window.document.querySelectorAll("article")){ @@ -108,7 +115,7 @@ async function sitemap(){ json.articles.unshift(...diff); s.echo(JSON.stringify(json, null, "\t")).to(path); - return json; + return { json, changed: [ path ] }; } /** @param {Sitemap} json */ async function syncDrops(json){ diff --git a/package.json b/package.json index 8078bbd..ae8ee23 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pagenotfound-cli", - "version": "1.0.0", + "version": "1.1.0", "description": "Utility primary for generating RSS feed for Pagenotfound", "bin": "cli.js", "scripts": { diff --git a/sitemap.json b/sitemap.json index 584bffd..23f2bb2 100644 --- a/sitemap.json +++ b/sitemap.json @@ -2,10 +2,6 @@ "drops": [ { "drop": "drop-005", - "date": "2024-06-27T14:32:57.000Z" - }, - { - "drop": "drop-004", "date": "2024-06-25" }, { @@ -265,4 +261,4 @@ "drop": "0" } ] -} \ No newline at end of file +}