Refactor `nodebb`, move `build.js`, add `--dev`

v1.18.x
Peter Jaszkowiak 8 years ago
parent bf44ca20f2
commit 553567c3b2

@ -75,7 +75,7 @@ if (nconf.get('setup') || nconf.get('install')) {
} else if (nconf.get('reset')) { } else if (nconf.get('reset')) {
async.waterfall([ async.waterfall([
async.apply(require('./src/reset').reset), async.apply(require('./src/reset').reset),
async.apply(require('./build').buildAll) async.apply(require('./src/meta/build').buildAll)
], function (err) { ], function (err) {
process.exit(err ? 1 : 0); process.exit(err ? 1 : 0);
}); });
@ -84,7 +84,7 @@ if (nconf.get('setup') || nconf.get('install')) {
} else if (nconf.get('plugins')) { } else if (nconf.get('plugins')) {
listPlugins(); listPlugins();
} else if (nconf.get('build')) { } else if (nconf.get('build')) {
require('./build').build(nconf.get('build')); require('./src/meta/build').build(nconf.get('build'));
} else { } else {
require('./src/start').start(); require('./src/start').start();
} }
@ -126,7 +126,7 @@ function setup() {
winston.info('NodeBB Setup Triggered via Command Line'); winston.info('NodeBB Setup Triggered via Command Line');
var install = require('./src/install'); var install = require('./src/install');
var build = require('./build'); var build = require('./src/meta/build');
process.stdout.write('\nWelcome to NodeBB!\n'); process.stdout.write('\nWelcome to NodeBB!\n');
process.stdout.write('\nThis looks like a new installation, so you\'ll have to answer a few questions about your environment before we can proceed.\n'); process.stdout.write('\nThis looks like a new installation, so you\'ll have to answer a few questions about your environment before we can proceed.\n');
@ -174,7 +174,7 @@ function upgrade() {
var db = require('./src/database'); var db = require('./src/database');
var meta = require('./src/meta'); var meta = require('./src/meta');
var upgrade = require('./src/upgrade'); var upgrade = require('./src/upgrade');
var build = require('./build'); var build = require('./src/meta/build');
async.series([ async.series([
async.apply(db.init), async.apply(db.init),

321
nodebb

@ -1,15 +1,17 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict';
try { try {
var colors = require('colors'), require('colors');
cproc = require('child_process'), var cproc = require('child_process');
argv = require('minimist')(process.argv.slice(2)), var args = require('minimist')(process.argv.slice(2));
fs = require('fs'), var fs = require('fs');
path = require('path'), var path = require('path');
request = require('request'), var request = require('request');
semver = require('semver'), var semver = require('semver');
prompt = require('prompt'), var prompt = require('prompt');
async = require('async'); var async = require('async');
} catch (e) { } catch (e) {
if (e.code === 'MODULE_NOT_FOUND') { 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('NodeBB could not be started because it\'s dependencies have not been installed.\n');
@ -21,7 +23,11 @@ try {
} }
} }
var getRunningPid = function (callback) { if (args.dev) {
process.env.NODE_ENV = 'development';
}
function getRunningPid(callback) {
fs.readFile(__dirname + '/pidfile', { fs.readFile(__dirname + '/pidfile', {
encoding: 'utf-8' encoding: 'utf-8'
}, function (err, pid) { }, function (err, pid) {
@ -36,8 +42,8 @@ var getRunningPid = function (callback) {
callback(e); callback(e);
} }
}); });
}, }
getCurrentVersion = function (callback) { function getCurrentVersion(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) { if (err) {
return callback(err); return callback(err);
@ -50,14 +56,14 @@ var getRunningPid = function (callback) {
return callback(err); return callback(err);
} }
}); });
}, }
fork = function (args) { function fork(args) {
cproc.fork('app.js', args, { return cproc.fork('app.js', args, {
cwd: __dirname, cwd: __dirname,
silent: false silent: false
}); });
}, }
getInstalledPlugins = function (callback) { function getInstalledPlugins(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' })
@ -97,10 +103,10 @@ var getRunningPid = function (callback) {
} }
if ( if (
payload.files.indexOf(moduleName) !== -1 // found in `node_modules/` payload.files.indexOf(moduleName) !== -1 && // found in `node_modules/`
&& payload.bundled.indexOf(moduleName) === -1 // not found in `package.json` payload.bundled.indexOf(moduleName) === -1 && // not found in `package.json`
&& !fs.lstatSync(path.join(__dirname, 'node_modules/' + moduleName)).isSymbolicLink() // is not a symlink !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 !isGitRepo // .git/ does not exist, so it is not a git repository
) { ) {
payload.installed.push(moduleName); payload.installed.push(moduleName);
} }
@ -108,8 +114,8 @@ var getRunningPid = function (callback) {
getModuleVersions(payload.installed, callback); getModuleVersions(payload.installed, callback);
}); });
}, }
getModuleVersions = function (modules, callback) { function getModuleVersions(modules, callback) {
var versionHash = {}; var versionHash = {};
async.eachLimit(modules, 50, function (module, next) { async.eachLimit(modules, 50, function (module, next) {
@ -129,8 +135,8 @@ var getRunningPid = function (callback) {
}, function (err) { }, function (err) {
callback(err, versionHash); callback(err, versionHash);
}); });
}, }
checkPlugins = function (standalone, callback) { function checkPlugins(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... ');
} }
@ -183,13 +189,13 @@ var getRunningPid = function (callback) {
}); });
} }
], callback); ], callback);
}, }
upgradePlugins = function (callback) { function upgradePlugins(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) {
@ -243,10 +249,13 @@ var getRunningPid = function (callback) {
} }
}); });
}); });
}; }
switch(process.argv[2]) { var commands = {
case 'status': status: {
description: 'View the status of the NodeBB server',
usage: 'Usage: ' + './nodebb status'.yellow,
handler: function () {
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);
@ -258,9 +267,12 @@ switch(process.argv[2]) {
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; },
},
case 'start': start: {
description: 'Start the NodeBB server',
usage: 'Usage: ' + './nodebb start'.yellow,
handler: function () {
process.stdout.write('\nStarting NodeBB\n'.bold); process.stdout.write('\nStarting NodeBB\n'.bold);
process.stdout.write(' "' + './nodebb stop'.yellow + '" to stop the NodeBB server\n'); 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 log'.yellow + '" to view server output\n');
@ -270,24 +282,12 @@ switch(process.argv[2]) {
cproc.fork(__dirname + '/loader.js', { cproc.fork(__dirname + '/loader.js', {
env: process.env env: process.env
}); });
break; },
},
case 'slog': stop: {
process.stdout.write('\nStarting NodeBB with logging output\n'.bold); description: 'Stop the NodeBB server',
process.stdout.write('\nHit '.red + 'Ctrl-C '.bold + 'to exit'.red); usage: 'Usage: ' + './nodebb stop'.yellow,
process.stdout.write('\n\n'.reset); handler: function () {
// 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) { if (!err) {
process.kill(pid, 'SIGTERM'); process.kill(pid, 'SIGTERM');
@ -296,9 +296,12 @@ switch(process.argv[2]) {
process.stdout.write('NodeBB is already stopped.\n'); process.stdout.write('NodeBB is already stopped.\n');
} }
}); });
break; },
},
case 'restart': restart: {
description: 'Restart the NodeBB server',
usage: 'Usage: ' + './nodebb restart'.yellow,
handler: function () {
getRunningPid(function (err, pid) { getRunningPid(function (err, pid) {
if (!err) { if (!err) {
process.kill(pid, 'SIGHUP'); process.kill(pid, 'SIGHUP');
@ -307,71 +310,102 @@ switch(process.argv[2]) {
process.stdout.write('NodeBB could not be restarted, as a running instance could not be found.\n'); process.stdout.write('NodeBB could not be restarted, as a running instance could not be found.\n');
} }
}); });
break; },
},
case 'reload': log: {
getRunningPid(function (err, pid) { description: 'Open the output log (useful for debugging)',
if (!err) { usage: 'Usage: ' + './nodebb log'.yellow,
process.kill(pid, 'SIGUSR2'); handler: function () {
} 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('\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,
stdio: 'inherit' stdio: 'inherit'
}); });
break; },
},
case 'build': slog: {
var args = process.argv.slice(0); description: 'Start the NodeBB server and view the live output log',
args[2] = '--' + args[2]; usage: 'Usage: ' + './nodebb slog'.yellow,
handler: function () {
fork(args); process.stdout.write('\nStarting NodeBB with logging output\n'.bold);
break; process.stdout.write('\nHit '.red + 'Ctrl-C '.bold + 'to exit'.red);
process.stdout.write('\n\n'.reset);
case 'setup': // Spawn a new NodeBB process
cproc.fork('app.js', ['--setup'], { cproc.fork(__dirname + '/loader.js', {
env: process.env
});
cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname, cwd: __dirname,
silent: false stdio: 'inherit'
}); });
break; },
},
case 'reset': dev: {
var args = process.argv.slice(0); description: 'Start NodeBB in verbose development mode',
args.unshift('--reset'); usage: 'Usage: ' + './nodebb dev'.yellow,
fork(args); handler: function () {
break; process.env.NODE_ENV = 'development';
cproc.fork(__dirname + '/loader.js', ['--no-daemon', '--no-silent'], {
case 'activate': env: process.env
var args = process.argv.slice(0); });
args.unshift('--activate=' + process.argv[3]); },
fork(args); },
break; build: {
description: 'Compile static assets (CSS, Javascript, etc)',
case 'plugins': usage: 'Usage: ' + './nodebb build'.yellow + ' [js,clientCSS,acpCSS,tpl,lang]'.red + '\n' +
var args = process.argv.slice(0); ' e.g. ' + './nodebb build js,tpl'.yellow + '\tbuilds JS and templates\n' +
args.unshift('--plugins'); ' ' + './nodebb build'.yellow + '\t\tbuilds all targets\n',
fork(args); handler: function () {
break; var arr = ['--build'].concat(process.argv.slice(3));
fork(arr);
case 'upgrade-plugins': },
upgradePlugins(); },
break; setup: {
description: 'Run the NodeBB setup script',
case 'upgrade': 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([ 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);
@ -384,10 +418,8 @@ switch(process.argv[2]) {
}, },
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 arr = ['--upgrade'].concat(process.argv.slice(3));
cwd: __dirname, var upgradeProc = fork(arr);
silent: false
});
upgradeProc.on('close', next); upgradeProc.on('close', next);
} }
@ -404,24 +436,47 @@ switch(process.argv[2]) {
process.stdout.write('\n' + spaces + message.green.bold + '\n\n'.reset); process.stdout.write('\n' + spaces + message.green.bold + '\n\n'.reset);
} }
}); });
break; },
},
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;
});
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|slog|stop|reload|restart|log|build|setup|reset|upgrade|dev}\n\n'); process.stdout.write('Usage: ./nodebb {' + keys.join('|') + '}\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'); var usage = keys.map(function (key) {
process.stdout.write('\t' + 'stop'.yellow + '\t\tStops the NodeBB server\n'); var line = '\t' + key.yellow + (key.length < 8 ? '\t\t' : '\t');
process.stdout.write('\t' + 'reload'.yellow + '\t\tRestarts NodeBB\n'); return line + commands[key].description;
process.stdout.write('\t' + 'restart'.yellow + '\t\tRestarts NodeBB\n'); }).join('\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(usage + '\n'.reset);
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'); commands['upgrade-plugins'] = commands.upgradePlugins;
process.stdout.write('\t' + 'dev'.yellow + '\t\tStart NodeBB in interactive development mode\n');
process.stdout.write('\n'.reset); if (!commands[args._[0]]) {
break; commands.help.handler();
} else {
commands[args._[0]].handler();
} }

