Updates workflow (more functional and straightforward)
All checks were successful
Update RSS / update-rss (push) Successful in 5m16s

This commit is contained in:
Jan Andrle 2024-06-27 16:35:15 +02:00
parent 39bb5ef62d
commit df82405ed8
Signed by: jaandrle
GPG Key ID: B3A25AED155AFFAB
3 changed files with 27 additions and 24 deletions

43
cli.mjs
View File

@ -2,10 +2,6 @@
/* jshint esversion: 11,-W097, -W040, module: true, node: true, expr: true, undef: true *//* global echo, $, pipe, s, fetch, cyclicLoop */ /* 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 url_drops= "https://pagenotfound.cz/drop/";
const { version, description }= s.cat("package.json").xargs(JSON.parse); const { version, description }= s.cat("package.json").xargs(JSON.parse);
const paths= {
sitemap: "sitemap.json",
rss: "rss.xml",
};
/** /**
* @typedef {Object} Article * @typedef {Object} Article
* @property {string} title * @property {string} title
@ -24,6 +20,11 @@ const paths= {
* @property {Article[]} articles * @property {Article[]} articles
* @property {Drop[]} drops * @property {Drop[]} drops
* */ * */
/**
* @typedef {Object} State
* @property {Sitemap} json
* @property {string[]} changed Changed files
* */
$.api() $.api()
.version(version) .version(version)
@ -31,16 +32,15 @@ $.api()
.command("pull", "Update article list") .command("pull", "Update article list")
.option("--git", "Update git repository") .option("--git", "Update git repository")
.action(async function pull({ git: is_git= false }){ .action(async function pull({ git: is_git= false }){
const json= await sitemap(); const { changed }= await sitemap().then(toRSS);
toRSS(json); echo("Changed files:", changed.length ? changed.join(", ") : "—");
if(is_git) if(is_git) gitCommit(changed, "pull");
gitCommit(Object.values(paths));
$.exit(0); $.exit(0);
}) })
.parse(); .parse();
function gitCommit(files, des= "not specified"){ 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"); return echo("Nothig todo");
echo("Diff to save"); echo("Diff to save");
@ -51,9 +51,14 @@ function gitCommit(files, des= "not specified"){
s.run`git push`; s.run`git push`;
s.run`git config --remove-section user`; s.run`git config --remove-section user`;
} }
/** @param {Sitemap} json */ /**
async function toRSS(json){ * @param {State} state
const path= paths.rss; * @returns {State} state
* */
async function toRSS({ json, changed }){
if(!changed.length) return { json, changed };
const path= "rss.xml";
const host= "https://pagenotfound.cz"; const host= "https://pagenotfound.cz";
const articles= json.articles.map(function({ title, perex, author, loc, drop }){ const articles= json.articles.map(function({ title, perex, author, loc, drop }){
return [ return [
@ -80,20 +85,22 @@ async function toRSS(json){
"</channel>", "</channel>",
"</rss>" "</rss>"
].join("\n")).to(path); ].join("\n")).to(path);
return { json, changed: [...changed, path] };
} }
import { JSDOM } from "jsdom"; import { JSDOM } from "jsdom";
/** @returns {Promise<State>} */
async function sitemap(){ async function sitemap(){
const path= paths.sitemap; const path= "sitemap.json";
/** @type {Sitemap} */ /** @type {Sitemap} */
const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] }; const json= s.test("-f", path) ? s.cat(path).xargs(JSON.parse) : { drops: [], articles: [] };
const [ article_last= { drop: "" } ]= json.articles;
await syncDrops(json); await syncDrops(json);
const [ { drop: drop_last } ]= json.drops; 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); 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 dom= new JSDOM(await res.text());
const diff= []; const diff= [];
for(const article of dom.window.document.querySelectorAll("article")){ for(const article of dom.window.document.querySelectorAll("article")){
@ -108,7 +115,7 @@ async function sitemap(){
json.articles.unshift(...diff); json.articles.unshift(...diff);
s.echo(JSON.stringify(json, null, "\t")).to(path); s.echo(JSON.stringify(json, null, "\t")).to(path);
return json; return { json, changed: [ path ] };
} }
/** @param {Sitemap} json */ /** @param {Sitemap} json */
async function syncDrops(json){ async function syncDrops(json){

View File

@ -1,6 +1,6 @@
{ {
"name": "pagenotfound-cli", "name": "pagenotfound-cli",
"version": "1.0.0", "version": "1.1.0",
"description": "Utility primary for generating RSS feed for Pagenotfound", "description": "Utility primary for generating RSS feed for Pagenotfound",
"bin": "cli.js", "bin": "cli.js",
"scripts": { "scripts": {

View File

@ -2,10 +2,6 @@
"drops": [ "drops": [
{ {
"drop": "drop-005", "drop": "drop-005",
"date": "2024-06-27T14:32:57.000Z"
},
{
"drop": "drop-004",
"date": "2024-06-25" "date": "2024-06-25"
}, },
{ {