Skip to content

Commit

Permalink
Merge pull request facebook#606 from spicyj/server-fail
Browse files Browse the repository at this point in the history
Fail generating website if any page throws an error
  • Loading branch information
vjeux committed Apr 2, 2015
2 parents bd8671f + efe446e commit adbdfe6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
3 changes: 3 additions & 0 deletions website/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,8 @@
"react-docgen": "^1.1.0",
"react-page-middleware": "git://github.com/facebook/react-page-middleware.git",
"request": "*"
},
"devDependencies": {
"bluebird": "^2.9.21"
}
}
59 changes: 25 additions & 34 deletions website/server/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

var Promise = require('bluebird');
var request = require('request');
var glob = require('glob');
var fs = require('fs.extra');
Expand All @@ -19,54 +20,44 @@ 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 = (function() {
var is_executing = false;
var queue = [];
function push(fn) {
queue.push(fn);
execute();
}
function execute() {
if (is_executing) {
return;
}
if (queue.length === 0) {
return;
}
var fn = queue.shift();
is_executing = true;
fn(function() {
is_executing = false;
execute();
});
}
return {push: push};
})();
var queue = Promise.resolve();

glob('src/**/*.*', function(er, files) {
files.forEach(function(file) {
var targetFile = file.replace(/^src/, 'build');

if (file.match(/\.js$/)) {
targetFile = targetFile.replace(/\.js$/, '.html');
queue.push(function(cb) {
request('http://localhost:8079/' + targetFile.replace(/^build\//, ''), function(error, response, body) {
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
fs.writeFileSync(targetFile, body);
cb();
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.push(function(cb) {
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
fs.copy(file, targetFile, cb);
queue = queue.then(function() {
return new Promise(function(resolve, reject) {
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), ''));
fs.copy(file, targetFile, resolve);
});
});
}
});

queue.push(function(cb) {
queue = queue.then(function() {
console.log('It is live at: http://facebook.github.io/react-native/');
}).finally(function() {
server.close();
console.log('It is live at: http://facebook.github.io/react-native/')
cb();
});
});

0 comments on commit adbdfe6

Please sign in to comment.