Files
NodeBB/src/cli/reset.js

175 lines
4.4 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');
2017-03-30 03:17:45 -06:00
var path = require('path');
2015-09-17 21:54:12 -04:00
var winston = require('winston');
2015-11-28 23:22:39 -05:00
var async = require('async');
var fs = require('fs');
2015-09-17 21:54:12 -04:00
var db = require('../database');
var events = require('../events');
var meta = require('../meta');
var plugins = require('../plugins');
var widgets = require('../widgets');
2015-09-17 21:54:12 -04:00
var dirname = require('./paths').baseDir;
2015-09-17 21:54:12 -04:00
exports.reset = function (options, callback) {
var map = {
theme: function (next) {
var themeId = options.theme;
if (themeId === true) {
resetThemes(next);
} else {
if (!themeId.startsWith('nodebb-theme-')) {
// Allow omission of `nodebb-theme-`
themeId = 'nodebb-theme-' + themeId;
}
resetTheme(themeId, next);
}
},
plugin: function (next) {
var pluginId = options.plugin;
if (pluginId === true) {
resetPlugins(next);
2015-09-17 21:54:12 -04:00
} else {
if (!pluginId.startsWith('nodebb-plugin-')) {
// Allow omission of `nodebb-plugin-`
pluginId = 'nodebb-plugin-' + pluginId;
}
resetPlugin(pluginId, next);
2015-09-17 21:54:12 -04:00
}
},
widgets: resetWidgets,
settings: resetSettings,
all: function (next) {
async.series([resetWidgets, resetThemes, resetPlugins, resetSettings], next);
},
};
var tasks = Object.keys(map)
.filter(function (x) { return options[x]; })
.map(function (x) { return map[x]; });
if (!tasks.length) {
process.stdout.write('\nNodeBB Reset\n'.bold);
process.stdout.write('No arguments passed in, so nothing was reset.\n\n'.yellow);
process.stdout.write('Use ./nodebb reset ' + '{-t|-p|-w|-s|-a}\n'.red);
process.stdout.write(' -t\tthemes\n');
process.stdout.write(' -p\tplugins\n');
process.stdout.write(' -w\twidgets\n');
process.stdout.write(' -s\tsettings\n');
process.stdout.write(' -a\tall of the above\n');
process.stdout.write('\nPlugin and theme reset flags (-p & -t) can take a single argument\n');
process.stdout.write(' e.g. ./nodebb reset -p nodebb-plugin-mentions, ./nodebb reset -t nodebb-theme-persona\n');
process.stdout.write(' Prefix is optional, e.g. ./nodebb reset -p markdown, ./nodebb reset -t persona\n');
process.exit(0);
}
async.series([db.init].concat(tasks), function (err) {
if (err) {
winston.error('[reset] Errors were encountered during reset', err);
throw err;
2015-09-17 21:54:12 -04:00
}
winston.info('[reset] Reset complete');
callback();
2015-09-17 21:54:12 -04:00
});
};
function resetSettings(callback) {
meta.configs.set('allowLocalLogin', 1, function (err) {
2015-09-17 21:54:12 -04:00
winston.info('[reset] Settings reset to default');
callback(err);
2015-09-17 21:54:12 -04:00
});
}
2016-12-05 10:23:28 -05:00
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',
2017-02-17 19:31:21 -07:00
id: themeId,
}, function (err) {
2016-08-16 19:46:59 +02:00
if (err) {
winston.warn('[reset] Failed to reset theme to ' + themeId);
} else {
winston.info('[reset] Theme reset to ' + themeId);
}
callback();
2017-02-18 01:27:46 -07:00
});
}
});
}
2015-09-17 21:54:12 -04:00
function resetThemes(callback) {
meta.themes.set({
type: 'local',
2017-02-17 19:31:21 -07:00
id: 'nodebb-theme-persona',
}, function (err) {
2015-09-17 21:54:12 -04:00
winston.info('[reset] Theme reset to Persona');
callback(err);
2015-09-17 21:54:12 -04:00
});
}
2016-12-05 10:23:28 -05:00
function resetPlugin(pluginId, callback) {
2015-11-28 23:22:39 -05:00
var active = false;
async.waterfall([
async.apply(db.isSortedSetMember, 'plugins:active', pluginId),
function (isMember, next) {
2015-11-28 23:22:39 -05:00
active = isMember;
if (isMember) {
db.sortedSetRemove('plugins:active', pluginId, next);
} else {
next();
}
2017-02-17 19:31:21 -07:00
},
2017-06-01 16:24:40 -04:00
function (next) {
events.log({
type: 'plugin-deactivate',
text: pluginId,
}, next);
},
], function (err) {
2015-09-17 21:54:12 -04:00
if (err) {
winston.error('[reset] Could not disable plugin: %s encountered error %s', pluginId, err);
2017-02-18 14:32:35 -07:00
} else if (active) {
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.');
err = new Error('plugin-not-active');
2015-09-17 21:54:12 -04:00
}
callback(err);
2015-09-17 21:54:12 -04:00
});
}
function resetPlugins(callback) {
db.delete('plugins:active', function (err) {
2015-09-17 21:54:12 -04:00
winston.info('[reset] All Plugins De-activated');
callback(err);
2015-09-17 21:54:12 -04:00
});
}
function resetWidgets(callback) {
async.waterfall([
plugins.reload,
widgets.reset,
function (next) {
winston.info('[reset] All Widgets moved to Draft Zone');
next();
},
], callback);
2015-09-17 22:15:28 -04:00
}