forked from NodeBB/NodeBB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodebb
executable file
·427 lines (378 loc) · 12.8 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
#!/usr/bin/env node
try {
var colors = require('colors'),
cproc = require('child_process'),
argv = require('minimist')(process.argv.slice(2)),
fs = require('fs'),
path = require('path'),
request = require('request'),
semver = require('semver'),
prompt = require('prompt'),
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);
}
}
var getRunningPid = function (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);
}
});
},
getCurrentVersion = function (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);
}
});
},
fork = function (args) {
cproc.fork('app.js', args, {
cwd: __dirname,
silent: false
});
},
getInstalledPlugins = function (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);
});
},
getModuleVersions = function (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);
});
},
checkPlugins = function (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);
},
upgradePlugins = function (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 = ['npm', 'i'];
found.forEach(function (suggestObj) {
args.push(suggestObj.name + '@' + suggestObj.suggested);
});
require('child_process').execFile('/usr/bin/env', 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();
}
});
});
};
switch(process.argv[2]) {
case 'status':
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);
}
});
break;
case 'start':
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
});
break;
case 'slog':
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'
});
break;
case 'stop':
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');
}
});
break;
case 'restart':
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');
}
});
break;
case 'reload':
getRunningPid(function (err, pid) {
if (!err) {
process.kill(pid, 'SIGUSR2');
} else {
process.stdout.write('NodeBB could not be reloaded, as a running instance could not be found.\n');
}
});
break;
case 'dev':
process.env.NODE_ENV = 'development';
cproc.fork(__dirname + '/loader.js', ['--no-daemon', '--no-silent'], {
env: process.env
});
break;
case 'log':
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'
});
break;
case 'build':
var args = process.argv.slice(0);
args[2] = '--' + args[2];
fork(args);
break;
case 'setup':
cproc.fork('app.js', ['--setup'], {
cwd: __dirname,
silent: false
});
break;
case 'reset':
var args = process.argv.slice(0);
args.unshift('--reset');
fork(args);
break;
case 'activate':
var args = process.argv.slice(0);
args.unshift('--activate=' + process.argv[3]);
fork(args);
break;
case 'plugins':
var args = process.argv.slice(0);
args.unshift('--plugins');
fork(args);
break;
case 'upgrade-plugins':
upgradePlugins();
break;
case 'upgrade':
async.series([
function (next) {
process.stdout.write('1. '.bold + 'Bringing base dependencies up to date... '.yellow);
require('child_process').execFile('/usr/bin/env', ['npm', 'i', '--production'], { 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 upgradeProc = cproc.fork('app.js', ['--upgrade'], {
cwd: __dirname,
silent: false
});
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);
}
});
break;
default:
process.stdout.write('\nWelcome to NodeBB\n\n'.bold);
process.stdout.write('Usage: ./nodebb {start|slog|stop|reload|restart|log|build|setup|reset|upgrade|dev}\n\n');
process.stdout.write('\t' + 'start'.yellow + '\t\tStart the NodeBB server\n');
process.stdout.write('\t' + 'slog'.yellow + '\t\tStarts the NodeBB server and displays the live output log\n');
process.stdout.write('\t' + 'stop'.yellow + '\t\tStops the NodeBB server\n');
process.stdout.write('\t' + 'reload'.yellow + '\t\tRestarts NodeBB\n');
process.stdout.write('\t' + 'restart'.yellow + '\t\tRestarts NodeBB\n');
process.stdout.write('\t' + 'log'.yellow + '\t\tOpens the logging interface (useful for debugging)\n');
process.stdout.write('\t' + 'build'.yellow + '\t\tCompiles javascript, css stylesheets, and templates\n');
process.stdout.write('\t' + 'setup'.yellow + '\t\tRuns the NodeBB setup script\n');
process.stdout.write('\t' + 'reset'.yellow + '\t\tDisables all plugins, restores the default theme.\n');
process.stdout.write('\t' + 'activate'.yellow + '\tActivates a plugin for the next startup of NodeBB.\n');
process.stdout.write('\t' + 'plugins'.yellow + '\t\tList all plugins that have been installed.\n');
process.stdout.write('\t' + 'upgrade'.yellow + '\t\tRun NodeBB upgrade scripts, ensure packages are up-to-date\n');
process.stdout.write('\t' + 'dev'.yellow + '\t\tStart NodeBB in interactive development mode\n');
process.stdout.write('\n'.reset);
break;
}