diff --git a/app.js b/app.js index c7630525e7..3c83082bb8 100644 --- a/app.js +++ b/app.js @@ -21,25 +21,24 @@ require('./require-main'); -var nconf = require('nconf'); +const nconf = require('nconf'); nconf.argv().env({ separator: '__', }); -var async = require('async'); -var winston = require('winston'); -var path = require('path'); +const winston = require('winston'); +const path = require('path'); -var file = require('./src/file'); +const file = require('./src/file'); global.env = process.env.NODE_ENV || 'production'; // Alternate configuration file support -var configFile = path.resolve(__dirname, nconf.any(['config', 'CONFIG']) || 'config.json'); +const configFile = path.resolve(__dirname, nconf.any(['config', 'CONFIG']) || 'config.json'); -var configExists = file.existsSync(configFile) || (nconf.get('url') && nconf.get('secret') && nconf.get('database')); +const configExists = file.existsSync(configFile) || (nconf.get('url') && nconf.get('secret') && nconf.get('database')); -var prestart = require('./src/prestart'); +const prestart = require('./src/prestart'); prestart.loadConfig(configFile); prestart.setupWinston(); prestart.versionCheck(); @@ -60,23 +59,12 @@ if (nconf.get('setup') || nconf.get('install')) { } else if (nconf.get('upgrade')) { require('./src/cli/upgrade').upgrade(true); } else if (nconf.get('reset')) { - var options = { + require('./src/cli/reset').reset({ theme: nconf.get('t'), plugin: nconf.get('p'), widgets: nconf.get('w'), settings: nconf.get('s'), all: nconf.get('a'), - }; - - async.series([ - async.apply(require('./src/cli/reset').reset, options), - require('./src/meta/build').buildAll, - ], function (err) { - if (err) { - throw err; - } - - process.exit(0); }); } else if (nconf.get('activate')) { require('./src/cli/manage').activate(nconf.get('activate')); diff --git a/src/cli/reset.js b/src/cli/reset.js index 1e1f9a0a50..65a0e0ff5c 100644 --- a/src/cli/reset.js +++ b/src/cli/reset.js @@ -1,61 +1,65 @@ 'use strict'; require('colors'); -var path = require('path'); -var winston = require('winston'); -var async = require('async'); -var fs = require('fs'); - -var db = require('../database'); -var events = require('../events'); -var meta = require('../meta'); -var plugins = require('../plugins'); -var widgets = require('../widgets'); -var privileges = require('../privileges'); - -var dirname = require('./paths').baseDir; - -var themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; -var pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; - -exports.reset = function (options, callback) { - var map = { - theme: function (next) { - var themeId = options.theme; +const path = require('path'); +const winston = require('winston'); +const async = require('async'); +const fs = require('fs'); +const util = require('util'); + +const fsAccessAsync = util.promisify(fs.access); + +const db = require('../database'); +const events = require('../events'); +const meta = require('../meta'); +const plugins = require('../plugins'); +const widgets = require('../widgets'); +const privileges = require('../privileges'); + +const dirname = require('./paths').baseDir; + +const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/; +const pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; + +exports.reset = async function (options) { + const map = { + theme: async function () { + let themeId = options.theme; if (themeId === true) { - resetThemes(next); + await resetThemes(); } else { if (!themeNamePattern.test(themeId)) { // Allow omission of `nodebb-theme-` themeId = 'nodebb-theme-' + themeId; } - resetTheme(themeId, next); + await resetTheme(themeId); } }, - plugin: function (next) { - var pluginId = options.plugin; + plugin: async function () { + let pluginId = options.plugin; if (pluginId === true) { - resetPlugins(next); + await resetPlugins(); } else { if (!pluginNamePattern.test(pluginId)) { // Allow omission of `nodebb-plugin-` pluginId = 'nodebb-plugin-' + pluginId; } - resetPlugin(pluginId, next); + await resetPlugin(pluginId); } }, widgets: resetWidgets, settings: resetSettings, - all: function (next) { - async.series([resetWidgets, resetThemes, resetPlugins, resetSettings], next); + all: async function () { + await resetWidgets(); + await resetThemes(); + await resetPlugin(); + await resetSettings(); }, }; - var tasks = Object.keys(map) - .filter(function (x) { return options[x]; }) - .map(function (x) { return map[x]; }); + const tasks = Object.keys(map).filter(x => options[x]).map(x => map[x]); if (!tasks.length) { console.log([ @@ -75,58 +79,48 @@ exports.reset = function (options, callback) { process.exit(0); } - async.series([db.init].concat(tasks), function (err) { - if (err) { - winston.error('[reset] Errors were encountered during reset -- ' + err.message); - return callback(err); + try { + await db.init(); + for (const task of tasks) { + /* eslint-disable no-await-in-loop */ + await task(); } - winston.info('[reset] Reset complete'); - callback(); - }); + await require('../meta/build').buildAll(); + process.exit(0); + } catch (err) { + winston.error('[reset] Errors were encountered during reset -- ' + err.message); + throw err; + } }; -function resetSettings(callback) { - privileges.global.give(['local:login'], 'registered-users', function (err) { - if (err) { - return callback(err); - } - winston.info('[reset] registered-users given login privilege'); - winston.info('[reset] Settings reset to default'); - callback(); - }); +async function resetSettings() { + await privileges.global.give(['local:login'], 'registered-users'); + winston.info('[reset] registered-users given login privilege'); + winston.info('[reset] Settings reset to default'); } -function resetTheme(themeId, callback) { - fs.access(path.join(dirname, 'node_modules', themeId, 'package.json'), function (err) { - if (err) { - winston.warn('[reset] Theme `%s` is not installed on this forum', themeId); - callback(new Error('theme-not-found')); - } else { - meta.themes.set({ - type: 'local', - id: themeId, - }, function (err) { - if (err) { - winston.warn('[reset] Failed to reset theme to ' + themeId); - } else { - winston.info('[reset] Theme reset to ' + themeId); - } +async function resetTheme(themeId) { + try { + await fsAccessAsync(path.join(dirname, 'node_modules', themeId, 'package.json')); + } catch (err) { + winston.warn('[reset] Theme `%s` is not installed on this forum', themeId); + throw new Error('theme-not-found'); + } + await resetThemeTo(themeId); +} - callback(); - }); - } - }); +async function resetThemes() { + await resetThemeTo('nodebb-theme-persona'); } -function resetThemes(callback) { - meta.themes.set({ +async function resetThemeTo(themeId) { + await meta.themes.set({ type: 'local', - id: 'nodebb-theme-persona', - }, function (err) { - winston.info('[reset] Theme reset to Persona'); - callback(err); + id: themeId, }); + await meta.configs.set('bootswatchSkin', ''); + winston.info('[reset] Theme reset to ' + themeId + ' and default skin'); } function resetPlugin(pluginId, callback) {