forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodebb
executable file
·482 lines (441 loc) · 14 KB
/
nodebb
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
#!/usr/bin/env node
'use strict';
try {
require('colors');
var cproc = require('child_process');
var args = require('minimist')(process.argv.slice(2));
var fs = require('fs');
var path = require('path');
var request = require('request');
var semver = require('semver');
var prompt = require('prompt');
var async = require('async');
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
process.stdout.write('NodeBB could not be started because it\'s dependencies have not been installed.\n');
process.stdout.write('Please ensure that you have executed "npm install --production" prior to running NodeBB.\n\n');
process.stdout.write('For more information, please see: https://docs.nodebb.org/en/latest/installing/os.html\n\n');
process.stdout.write('Could not start: ' + e.code + '\n');
process.exit(1);
}
}
if (args.dev) {
process.env.NODE_ENV = 'development';
}
function getRunningPid(callback) {
fs.readFile(__dirname + '/pidfile', {
encoding: 'utf-8'
}, function (err, pid) {
if (err) {
return callback(err);
}
try {
process.kill(parseInt(pid, 10), 0);
callback(null, parseInt(pid, 10));
} catch(e) {
callback(e);
}
});
}
function getCurrentVersion(callback) {
fs.readFile(path.join(__dirname, 'package.json'), { encoding: 'utf-8' }, function (err, pkg) {
if (err) {
return callback(err);
}
try {
pkg = JSON.parse(pkg);
return callback(null, pkg.version);
} catch(err) {
return callback(err);
}
});
}
function fork(args) {
return cproc.fork('app.js', args, {
cwd: __dirname,
silent: false
});
}
function getInstalledPlugins(callback) {
async.parallel({
files: async.apply(fs.readdir, path.join(__dirname, 'node_modules')),
deps: async.apply(fs.readFile, path.join(__dirname, 'package.json'), { encoding: 'utf-8' })
}, function (err, payload) {
if (err) {
return callback(err);
}
var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w\-]+$/,
moduleName, isGitRepo;
payload.files = payload.files.filter(function (file) {
return isNbbModule.test(file);
});
try {
payload.deps = JSON.parse(payload.deps).dependencies;
payload.bundled = [];
payload.installed = [];
} catch (err) {
return callback(err);
}
for (moduleName in payload.deps) {
if (isNbbModule.test(moduleName)) {
payload.bundled.push(moduleName);
}
}
// Whittle down deps to send back only extraneously installed plugins/themes/etc
payload.files.forEach(function (moduleName) {
try {
fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git'));
isGitRepo = true;
} catch(e) {
isGitRepo = false;
}
if (
payload.files.indexOf(moduleName) !== -1 && // found in `node_modules/`
payload.bundled.indexOf(moduleName) === -1 && // not found in `package.json`
!fs.lstatSync(path.join(__dirname, 'node_modules/' + moduleName)).isSymbolicLink() && // is not a symlink
!isGitRepo // .git/ does not exist, so it is not a git repository
) {
payload.installed.push(moduleName);
}
});
getModuleVersions(payload.installed, callback);
});
}
function getModuleVersions(modules, callback) {
var versionHash = {};
async.eachLimit(modules, 50, function (module, next) {
fs.readFile(path.join(__dirname, 'node_modules/' + module + '/package.json'), { encoding: 'utf-8' }, function (err, pkg) {
if (err) {
return next(err);
}
try {
pkg = JSON.parse(pkg);
versionHash[module] = pkg.version;
next();
} catch (err) {
next(err);
}
});
}, function (err) {
callback(err, versionHash);
});
}
function checkPlugins(standalone, callback) {
if (standalone) {
process.stdout.write('Checking installed plugins and themes for updates... ');
}
async.waterfall([
async.apply(async.parallel, {
plugins: async.apply(getInstalledPlugins),
version: async.apply(getCurrentVersion)
}),
function (payload, next) {
var toCheck = Object.keys(payload.plugins);
if (!toCheck.length) {
process.stdout.write('OK'.green + '\n'.reset);
return next(null, []); // no extraneous plugins installed
}
request({
method: 'GET',
url: 'https://packages.nodebb.org/api/v1/suggest?version=' + payload.version + '&package[]=' + toCheck.join('&package[]='),
json: true
}, function (err, res, body) {
if (err) {
process.stdout.write('error'.red + '\n'.reset);
return next(err);
}
process.stdout.write('OK'.green + '\n'.reset);
if (!Array.isArray(body) && toCheck.length === 1) {
body = [body];
}
var current, suggested,
upgradable = body.map(function (suggestObj) {
current = payload.plugins[suggestObj.package];
suggested = suggestObj.version;
if (suggestObj.code === 'match-found' && semver.gt(suggested, current)) {
return {
name: suggestObj.package,
current: current,
suggested: suggested
};
} else {
return null;
}
}).filter(Boolean);
next(null, upgradable);
});
}
], callback);
}
function upgradePlugins(callback) {
var standalone = false;
if (typeof callback !== 'function') {
callback = function () {};
standalone = true;
}
checkPlugins(standalone, function (err, found) {
if (err) {
process.stdout.write('\Warning'.yellow + ': An unexpected error occured when attempting to verify plugin upgradability\n'.reset);
return callback(err);
}
if (found && found.length) {
process.stdout.write('\nA total of ' + String(found.length).bold + ' package(s) can be upgraded:\n');
found.forEach(function (suggestObj) {
process.stdout.write(' * '.yellow + suggestObj.name.reset + ' (' + suggestObj.current.yellow + ' -> '.reset + suggestObj.suggested.green + ')\n'.reset);
});
process.stdout.write('\n');
} else {
if (standalone) {
process.stdout.write('\nAll packages up-to-date!'.green + '\n'.reset);
}
return callback();
}
prompt.message = '';
prompt.delimiter = '';
prompt.start();
prompt.get({
name: 'upgrade',
description: 'Proceed with upgrade (y|n)?'.reset,
type: 'string'
}, function (err, result) {
if (err) {
return callback(err);
}
if (['y', 'Y', 'yes', 'YES'].indexOf(result.upgrade) !== -1) {
process.stdout.write('\nUpgrading packages...');
var args = ['i'];
found.forEach(function (suggestObj) {
args.push(suggestObj.name + '@' + suggestObj.suggested);
});
cproc.execFile((process.platform === 'win32') ? 'npm.cmd' : 'npm', args, { stdio: 'ignore' }, function (err) {
if (!err) {
process.stdout.write(' OK\n'.green);
}
callback(err);
});
} else {
process.stdout.write('\nPackage upgrades skipped'.yellow + '. Check for upgrades at any time by running "'.reset + './nodebb upgrade-plugins'.green + '".\n'.reset);
callback();
}
});
});
}
var commands = {
status: {
description: 'View the status of the NodeBB server',
usage: 'Usage: ' + './nodebb status'.yellow,
handler: function () {
getRunningPid(function (err, pid) {
if (!err) {
process.stdout.write('\nNodeBB Running '.bold + '(pid '.cyan + pid.toString().cyan + ')\n'.cyan);
process.stdout.write('\t"' + './nodebb stop'.yellow + '" to stop the NodeBB server\n');
process.stdout.write('\t"' + './nodebb log'.yellow + '" to view server output\n');
process.stdout.write('\t"' + './nodebb restart'.yellow + '" to restart NodeBB\n\n');
} else {
process.stdout.write('\nNodeBB is not running\n'.bold);
process.stdout.write('\t"' + './nodebb start'.yellow + '" to launch the NodeBB server\n\n'.reset);
}
});
},
},
start: {
description: 'Start the NodeBB server',
usage: 'Usage: ' + './nodebb start'.yellow,
handler: function () {
process.stdout.write('\nStarting NodeBB\n'.bold);
process.stdout.write(' "' + './nodebb stop'.yellow + '" to stop the NodeBB server\n');
process.stdout.write(' "' + './nodebb log'.yellow + '" to view server output\n');
process.stdout.write(' "' + './nodebb restart'.yellow + '" to restart NodeBB\n\n'.reset);
// Spawn a new NodeBB process
cproc.fork(__dirname + '/loader.js', {
env: process.env
});
},
},
stop: {
description: 'Stop the NodeBB server',
usage: 'Usage: ' + './nodebb stop'.yellow,
handler: function () {
getRunningPid(function (err, pid) {
if (!err) {
process.kill(pid, 'SIGTERM');
process.stdout.write('Stopping NodeBB. Goodbye!\n');
} else {
process.stdout.write('NodeBB is already stopped.\n');
}
});
},
},
restart: {
description: 'Restart the NodeBB server',
usage: 'Usage: ' + './nodebb restart'.yellow,
handler: function () {
getRunningPid(function (err, pid) {
if (!err) {
process.kill(pid, 'SIGHUP');
process.stdout.write('\nRestarting NodeBB\n'.bold);
} else {
process.stdout.write('NodeBB could not be restarted, as a running instance could not be found.\n');
}
});
},
},
log: {
description: 'Open the output log (useful for debugging)',
usage: 'Usage: ' + './nodebb log'.yellow,
handler: function () {
process.stdout.write('\nHit '.red + 'Ctrl-C '.bold + 'to exit'.red);
process.stdout.write('\n\n'.reset);
cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname,
stdio: 'inherit'
});
},
},
slog: {
description: 'Start the NodeBB server and view the live output log',
usage: 'Usage: ' + './nodebb slog'.yellow,
handler: function () {
process.stdout.write('\nStarting NodeBB with logging output\n'.bold);
process.stdout.write('\nHit '.red + 'Ctrl-C '.bold + 'to exit'.red);
process.stdout.write('\n\n'.reset);
// Spawn a new NodeBB process
cproc.fork(__dirname + '/loader.js', {
env: process.env
});
cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname,
stdio: 'inherit'
});
},
},
dev: {
description: 'Start NodeBB in verbose development mode',
usage: 'Usage: ' + './nodebb dev'.yellow,
handler: function () {
process.env.NODE_ENV = 'development';
cproc.fork(__dirname + '/loader.js', ['--no-daemon', '--no-silent'], {
env: process.env
});
},
},
build: {
description: 'Compile static assets (CSS, Javascript, etc)',
usage: 'Usage: ' + './nodebb build'.yellow + ' [js,clientCSS,acpCSS,tpl,lang]'.red + '\n' +
' e.g. ' + './nodebb build js,tpl'.yellow + '\tbuilds JS and templates\n' +
' ' + './nodebb build'.yellow + '\t\tbuilds all targets\n',
handler: function () {
var arr = ['--build'].concat(process.argv.slice(3));
fork(arr);
},
},
setup: {
description: 'Run the NodeBB setup script',
usage: 'Usage: ' + './nodebb setup'.yellow,
handler: function () {
var arr = ['--setup'].concat(process.argv.slice(3));
fork(arr);
},
},
reset: {
description: 'Disable plugins and restore the default theme',
usage: 'Usage: ' + './nodebb reset '.yellow + '{-t|-p|-w|-s|-a}'.red + '\n' +
' -t <theme>\tuse specified theme\n' +
' -p <plugin>\tdisable specified plugin\n' +
'\n' +
' -t\t\tuse default theme\n' +
' -p\t\tdisable all but core plugins\n' +
' -w\t\twidgets\n' +
' -s\t\tsettings\n' +
' -a\t\tall of the above\n',
handler: function () {
var arr = ['--reset'].concat(process.argv.slice(3));
fork(arr);
},
},
activate: {
description: 'Activate a plugin for the next startup of NodeBB',
usage: 'Usage: ' + './nodebb activate <plugin>'.yellow,
handler: function () {
var arr = ['--activate=' + args._[1]].concat(process.argv.slice(4));
fork(arr);
},
},
plugins: {
description: 'List all installed plugins',
usage: 'Usage: ' + './nodebb plugins'.yellow,
handler: function () {
var arr = ['--plugins'].concat(process.argv.slice(3));
fork(arr);
},
},
upgrade: {
description: 'Run NodeBB upgrade scripts, ensure packages are up-to-date',
usage: 'Usage: ' + './nodebb upgrade'.yellow,
handler: function () {
async.series([
function (next) {
process.stdout.write('1. '.bold + 'Bringing base dependencies up to date... '.yellow);
cproc.exec('npm i --production', { cwd: __dirname, stdio: 'ignore' }, next);
},
function (next) {
process.stdout.write('OK\n'.green);
process.stdout.write('2. '.bold + 'Checking installed plugins for updates... '.yellow);
upgradePlugins(next);
},
function (next) {
process.stdout.write('3. '.bold + 'Updating NodeBB data store schema...\n'.yellow);
var arr = ['--upgrade'].concat(process.argv.slice(3));
var upgradeProc = fork(arr);
upgradeProc.on('close', next);
}
], function (err) {
if (err) {
process.stdout.write('\nError'.red + ': ' + err.message + '\n');
} else {
var message = 'NodeBB Upgrade Complete!';
// some consoles will return undefined/zero columns, so just use 2 spaces in upgrade script if we can't get our column count
var columns = process.stdout.columns;
var spaces = columns ? new Array(Math.floor(columns / 2) - (message.length / 2) + 1).join(' ') : " ";
process.stdout.write('OK\n'.green);
process.stdout.write('\n' + spaces + message.green.bold + '\n\n'.reset);
}
});
},
},
upgradePlugins: {
hidden: true,
description: '',
handler: function () {
upgradePlugins();
},
},
help: {
description: 'Display the help message for a given command',
usage: 'Usage: ' + './nodebb help <command>'.yellow,
handler: function () {
var command = commands[args._[1]];
if (command) {
process.stdout.write(command.description + '\n'.reset);
process.stdout.write(command.usage + '\n'.reset);
return;
}
var keys = Object.keys(commands).filter(function (key) {
return !commands[key].hidden;
});
process.stdout.write('\nWelcome to NodeBB\n\n'.bold);
process.stdout.write('Usage: ./nodebb {' + keys.join('|') + '}\n\n');
var usage = keys.map(function (key) {
var line = '\t' + key.yellow + (key.length < 8 ? '\t\t' : '\t');
return line + commands[key].description;
}).join('\n');
process.stdout.write(usage + '\n'.reset);
},
},
};
commands['upgrade-plugins'] = commands.upgradePlugins;
if (!commands[args._[0]]) {
commands.help.handler();
} else {
commands[args._[0]].handler();
}