Added watch-mode. Waiting for profile detail write to finalize before new watch cycle and capture details in index file.

This commit is contained in:
2024-09-11 05:16:56 +02:00
parent dcf7fdd274
commit fcb85f57c8
9 changed files with 99 additions and 31 deletions

View File

@@ -6,10 +6,11 @@ const yaml = require('js-yaml');
const interpolate = require('../interpolate.js');
async function writeToIndex(posts, user) {
async function writeToIndex(posts, profilePaths, user) {
const filename = interpolate(config.library.index.file, user, null, false);
const now = new Date();
/*
// Individual posts are wrapped in [] to get a YAML array value for each individual item, allowing them to be joined manually with a newline
// between each entry to improve human readability of the index while maintaining a valid YAML list
const originalEntries = user.indexed.original.map(entry => yaml.safeDump([entry]));
@@ -25,8 +26,28 @@ async function writeToIndex(posts, user) {
}]));
const entries = newAndUpdatedEntries.concat(originalEntries).join('\n');
*/
return fs.writeFile(filename, entries);
const newAndUpdatedEntries = posts.concat(user.indexed.updated).map(post => ({
id: post.id,
subreddit: post.subreddit,
permalink: post.permalink,
url: post.url,
hostId: post.host.id,
date: post.datetime,
indexed: now,
title: post.title,
}));
const data = {
profile: {
image: profilePaths.image,
description: profilePaths.description,
},
posts: newAndUpdatedEntries.concat(user.indexed.original),
};
return fs.writeFile(filename, yaml.safeDump(data));
}
module.exports = writeToIndex;