@ -12,7 +12,7 @@
"scripts": { "scripts": {
"start": "node loader.js", "start": "node loader.js",
"lint": "eslint --cache .", "lint": "eslint --cache .",
"pretest": "npm run lint", "pretest": "npm run lint && node app --build",
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R dot", "test": "istanbul cover node_modules/mocha/bin/_mocha -- -R dot",
"coveralls": "istanbul cover _mocha --report lcovonly -- -R dot && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" "coveralls": "istanbul cover _mocha --report lcovonly -- -R dot && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
}, },

@ -14,9 +14,9 @@ exports.buildAll = function (callback) {
exports.build = function build(targets, callback) { exports.build = function build(targets, callback) {
buildStart = Date.now(); buildStart = Date.now();
var db = require('./src/database'); var db = require('../database');
var meta = require('./src/meta'); var meta = require('../meta');
var plugins = require('./src/plugins'); var plugins = require('../plugins');
targets = (targets === true ? valid : targets.split(',').filter(function (target) { targets = (targets === true ? valid : targets.split(',').filter(function (target) {
@ -43,7 +43,7 @@ exports.build = function build(targets, callback) {
}; };
exports.buildTargets = function (targets, callback) { exports.buildTargets = function (targets, callback) {
var meta = require('./src/meta'); var meta = require('../meta');
buildStart = buildStart || Date.now(); buildStart = buildStart || Date.now();
var step = function (startTime, target, next) { var step = function (startTime, target, next) {

@ -57,7 +57,7 @@ SocketAdmin.reload = function (socket, data, callback) {
}; };
SocketAdmin.restart = function (socket, data, callback) { SocketAdmin.restart = function (socket, data, callback) {
require('../../build').buildAll(function (err) { require('../meta/build').buildAll(function (err) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

@ -143,7 +143,7 @@
nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json')); nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
nconf.set('bcrypt_rounds', 1); nconf.set('bcrypt_rounds', 1);
require('../../build').buildTargets(['js', 'clientCSS', 'acpCSS', 'tpl'], next); next();
}, },
function (next) { function (next) {
var webserver = require('../../src/webserver'); var webserver = require('../../src/webserver');

Loading…
Cancel
Save