Misc fixes and improvements (#6143)

* `setup` command fixes and improvements

- Enable using the `./nodebb setup` command for auto-setup with a JSON argument
- Change CLI so package-install and dependency install are separate steps
- Fix #6142

* Prevent compiling templates multiple times

- Multiple requests for same template get pooled
- Hopefully fixes the "templateFunction is not a function" error which happens if site is restarted during high-traffic times

* More helpful upgrade template
This commit is contained in:
Peter Jaszkowiak
2017-12-04 13:49:44 -07:00
committed by Julian Lam
parent 3551d7d68e
commit fc19f3af61
6 changed files with 83 additions and 22 deletions

View File

@@ -203,12 +203,19 @@ middleware.delayLoading = function (req, res, next) {
};
var viewsDir = nconf.get('views_dir');
var workingCache = {};
middleware.templatesOnDemand = function (req, res, next) {
var filePath = req.filePath || path.join(viewsDir, req.path);
if (!filePath.endsWith('.js')) {
return next();
}
if (workingCache[filePath]) {
workingCache[filePath].push(next);
return;
}
async.waterfall([
function (cb) {
file.exists(filePath, cb);
@@ -218,6 +225,14 @@ middleware.templatesOnDemand = function (req, res, next) {
return next();
}
// need to check here again
// because compilation could have started since last check
if (workingCache[filePath]) {
workingCache[filePath].push(next);
return;
}
workingCache[filePath] = [next];
fs.readFile(filePath.replace(/\.js$/, '.tpl'), 'utf8', cb);
},
function (source, cb) {
@@ -229,5 +244,12 @@ middleware.templatesOnDemand = function (req, res, next) {
function (compiled, cb) {
fs.writeFile(filePath, compiled, cb);
},
], next);
], function (err) {
var arr = workingCache[filePath];
workingCache[filePath] = null;
arr.forEach(function (callback) {
callback(err);
});
});
};