diff --git a/bin/cli-serve b/bin/cli-serve index 63eda3af2..f2a6d3592 100755 --- a/bin/cli-serve +++ b/bin/cli-serve @@ -2,10 +2,13 @@ const program = require('./commander'); const app = require('../app'); +const debug = require('debug')('talk:cli:serve'); +const errors = require('../errors'); const {createServer} = require('http'); const scraper = require('../services/scraper'); const mailer = require('../services/mailer'); const MigrationService = require('../services/migration'); +const SetupService = require('../services/setup'); const kue = require('../services/kue'); const mongoose = require('../services/mongoose'); const util = require('./util'); @@ -92,12 +95,41 @@ async function startApp(program) { try { - // Verify that the minimum migration version is met. - await MigrationService.verify(); + // Check to see if the application is installed. If the application + // has been installed, then it will throw errors.ErrSettingsNotInit, this + // just means we don't have to check that the migrations have run. + await SetupService.isAvailable(); - } catch(e) { - console.error(e); - process.exit(1); + debug('setup is currently available, migrations not being checked'); + + } catch (e) { + + // Check the error. + switch (e) { + case errors.ErrInstallLock, errors.ErrSettingsInit: + + debug('setup is not currently available, migrations now being checked'); + + // The error was expected, just continue. + break; + default: + + // The error was not expected, throw the error! + throw e; + } + + // Now try and check the migration status. + try { + + // Verify that the minimum migration version is met. + await MigrationService.verify(); + + } catch (e) { + console.error(e); + process.exit(1); + } + + debug('migrations do not have to be run'); } /** diff --git a/services/setup.js b/services/setup.js index 233021c17..053a85789 100644 --- a/services/setup.js +++ b/services/setup.js @@ -1,5 +1,6 @@ const UsersService = require('./users'); const SettingsService = require('./settings'); +const MigrationService = require('./migration'); const SettingsModel = require('../models/setting'); const errors = require('../errors'); const { @@ -15,34 +16,32 @@ module.exports = class SetupService { /** * This returns a promise which resolves if the setup is available. */ - static isAvailable() { + static async isAvailable() { // Check if we have an install lock present. if (INSTALL_LOCK) { - return Promise.reject(errors.ErrInstallLock); + throw errors.ErrInstallLock; } - // Get the current settings, we are expecing an error here. - return SettingsService - .retrieve() - .then(() => { + try { - // We should NOT have gotten a settings object, this means that the - // application is already setup. Error out here. - return Promise.reject(errors.ErrSettingsInit); + // Get the current settings, we are expecing an error here. + await SettingsService.retrieve(); + + // We should NOT have gotten a settings object, this means that the + // application is already setup. Error out here. + throw errors.ErrSettingsInit; + } catch (e) { - }) - .catch((err) => { + // If the error is `not init`, then we're good, otherwise, it's something + // else. + if (e !== errors.ErrSettingsNotInit) { + throw e; + } - // If the error is `not init`, then we're good, otherwise, it's something - // else. - if (err !== errors.ErrSettingsNotInit) { - return Promise.reject(err); - } - - // Allow the request to keep going here. - return; - }); + // Allow the request to keep going here. + return; + } } /** @@ -69,35 +68,34 @@ module.exports = class SetupService { /** * This will perform the setup. */ - static setup({settings, user: {email, password, username}}) { + static async setup({settings, user: {email, password, username}}) { // Validate the settings first. - return SetupService - .validate({settings, user: {email, password, username}}) - .then(() => { - return SettingsService.update(settings); - }) - .then((settings) => { + await SetupService.validate({settings, user: {email, password, username}}); - // Settings are created! Create the user. + // Get the migrations to run. + let migrations = await MigrationService.listPending(); - // Create the user. - return UsersService - .createLocalUser(email, password, username) + // Perform all migrations. + await MigrationService.run(migrations); - // Grant them administrative privileges and confirm the email account. - .then((user) => { + settings = await SettingsService.update(settings); - return Promise.all([ - UsersService.addRoleToUser(user.id, 'ADMIN'), - UsersService.confirmEmail(user.id, email) - ]) - .then(() => ({ - settings, - user - })); - }); - }); + // Settings are created! Create the user. + + // Create the user. + let user = await UsersService.createLocalUser(email, password, username); + + // Grant them administrative privileges and confirm the email account. + await Promise.all([ + UsersService.addRoleToUser(user.id, 'ADMIN'), + UsersService.confirmEmail(user.id, email) + ]); + + return { + settings, + user + }; } };