ripunzel/src/save/writeToIndex.js

52 lines
1.5 KiB
JavaScript

'use strict';
const config = require('config');
const yaml = require('js-yaml');
const interpolate = require('../interpolate');
// const textToStream = require('./textToStream');
const save = require('./save');
async function writeToIndex(posts, profilePaths, user, args) {
const filepath = interpolate(config.library.index.file, null, null, null, null, user, false);
const now = new Date();
const newAndUpdatedEntries = posts.concat(user.indexed.updated, args.indexIgnored ? user.indexed.ignored : []).map((post) => {
const entryPost = {
id: post.id,
subreddit: post.subreddit,
permalink: post.permalink,
url: post.url,
hostId: post.host.id,
date: post.datetime,
indexed: now,
score: post.score,
title: post.title,
hash: post.hash,
};
if (post.previewFallback) {
entryPost.preview = true;
}
return entryPost;
});
const data = {
profile: {
image: profilePaths.image,
description: profilePaths.description,
},
posts: newAndUpdatedEntries.concat(user.indexed.original),
};
if (!data.profile.image && !data.profile.description && !data.posts.length) {
return false;
}
// return save(filepath, textToStream(yaml.safeDump(data)));
return save(filepath, Buffer.from(yaml.safeDump(data), 'utf8'));
}
module.exports = writeToIndex;