Merge pull request #5589 from NodeBB/fix-require.main.require

Fix using `./nodebb` in other CWDs
v1.18.x
Julian Lam 8 years ago committed by GitHub
commit e9fb42eb03

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

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

111
nodebb

@ -2,16 +2,25 @@
'use strict';
var cproc;
var args;
var fs;
var path;
var request;
var semver;
var prompt;
var async;
try {
require('colors');
var cproc = require('child_process');
var args = require('minimist')(process.argv.slice(2));
var fs = require('fs');
var path = require('path');
var request = require('request');
var semver = require('semver');
var prompt = require('prompt');
var async = require('async');
cproc = require('child_process');
args = 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');
@ -23,13 +32,16 @@ try {
}
}
var loaderPath = path.join(__dirname, 'loader.js');
var appPath = path.join(__dirname, 'app.js');
if (args.dev) {
process.env.NODE_ENV = 'development';
}
function getRunningPid(callback) {
fs.readFile(__dirname + '/pidfile', {
encoding: 'utf-8'
fs.readFile(path.join(__dirname, 'pidfile'), {
encoding: 'utf-8',
}, function (err, pid) {
if (err) {
return callback(err);
@ -38,7 +50,7 @@ function getRunningPid(callback) {
try {
process.kill(parseInt(pid, 10), 0);
callback(null, parseInt(pid, 10));
} catch(e) {
} catch (e) {
callback(e);
}
});
@ -52,28 +64,29 @@ function getCurrentVersion(callback) {
try {
pkg = JSON.parse(pkg);
return callback(null, pkg.version);
} catch(err) {
} catch (err) {
return callback(err);
}
});
}
function fork(args) {
return cproc.fork('app.js', args, {
return cproc.fork(appPath, args, {
cwd: __dirname,
silent: false
silent: false,
});
}
function getInstalledPlugins(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' })
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;
var isNbbModule = /^nodebb-(?:plugin|theme|widget|rewards)-[\w-]+$/;
var moduleName;
var isGitRepo;
payload.files = payload.files.filter(function (file) {
return isNbbModule.test(file);
@ -98,7 +111,7 @@ function getInstalledPlugins(callback) {
try {
fs.accessSync(path.join(__dirname, 'node_modules/' + moduleName, '.git'));
isGitRepo = true;
} catch(e) {
} catch (e) {
isGitRepo = false;
}
@ -144,7 +157,7 @@ function checkPlugins(standalone, callback) {
async.waterfall([
async.apply(async.parallel, {
plugins: async.apply(getInstalledPlugins),
version: async.apply(getCurrentVersion)
version: async.apply(getCurrentVersion),
}),
function (payload, next) {
var toCheck = Object.keys(payload.plugins);
@ -157,7 +170,7 @@ function checkPlugins(standalone, callback) {
request({
method: 'GET',
url: 'https://packages.nodebb.org/api/v1/suggest?version=' + payload.version + '&package[]=' + toCheck.join('&package[]='),
json: true
json: true,
}, function (err, res, body) {
if (err) {
process.stdout.write('error'.red + '\n'.reset);
@ -169,25 +182,25 @@ function checkPlugins(standalone, callback) {
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);
var current;
var suggested;
var 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,
};
}
return null;
}).filter(Boolean);
next(null, upgradable);
});
}
},
], callback);
}
function upgradePlugins(callback) {
@ -199,7 +212,7 @@ function upgradePlugins(callback) {
checkPlugins(standalone, function (err, found) {
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);
}
@ -223,7 +236,7 @@ function upgradePlugins(callback) {
prompt.get({
name: 'upgrade',
description: 'Proceed with upgrade (y|n)?'.reset,
type: 'string'
type: 'string',
}, function (err, result) {
if (err) {
return callback(err);
@ -279,8 +292,8 @@ var commands = {
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
cproc.fork(loaderPath, {
env: process.env,
});
},
},
@ -320,7 +333,7 @@ var commands = {
process.stdout.write('\n\n'.reset);
cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname,
stdio: 'inherit'
stdio: 'inherit',
});
},
},
@ -333,12 +346,12 @@ var commands = {
process.stdout.write('\n\n'.reset);
// Spawn a new NodeBB process
cproc.fork(__dirname + '/loader.js', {
env: process.env
cproc.fork(loaderPath, {
env: process.env,
});
cproc.spawn('tail', ['-F', './logs/output.log'], {
cwd: __dirname,
stdio: 'inherit'
stdio: 'inherit',
});
},
},
@ -347,14 +360,14 @@ var commands = {
usage: 'Usage: ' + './nodebb dev'.yellow,
handler: function () {
process.env.NODE_ENV = 'development';
cproc.fork(__dirname + '/loader.js', ['--no-daemon', '--no-silent'], {
env: process.env
cproc.fork(loaderPath, ['--no-daemon', '--no-silent'], {
env: process.env,
});
},
},
build: {
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' +
' ' + './nodebb build'.yellow + '\t\tbuilds all targets\n',
handler: function () {
@ -422,7 +435,7 @@ var commands = {
var upgradeProc = fork(arr);
upgradeProc.on('close', next);
}
},
], function (err) {
if (err) {
process.stdout.write('\nError'.red + ': ' + err.message + '\n');
@ -430,7 +443,7 @@ var commands = {
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(' ') : " ";
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);

@ -11,7 +11,7 @@
"main": "app.js",
"scripts": {
"start": "node loader.js",
"lint": "eslint --cache .",
"lint": "eslint --cache ./nodebb .",
"pretest": "npm run lint",
"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"

@ -20,8 +20,8 @@ module.exports = function (Meta) {
target: {},
scripts: {
base: [
'./node_modules/jquery/dist/jquery.js',
'./node_modules/socket.io-client/dist/socket.io.js',
'node_modules/jquery/dist/jquery.js',
'node_modules/socket.io-client/dist/socket.io.js',
'public/vendor/jquery/timeago/jquery.timeago.js',
'public/vendor/jquery/js/jquery.form.min.js',
'public/vendor/visibility/visibility.min.js',
@ -35,14 +35,14 @@ module.exports = function (Meta) {
'public/vendor/tinycon/tinycon.js',
'public/vendor/xregexp/xregexp.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/sockets.js',
'public/src/app.js',
'public/src/ajaxify.js',
'public/src/overrides.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
@ -84,12 +84,12 @@ module.exports = function (Meta) {
// modules listed below are built (/src/modules) so they can be defined anonymously
modules: {
'Chart.js': './node_modules/chart.js/dist/Chart.min.js',
'mousetrap.js': './node_modules/mousetrap/mousetrap.min.js',
'Chart.js': 'node_modules/chart.js/dist/Chart.min.js',
'mousetrap.js': 'node_modules/mousetrap/mousetrap.min.js',
'jqueryui.js': 'public/vendor/jquery/js/jquery-ui.js',
'buzz.js': 'public/vendor/buzz/buzz.js',
'cropper.js': './node_modules/cropperjs/dist/cropper.min.js',
'zxcvbn.js': './node_modules/zxcvbn/dist/zxcvbn.js',
'cropper.js': 'node_modules/cropperjs/dist/cropper.min.js',
'zxcvbn.js': 'node_modules/zxcvbn/dist/zxcvbn.js',
},
},
};
@ -196,10 +196,10 @@ module.exports = function (Meta) {
function clearModules(callback) {
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) {
rimraf(path.join(__dirname, builtPath), next);
rimraf(builtPath, next);
}, function (err) {
callback(err);
});
@ -314,7 +314,7 @@ module.exports = function (Meta) {
}
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();
@ -328,7 +328,7 @@ module.exports = function (Meta) {
};
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);
});
};

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

Loading…
Cancel
Save