Fix using nodebb in other CWDs

v1.18.x
Peter Jaszkowiak 8 years ago
parent 73b6b06294
commit 75d291183d

@ -19,6 +19,12 @@
'use strict'; 'use strict';
if (require.main !== module) {
require.main.require = function (path) {
return require(path);
};
}
var nconf = require('nconf'); var nconf = require('nconf');
nconf.argv().env('__'); nconf.argv().env('__');

@ -7,6 +7,7 @@ var path = require('path');
var fork = require('child_process').fork; var fork = require('child_process').fork;
var async = require('async'); var async = require('async');
var logrotate = require('logrotate-stream'); var logrotate = require('logrotate-stream');
var file = require('./src/file'); var file = require('./src/file');
var pkg = require('./package.json'); var pkg = require('./package.json');
@ -23,6 +24,7 @@ var workers = [];
var Loader = { var Loader = {
timesStarted: 0, timesStarted: 0,
}; };
var appPath = path.join(__dirname, 'app.js');
Loader.init = function (callback) { Loader.init = function (callback) {
if (silent) { if (silent) {
@ -114,7 +116,7 @@ function forkWorker(index, isPrimary) {
process.env.isCluster = ports.length > 1; process.env.isCluster = ports.length > 1;
process.env.port = ports[index]; process.env.port = ports[index];
var worker = fork('app.js', args, { var worker = fork(appPath, args, {
silent: silent, silent: silent,
env: process.env, env: process.env,
}); });

111
nodebb

@ -2,16 +2,25 @@
'use strict'; 'use strict';
var cproc;
var args;
var fs;
var path;
var request;
var semver;
var prompt;
var async;
try { try {
require('colors'); require('colors');
var cproc = require('child_process'); cproc = require('child_process');
var args = require('minimist')(process.argv.slice(2)); args = require('minimist')(process.argv.slice(2));
var fs = require('fs'); fs = require('fs');
var path = require('path'); path = require('path');
var request = require('request'); request = require('request');
var semver = require('semver'); semver = require('semver');
var prompt = require('prompt'); prompt = require('prompt');
var async = require('async'); 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');
@ -23,13 +32,16 @@ try {
} }
} }
var loaderPath = path.join(__dirname, 'loader.js');
var appPath = path.join(__dirname, 'app.js');
if (args.dev) { if (args.dev) {
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
} }
function getRunningPid(callback) { function getRunningPid(callback) {
fs.readFile(__dirname + '/pidfile', { fs.readFile(path.join(__dirname, 'pidfile'), {
encoding: 'utf-8' encoding: 'utf-8',
}, function (err, pid) { }, function (err, pid) {
if (err) { if (err) {
return callback(err); return callback(err);
@ -38,7 +50,7 @@ function getRunningPid(callback) {
try { try {
process.kill(parseInt(pid, 10), 0); process.kill(parseInt(pid, 10), 0);
callback(null, parseInt(pid, 10)); callback(null, parseInt(pid, 10));
} catch(e) { } catch (e) {
callback(e); callback(e);
} }
}); });
@ -52,28 +64,29 @@ function getCurrentVersion(callback) {
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);
} }
}); });
} }
function fork(args) { function fork(args) {
return cproc.fork('app.js', args, { return cproc.fork(appPath, args, {
cwd: __dirname, cwd: __dirname,
silent: false silent: false,
}); });
} }
function getInstalledPlugins(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' }),
}, function (err, payload) { }, function (err, payload) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w\-]+$/, var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w-]+$/;
moduleName, isGitRepo; var moduleName;
var isGitRepo;
payload.files = payload.files.filter(function (file) { payload.files = payload.files.filter(function (file) {
return isNbbModule.test(file); return isNbbModule.test(file);
@ -98,7 +111,7 @@ function getInstalledPlugins(callback) {
try { try {
fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git')); fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git'));
isGitRepo = true; isGitRepo = true;
} catch(e) { } catch (e) {
isGitRepo = false; isGitRepo = false;
} }
@ -144,7 +157,7 @@ function checkPlugins(standalone, callback) {
async.waterfall([ async.waterfall([
async.apply(async.parallel, { async.apply(async.parallel, {
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);
@ -157,7 +170,7 @@ function checkPlugins(standalone, callback) {
request({ request({
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);
@ -169,25 +182,25 @@ function checkPlugins(standalone, callback) {
body = [body]; body = [body];
} }
var current, suggested, var current;
upgradable = body.map(function (suggestObj) { var suggested;
current = payload.plugins[suggestObj.package]; var upgradable = body.map(function (suggestObj) {
suggested = suggestObj.version; current = payload.plugins[suggestObj.package];
suggested = suggestObj.version;
if (suggestObj.code === 'match-found' && semver.gt(suggested, current)) {
return { if (suggestObj.code === 'match-found' && semver.gt(suggested, current)) {
name: suggestObj.package, return {
current: current, name: suggestObj.package,
suggested: suggested current: current,
}; suggested: suggested,
} else { };
return null; }
} return null;
}).filter(Boolean); }).filter(Boolean);
next(null, upgradable); next(null, upgradable);
}); });
} },
], callback); ], callback);
} }
function upgradePlugins(callback) { function upgradePlugins(callback) {
@ -199,7 +212,7 @@ function upgradePlugins(callback) {
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);
} }
@ -223,7 +236,7 @@ function upgradePlugins(callback) {
prompt.get({ prompt.get({
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) { if (err) {
return callback(err); return callback(err);
@ -279,8 +292,8 @@ var commands = {
process.stdout.write(' "' + './nodebb restart'.yellow + '" to restart NodeBB\n\n'.reset); process.stdout.write(' "' + './nodebb restart'.yellow + '" to restart NodeBB\n\n'.reset);
// Spawn a new NodeBB process // Spawn a new NodeBB process
cproc.fork(__dirname + '/loader.js', { cproc.fork(loaderPath, {
env: process.env env: process.env,
}); });
}, },
}, },
@ -320,7 +333,7 @@ var commands = {
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',
}); });
}, },
}, },
@ -333,12 +346,12 @@ var commands = {
process.stdout.write('\n\n'.reset); process.stdout.write('\n\n'.reset);
// Spawn a new NodeBB process // Spawn a new NodeBB process
cproc.fork(__dirname + '/loader.js', { cproc.fork(loaderPath, {
env: process.env env: process.env,
}); });
cproc.spawn('tail', ['-F', './logs/output.log'], { cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname, cwd: __dirname,
stdio: 'inherit' stdio: 'inherit',
}); });
}, },
}, },
@ -347,14 +360,14 @@ var commands = {
usage: 'Usage: ' + './nodebb dev'.yellow, usage: 'Usage: ' + './nodebb dev'.yellow,
handler: function () { handler: function () {
process.env.NODE_ENV = 'development'; process.env.NODE_ENV = 'development';
cproc.fork(__dirname + '/loader.js', ['--no-daemon', '--no-silent'], { cproc.fork(loaderPath, ['--no-daemon', '--no-silent'], {
env: process.env env: process.env,
}); });
}, },
}, },
build: { build: {
description: 'Compile static assets (CSS, Javascript, etc)', description: 'Compile static assets (CSS, Javascript, etc)',
usage: 'Usage: ' + './nodebb build'.yellow + ' [js,clientCSS,acpCSS,tpl,lang]'.red + '\n' + usage: 'Usage: ' + './nodebb build'.yellow + ' [js,clientCSS,acpCSS,tpl,lang]'.red + '\n' +
' e.g. ' + './nodebb build js,tpl'.yellow + '\tbuilds JS and templates\n' + ' e.g. ' + './nodebb build js,tpl'.yellow + '\tbuilds JS and templates\n' +
' ' + './nodebb build'.yellow + '\t\tbuilds all targets\n', ' ' + './nodebb build'.yellow + '\t\tbuilds all targets\n',
handler: function () { handler: function () {
@ -422,7 +435,7 @@ var commands = {
var upgradeProc = fork(arr); var upgradeProc = fork(arr);
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');
@ -430,7 +443,7 @@ var commands = {
var message = 'NodeBB Upgrade Complete!'; 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 // 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 columns = process.stdout.columns;
var spaces = columns ? new Array(Math.floor(columns / 2) - (message.length / 2) + 1).join(' ') : " "; var spaces = columns ? new Array(Math.floor(columns / 2) - (message.length / 2) + 1).join(' ') : ' ';
process.stdout.write('OK\n'.green); process.stdout.write('OK\n'.green);
process.stdout.write('\n' + spaces + message.green.bold + '\n\n'.reset); process.stdout.write('\n' + spaces + message.green.bold + '\n\n'.reset);

@ -20,8 +20,8 @@ module.exports = function (Meta) {
target: {}, target: {},
scripts: { scripts: {
base: [ base: [
'./node_modules/jquery/dist/jquery.js', 'node_modules/jquery/dist/jquery.js',
'./node_modules/socket.io-client/dist/socket.io.js', 'node_modules/socket.io-client/dist/socket.io.js',
'public/vendor/jquery/timeago/jquery.timeago.js', 'public/vendor/jquery/timeago/jquery.timeago.js',
'public/vendor/jquery/js/jquery.form.min.js', 'public/vendor/jquery/js/jquery.form.min.js',
'public/vendor/visibility/visibility.min.js', 'public/vendor/visibility/visibility.min.js',
@ -35,14 +35,14 @@ module.exports = function (Meta) {
'public/vendor/tinycon/tinycon.js', 'public/vendor/tinycon/tinycon.js',
'public/vendor/xregexp/xregexp.js', 'public/vendor/xregexp/xregexp.js',
'public/vendor/xregexp/unicode/unicode-base.js', 'public/vendor/xregexp/unicode/unicode-base.js',
'./node_modules/templates.js/lib/templates.js', 'node_modules/templates.js/lib/templates.js',
'public/src/utils.js', 'public/src/utils.js',
'public/src/sockets.js', 'public/src/sockets.js',
'public/src/app.js', 'public/src/app.js',
'public/src/ajaxify.js', 'public/src/ajaxify.js',
'public/src/overrides.js', 'public/src/overrides.js',
'public/src/widgets.js', 'public/src/widgets.js',
'./node_modules/promise-polyfill/promise.js', 'node_modules/promise-polyfill/promise.js',
], ],
// files listed below are only available client-side, or are bundled in to reduce # of network requests on cold load // files listed below are only available client-side, or are bundled in to reduce # of network requests on cold load
@ -84,12 +84,12 @@ module.exports = function (Meta) {
// modules listed below are built (/src/modules) so they can be defined anonymously // modules listed below are built (/src/modules) so they can be defined anonymously
modules: { modules: {
'Chart.js': './node_modules/chart.js/dist/Chart.min.js', 'Chart.js': 'node_modules/chart.js/dist/Chart.min.js',
'mousetrap.js': './node_modules/mousetrap/mousetrap.min.js', 'mousetrap.js': 'node_modules/mousetrap/mousetrap.min.js',
'jqueryui.js': 'public/vendor/jquery/js/jquery-ui.js', 'jqueryui.js': 'public/vendor/jquery/js/jquery-ui.js',
'buzz.js': 'public/vendor/buzz/buzz.js', 'buzz.js': 'public/vendor/buzz/buzz.js',
'cropper.js': './node_modules/cropperjs/dist/cropper.min.js', 'cropper.js': 'node_modules/cropperjs/dist/cropper.min.js',
'zxcvbn.js': './node_modules/zxcvbn/dist/zxcvbn.js', 'zxcvbn.js': 'node_modules/zxcvbn/dist/zxcvbn.js',
}, },
}, },
}; };
@ -196,10 +196,10 @@ module.exports = function (Meta) {
function clearModules(callback) { function clearModules(callback) {
var builtPaths = moduleDirs.map(function (p) { var builtPaths = moduleDirs.map(function (p) {
return '../../build/public/src/' + p; return path.join(__dirname, '../../build/public/src', p);
}); });
async.each(builtPaths, function (builtPath, next) { async.each(builtPaths, function (builtPath, next) {
rimraf(path.join(__dirname, builtPath), next); rimraf(builtPath, next);
}, function (err) { }, function (err) {
callback(err); callback(err);
}); });
@ -314,7 +314,7 @@ module.exports = function (Meta) {
} }
Meta.js.target[target].scripts = Meta.js.target[target].scripts.map(function (script) { Meta.js.target[target].scripts = Meta.js.target[target].scripts.map(function (script) {
return path.relative(basePath, script).replace(/\\/g, '/'); return path.resolve(basePath, script).replace(/\\/g, '/');
}); });
callback(); callback();
@ -328,7 +328,7 @@ module.exports = function (Meta) {
}; };
Meta.js.commitToFile = function (target, callback) { Meta.js.commitToFile = function (target, callback) {
fs.writeFile(path.join(__dirname, '../../build/public/' + target), Meta.js.target[target].cache, function (err) { fs.writeFile(path.join(__dirname, '../../build/public', target), Meta.js.target[target].cache, function (err) {
callback(err); callback(err);
}); });
}; };

@ -2,6 +2,7 @@
(function (module) { (function (module) {
var fork = require('child_process').fork; var fork = require('child_process').fork;
var path = require('path');
module.hash = function (rounds, password, callback) { module.hash = function (rounds, password, callback) {
forkChild({ type: 'hash', rounds: rounds, password: password }, callback); forkChild({ type: 'hash', rounds: rounds, password: password }, callback);
@ -16,7 +17,7 @@
if (global.v8debug || parseInt(process.execArgv.indexOf('--debug'), 10) !== -1) { if (global.v8debug || parseInt(process.execArgv.indexOf('--debug'), 10) !== -1) {
forkProcessParams = { execArgv: ['--debug=' + (5859), '--nolazy'] }; forkProcessParams = { execArgv: ['--debug=' + (5859), '--nolazy'] };
} }
var child = fork('./bcrypt', [], forkProcessParams); var child = fork(path.join(__dirname, 'bcrypt'), [], forkProcessParams);
child.on('message', function (msg) { child.on('message', function (msg) {
if (msg.err) { if (msg.err) {

Loading…
Cancel
Save