Skip to content

Node.js based forum software built for the modern web

Resolve merge conflicts

Peter Jaszkowiakcd1f0ddc79b8
diff --cc src/meta/build.js
index dfeb4d7825,290957512a..edb7d526ba
--- a/src/meta/build.js
+++ b/src/meta/build.js
@@@ -183,11 -178,14 +182,14 @@@ function build(targets, callback)
var startTime;
var totalTime;
async.series([
- beforeBuild,
++ async.apply(beforeBuild, targets),
function (next) {
- beforeBuild(targets, next);
- },
- function (next) {
- var parallel = os.cpus().length > 1 && !nconf.get('series');
+ var threads = parseInt(nconf.get('threads'), 10);
+ if (threads) {
+ require('./minifier').maxThreads = threads - 1;
+ }
+
+ var parallel = !nconf.get('series');
if (parallel) {
winston.info('[build] Building in parallel mode');
} else {
diff --cc test/build.js
index 1f73f5746b,215b823ce2..ceb33962be
--- a/test/build.js
+++ b/test/build.js
@@@ -1,19 -1,195 +1,199 @@@
'use strict';
+ var string = require('string');
+ var path = require('path');
+ var fs = require('fs');
var assert = require('assert');
+ var mkdirp = require('mkdirp');
+ var rimraf = require('rimraf');
++var async = require('async');
var db = require('./mocks/databasemock');
+ var file = require('../src/file');
- describe('Build', function () {
+ describe('minifier', function () {
before(function (done) {
- db.setupMockDefaults(done);
+ mkdirp(path.join(__dirname, '../build/test'), done);
});
- it('should build all assets', function (done) {
- this.timeout(50000);
- var build = require('../src/meta/build');
- build.buildAll(function (err) {
+ var minifier = require('../src/meta/minifier');
+ var scripts = [
+ path.resolve(__dirname, './files/1.js'),
+ path.resolve(__dirname, './files/2.js'),
+ ];
+ it('.js.bundle() should concat scripts', function (done) {
+ minifier.js.bundle(scripts, false, false, function (err, bundle) {
assert.ifError(err);
+ assert.strictEqual(
+ bundle.code,
+ '(function (window, document) {' +
+ '\n\twindow.doStuff = function () {' +
+ '\n\t\tdocument.body.innerHTML = \'Stuff has been done\';' +
+ '\n\t};' +
+ '\n})(window, document);' +
+ '\n' +
+ '\n;function foo(name, age) {' +
+ '\n\treturn \'The person known as "\' + name + \'" is \' + age + \' years old\';' +
+ '\n}' +
+ '\n'
+ );
+ done();
+ });
+ });
+
+ it('.js.bundle() should minify scripts', function (done) {
+ minifier.js.bundle(scripts, true, false, function (err, bundle) {
+ assert.ifError(err);
+ assert.strictEqual(
+ bundle.code,
+ '(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);function foo(n,o){return\'The person known as "\'+n+\'" is \'+o+" years old"}'
+ );
+ done();
+ });
+ });
+
+ it('.js.minifyBatch() should minify each script', function (done) {
+ var s = scripts.map(function (script) {
+ return {
+ srcPath: script,
+ destPath: path.resolve(__dirname, '../build/test', path.basename(script)),
+ };
+ });
+ minifier.js.minifyBatch(s, false, function (err) {
+ assert.ifError(err);
+
+ assert(file.existsSync(s[0].destPath));
+ assert(file.existsSync(s[1].destPath));
+
+ fs.readFile(s[0].destPath, function (err, buffer) {
+ assert.ifError(err);
+ assert.strictEqual(
+ buffer.toString(),
+ '(function(n,o){n.doStuff=function(){o.body.innerHTML="Stuff has been done"}})(window,document);'
+ );
+ done();
+ });
+ });
+ });
+
+ var styles = [
+ '@import (inline) "./1.css";',
+ '@import "./2.less";',
+ ].join('\n');
+ var paths = [
+ path.resolve(__dirname, './files'),
+ ];
+ it('.css.bundle() should concat styles', function (done) {
+ minifier.css.bundle(styles, paths, false, false, function (err, bundle) {
+ assert.ifError(err);
+ assert.strictEqual(bundle.code, '.help { margin: 10px; } .yellow { background: yellow; }\n.help {\n display: block;\n}\n.help .blue {\n background: blue;\n}\n');
+ done();
+ });
+ });
+
+ it('.css.bundle() should minify styles', function (done) {
+ minifier.css.bundle(styles, paths, true, false, function (err, bundle) {
+ assert.ifError(err);
+ assert.strictEqual(bundle.code, '.help{margin:10px;display:block}.yellow{background:#ff0}.help .blue{background:#00f}');
+ done();
+ });
+ });
+ });
+
+ describe('Build', function (done) {
+ var build = require('../src/meta/build');
+
+ before(function (done) {
- rimraf(path.join(__dirname, '../build/public'), done);
++ async.parallel([
++ async.apply(rimraf, path.join(__dirname, '../build/public')),
++ db.setupMockDefaults,
++ ], done);
+ });
+
+ it('should build plugin static dirs', function (done) {
+ build.build(['plugin static dirs'], function (err) {
+ assert.ifError(err);
+ assert(file.existsSync(path.join(__dirname, '../build/public/plugins/nodebb-plugin-dbsearch/dbsearch')));
+ done();
+ });
+ });
+
+ it('should build requirejs modules', function (done) {
+ build.build(['requirejs modules'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/src/modules/Chart.js');
+ assert(file.existsSync(filename));
+ assert(fs.readFileSync(filename).toString().startsWith('/*!\n * Chart.js'));
+ done();
+ });
+ });
+
+ it('should build client js bundle', function (done) {
+ build.build(['client js bundle'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/nodebb.min.js');
+ assert(file.existsSync(filename));
+ assert(fs.readFileSync(filename).length > 1000);
+ done();
+ });
+ });
+
+ it('should build admin js bundle', function (done) {
+ build.build(['admin js bundle'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/acp.min.js');
+ assert(file.existsSync(filename));
+ assert(fs.readFileSync(filename).length > 1000);
+ done();
+ });
+ });
+
+ it('should build client side styles', function (done) {
+ build.build(['client side styles'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/stylesheet.css');
+ assert(file.existsSync(filename));
+ assert(fs.readFileSync(filename).toString().startsWith('/*! normalize.css'));
+ done();
+ });
+ });
+
+ it('should build admin control panel styles', function (done) {
+ build.build(['admin control panel styles'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/admin.css');
+ assert(file.existsSync(filename));
+ assert(fs.readFileSync(filename).toString().startsWith('@charset "UTF-8";'));
+ done();
+ });
+ });
+
+ it('should build templates', function (done) {
+ build.build(['templates'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/templates/admin/header.tpl');
+ assert(file.existsSync(filename));
+ assert(fs.readFileSync(filename).toString().startsWith('<!DOCTYPE html>'));
+ done();
+ });
+ });
+
+ it('should build languages', function (done) {
+ build.build(['languages'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/language/en-GB/global.json');
+ assert(file.existsSync(filename));
+ var global = fs.readFileSync(filename).toString();
+ assert.strictEqual(JSON.parse(global).home, 'Home');
+ done();
+ });
+ });
+
+ it('should build sounds', function (done) {
+ build.build(['sounds'], function (err) {
+ assert.ifError(err);
+ var filename = path.join(__dirname, '../build/public/sounds/fileMap.json');
+ assert(file.existsSync(filename));
done();
});
});
diff --cc test/mocks/databasemock.js
index ca280dda9e,bd7831ac82..f64c02942a
--- a/test/mocks/databasemock.js
+++ b/test/mocks/databasemock.js
@@@ -5,185 -5,178 +5,182 @@@
* ATTENTION: testing db is flushed before every use!
*/
- (function (module) {
- var async = require('async');
- var winston = require('winston');
- var path = require('path');
- var nconf = require('nconf');
- var url = require('url');
- var errorText;
-
-
- nconf.file({ file: path.join(__dirname, '../../config.json') });
- nconf.defaults({
- base_dir: path.join(__dirname, '../..'),
- themes_path: path.join(__dirname, '../../node_modules'),
- upload_path: 'public/uploads',
- views_dir: path.join(__dirname, '../../build/public/templates'),
- relative_path: '',
- });
-
- if (!nconf.get('isCluster')) {
- nconf.set('isPrimary', 'true');
- nconf.set('isCluster', 'false');
- }
-
- var dbType = nconf.get('database');
- var testDbConfig = nconf.get('test_database');
- var productionDbConfig = nconf.get(dbType);
-
- if (!testDbConfig) {
- errorText = 'test_database is not defined';
- winston.info(
- '\n===========================================================\n' +
- 'Please, add parameters for test database in config.json\n' +
- 'For example (redis):\n' +
- '"test_database": {\n' +
- ' "host": "127.0.0.1",\n' +
- ' "port": "6379",\n' +
- ' "password": "",\n' +
- ' "database": "1"\n' +
- '}\n' +
- ' or (mongo):\n' +
- '"test_database": {\n' +
- ' "host": "127.0.0.1",\n' +
- ' "port": "27017",\n' +
- ' "password": "",\n' +
- ' "database": "1\n' +
- '}\n' +
- ' or (mongo) in a replicaset\n' +
- '"test_database": {\n' +
- ' "host": "127.0.0.1,127.0.0.1,127.0.0.1",\n' +
- ' "port": "27017,27018,27019",\n' +
- ' "username": "",\n' +
- ' "password": "",\n' +
- ' "database": "nodebb_test"\n' +
- '}\n' +
- '==========================================================='
- );
- winston.error(errorText);
- throw new Error(errorText);
- }
-
- if (testDbConfig.database === productionDbConfig.database &&
- testDbConfig.host === productionDbConfig.host &&
- testDbConfig.port === productionDbConfig.port) {
- errorText = 'test_database has the same config as production db';
- winston.error(errorText);
- throw new Error(errorText);
- }
-
- nconf.set(dbType, testDbConfig);
-
- winston.info('database config');
- winston.info(dbType);
- winston.info(testDbConfig);
-
- var db = require('../../src/database');
-
- before(function (done) {
- this.timeout(30000);
- async.series([
- function (next) {
- db.init(next);
- },
- function (next) {
- setupMockDefaults(next);
- },
- function (next) {
- db.initSessionStore(next);
- },
- function (next) {
- var meta = require('../../src/meta');
-
- // nconf defaults, if not set in config
- if (!nconf.get('sessionKey')) {
- nconf.set('sessionKey', 'express.sid');
- }
- // Parse out the relative_url and other goodies from the configured URL
- var urlObject = url.parse(nconf.get('url'));
- var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
- nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
- nconf.set('secure', urlObject.protocol === 'https:');
- nconf.set('use_port', !!urlObject.port);
- nconf.set('relative_path', relativePath);
- nconf.set('port', urlObject.port || nconf.get('port') || nconf.get('PORT') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
- nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path')));
-
- nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
- nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
- nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
- nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
- nconf.set('bcrypt_rounds', 1);
-
- next();
- },
- function (next) {
- var webserver = require('../../src/webserver');
- var sockets = require('../../src/socket.io');
- sockets.init(webserver.server);
-
- require('../../src/notifications').startJobs();
- require('../../src/user').startJobs();
-
- webserver.listen(next);
- },
- ], done);
- });
-
- function setupMockDefaults(callback) {
- var meta = require('../../src/meta');
-
- async.series([
- function (next) {
- db.emptydb(next);
- },
- function (next) {
- winston.info('test_database flushed');
- setupDefaultConfigs(meta, next);
- },
- function (next) {
- meta.configs.init(next);
- },
- function (next) {
- meta.dependencies.check(next);
- },
- function (next) {
- meta.config.postDelay = 0;
- meta.config.initialPostDelay = 0;
- meta.config.newbiePostDelay = 0;
-
- enableDefaultPlugins(next);
- },
- function (next) {
- meta.themes.set({
- type: 'local',
- id: 'nodebb-theme-persona',
- }, next);
- },
- ], callback);
- }
- db.setupMockDefaults = setupMockDefaults;
-
- function setupDefaultConfigs(meta, next) {
- winston.info('Populating database with default configs, if not already set...\n');
-
- var defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json'));
-
- meta.configs.setOnEmpty(defaults, next);
- }
-
- function enableDefaultPlugins(callback) {
- winston.info('Enabling default plugins\n');
-
- var defaultEnabled = [
- 'nodebb-plugin-dbsearch',
- ];
-
- winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
-
- db.sortedSetAdd('plugins:active', [0], defaultEnabled, callback);
- }
-
- module.exports = db;
- }(module));
+ var async = require('async');
+ var winston = require('winston');
+ var path = require('path');
+ var nconf = require('nconf');
+ var url = require('url');
+ var errorText;
+
+
+ nconf.file({ file: path.join(__dirname, '../../config.json') });
+ nconf.defaults({
+ base_dir: path.join(__dirname, '../..'),
+ themes_path: path.join(__dirname, '../../node_modules'),
+ upload_path: 'public/uploads',
+ views_dir: path.join(__dirname, '../../build/public/templates'),
+ relative_path: '',
+ });
+
+ if (!nconf.get('isCluster')) {
+ nconf.set('isPrimary', 'true');
+ nconf.set('isCluster', 'false');
+ }
+
+ var dbType = nconf.get('database');
+ var testDbConfig = nconf.get('test_database');
+ var productionDbConfig = nconf.get(dbType);
+
+ if (!testDbConfig) {
+ errorText = 'test_database is not defined';
+ winston.info(
+ '\n===========================================================\n' +
+ 'Please, add parameters for test database in config.json\n' +
+ 'For example (redis):\n' +
+ '"test_database": {\n' +
+ ' "host": "127.0.0.1",\n' +
+ ' "port": "6379",\n' +
+ ' "password": "",\n' +
+ ' "database": "1"\n' +
+ '}\n' +
+ ' or (mongo):\n' +
+ '"test_database": {\n' +
+ ' "host": "127.0.0.1",\n' +
+ ' "port": "27017",\n' +
+ ' "password": "",\n' +
+ ' "database": "1\n' +
+ '}\n' +
+ ' or (mongo) in a replicaset\n' +
+ '"test_database": {\n' +
+ ' "host": "127.0.0.1,127.0.0.1,127.0.0.1",\n' +
+ ' "port": "27017,27018,27019",\n' +
+ ' "username": "",\n' +
+ ' "password": "",\n' +
+ ' "database": "nodebb_test"\n' +
+ '}\n' +
+ '==========================================================='
+ );
+ winston.error(errorText);
+ throw new Error(errorText);
+ }
+
+ if (testDbConfig.database === productionDbConfig.database &&
+ testDbConfig.host === productionDbConfig.host &&
+ testDbConfig.port === productionDbConfig.port) {
+ errorText = 'test_database has the same config as production db';
+ winston.error(errorText);
+ throw new Error(errorText);
+ }
+
+ nconf.set(dbType, testDbConfig);
+
+ winston.info('database config');
+ winston.info(dbType);
+ winston.info(testDbConfig);
+
+ var db = require('../../src/database');
+ module.exports = db;
+
-after(function (done) {
- db.close(done);
-});
-
+ before(function (done) {
+ this.timeout(30000);
- var meta;
- async.waterfall([
++ async.series([
+ function (next) {
+ db.init(next);
+ },
+ function (next) {
- db.emptydb(next);
- },
- function (next) {
- db.createIndices(next);
- },
- function (next) {
- winston.info('test_database flushed');
- meta = require('../../src/meta');
- setupDefaultConfigs(meta, next);
- },
- function (next) {
- meta.configs.init(next);
++ setupMockDefaults(next);
+ },
+ function (next) {
+ db.initSessionStore(next);
+ },
+ function (next) {
- meta.dependencies.check(next);
- },
- function (next) {
- meta.config.postDelay = 0;
- meta.config.initialPostDelay = 0;
- meta.config.newbiePostDelay = 0;
++ var meta = require('../../src/meta');
+
- enableDefaultPlugins(next);
- },
- function (next) {
- meta.themes.set({
- type: 'local',
- id: 'nodebb-theme-persona',
- }, next);
- },
- function (next) {
+ // nconf defaults, if not set in config
+ if (!nconf.get('sessionKey')) {
+ nconf.set('sessionKey', 'express.sid');
+ }
+ // Parse out the relative_url and other goodies from the configured URL
+ var urlObject = url.parse(nconf.get('url'));
+ var relativePath = urlObject.pathname !== '/' ? urlObject.pathname : '';
+ nconf.set('base_url', urlObject.protocol + '//' + urlObject.host);
+ nconf.set('secure', urlObject.protocol === 'https:');
+ nconf.set('use_port', !!urlObject.port);
+ nconf.set('relative_path', relativePath);
+ nconf.set('port', urlObject.port || nconf.get('port') || nconf.get('PORT') || (nconf.get('PORT_ENV_VAR') ? nconf.get(nconf.get('PORT_ENV_VAR')) : false) || 4567);
+ nconf.set('upload_path', path.join(nconf.get('base_dir'), nconf.get('upload_path')));
+
+ nconf.set('core_templates_path', path.join(__dirname, '../../src/views'));
+ nconf.set('base_templates_path', path.join(nconf.get('themes_path'), 'nodebb-theme-persona/templates'));
+ nconf.set('theme_templates_path', meta.config['theme:templates'] ? path.join(nconf.get('themes_path'), meta.config['theme:id'], meta.config['theme:templates']) : nconf.get('base_templates_path'));
+ nconf.set('theme_config', path.join(nconf.get('themes_path'), 'nodebb-theme-persona', 'theme.json'));
+ nconf.set('bcrypt_rounds', 1);
+
+ next();
+ },
+ function (next) {
+ var webserver = require('../../src/webserver');
+ var sockets = require('../../src/socket.io');
+ sockets.init(webserver.server);
+
+ require('../../src/notifications').startJobs();
+ require('../../src/user').startJobs();
+
+ webserver.listen(next);
+ },
+ ], done);
+ });
+
++function setupMockDefaults(callback) {
++ var meta = require('../../src/meta');
++
++ async.series([
++ function (next) {
++ db.emptydb(next);
++ },
++ function (next) {
++ winston.info('test_database flushed');
++ setupDefaultConfigs(meta, next);
++ },
++ function (next) {
++ meta.configs.init(next);
++ },
++ function (next) {
++ meta.dependencies.check(next);
++ },
++ function (next) {
++ meta.config.postDelay = 0;
++ meta.config.initialPostDelay = 0;
++ meta.config.newbiePostDelay = 0;
++
++ enableDefaultPlugins(next);
++ },
++ function (next) {
++ meta.themes.set({
++ type: 'local',
++ id: 'nodebb-theme-persona',
++ }, next);
++ },
++ ], callback);
++}
++db.setupMockDefaults = setupMockDefaults;
++
+ function setupDefaultConfigs(meta, next) {
+ winston.info('Populating database with default configs, if not already set...\n');
+
+ var defaults = require(path.join(nconf.get('base_dir'), 'install/data/defaults.json'));
+
+ meta.configs.setOnEmpty(defaults, next);
+ }
+
+ function enableDefaultPlugins(callback) {
+ winston.info('Enabling default plugins\n');
+
+ var defaultEnabled = [
+ 'nodebb-plugin-dbsearch',
+ ];
+
+ winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
+
+ db.sortedSetAdd('plugins:active', [0], defaultEnabled, callback);
+ }