|
| 1 | +const _ = require('lodash'); |
| 2 | +const chalk = require('chalk'); |
| 3 | +const fs = require('fs'); |
| 4 | +const mkdirp = require('mkdirp'); |
| 5 | +const path = require('path'); |
| 6 | +const prettyHrtime = require('pretty-hrtime'); |
| 7 | +const RSS = require('rss'); |
| 8 | + |
| 9 | +const defaults = require('./components-build-defaults'); |
| 10 | + |
| 11 | +module.exports = _options => new Promise((resolve, reject) => { |
| 12 | + const options = _.merge({}, defaults, _options); |
| 13 | + const startTime = process.hrtime(); |
| 14 | + console.log(chalk.magenta('Working on components RSS feed...')); |
| 15 | + if (options.components.tempListPath === undefined || |
| 16 | + !fs.existsSync(options.components.tempListPath)) { |
| 17 | + reject('Can not find components list (JSON)'); |
| 18 | + return; |
| 19 | + } |
| 20 | + |
| 21 | + const components = JSON.parse(fs.readFileSync(options.components.tempListPath, 'utf8')); |
| 22 | + const recentComponents = _.orderBy(components, ['creationTime'], ['desc']) |
| 23 | + .slice(0, options.components.rss.count); |
| 24 | + |
| 25 | + // Create the feed |
| 26 | + // See https://github.com/dylang/node-rss |
| 27 | + const feedOptions = { |
| 28 | + title: options.components.rss.title, |
| 29 | + description: options.siteDescription, |
| 30 | + feed_url: `${options.siteUrl}/${options.components.rss.path}`, |
| 31 | + site_url: options.siteUrl, |
| 32 | + categories: options.components.rss.categories, |
| 33 | + ttl: options.components.rss.ttl, |
| 34 | + }; |
| 35 | + const feed = new RSS(feedOptions); |
| 36 | + |
| 37 | + // Add feed items |
| 38 | + recentComponents.forEach((component) => { |
| 39 | + const url = `${options.siteUrl}${component.page.href}`; |
| 40 | + const screenshotUrl = `${options.siteUrl}${component.screenshot.href}`; |
| 41 | + const description = ` |
| 42 | + <p>${options.siteDescription}</p> |
| 43 | + <a href="${url}"><img src="${screenshotUrl}"></a> |
| 44 | + <p><small>Component by ${_.escape(component.author)}</small></p> |
| 45 | + `; |
| 46 | + feed.item({ |
| 47 | + title: component.page.title, |
| 48 | + description, |
| 49 | + url, |
| 50 | + guid: `${component.category}:${component.id}`, |
| 51 | + categories: options.components.rss.categories, |
| 52 | + date: component.creationTime, |
| 53 | + author: component.author, |
| 54 | + // None of this below appears to work. |
| 55 | + // Let's put the screenshot straight in the description. |
| 56 | + // enclosure: { |
| 57 | + // url: `${options.siteUrl}${component.screenshot.href}`, |
| 58 | + // type: 'image/jpeg', |
| 59 | + // }, |
| 60 | + // custom_elements: [{ |
| 61 | + // 'media:content': [{ |
| 62 | + // _attr: { |
| 63 | + // medium: 'image', |
| 64 | + // href: `${options.siteUrl}${component.screenshot.href}`, |
| 65 | + // // height: media.height, |
| 66 | + // // width: media.width |
| 67 | + // }, |
| 68 | + // }], |
| 69 | + // }], |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + // Save the feed |
| 74 | + mkdirp.sync(path.dirname(options.components.rss.path)); |
| 75 | + fs.writeFileSync(options.components.rss.path, feed.xml()); |
| 76 | + console.log('- Created RSS feed:', options.components.rss.path); |
| 77 | + |
| 78 | + const elapsed = process.hrtime(startTime); |
| 79 | + console.log(chalk.magenta('Done with components RSS feed!'), chalk.dim(prettyHrtime(elapsed))); |
| 80 | + resolve(); |
| 81 | +}); // return promise |
0 commit comments