Merge pull request #606 from spicyj/server-fail

Fail generating website if any page throws an error
This commit is contained in:
Christopher Chedeau 2015-04-01 17:57:25 -07:00
commit adbdfe65e1
2 changed files with 28 additions and 34 deletions

View File

@ -14,5 +14,8 @@
"react-docgen": "^1.1.0", "react-docgen": "^1.1.0",
"react-page-middleware": "git://github.com/facebook/react-page-middleware.git", "react-page-middleware": "git://github.com/facebook/react-page-middleware.git",
"request": "*" "request": "*"
},
"devDependencies": {
"bluebird": "^2.9.21"
} }
} }

View File

@ -7,6 +7,7 @@
* of patent rights can be found in the PATENTS file in the same directory. * of patent rights can be found in the PATENTS file in the same directory.
*/ */
var Promise = require('bluebird');
var request = require('request'); var request = require('request');
var glob = require('glob'); var glob = require('glob');
var fs = require('fs.extra'); var fs = require('fs.extra');
@ -19,29 +20,7 @@ server.noconvert = true;
// Sadly, our setup fatals when doing multiple concurrent requests // 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 // I don't have the time to dig into why, it's easier to just serialize
// requests. // requests.
var queue = (function() { var queue = Promise.resolve();
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};
})();
glob('src/**/*.*', function(er, files) { glob('src/**/*.*', function(er, files) {
files.forEach(function(file) { files.forEach(function(file) {
@ -49,24 +28,36 @@ glob('src/**/*.*', function(er, files) {
if (file.match(/\.js$/)) { if (file.match(/\.js$/)) {
targetFile = targetFile.replace(/\.js$/, '.html'); targetFile = targetFile.replace(/\.js$/, '.html');
queue.push(function(cb) { queue = queue.then(function() {
request('http://localhost:8079/' + targetFile.replace(/^build\//, ''), function(error, response, body) { return new Promise(function(resolve, reject) {
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), '')); request('http://localhost:8079/' + targetFile.replace(/^build\//, ''), function(error, response, body) {
fs.writeFileSync(targetFile, body); if (error) {
cb(); 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 { } else {
queue.push(function(cb) { queue = queue.then(function() {
mkdirp.sync(targetFile.replace(new RegExp('/[^/]*$'), '')); return new Promise(function(resolve, reject) {
fs.copy(file, targetFile, cb); 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(); server.close();
console.log('It is live at: http://facebook.github.io/react-native/')
cb();
}); });
}); });