Files
NodeBB/install/web.js

134 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-04-21 14:32:21 -04:00
"use strict";
2015-04-21 14:52:57 -04:00
var winston = require('winston'),
express = require('express'),
2015-04-21 19:50:58 -04:00
bodyParser = require('body-parser'),
2015-04-21 16:21:04 -04:00
fs = require('fs'),
2015-04-21 14:52:57 -04:00
path = require('path'),
2015-04-21 16:21:04 -04:00
less = require('less'),
2015-04-21 17:02:36 -04:00
async = require('async'),
uglify = require('uglify-js'),
nconf = require('nconf'),
2015-04-22 14:49:31 -04:00
app = express(),
server;
2015-04-21 14:52:57 -04:00
2015-04-21 17:02:36 -04:00
var web = {},
scripts = [
'public/vendor/xregexp/xregexp.js',
'public/vendor/xregexp/unicode/unicode-base.js',
'public/src/utils.js',
'public/src/installer/install.js'
];
2015-04-21 14:32:21 -04:00
2015-04-21 14:52:57 -04:00
web.install = function(port) {
port = port || 8080;
winston.info('Launching web installer on port', port);
2015-04-21 14:32:21 -04:00
2015-04-21 19:50:58 -04:00
app.use(express.static('public', {}));
app.engine('tpl', require('templates.js').__express);
app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, '../src/views'));
app.use(bodyParser.urlencoded({
extended: true
}));
2015-04-21 17:02:36 -04:00
async.parallel([compileLess, compileJS], function() {
2015-04-21 16:21:04 -04:00
setupRoutes();
launchExpress(port);
});
2015-04-21 14:32:21 -04:00
};
2015-04-21 14:52:57 -04:00
function launchExpress(port) {
2015-04-22 14:49:31 -04:00
server = app.listen(port, function() {
2015-04-21 14:52:57 -04:00
var host = server.address().address;
winston.info('Web installer listening on http://%s:%s', host, port);
});
}
function setupRoutes() {
2015-04-21 19:50:58 -04:00
app.get('/', welcome);
app.post('/', install);
2015-04-22 14:49:31 -04:00
app.post('/launch', launch);
2015-04-21 14:52:57 -04:00
}
2015-04-21 19:50:58 -04:00
function welcome(req, res) {
2015-04-21 19:10:47 -04:00
var dbs = ['redis', 'mongo'],
databases = [];
dbs.forEach(function(el) {
databases.push({
name: el,
questions: require('../src/database/' + el).questions
});
});
2015-04-22 15:29:29 -04:00
res.render('install/index', {
databases: databases,
error: res.locals.error ? true : false,
success: res.locals.success ? true : false,
values: req.body
2015-04-21 19:10:47 -04:00
});
2015-04-21 14:52:57 -04:00
}
2015-04-21 19:50:58 -04:00
function install(req, res) {
2015-04-22 11:36:19 -04:00
var env = {};
2015-04-22 11:36:19 -04:00
for (var i in req.body) {
if (req.body.hasOwnProperty(i)) {
env[i.replace(':', '__')] = req.body[i];
}
}
2015-04-22 11:22:55 -04:00
var child = require('child_process').fork('app', ['--setup'], {
2015-04-22 11:36:19 -04:00
env: env
2015-04-22 11:22:55 -04:00
});
2015-04-22 11:36:19 -04:00
child.on('close', function(data) {
if (data === 0) {
res.locals.success = true;
} else {
res.locals.error = true;
}
welcome(req, res);
});
2015-04-21 19:50:58 -04:00
}
2015-04-22 14:49:31 -04:00
function launch(req, res) {
2015-04-22 15:29:29 -04:00
res.json({});
2015-04-22 14:49:31 -04:00
}
2015-04-21 16:21:04 -04:00
function compileLess(callback) {
if ((nconf.get('from-file') || '').indexOf('less') !== -1) {
winston.info('LESS compilation skipped');
return callback(false);
}
2015-04-21 16:21:04 -04:00
fs.readFile(path.join(__dirname, '../public/less/install.less'), function(err, style) {
less.render(style.toString(), function(err, css) {
if(err) {
return winston.error('Unable to compile LESS: ', err);
}
fs.writeFile(path.join(__dirname, '../public/stylesheet.css'), css.css, callback);
});
});
}
2015-04-21 14:32:21 -04:00
2015-04-21 17:02:36 -04:00
function compileJS(callback) {
if ((nconf.get('from-file') || '').indexOf('js') !== -1) {
winston.info('Client-side JS compilation skipped');
return callback(false);
}
2015-04-21 17:02:36 -04:00
var scriptPath = path.join(__dirname, '..'),
result = uglify.minify(scripts.map(function(script) {
return path.join(scriptPath, script);
}));
fs.writeFile(path.join(__dirname, '../public/nodebb.min.js'), result.code, callback);
}
2015-04-21 14:32:21 -04:00
module.exports = web;