You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

219 lines
5.1 KiB
JavaScript

8 years ago
'use strict';
10 years ago
9 years ago
var winston = require('winston');
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var path = require('path');
var childProcess = require('child_process');
9 years ago
var less = require('less');
var async = require('async');
var uglify = require('uglify-js');
var nconf = require('nconf');
7 years ago
var _ = require('lodash');
var Benchpress = require('benchpressjs');
9 years ago
var app = express();
var server;
winston.add(winston.transports.File, {
filename: 'logs/webinstall.log',
colorize: true,
timestamp: function () {
var date = new Date();
return date.getDate() + '/' + (date.getMonth() + 1) + ' ' + date.toTimeString().substr(0, 5) + ' [' + global.process.pid + ']';
},
level: 'verbose',
});
9 years ago
var web = {};
var scripts = [
'node_modules/jquery/dist/jquery.js',
9 years ago
'public/vendor/xregexp/xregexp.js',
'public/vendor/xregexp/unicode/unicode-base.js',
'public/src/utils.js',
'public/src/installer/install.js',
9 years ago
];
10 years ago
web.install = function (port) {
port = port || 4567;
winston.info('Launching web installer on port', port);
10 years ago
app.use(express.static('public', {}));
app.engine('tpl', function (filepath, options, callback) {
async.waterfall([
function (next) {
fs.readFile(filepath, 'utf-8', next);
},
function (buffer, next) {
Benchpress.compileParse(buffer.toString(), options, next);
7 years ago
},
], callback);
});
app.set('view engine', 'tpl');
app.set('views', path.join(__dirname, '../src/views'));
app.use(bodyParser.urlencoded({
extended: true,
10 years ago
}));
async.parallel([compileLess, compileJS, copyCSS], function (err) {
8 years ago
if (err) {
winston.error(err);
}
setupRoutes();
launchExpress(port);
});
10 years ago
};
function launchExpress(port) {
server = app.listen(port, function () {
winston.info('Web installer listening on http://%s:%s', '0.0.0.0', port);
});
}
function setupRoutes() {
app.get('/', welcome);
app.post('/', install);
app.post('/launch', launch);
}
function welcome(req, res) {
9 years ago
var dbs = ['redis', 'mongo'];
7 years ago
var databases = dbs.map(function (databaseName) {
var questions = require('../src/database/' + databaseName).questions.filter(function (question) {
return question && !question.hideOnWebInstall;
});
9 years ago
return {
7 years ago
name: databaseName,
questions: questions,
9 years ago
};
});
9 years ago
var defaults = require('./data/defaults');
res.render('install/index', {
databases: databases,
skipDatabaseSetup: !!nconf.get('database'),
error: !!res.locals.error,
success: !!res.locals.success,
9 years ago
values: req.body,
minimumPasswordLength: defaults.minimumPasswordLength,
});
}
function install(req, res) {
req.setTimeout(0);
7 years ago
var setupEnvVars = _.assign({}, process.env);
for (var i in req.body) {
if (req.body.hasOwnProperty(i) && !process.env.hasOwnProperty(i)) {
7 years ago
setupEnvVars[i.replace(':', '__')] = req.body[i];
}
}
var child = require('child_process').fork('app', ['--setup'], {
7 years ago
env: setupEnvVars,
});
child.on('close', function (data) {
if (data === 0) {
res.locals.success = true;
} else {
res.locals.error = true;
}
welcome(req, res);
});
}
function launch(req, res) {
res.json({});
server.close();
var child = childProcess.spawn('node', ['loader.js'], {
detached: true,
stdio: ['ignore', 'ignore', 'ignore'],
});
console.log('\nStarting NodeBB');
console.log(' "./nodebb stop" to stop the NodeBB server');
console.log(' "./nodebb log" to view server output');
console.log(' "./nodebb restart" to restart NodeBB');
10 years ago
var filesToDelete = [
'installer.css',
'installer.min.js',
'bootstrap.min.css',
];
async.each(filesToDelete, function (filename, next) {
fs.unlink(path.join(__dirname, '../public', filename), next);
}, function (err) {
if (err) {
winston.warn('Unable to remove installer files');
}
10 years ago
child.unref();
process.exit(0);
});
}
function compileLess(callback) {
fs.readFile(path.join(__dirname, '../public/less/install.less'), function (err, style) {
if (err) {
return winston.error('Unable to read LESS install file: ', err);
}
less.render(style.toString(), function (err, css) {
if (err) {
return winston.error('Unable to compile LESS: ', err);
}
fs.writeFile(path.join(__dirname, '../public/installer.css'), css.css, callback);
});
});
}
10 years ago
function compileJS(callback) {
8 years ago
var code = '';
async.eachSeries(scripts, function (srcPath, next) {
fs.readFile(path.join(__dirname, '..', srcPath), function (err, buffer) {
if (err) {
return next(err);
}
8 years ago
code += buffer.toString();
next();
});
}, function (err) {
if (err) {
return callback(err);
}
try {
var minified = uglify.minify(code, {
compress: false,
});
if (!minified.code) {
return callback(new Error('[[error:failed-to-minify]]'));
}
fs.writeFile(path.join(__dirname, '../public/installer.min.js'), minified.code, callback);
} catch (e) {
callback(e);
}
});
}
function copyCSS(next) {
async.waterfall([
function (next) {
fs.readFile(path.join(__dirname, '../node_modules/bootstrap/dist/css/bootstrap.min.css'), 'utf8', next);
},
function (src, next) {
fs.writeFile(path.join(__dirname, '../public/bootstrap.min.css'), src, next);
},
], next);
}
8 years ago
module.exports = web;