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 */
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){
"</channel>",
"</rss>"
].join("\n")).to(path);
return { json, changed: [...changed, path] };
}
import { JSDOM } from "jsdom";
/** @returns {Promise<State>} */
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){

View File

@ -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": {

View File

@ -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"
}
]
}
}