Files
NodeBB/src/cli/reset.js

151 lines
3.9 KiB
JavaScript
Raw Normal View History

2015-09-17 21:54:12 -04:00
'use strict';
2017-06-01 15:46:35 -04:00
require('colors');
const path = require('path');
const winston = require('winston');
const fs = require('fs');
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 { paths, pluginNamePattern, themeNamePattern } = require('../constants');
exports.reset = async function (options) {
const map = {
theme: async function () {
let themeId = options.theme;
if (themeId === true) {
await resetThemes();
} else {
2018-06-09 16:26:28 -06:00
if (!themeNamePattern.test(themeId)) {
// Allow omission of `nodebb-theme-`
themeId = 'nodebb-theme-' + themeId;
}
await resetTheme(themeId);
}
},
plugin: async function () {
let pluginId = options.plugin;
if (pluginId === true) {
await resetPlugins();
2015-09-17 21:54:12 -04:00
} else {
2018-06-09 16:26:28 -06:00
if (!pluginNamePattern.test(pluginId)) {
// Allow omission of `nodebb-plugin-`
pluginId = 'nodebb-plugin-' + pluginId;
}
await resetPlugin(pluginId);
2015-09-17 21:54:12 -04:00
}
},
widgets: resetWidgets,
settings: resetSettings,
all: async function () {
await resetWidgets();
await resetThemes();
await resetPlugin();
await resetSettings();
},
};
const tasks = Object.keys(map).filter(x => options[x]).map(x => map[x]);
if (!tasks.length) {
console.log([
'No arguments passed in, so nothing was reset.\n'.yellow,
'Use ./nodebb reset ' + '{-t|-p|-w|-s|-a}'.red,
' -t\tthemes',
' -p\tplugins',
' -w\twidgets',
' -s\tsettings',
' -a\tall of the above',
'',
'Plugin and theme reset flags (-p & -t) can take a single argument',
' e.g. ./nodebb reset -p nodebb-plugin-mentions, ./nodebb reset -t nodebb-theme-persona',
' Prefix is optional, e.g. ./nodebb reset -p markdown, ./nodebb reset -t persona',
].join('\n'));
process.exit(0);
}
try {
await db.init();
for (const task of tasks) {
/* eslint-disable no-await-in-loop */
await task();
2015-09-17 21:54:12 -04:00
}
winston.info('[reset] Reset complete. Please run `./nodebb build` to rebuild assets.');
process.exit(0);
} catch (err) {
winston.error('[reset] Errors were encountered during reset -- ' + err.message);
process.exit(1);
}
2015-09-17 21:54:12 -04:00
};
async function resetSettings() {
await privileges.global.give(['groups:local:login'], 'registered-users');
winston.info('[reset] registered-users given login privilege');
winston.info('[reset] Settings reset to default');
2015-09-17 21:54:12 -04:00
}
async function resetTheme(themeId) {
try {
await fs.promises.access(path.join(paths.nodeModules, 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);
}
2016-08-16 19:46:59 +02:00
async function resetThemes() {
await resetThemeTo('nodebb-theme-persona');
}
async function resetThemeTo(themeId) {
await meta.themes.set({
2015-09-17 21:54:12 -04:00
type: 'local',
id: themeId,
2015-09-17 21:54:12 -04:00
});
await meta.configs.set('bootswatchSkin', '');
winston.info('[reset] Theme reset to ' + themeId + ' and default skin');
2015-09-17 21:54:12 -04:00
}
2019-10-16 20:50:38 -04:00
async function resetPlugin(pluginId) {
try {
const isActive = await db.isSortedSetMember('plugins:active', pluginId);
if (isActive) {
await db.sortedSetRemove('plugins:active', pluginId);
}
2015-11-28 23:22:39 -05:00
2019-10-16 20:50:38 -04:00
await events.log({
type: 'plugin-deactivate',
text: pluginId,
});
2015-11-28 23:22:39 -05:00
2019-10-16 20:50:38 -04:00
if (isActive) {
2017-02-18 14:32:35 -07:00
winston.info('[reset] Plugin `%s` disabled', pluginId);
2015-09-17 21:54:12 -04:00
} else {
2017-02-18 14:32:35 -07:00
winston.warn('[reset] Plugin `%s` was not active on this forum', pluginId);
winston.info('[reset] No action taken.');
2015-09-17 21:54:12 -04:00
}
2019-10-16 20:50:38 -04:00
} catch (err) {
2020-11-26 13:56:34 -05:00
winston.error('[reset] Could not disable plugin: ' + pluginId + ' encountered error %s\n' + err.stack);
2019-10-16 20:50:38 -04:00
throw err;
}
2015-09-17 21:54:12 -04:00
}
2019-10-16 20:50:38 -04:00
async function resetPlugins() {
await db.delete('plugins:active');
winston.info('[reset] All Plugins De-activated');
2015-09-17 21:54:12 -04:00
}
2019-10-16 20:50:38 -04:00
async function resetWidgets() {
await plugins.reload();
await widgets.reset();
winston.info('[reset] All Widgets moved to Draft Zone');
2015-09-17 22:15:28 -04:00
}