feat: resetting theme will reset skin

async/await cli/reset
v1.18.x
Barış Soner Uşaklı 5 years ago
parent 565f9726f7
commit 03827fa695

@ -21,25 +21,24 @@
require('./require-main'); require('./require-main');
var nconf = require('nconf'); const nconf = require('nconf');
nconf.argv().env({ nconf.argv().env({
separator: '__', separator: '__',
}); });
var async = require('async'); const winston = require('winston');
var winston = require('winston'); const path = require('path');
var path = require('path');
var file = require('./src/file'); const file = require('./src/file');
global.env = process.env.NODE_ENV || 'production'; global.env = process.env.NODE_ENV || 'production';
// Alternate configuration file support // 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.loadConfig(configFile);
prestart.setupWinston(); prestart.setupWinston();
prestart.versionCheck(); prestart.versionCheck();
@ -60,23 +59,12 @@ if (nconf.get('setup') || nconf.get('install')) {
} else if (nconf.get('upgrade')) { } else if (nconf.get('upgrade')) {
require('./src/cli/upgrade').upgrade(true); require('./src/cli/upgrade').upgrade(true);
} else if (nconf.get('reset')) { } else if (nconf.get('reset')) {
var options = { require('./src/cli/reset').reset({
theme: nconf.get('t'), theme: nconf.get('t'),
plugin: nconf.get('p'), plugin: nconf.get('p'),
widgets: nconf.get('w'), widgets: nconf.get('w'),
settings: nconf.get('s'), settings: nconf.get('s'),
all: nconf.get('a'), 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')) { } else if (nconf.get('activate')) {
require('./src/cli/manage').activate(nconf.get('activate')); require('./src/cli/manage').activate(nconf.get('activate'));

@ -1,61 +1,65 @@
'use strict'; 'use strict';
require('colors'); require('colors');
var path = require('path'); const path = require('path');
var winston = require('winston'); const winston = require('winston');
var async = require('async'); const async = require('async');
var fs = require('fs'); const fs = require('fs');
const util = require('util');
var db = require('../database');
var events = require('../events'); const fsAccessAsync = util.promisify(fs.access);
var meta = require('../meta');
var plugins = require('../plugins'); const db = require('../database');
var widgets = require('../widgets'); const events = require('../events');
var privileges = require('../privileges'); const meta = require('../meta');
const plugins = require('../plugins');
var dirname = require('./paths').baseDir; const widgets = require('../widgets');
const privileges = require('../privileges');
var themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/;
var pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/; const dirname = require('./paths').baseDir;
exports.reset = function (options, callback) { const themeNamePattern = /^(@.*?\/)?nodebb-theme-.*$/;
var map = { const pluginNamePattern = /^(@.*?\/)?nodebb-(theme|plugin|widget|rewards)-.*$/;
theme: function (next) {
var themeId = options.theme; exports.reset = async function (options) {
const map = {
theme: async function () {
let themeId = options.theme;
if (themeId === true) { if (themeId === true) {
resetThemes(next); await resetThemes();
} else { } else {
if (!themeNamePattern.test(themeId)) { if (!themeNamePattern.test(themeId)) {
// Allow omission of `nodebb-theme-` // Allow omission of `nodebb-theme-`
themeId = 'nodebb-theme-' + themeId; themeId = 'nodebb-theme-' + themeId;
} }
resetTheme(themeId, next); await resetTheme(themeId);
} }
}, },
plugin: function (next) { plugin: async function () {
var pluginId = options.plugin; let pluginId = options.plugin;
if (pluginId === true) { if (pluginId === true) {
resetPlugins(next); await resetPlugins();
} else { } else {
if (!pluginNamePattern.test(pluginId)) { if (!pluginNamePattern.test(pluginId)) {
// Allow omission of `nodebb-plugin-` // Allow omission of `nodebb-plugin-`
pluginId = 'nodebb-plugin-' + pluginId; pluginId = 'nodebb-plugin-' + pluginId;
} }
resetPlugin(pluginId, next); await resetPlugin(pluginId);
} }
}, },
widgets: resetWidgets, widgets: resetWidgets,
settings: resetSettings, settings: resetSettings,
all: function (next) { all: async function () {
async.series([resetWidgets, resetThemes, resetPlugins, resetSettings], next); await resetWidgets();
await resetThemes();
await resetPlugin();
await resetSettings();
}, },
}; };
var tasks = Object.keys(map) const tasks = Object.keys(map).filter(x => options[x]).map(x => map[x]);
.filter(function (x) { return options[x]; })
.map(function (x) { return map[x]; });
if (!tasks.length) { if (!tasks.length) {
console.log([ console.log([
@ -75,58 +79,48 @@ exports.reset = function (options, callback) {
process.exit(0); process.exit(0);
} }
async.series([db.init].concat(tasks), function (err) { try {
if (err) { await db.init();
winston.error('[reset] Errors were encountered during reset -- ' + err.message); for (const task of tasks) {
return callback(err); /* eslint-disable no-await-in-loop */
await task();
} }
winston.info('[reset] Reset complete'); 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) { async function resetSettings() {
privileges.global.give(['local:login'], 'registered-users', function (err) { await privileges.global.give(['local:login'], 'registered-users');
if (err) {
return callback(err);
}
winston.info('[reset] registered-users given login privilege'); winston.info('[reset] registered-users given login privilege');
winston.info('[reset] Settings reset to default'); winston.info('[reset] Settings reset to default');
callback();
});
} }
function resetTheme(themeId, callback) { async function resetTheme(themeId) {
fs.access(path.join(dirname, 'node_modules', themeId, 'package.json'), function (err) { try {
if (err) { await fsAccessAsync(path.join(dirname, 'node_modules', themeId, 'package.json'));
} catch (err) {
winston.warn('[reset] Theme `%s` is not installed on this forum', themeId); winston.warn('[reset] Theme `%s` is not installed on this forum', themeId);
callback(new Error('theme-not-found')); throw 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);
} }
await resetThemeTo(themeId);
}
callback(); async function resetThemes() {
}); await resetThemeTo('nodebb-theme-persona');
}
});
} }
function resetThemes(callback) { async function resetThemeTo(themeId) {
meta.themes.set({ await meta.themes.set({
type: 'local', type: 'local',
id: 'nodebb-theme-persona', id: themeId,
}, function (err) {
winston.info('[reset] Theme reset to Persona');
callback(err);
}); });
await meta.configs.set('bootswatchSkin', '');
winston.info('[reset] Theme reset to ' + themeId + ' and default skin');
} }
function resetPlugin(pluginId, callback) { function resetPlugin(pluginId, callback) {

Loading…
Cancel
Save