added setup script that runs on first run of server

This commit is contained in:
David Baldwynn 2017-11-21 13:10:43 -08:00
parent 563b8c6cd2
commit 31985ddda7
3 changed files with 123 additions and 89 deletions

View File

@ -12,10 +12,24 @@ var mongoose = require('mongoose'),
constants = require('./setup_constants'),
_ = require('lodash');
var exitProcess = function() {
var exitSuccess = function(cb) {
console.log(chalk.green('TellForm has been successfully setup'));
console.log(chalk.green('Have fun using TellForm!'));
process.exit(1);
if(require.main === module){
process.exit(1);
} else if(cb && typeof cb === 'function'){
cb();
}
}
var exitError = function(err, cb){
console.error(chalk.red(err.message || err));
if(require.main === module){
process.exit(-1);
} else if(cb && typeof cb === 'function'){
cb();
}
}
var removeENVFile = function() {
@ -83,7 +97,7 @@ var createOrUpdateAdminUser = function(username, email, password, cb){
}
var createENVFile = function() {
var createENVFile = function(cb) {
inquirer.prompt(constants.questionsPart1).then(function (answersPart1) {
var nextQuestions = constants.mailerWellKnownQuestions.concat(constants.questionsPart2);
if(answersPart1['MAILER_SERVICE_PROVIDER'] === 'Custom Mailserver'){
@ -106,22 +120,20 @@ var createENVFile = function() {
delete answers['password'];
envfile.stringify(answers, function (err, str) {
try {
fs.outputFileSync('./\.env', str);
} catch (fileErr) {
console.error(chalk.red(fileErr));
process.exit(-1);
}
try {
fs.outputFileSync('./\.env', str);
} catch (fileErr) {
console.error(chalk.red(fileErr));
process.exit(-1);
}
console.log(chalk.green('Successfully created .env file'));
console.log(chalk.green('Successfully created .env file'));
createOrUpdateAdminUser(username, email, pass, function(err){
if(err) {
console.error(chalk.red(err.message));
process.exit(-1);
return exitError(err, cb);
}
exitProcess();
exitSuccess(cb);
});
});
@ -129,27 +141,34 @@ var createENVFile = function() {
});
}
var runSetup = function(){
console.log(chalk.green('\n\nWelcome to TellForm Setup'));
console.log(chalk.green('You should only need to run this script the first time you run TellForm\n------------------------------------------------------------------------\n\n'));
var checkENVAndRunSetup = function(cb) {
if(require.main === module){
console.log(chalk.green('\n\nWelcome to TellForm\'s Setup Tool'));
console.log(chalk.green('Follow the prompts to begin.\n---------------------------------------------------------------------\n\n'));
}
if(fs.existsSync('./\.env') && require.main === module) {
inquirer.prompt([constants.replaceENVQuestion]).then(function (envAnswer) {
if (envAnswer['replaceENVFile']) {
removeENVFile();
createENVFile();
createENVFile(cb);
} else {
exitProcess();
exitSuccess(cb);
}
});
} else {
if(require.main !== module){
console.log(chalk.green('\nWelcome to TellForm!\n'));
console.log(chalk.green('The following prompts will help you configure your TellForm instance for your needs'));
console.log(chalk.green('These prompts won\'t show up again after the initial setup.\n---------------------------------------------------------------------\n\n'));
}
createENVFile();
}
}
module.exports.runSetup = runSetup;
module.exports.checkENVAndRunSetup = checkENVAndRunSetup;
if(require.main === module) {
runSetup();
checkENVAndRunSetup();
}

133
server.js
View File

@ -1,22 +1,9 @@
'use strict';
/**
* Module dependencies.
*/
if(!process.env.NODE_ENV){
process.env.NODE_ENV = 'development';
}
//Don't check .env file if we are in travis-ci
if(!process.env.TRAVIS){
require('dotenv').config({path: './.env'});
}
require('events').EventEmitter.prototype._maxListeners = 0;
var config = require('./config/config'),
mongoose = require('mongoose'),
var mongoose = require('mongoose'),
chalk = require('chalk'),
nodemailer = require('nodemailer');
@ -24,62 +11,74 @@ var config = require('./config/config'),
* Main application entry file.
* Please note that the order of loading is important.
*/
// Bootstrap db connection
var db = mongoose.connect(config.db.uri, config.db.options, function (err) {
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(chalk.red(err));
module.exports.bootstrap = function() {
//Don't check .env file if we are in travis-ci
if(!process.env.TRAVIS) {
require('dotenv').config({path: './.env'});
}
});
mongoose.connection.on('error', function (err) {
console.error(chalk.red('MongoDB connection error: ' + err));
process.exit(-1);
});
const smtpTransport = nodemailer.createTransport(config.mailer.options);
// verify connection configuration on startup
smtpTransport.verify(function(error, success) {
if (error) {
console.error(chalk.red('Your mail configuration is incorrect: ' + error));
process.exit(-1);
if(!process.env.NODE_ENV) {
process.env.NODE_ENV = 'development';
}
});
// Init the express application
var app = require('./config/express')(db);
var config = require('./config/config');
//Create admin account
if (process.env.CREATE_ADMIN_ACCOUNT === 'TRUE') {
var create_admin = require('./scripts/create_admin');
create_admin.run(app, db, function(err){
if(err){
console.error(chalk.red('Could not create Admin Account: ' + err));
// Bootstrap db connection
var db = mongoose.connect(config.db.uri, config.db.options, function (err) {
if (err) {
console.error(chalk.red('Could not connect to MongoDB!'));
console.log(chalk.red(err));
}
});
mongoose.connection.on('error', function (err) {
console.error(chalk.red('MongoDB connection error: ' + err));
process.exit(-1);
});
const smtpTransport = nodemailer.createTransport(config.mailer.options);
// verify connection configuration on startup
smtpTransport.verify(function(error, success) {
if (error) {
console.error(chalk.red('Your mail configuration is incorrect: ' + error));
process.exit(-1);
}
});
// Init the express application
var app = require('./config/express')(db);
//Create admin account
if (process.env.CREATE_ADMIN_ACCOUNT === 'TRUE') {
var create_admin = require('./scripts/create_admin');
create_admin.run(app, db, function(err){
if(err){
console.error(chalk.red('Could not create Admin Account: ' + err));
}
});
}
// Bootstrap passport config
require('./config/passport')();
// Start the app by listening on <port>
app.listen(config.port);
// Expose app
exports = module.exports = app;
// Logging initialization
console.log('--');
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
console.log(chalk.green('Port:\t\t\t\t' + config.port));
console.log(chalk.green('Database:\t\t\t' + config.db.uri));
console.log('--');
process.on('uncaughtException', function (err) {
console.error((new Date()).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
});
}
// Bootstrap passport config
require('./config/passport')();
// Start the app by listening on <port>
app.listen(config.port);
// Expose app
exports = module.exports = app;
// Logging initialization
console.log('--');
console.log(chalk.green('Environment:\t\t\t' + process.env.NODE_ENV));
console.log(chalk.green('Port:\t\t\t\t' + config.port));
console.log(chalk.green('Database:\t\t\t' + config.db.uri));
console.log('--');
process.on('uncaughtException', function (err) {
console.error((new Date()).toUTCString() + ' uncaughtException:', err.message);
console.error(err.stack);
process.exit(1);
});

16
start.js Normal file
View File

@ -0,0 +1,16 @@
var fs = require('fs'),
setup = require('./scripts/setup'),
server = require('./server');
//Set this to infinity to increase server capacity
require('events').EventEmitter.prototype._maxListeners = 0;
//Run setup script if no .env file is detected
if(process.stdout.isTTY) {
setup.checkENVAndRunSetup(function() {
server.bootstrap();
});
} else {
server.bootstrap();
}