fixes #4252, linting, and added ./nodebb slog

v1.18.x
Julian Lam 8 years ago
parent 3eacd33951
commit 881b381b8b

@ -294,18 +294,23 @@ function upgrade() {
} }
function activate() { function activate() {
require('./src/database').init(function (err) { var db = require('./src/database');
db.init(function (err) {
if (err) { if (err) {
winston.error(err.stack); winston.error(err.stack);
process.exit(1); process.exit(1);
} }
var plugin = nconf.get('_')[1] ? nconf.get('_')[1] : nconf.get('activate'), var plugin = nconf.get('activate');
db = require('./src/database'); if (plugin.indexOf('nodebb-') !== 0) {
// Allow omission of `nodebb-plugin-`
winston.info('Activating plugin %s', plugin); plugin = 'nodebb-plugin-' + plugin;
}
db.sortedSetAdd('plugins:active', 0, plugin, start); winston.info('Activating plugin `%s`', plugin);
db.sortedSetAdd('plugins:active', 0, plugin, function(err) {
process.exit(err ? 1 : 0);
});
}); });
} }

136
nodebb

@ -21,10 +21,10 @@ try {
} }
} }
var getRunningPid = function(callback) { var getRunningPid = function (callback) {
fs.readFile(__dirname + '/pidfile', { fs.readFile(__dirname + '/pidfile', {
encoding: 'utf-8' encoding: 'utf-8'
}, function(err, pid) { }, function (err, pid) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -37,15 +37,19 @@ var getRunningPid = function(callback) {
} }
}); });
}, },
getCurrentVersion = function(callback) { getCurrentVersion = function (callback) {
fs.readFile(path.join(__dirname, 'package.json'), { encoding: 'utf-8' }, function(err, pkg) { fs.readFile(path.join(__dirname, 'package.json'), { encoding: 'utf-8' }, function (err, pkg) {
if (err) {
return callback(err);
}
try { try {
pkg = JSON.parse(pkg); pkg = JSON.parse(pkg);
return callback(null, pkg.version); return callback(null, pkg.version);
} catch(err) { } catch(err) {
return callback(err); return callback(err);
} }
}) });
}, },
fork = function (args) { fork = function (args) {
cproc.fork('app.js', args, { cproc.fork('app.js', args, {
@ -53,15 +57,19 @@ var getRunningPid = function(callback) {
silent: false silent: false
}); });
}, },
getInstalledPlugins = function(callback) { getInstalledPlugins = function (callback) {
async.parallel({ async.parallel({
files: async.apply(fs.readdir, path.join(__dirname, 'node_modules')), files: async.apply(fs.readdir, path.join(__dirname, 'node_modules')),
deps: async.apply(fs.readFile, path.join(__dirname, 'package.json'), { encoding: 'utf-8' }) deps: async.apply(fs.readFile, path.join(__dirname, 'package.json'), { encoding: 'utf-8' })
}, function(err, payload) { }, function (err, payload) {
if (err) {
return callback(err);
}
var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w\-]+$/, var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w\-]+$/,
moduleName, isGitRepo; moduleName, isGitRepo;
payload.files = payload.files.filter(function(file) { payload.files = payload.files.filter(function (file) {
return isNbbModule.test(file); return isNbbModule.test(file);
}); });
@ -80,7 +88,7 @@ var getRunningPid = function(callback) {
} }
// Whittle down deps to send back only extraneously installed plugins/themes/etc // Whittle down deps to send back only extraneously installed plugins/themes/etc
payload.files.forEach(function(moduleName) { payload.files.forEach(function (moduleName) {
try { try {
fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git')); fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git'));
isGitRepo = true; isGitRepo = true;
@ -101,11 +109,15 @@ var getRunningPid = function(callback) {
getModuleVersions(payload.installed, callback); getModuleVersions(payload.installed, callback);
}); });
}, },
getModuleVersions = function(modules, callback) { getModuleVersions = function (modules, callback) {
var versionHash = {}; var versionHash = {};
async.eachLimit(modules, 50, function(module, next) { async.eachLimit(modules, 50, function (module, next) {
fs.readFile(path.join(__dirname, 'node_modules/' + module + '/package.json'), { encoding: 'utf-8' }, function(err, pkg) { fs.readFile(path.join(__dirname, 'node_modules/' + module + '/package.json'), { encoding: 'utf-8' }, function (err, pkg) {
if (err) {
return next(err);
}
try { try {
pkg = JSON.parse(pkg); pkg = JSON.parse(pkg);
versionHash[module] = pkg.version; versionHash[module] = pkg.version;
@ -114,11 +126,11 @@ var getRunningPid = function(callback) {
next(err); next(err);
} }
}); });
}, function(err) { }, function (err) {
callback(err, versionHash); callback(err, versionHash);
}); });
}, },
checkPlugins = function(standalone, callback) { checkPlugins = function (standalone, callback) {
if (standalone) { if (standalone) {
process.stdout.write('Checking installed plugins and themes for updates... '); process.stdout.write('Checking installed plugins and themes for updates... ');
} }
@ -128,7 +140,7 @@ var getRunningPid = function(callback) {
plugins: async.apply(getInstalledPlugins), plugins: async.apply(getInstalledPlugins),
version: async.apply(getCurrentVersion) version: async.apply(getCurrentVersion)
}), }),
function(payload, next) { function (payload, next) {
var toCheck = Object.keys(payload.plugins); var toCheck = Object.keys(payload.plugins);
if (!toCheck.length) { if (!toCheck.length) {
@ -140,7 +152,7 @@ var getRunningPid = function(callback) {
method: 'GET', method: 'GET',
url: 'https://packages.nodebb.org/api/v1/suggest?version=' + payload.version + '&package[]=' + toCheck.join('&package[]='), url: 'https://packages.nodebb.org/api/v1/suggest?version=' + payload.version + '&package[]=' + toCheck.join('&package[]='),
json: true json: true
}, function(err, res, body) { }, function (err, res, body) {
if (err) { if (err) {
process.stdout.write('error'.red + '\n'.reset); process.stdout.write('error'.red + '\n'.reset);
return next(err); return next(err);
@ -152,7 +164,7 @@ var getRunningPid = function(callback) {
} }
var current, suggested, var current, suggested,
upgradable = body.map(function(suggestObj) { upgradable = body.map(function (suggestObj) {
current = payload.plugins[suggestObj.package]; current = payload.plugins[suggestObj.package];
suggested = suggestObj.version; suggested = suggestObj.version;
@ -161,33 +173,33 @@ var getRunningPid = function(callback) {
name: suggestObj.package, name: suggestObj.package,
current: current, current: current,
suggested: suggested suggested: suggested
} };
} else { } else {
return null; return null;
} }
}).filter(Boolean); }).filter(Boolean);
next(null, upgradable); next(null, upgradable);
}) });
} }
], callback); ], callback);
}, },
upgradePlugins = function(callback) { upgradePlugins = function (callback) {
var standalone = false; var standalone = false;
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
callback = function() {}; callback = function () {};
standalone = true; standalone = true;
}; };
checkPlugins(standalone, function(err, found) { checkPlugins(standalone, function (err, found) {
if (err) { if (err) {
process.stdout.write('\Warning'.yellow + ': An unexpected error occured when attempting to verify plugin upgradability\n'.reset); process.stdout.write('\Warning'.yellow + ': An unexpected error occured when attempting to verify plugin upgradability\n'.reset);
return callback(err); return callback(err);
} }
if (found && found.length) { if (found && found.length) {
process.stdout.write('\nA total of ' + new String(found.length).bold + ' package(s) can be upgraded:\n'); process.stdout.write('\nA total of ' + String(found.length).bold + ' package(s) can be upgraded:\n');
found.forEach(function(suggestObj) { found.forEach(function (suggestObj) {
process.stdout.write(' * '.yellow + suggestObj.name.reset + ' (' + suggestObj.current.yellow + ' -> '.reset + suggestObj.suggested.green + ')\n'.reset); process.stdout.write(' * '.yellow + suggestObj.name.reset + ' (' + suggestObj.current.yellow + ' -> '.reset + suggestObj.suggested.green + ')\n'.reset);
}); });
process.stdout.write('\n'); process.stdout.write('\n');
@ -206,15 +218,19 @@ var getRunningPid = function(callback) {
name: 'upgrade', name: 'upgrade',
description: 'Proceed with upgrade (y|n)?'.reset, description: 'Proceed with upgrade (y|n)?'.reset,
type: 'string' type: 'string'
}, function(err, result) { }, function (err, result) {
if (err) {
return callback(err);
}
if (['y', 'Y', 'yes', 'YES'].indexOf(result.upgrade) !== -1) { if (['y', 'Y', 'yes', 'YES'].indexOf(result.upgrade) !== -1) {
process.stdout.write('\nUpgrading packages...'); process.stdout.write('\nUpgrading packages...');
var args = ['npm', 'i']; var args = ['npm', 'i'];
found.forEach(function(suggestObj) { found.forEach(function (suggestObj) {
args.push(suggestObj.name + '@' + suggestObj.suggested); args.push(suggestObj.name + '@' + suggestObj.suggested);
}); });
require('child_process').execFile('/usr/bin/env', args, { stdio: 'ignore' }, function(err) { require('child_process').execFile('/usr/bin/env', args, { stdio: 'ignore' }, function (err) {
if (!err) { if (!err) {
process.stdout.write(' OK\n'.green); process.stdout.write(' OK\n'.green);
} }
@ -225,13 +241,13 @@ var getRunningPid = function(callback) {
process.stdout.write('\nPackage upgrades skipped'.yellow + '. Check for upgrades at any time by running "'.reset + './nodebb upgrade-plugins'.green + '".\n'.reset); process.stdout.write('\nPackage upgrades skipped'.yellow + '. Check for upgrades at any time by running "'.reset + './nodebb upgrade-plugins'.green + '".\n'.reset);
callback(); callback();
} }
}) });
}); });
}; };
switch(process.argv[2]) { switch(process.argv[2]) {
case 'status': case 'status':
getRunningPid(function(err, pid) { getRunningPid(function (err, pid) {
if (!err) { if (!err) {
process.stdout.write('\nNodeBB Running '.bold + '(pid '.cyan + pid.toString().cyan + ')\n'.cyan); 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 stop'.yellow + '" to stop the NodeBB server\n');
@ -241,7 +257,7 @@ switch(process.argv[2]) {
process.stdout.write('\nNodeBB is not running\n'.bold); process.stdout.write('\nNodeBB is not running\n'.bold);
process.stdout.write('\t"' + './nodebb start'.yellow + '" to launch the NodeBB server\n\n'.reset); process.stdout.write('\t"' + './nodebb start'.yellow + '" to launch the NodeBB server\n\n'.reset);
} }
}) });
break; break;
case 'start': case 'start':
@ -255,12 +271,27 @@ switch(process.argv[2]) {
env: process.env env: process.env
}); });
break; 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': case 'stop':
getRunningPid(function(err, pid) { getRunningPid(function (err, pid) {
if (!err) { if (!err) {
process.kill(pid, 'SIGTERM'); process.kill(pid, 'SIGTERM');
process.stdout.write('Stopping NodeBB. Goodbye!\n') process.stdout.write('Stopping NodeBB. Goodbye!\n');
} else { } else {
process.stdout.write('NodeBB is already stopped.\n'); process.stdout.write('NodeBB is already stopped.\n');
} }
@ -268,7 +299,7 @@ switch(process.argv[2]) {
break; break;
case 'restart': case 'restart':
getRunningPid(function(err, pid) { getRunningPid(function (err, pid) {
if (!err) { if (!err) {
process.kill(pid, 'SIGHUP'); process.kill(pid, 'SIGHUP');
process.stdout.write('\nRestarting NodeBB\n'.bold); process.stdout.write('\nRestarting NodeBB\n'.bold);
@ -279,7 +310,7 @@ switch(process.argv[2]) {
break; break;
case 'reload': case 'reload':
getRunningPid(function(err, pid) { getRunningPid(function (err, pid) {
if (!err) { if (!err) {
process.kill(pid, 'SIGUSR2'); process.kill(pid, 'SIGUSR2');
} else { } else {
@ -296,7 +327,7 @@ switch(process.argv[2]) {
break; break;
case 'log': case 'log':
process.stdout.write('\nType '.red + 'Ctrl-C '.bold + 'to exit'.red); process.stdout.write('\nHit '.red + 'Ctrl-C '.bold + 'to exit'.red);
process.stdout.write('\n\n'.reset); process.stdout.write('\n\n'.reset);
cproc.spawn('tail', ['-F', './logs/output.log'], { cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname, cwd: __dirname,
@ -319,7 +350,7 @@ switch(process.argv[2]) {
case 'activate': case 'activate':
var args = process.argv.slice(0); var args = process.argv.slice(0);
args.unshift('--activate'); args.unshift('--activate=' + process.argv[3]);
fork(args); fork(args);
break; break;
@ -335,25 +366,25 @@ switch(process.argv[2]) {
case 'upgrade': case 'upgrade':
async.series([ async.series([
function(next) { function (next) {
process.stdout.write('1. '.bold + 'Bringing base dependencies up to date... '.yellow); 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); require('child_process').execFile('/usr/bin/env', ['npm', 'i', '--production'], { stdio: 'ignore' }, next);
}, },
function(next) { function (next) {
process.stdout.write('OK\n'.green); process.stdout.write('OK\n'.green);
process.stdout.write('2. '.bold + 'Checking installed plugins for updates... '.yellow); process.stdout.write('2. '.bold + 'Checking installed plugins for updates... '.yellow);
upgradePlugins(next); upgradePlugins(next);
}, },
function(next) { function (next) {
process.stdout.write('3. '.bold + 'Updating NodeBB data store schema...\n'.yellow); process.stdout.write('3. '.bold + 'Updating NodeBB data store schema...\n'.yellow);
var upgradeProc = cproc.fork('app.js', ['--upgrade'], { var upgradeProc = cproc.fork('app.js', ['--upgrade'], {
cwd: __dirname, cwd: __dirname,
silent: false silent: false
}); });
upgradeProc.on('close', next) upgradeProc.on('close', next);
} }
], function(err) { ], function (err) {
if (err) { if (err) {
process.stdout.write('\nError'.red + ': ' + err.message + '\n'); process.stdout.write('\nError'.red + ': ' + err.message + '\n');
} else { } else {
@ -371,17 +402,18 @@ switch(process.argv[2]) {
default: default:
process.stdout.write('\nWelcome to NodeBB\n\n'.bold); process.stdout.write('\nWelcome to NodeBB\n\n'.bold);
process.stdout.write('Usage: ./nodebb {start|stop|reload|restart|log|setup|reset|upgrade|dev}\n\n'); process.stdout.write('Usage: ./nodebb {start|stop|reload|restart|log|setup|reset|upgrade|dev}\n\n');
process.stdout.write('\t' + 'start'.yellow + '\tStart the NodeBB server\n'); process.stdout.write('\t' + 'start'.yellow + '\t\tStart the NodeBB server\n');
process.stdout.write('\t' + 'stop'.yellow + '\tStops 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' + 'reload'.yellow + '\tRestarts NodeBB\n'); process.stdout.write('\t' + 'stop'.yellow + '\t\tStops the NodeBB server\n');
process.stdout.write('\t' + 'restart'.yellow + '\tRestarts NodeBB\n'); process.stdout.write('\t' + 'reload'.yellow + '\t\tRestarts NodeBB\n');
process.stdout.write('\t' + 'log'.yellow + '\tOpens the logging interface (useful for debugging)\n'); process.stdout.write('\t' + 'restart'.yellow + '\t\tRestarts NodeBB\n');
process.stdout.write('\t' + 'setup'.yellow + '\tRuns the NodeBB setup script\n'); process.stdout.write('\t' + 'log'.yellow + '\t\tOpens the logging interface (useful for debugging)\n');
process.stdout.write('\t' + 'reset'.yellow + '\tDisables all plugins, restores the default theme.\n'); process.stdout.write('\t' + 'setup'.yellow + '\t\tRuns the NodeBB setup script\n');
process.stdout.write('\t' + 'activate'.yellow + '\tActivate a plugin on start up.\n'); process.stdout.write('\t' + 'reset'.yellow + '\t\tDisables all plugins, restores the default theme.\n');
process.stdout.write('\t' + 'plugins'.yellow + '\tList all plugins that have been installed.\n'); process.stdout.write('\t' + 'activate'.yellow + '\tActivates a plugin for the next startup of NodeBB.\n');
process.stdout.write('\t' + 'upgrade'.yellow + '\tRun NodeBB upgrade scripts, ensure packages are up-to-date\n'); process.stdout.write('\t' + 'plugins'.yellow + '\t\tList all plugins that have been installed.\n');
process.stdout.write('\t' + 'dev'.yellow + '\tStart NodeBB in interactive development mode\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); process.stdout.write('\n'.reset);
break; break;
} }

Loading…
Cancel
Save