From 881b381b8b5ad2731a5373295da85388e4ff7ac1 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Wed, 9 Nov 2016 10:25:18 -0500 Subject: [PATCH] fixes #4252, linting, and added ./nodebb slog --- app.js | 17 +++++--- nodebb | 136 +++++++++++++++++++++++++++++++++++---------------------- 2 files changed, 95 insertions(+), 58 deletions(-) diff --git a/app.js b/app.js index 16b9f6edb6..847c85236f 100644 --- a/app.js +++ b/app.js @@ -294,18 +294,23 @@ function upgrade() { } function activate() { - require('./src/database').init(function (err) { + var db = require('./src/database'); + db.init(function (err) { if (err) { winston.error(err.stack); process.exit(1); } - var plugin = nconf.get('_')[1] ? nconf.get('_')[1] : nconf.get('activate'), - db = require('./src/database'); - - winston.info('Activating plugin %s', plugin); + var plugin = nconf.get('activate'); + if (plugin.indexOf('nodebb-') !== 0) { + // Allow omission of `nodebb-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); + }); }); } diff --git a/nodebb b/nodebb index 53ae28db22..342032fe1a 100755 --- a/nodebb +++ b/nodebb @@ -21,10 +21,10 @@ try { } } -var getRunningPid = function(callback) { +var getRunningPid = function (callback) { fs.readFile(__dirname + '/pidfile', { encoding: 'utf-8' - }, function(err, pid) { + }, function (err, pid) { if (err) { return callback(err); } @@ -37,15 +37,19 @@ var getRunningPid = function(callback) { } }); }, - getCurrentVersion = function(callback) { - fs.readFile(path.join(__dirname, 'package.json'), { encoding: 'utf-8' }, function(err, pkg) { + 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, { @@ -53,15 +57,19 @@ var getRunningPid = function(callback) { silent: false }); }, - getInstalledPlugins = function(callback) { + 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) { + }, 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) { + payload.files = payload.files.filter(function (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 - payload.files.forEach(function(moduleName) { + payload.files.forEach(function (moduleName) { try { fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git')); isGitRepo = true; @@ -101,11 +109,15 @@ var getRunningPid = function(callback) { getModuleVersions(payload.installed, callback); }); }, - getModuleVersions = function(modules, 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) { + 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; @@ -114,11 +126,11 @@ var getRunningPid = function(callback) { next(err); } }); - }, function(err) { + }, function (err) { callback(err, versionHash); }); }, - checkPlugins = function(standalone, callback) { + checkPlugins = function (standalone, callback) { if (standalone) { process.stdout.write('Checking installed plugins and themes for updates... '); } @@ -128,7 +140,7 @@ var getRunningPid = function(callback) { plugins: async.apply(getInstalledPlugins), version: async.apply(getCurrentVersion) }), - function(payload, next) { + function (payload, next) { var toCheck = Object.keys(payload.plugins); if (!toCheck.length) { @@ -140,7 +152,7 @@ var getRunningPid = function(callback) { method: 'GET', url: 'https://packages.nodebb.org/api/v1/suggest?version=' + payload.version + '&package[]=' + toCheck.join('&package[]='), json: true - }, function(err, res, body) { + }, function (err, res, body) { if (err) { process.stdout.write('error'.red + '\n'.reset); return next(err); @@ -152,7 +164,7 @@ var getRunningPid = function(callback) { } var current, suggested, - upgradable = body.map(function(suggestObj) { + upgradable = body.map(function (suggestObj) { current = payload.plugins[suggestObj.package]; suggested = suggestObj.version; @@ -161,33 +173,33 @@ var getRunningPid = function(callback) { name: suggestObj.package, current: current, suggested: suggested - } + }; } else { return null; } }).filter(Boolean); next(null, upgradable); - }) + }); } ], callback); }, - upgradePlugins = function(callback) { + upgradePlugins = function (callback) { var standalone = false; if (typeof callback !== 'function') { - callback = function() {}; + callback = function () {}; standalone = true; }; - checkPlugins(standalone, function(err, found) { + 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 ' + new String(found.length).bold + ' package(s) can be upgraded:\n'); - found.forEach(function(suggestObj) { + 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'); @@ -206,15 +218,19 @@ var getRunningPid = function(callback) { name: 'upgrade', description: 'Proceed with upgrade (y|n)?'.reset, type: 'string' - }, function(err, result) { + }, 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) { + found.forEach(function (suggestObj) { 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) { 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); callback(); } - }) + }); }); }; switch(process.argv[2]) { case 'status': - getRunningPid(function(err, pid) { + 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'); @@ -241,7 +257,7 @@ switch(process.argv[2]) { 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': @@ -255,12 +271,27 @@ switch(process.argv[2]) { 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) { + getRunningPid(function (err, pid) { if (!err) { process.kill(pid, 'SIGTERM'); - process.stdout.write('Stopping NodeBB. Goodbye!\n') + process.stdout.write('Stopping NodeBB. Goodbye!\n'); } else { process.stdout.write('NodeBB is already stopped.\n'); } @@ -268,7 +299,7 @@ switch(process.argv[2]) { break; case 'restart': - getRunningPid(function(err, pid) { + getRunningPid(function (err, pid) { if (!err) { process.kill(pid, 'SIGHUP'); process.stdout.write('\nRestarting NodeBB\n'.bold); @@ -279,7 +310,7 @@ switch(process.argv[2]) { break; case 'reload': - getRunningPid(function(err, pid) { + getRunningPid(function (err, pid) { if (!err) { process.kill(pid, 'SIGUSR2'); } else { @@ -296,7 +327,7 @@ switch(process.argv[2]) { break; 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); cproc.spawn('tail', ['-F', './logs/output.log'], { cwd: __dirname, @@ -319,7 +350,7 @@ switch(process.argv[2]) { case 'activate': var args = process.argv.slice(0); - args.unshift('--activate'); + args.unshift('--activate=' + process.argv[3]); fork(args); break; @@ -335,25 +366,25 @@ switch(process.argv[2]) { case 'upgrade': async.series([ - function(next) { + 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) { + function (next) { process.stdout.write('OK\n'.green); process.stdout.write('2. '.bold + 'Checking installed plugins for updates... '.yellow); upgradePlugins(next); }, - function(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) + upgradeProc.on('close', next); } - ], function(err) { + ], function (err) { if (err) { process.stdout.write('\nError'.red + ': ' + err.message + '\n'); } else { @@ -371,17 +402,18 @@ switch(process.argv[2]) { default: 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('\t' + 'start'.yellow + '\tStart the NodeBB server\n'); - process.stdout.write('\t' + 'stop'.yellow + '\tStops the NodeBB server\n'); - process.stdout.write('\t' + 'reload'.yellow + '\tRestarts NodeBB\n'); - process.stdout.write('\t' + 'restart'.yellow + '\tRestarts NodeBB\n'); - process.stdout.write('\t' + 'log'.yellow + '\tOpens the logging interface (useful for debugging)\n'); - process.stdout.write('\t' + 'setup'.yellow + '\tRuns the NodeBB setup script\n'); - process.stdout.write('\t' + 'reset'.yellow + '\tDisables all plugins, restores the default theme.\n'); - process.stdout.write('\t' + 'activate'.yellow + '\tActivate a plugin on start up.\n'); - process.stdout.write('\t' + 'plugins'.yellow + '\tList all plugins that have been installed.\n'); - process.stdout.write('\t' + 'upgrade'.yellow + '\tRun NodeBB upgrade scripts, ensure packages are up-to-date\n'); - process.stdout.write('\t' + 'dev'.yellow + '\tStart NodeBB in interactive development mode\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' + '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; }