Files
react-native/website/server/generate.js
Héctor Ramos 9cb370dd5b Generate Atom feed for the React Native blog.
Summary:
An Atom feed is now generated as part of the build script. This is done statically and not as a React view because React is not the right tool for generating XML documents.

Some additional metadata is stored in `metadata-blog.js` and duplicated to `metadata-blog.json` in the `server/` directory to aid in the generation of the feed. Let me know if there's a better way to import this data using the existing Haste module that wouldn't require writing an additional JSON file.

The feed will be available at https://facebook.github.io/react-native/blog.xml

A sample output of the Atom feed is included at the bottom. It is a [valid Atom 1.0 feed](https://validator.w3.org/feed/check.cgi), with some additional recommendations that can be ignored for now.

> Congratulations!
>
> [Valid Atom 1.0] This is a valid Atom 1.0 feed.
> Recommendations
>
> This feed is valid, but interoperability with the widest range of feed readers could be improved by implementing the following recommendations.
> line 2, col
Closes https://github.com/facebook/react-native/pull/10611

Differential Revision: D4097381

Pulled By: mkonicek

fbshipit-source-id: 8d2e18923358d1903b2715b00c48680b0c4dff68
2016-10-28 13:28:37 -07:00

112 lines
3.5 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
'use strict';
var Promise = require('bluebird');
var request = require('request');
var glob = require('glob');
var fs = require('fs.extra');
var mkdirp = require('mkdirp');
var server = require('./server.js');
var Feed = require('feed');
require('./convert.js')();
server.noconvert = true;
// Sadly, our setup fatals when doing multiple concurrent requests
// I don't have the time to dig into why, it's easier to just serialize
// requests.
var queue = Promise.resolve();
// Generate RSS Feeds
queue = queue.then(function() {
return new Promise(function(resolve, reject) {
var targetFile = 'build/react-native/blog/feed.xml';
var basePath = 'https://facebook.github.io/react-native/';
var blogPath = basePath + 'blog/';
var metadataBlog = JSON.parse(fs.readFileSync('server/metadata-blog.json'));
var latestPost = metadataBlog.files[0];
var feed = new Feed({
title: 'React Native Blog',
description: 'The best place to stay up-to-date with the latest React Native news and events.',
id: blogPath,
link: blogPath,
image: basePath + 'img/header_logo.png',
copyright: 'Copyright © ' + new Date().getFullYear() + ' Facebook Inc.',
updated: new Date(latestPost.publishedAt),
});
metadataBlog.files.forEach(function(post) {
var url = blogPath + post.path;
feed.addItem({
title: post.title,
id: url,
link: url,
date: new Date(post.publishedAt),
author: [{
name: post.author,
link: post.authorURL
}],
description: post.content.trim().split('\n')[0],
});
});
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
fs.writeFileSync(targetFile, feed.render('atom-1.0'));
console.log('Generated RSS feed')
resolve();
});
});
// Generate HTML for each non-source code JS file
glob('src/**/*.*', function(er, files) {
files.forEach(function(file) {
var targetFile = file.replace(/^src/, 'build');
if (file.match(/\.js$/) && !file.match(/src\/react-native\/js/)) {
targetFile = targetFile.replace(/\.js$/, '.html');
queue = queue.then(function() {
return new Promise(function(resolve, reject) {
request('http://localhost:8079/' + targetFile.replace(/^build\//, ''), function(error, response, body) {
if (error) {
reject(error);
return;
}
if (response.statusCode != 200) {
reject(new Error('Status ' + response.statusCode + ':\n' + body));
return;
}
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
fs.writeFileSync(targetFile, body);
resolve();
});
});
});
} else {
queue = queue.then(function() {
return new Promise(function(resolve, reject) {
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
fs.copy(file, targetFile, resolve);
});
});
}
});
queue = queue.then(function() {
console.log('Generated HTML files from JS');
}).finally(function() {
server.close();
}).catch(function(e) {
console.error(e);
process.exit(1);
});
});