forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset.js
179 lines (158 loc) · 4.25 KB
/
reset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
'use strict';
var winston = require('winston');
var nconf = require('nconf');
var async = require('async');
var db = require('./database');
var Reset = {};
Reset.reset = function (callback) {
db.init(function (err) {
if (err) {
winston.error(err.message);
process.exit();
}
if (nconf.get('t')) {
if(nconf.get('t') === true) {
resetThemes(callback);
} else {
resetTheme(nconf.get('t'), callback);
}
} else if (nconf.get('p')) {
if (nconf.get('p') === true) {
resetPlugins(callback);
} else {
resetPlugin(nconf.get('p'), callback);
}
} else if (nconf.get('w')) {
resetWidgets(callback);
} else if (nconf.get('s')) {
resetSettings(callback);
} else if (nconf.get('a')) {
require('async').series([resetWidgets, resetThemes, resetPlugins, resetSettings], function (err) {
if (!err) {
winston.info('[reset] Reset complete.');
} else {
winston.error('[reset] Errors were encountered while resetting your forum settings: %s', err.message);
}
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
} else {
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.exit();
}
});
};
function resetSettings(callback) {
var meta = require('./meta');
meta.configs.set('allowLocalLogin', 1, function (err) {
winston.info('[reset] Settings reset to default');
if (typeof callback === 'function') {
callback(err);
} else {
process.exit();
}
});
}
function resetTheme(themeId, callback) {
var meta = require('./meta');
var fs = require('fs');
fs.access('node_modules/' + themeId + '/package.json', function (err, fd) {
if (err) {
winston.warn('[reset] Theme `%s` is not installed on this forum', themeId);
process.exit();
} 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);
}
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
}
});
}
function resetThemes(callback) {
var meta = require('./meta');
meta.themes.set({
type: 'local',
id: 'nodebb-theme-persona'
}, function (err) {
winston.info('[reset] Theme reset to Persona');
if (typeof callback === 'function') {
callback(err);
} else {
process.exit(0);
}
});
}
function resetPlugin(pluginId, callback) {
var active = false;
async.waterfall([
async.apply(db.isSortedSetMember, 'plugins:active', pluginId),
function (isMember, next) {
active = isMember;
if (isMember) {
db.sortedSetRemove('plugins:active', pluginId, next);
} else {
next();
}
}
], function (err) {
if (err) {
winston.error('[reset] Could not disable plugin: %s encountered error %s', pluginId, err.message);
} else {
if (active) {
winston.info('[reset] Plugin `%s` disabled', pluginId);
} else {
winston.warn('[reset] Plugin `%s` was not active on this forum', pluginId);
winston.info('[reset] No action taken.');
}
}
if (typeof callback === 'function') {
callback();
} else {
process.exit(0);
}
});
}
function resetPlugins(callback) {
db.delete('plugins:active', function (err) {
winston.info('[reset] All Plugins De-activated');
if (typeof callback === 'function') {
callback(err);
} else {
process.exit(0);
}
});
}
function resetWidgets(callback) {
require('./widgets').reset(function (err) {
winston.info('[reset] All Widgets moved to Draft Zone');
if (typeof callback === 'function') {
callback(err);
} else {
process.exit();
}
});
}
module.exports = Reset;