Merge pull request #5689 from NodeBB/plugin-load-ref

Plugin load refactor
v1.18.x
Julian Lam 8 years ago committed by GitHub
commit fc4f016932

@ -92,7 +92,7 @@ aliases = Object.keys(aliases).reduce(function (prev, key) {
return prev; return prev;
}, {}); }, {});
function beforeBuild(callback) { function beforeBuild(targets, callback) {
var db = require('../database'); var db = require('../database');
var plugins = require('../plugins'); var plugins = require('../plugins');
meta = require('../meta'); meta = require('../meta');
@ -100,7 +100,7 @@ function beforeBuild(callback) {
async.series([ async.series([
db.init, db.init,
meta.themes.setupPaths, meta.themes.setupPaths,
plugins.prepareForBuild, async.apply(plugins.prepareForBuild, targets),
], function (err) { ], function (err) {
if (err) { if (err) {
winston.error('[build] Encountered error preparing for build: ' + err.message); winston.error('[build] Encountered error preparing for build: ' + err.message);
@ -159,6 +159,8 @@ function build(targets, callback) {
return arr.indexOf(target) === i; return arr.indexOf(target) === i;
}); });
winston.verbose('[build] building the following targets: ' + targets.join(', '));
if (typeof callback !== 'function') { if (typeof callback !== 'function') {
callback = function (err) { callback = function (err) {
if (err) { if (err) {
@ -178,7 +180,7 @@ function build(targets, callback) {
var startTime; var startTime;
var totalTime; var totalTime;
async.series([ async.series([
beforeBuild, async.apply(beforeBuild, targets),
function (next) { function (next) {
var threads = parseInt(nconf.get('threads'), 10); var threads = parseInt(nconf.get('threads'), 10);
if (threads) { if (threads) {

@ -6,97 +6,44 @@ var async = require('async');
var fs = require('fs'); var fs = require('fs');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var rimraf = require('rimraf'); var rimraf = require('rimraf');
var _ = require('underscore');
var file = require('../file'); var file = require('../file');
var Plugins = require('../plugins'); var Plugins = require('../plugins');
var db = require('../database');
var buildLanguagesPath = path.join(__dirname, '../../build/public/language'); var buildLanguagesPath = path.join(__dirname, '../../build/public/language');
var coreLanguagesPath = path.join(__dirname, '../../public/language'); var coreLanguagesPath = path.join(__dirname, '../../public/language');
function getTranslationTree(callback) { function getTranslationTree(callback) {
async.waterfall([ async.waterfall([
// get plugin data // generate list of languages and namespaces
function (next) { function (next) {
db.getSortedSetRange('plugins:active', 0, -1, next); file.walk(coreLanguagesPath, next);
},
function (plugins, next) {
var pluginBasePath = path.join(__dirname, '../../node_modules');
var paths = plugins.map(function (plugin) {
return path.join(pluginBasePath, plugin);
});
// Filter out plugins with invalid paths
async.filter(paths, file.exists, next);
}, },
function (paths, next) { function (paths, next) {
async.map(paths, Plugins.loadPluginInfo, next);
},
// generate list of languages and namespaces
function (plugins, next) {
var languages = []; var languages = [];
var namespaces = []; var namespaces = [];
// pull languages and namespaces from paths paths.forEach(function (p) {
function extrude(languageDir, paths) { if (!p.endsWith('.json')) {
paths.forEach(function (p) { return;
var rel = p.split(languageDir)[1].split(/[/\\]/).slice(1);
var language = rel.shift().replace('_', '-').replace('@', '-x-');
var namespace = rel.join('/').replace(/\.json$/, '');
if (!language || !namespace) {
return;
}
if (languages.indexOf(language) === -1) {
languages.push(language);
}
if (namespaces.indexOf(namespace) === -1) {
namespaces.push(namespace);
}
});
}
plugins = plugins.filter(function (pluginData) {
return (typeof pluginData.languages === 'string');
});
async.parallel([
// get core languages and namespaces
function (nxt) {
file.walk(coreLanguagesPath, function (err, paths) {
if (err) {
return nxt(err);
}
extrude(coreLanguagesPath, paths);
nxt();
});
},
// get plugin languages and namespaces
function (nxt) {
async.each(plugins, function (pluginData, cb) {
var pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
file.walk(pathToFolder, function (err, paths) {
if (err) {
return cb(err);
}
extrude(pathToFolder, paths);
cb();
});
}, nxt);
},
], function (err) {
if (err) {
return next(err);
} }
next(null, { var rel = path.relative(coreLanguagesPath, p).split(/[/\\]/);
languages: languages, var language = rel.shift().replace('_', '-').replace('@', '-x-');
namespaces: namespaces, var namespace = rel.join('/').replace(/\.json$/, '');
plugins: plugins,
}); if (!language || !namespace) {
return;
}
languages.push(language);
namespaces.push(namespace);
});
next(null, {
languages: _.union(languages, Plugins.languageData.languages).sort().filter(Boolean),
namespaces: _.union(namespaces, Plugins.languageData.namespaces).sort().filter(Boolean),
}); });
}, },
@ -109,8 +56,8 @@ function getTranslationTree(callback) {
}, },
function (x, next) { function (x, next) {
fs.writeFile(path.join(buildLanguagesPath, 'metadata.json'), JSON.stringify({ fs.writeFile(path.join(buildLanguagesPath, 'metadata.json'), JSON.stringify({
languages: ref.languages.sort(), languages: ref.languages,
namespaces: ref.namespaces.sort(), namespaces: ref.namespaces,
}), next); }), next);
}, },
function (next) { function (next) {
@ -125,40 +72,42 @@ function getTranslationTree(callback) {
function (ref, next) { function (ref, next) {
var languages = ref.languages; var languages = ref.languages;
var namespaces = ref.namespaces; var namespaces = ref.namespaces;
var plugins = ref.plugins; var plugins = _.values(Plugins.pluginsData).filter(function (plugin) {
return typeof plugin.languages === 'string';
});
var tree = {}; var tree = {};
async.eachLimit(languages, 10, function (lang, nxt) { async.eachLimit(languages, 10, function (lang, next) {
async.eachLimit(namespaces, 10, function (ns, cb) { async.eachLimit(namespaces, 10, function (namespace, next) {
var translations = {}; var translations = {};
async.series([ async.series([
// core first // core first
function (n) { function (cb) {
fs.readFile(path.join(coreLanguagesPath, lang, ns + '.json'), function (err, buffer) { fs.readFile(path.join(coreLanguagesPath, lang, namespace + '.json'), function (err, buffer) {
if (err) { if (err) {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
return n(); return cb();
} }
return n(err); return cb(err);
} }
try { try {
Object.assign(translations, JSON.parse(buffer.toString())); Object.assign(translations, JSON.parse(buffer.toString()));
n(); cb();
} catch (err) { } catch (err) {
n(err); cb(err);
} }
}); });
}, },
function (n) { function (cb) {
// for each plugin, fallback in this order: // for each plugin, fallback in this order:
// 1. correct language string (en-GB) // 1. correct language string (en-GB)
// 2. old language string (en_GB) // 2. old language string (en_GB)
// 3. corrected plugin defaultLang (en-US) // 3. corrected plugin defaultLang (en-US)
// 4. old plugin defaultLang (en_US) // 4. old plugin defaultLang (en_US)
async.eachLimit(plugins, 10, function (pluginData, call) { async.eachLimit(plugins, 10, function (pluginData, done) {
var pluginLanguages = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages); var pluginLanguages = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
var defaultLang = pluginData.defaultLang || 'en-GB'; var defaultLang = pluginData.defaultLang || 'en-GB';
@ -168,7 +117,7 @@ function getTranslationTree(callback) {
defaultLang.replace('_', '-').replace('@', '-x-'), defaultLang.replace('_', '-').replace('@', '-x-'),
defaultLang.replace('-', '_').replace('-x-', '@'), defaultLang.replace('-', '_').replace('-x-', '@'),
], function (language, next) { ], function (language, next) {
fs.readFile(path.join(pluginLanguages, language, ns + '.json'), function (err, buffer) { fs.readFile(path.join(pluginLanguages, language, namespace + '.json'), function (err, buffer) {
if (err) { if (err) {
if (err.code === 'ENOENT') { if (err.code === 'ENOENT') {
return next(null, false); return next(null, false);
@ -183,21 +132,21 @@ function getTranslationTree(callback) {
next(err); next(err);
} }
}); });
}, call); }, done);
}, function (err) { }, function (err) {
if (err) { if (err) {
return n(err); return cb(err);
} }
if (Object.keys(translations).length) { if (Object.keys(translations).length) {
tree[lang] = tree[lang] || {}; tree[lang] = tree[lang] || {};
tree[lang][ns] = translations; tree[lang][namespace] = translations;
} }
n(); cb();
}); });
}, },
], cb); ], next);
}, nxt); }, next);
}, function (err) { }, function (err) {
next(err, tree); next(err, tree);
}); });
@ -208,9 +157,9 @@ function getTranslationTree(callback) {
// write translation hashes from the generated tree to language files // write translation hashes from the generated tree to language files
function writeLanguageFiles(tree, callback) { function writeLanguageFiles(tree, callback) {
// iterate over languages and namespaces // iterate over languages and namespaces
async.eachLimit(Object.keys(tree), 10, function (language, cb) { async.eachLimit(Object.keys(tree), 100, function (language, cb) {
var namespaces = tree[language]; var namespaces = tree[language];
async.eachLimit(Object.keys(namespaces), 100, function (namespace, next) { async.eachLimit(Object.keys(namespaces), 10, function (namespace, next) {
var translations = namespaces[namespace]; var translations = namespaces[namespace];
var filePath = path.join(buildLanguagesPath, language, namespace + '.json'); var filePath = path.join(buildLanguagesPath, language, namespace + '.json');

@ -8,10 +8,8 @@ var semver = require('semver');
var express = require('express'); var express = require('express');
var nconf = require('nconf'); var nconf = require('nconf');
var db = require('./database');
var hotswap = require('./hotswap'); var hotswap = require('./hotswap');
var file = require('./file'); var file = require('./file');
var languages = require('./languages');
var app; var app;
var middleware; var middleware;
@ -20,7 +18,12 @@ var middleware;
require('./plugins/install')(Plugins); require('./plugins/install')(Plugins);
require('./plugins/load')(Plugins); require('./plugins/load')(Plugins);
require('./plugins/hooks')(Plugins); require('./plugins/hooks')(Plugins);
Plugins.data = require('./plugins/data');
Plugins.getPluginPaths = Plugins.data.getPluginPaths;
Plugins.loadPluginInfo = Plugins.data.loadPluginInfo;
Plugins.pluginsData = {};
Plugins.libraries = {}; Plugins.libraries = {};
Plugins.loadedHooks = {}; Plugins.loadedHooks = {};
Plugins.staticDirs = {}; Plugins.staticDirs = {};
@ -30,8 +33,8 @@ var middleware;
Plugins.acpScripts = []; Plugins.acpScripts = [];
Plugins.libraryPaths = []; Plugins.libraryPaths = [];
Plugins.versionWarning = []; Plugins.versionWarning = [];
Plugins.languageCodes = [];
Plugins.soundpacks = []; Plugins.soundpacks = [];
Plugins.languageData = {};
Plugins.initialized = false; Plugins.initialized = false;
@ -84,21 +87,7 @@ var middleware;
Plugins.libraryPaths.length = 0; Plugins.libraryPaths.length = 0;
async.waterfall([ async.waterfall([
function (next) { Plugins.getPluginPaths,
// Build language code list
languages.list(function (err, languages) {
if (err) {
return next(err);
}
Plugins.languageCodes = languages.map(function (data) {
return data.code;
});
next();
});
},
async.apply(Plugins.getPluginPaths),
function (paths, next) { function (paths, next) {
async.eachSeries(paths, Plugins.loadPlugin, next); async.eachSeries(paths, Plugins.loadPlugin, next);
}, },
@ -150,21 +139,7 @@ var middleware;
var templates = {}; var templates = {};
var tplName; var tplName;
async.waterfall([ Plugins.data.getActive(function (err, plugins) {
async.apply(db.getSortedSetRange, 'plugins:active', 0, -1),
function (plugins, next) {
var pluginBasePath = path.join(__dirname, '../node_modules');
var paths = plugins.map(function (plugin) {
return path.join(pluginBasePath, plugin);
});
// Filter out plugins with invalid paths
async.filter(paths, file.exists, next);
},
function (paths, next) {
async.map(paths, Plugins.loadPluginInfo, next);
},
], function (err, plugins) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

@ -0,0 +1,336 @@
'use strict';
var fs = require('fs');
var path = require('path');
var async = require('async');
var winston = require('winston');
var db = require('../database');
var file = require('../file');
var Data = module.exports;
var basePath = path.join(__dirname, '../../');
function getPluginPaths(callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('plugins:active', 0, -1, next);
},
function (plugins, next) {
if (!Array.isArray(plugins)) {
return next();
}
plugins = plugins.filter(function (plugin) {
return plugin && typeof plugin === 'string';
}).map(function (plugin) {
return path.join(__dirname, '../../node_modules/', plugin);
});
async.filter(plugins, file.exists, next);
},
], callback);
}
Data.getPluginPaths = getPluginPaths;
function loadPluginInfo(pluginPath, callback) {
async.parallel({
package: function (next) {
fs.readFile(path.join(pluginPath, 'package.json'), next);
},
plugin: function (next) {
fs.readFile(path.join(pluginPath, 'plugin.json'), next);
},
}, function (err, results) {
if (err) {
return callback(err);
}
var pluginData;
var packageData;
try {
pluginData = JSON.parse(results.plugin);
packageData = JSON.parse(results.package);
pluginData.id = packageData.name;
pluginData.name = packageData.name;
pluginData.description = packageData.description;
pluginData.version = packageData.version;
pluginData.repository = packageData.repository;
pluginData.nbbpm = packageData.nbbpm;
pluginData.path = pluginPath;
} catch (err) {
var pluginDir = path.basename(pluginPath);
winston.error('[plugins/' + pluginDir + '] Error in plugin.json or package.json! ' + err.message);
return callback(new Error('[[error:parse-error]]'));
}
callback(null, pluginData);
});
}
Data.loadPluginInfo = loadPluginInfo;
function getAllPluginData(callback) {
async.waterfall([
function (next) {
getPluginPaths(next);
},
function (pluginPaths, next) {
async.map(pluginPaths, loadPluginInfo, next);
},
], callback);
}
Data.getActive = getAllPluginData;
function getStaticDirectories(pluginData, callback) {
var validMappedPath = /^[\w\-_]+$/;
if (!pluginData.staticDirs) {
return callback();
}
var dirs = Object.keys(pluginData.staticDirs);
if (!dirs.length) {
return callback();
}
var staticDirs = {};
async.each(dirs, function (route, next) {
if (!validMappedPath.test(route)) {
winston.warn('[plugins/' + pluginData.id + '] Invalid mapped path specified: ' +
route + '. Path must adhere to: ' + validMappedPath.toString());
return next();
}
var dirPath = path.join(pluginData.path, pluginData.staticDirs[route]);
fs.stat(dirPath, function (err, stats) {
if (err && err.code === 'ENOENT') {
winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' +
route + ' => ' + dirPath + '\' not found.');
return next();
}
if (err) {
return next(err);
}
if (!stats.isDirectory()) {
winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' +
route + ' => ' + dirPath + '\' is not a directory.');
return next();
}
staticDirs[pluginData.id + '/' + route] = dirPath;
next();
});
}, function (err) {
if (err) {
return callback(err);
}
winston.verbose('[plugins] found ' + Object.keys(staticDirs).length +
' static directories for ' + pluginData.id);
callback(null, staticDirs);
});
}
Data.getStaticDirectories = getStaticDirectories;
function getFiles(pluginData, type, callback) {
if (!Array.isArray(pluginData[type]) || !pluginData[type].length) {
return callback();
}
winston.verbose('[plugins] Found ' + pluginData[type].length + ' ' + type + ' file(s) for plugin ' + pluginData.id);
var files = pluginData[type].map(function (file) {
return path.join(pluginData.id, file);
});
callback(null, files);
}
Data.getFiles = getFiles;
/**
* With npm@3, dependencies can become flattened, and appear at the root level.
* This method resolves these differences if it can.
*/
function resolveModulePath(basePath, modulePath, callback) {
var isNodeModule = /node_modules/;
var currentPath = path.join(basePath, modulePath);
file.exists(currentPath, function (err, exists) {
if (err) {
return callback(err);
}
if (exists) {
return callback(null, currentPath);
}
if (!isNodeModule.test(modulePath)) {
winston.warn('[plugins] File not found: ' + currentPath + ' (Ignoring)');
return callback();
}
var dirPath = path.dirname(basePath);
if (dirPath === basePath) {
winston.warn('[plugins] File not found: ' + currentPath + ' (Ignoring)');
return callback();
}
resolveModulePath(dirPath, modulePath, callback);
});
}
function getScripts(pluginData, target, callback) {
target = (target === 'client') ? 'scripts' : 'acpScripts';
var input = pluginData[target];
if (!Array.isArray(input) || !input.length) {
return callback();
}
var scripts = [];
async.each(input, function (filePath, next) {
resolveModulePath(pluginData.path, filePath, function (err, modulePath) {
if (err) {
return next(err);
}
if (modulePath) {
scripts.push(modulePath);
}
next();
});
}, function (err) {
if (err) {
return callback(err);
}
if (scripts.length) {
winston.verbose('[plugins] Found ' + scripts.length + ' js file(s) for plugin ' + pluginData.id);
}
callback(err, scripts);
});
}
Data.getScripts = getScripts;
function getModules(pluginData, callback) {
if (!pluginData.modules || !pluginData.hasOwnProperty('modules')) {
return callback();
}
var pluginModules = pluginData.modules;
if (Array.isArray(pluginModules)) {
var strip = parseInt(pluginData.modulesStrip, 10) || 0;
pluginModules = pluginModules.reduce(function (prev, modulePath) {
var key;
if (strip) {
key = modulePath.replace(new RegExp('.?(/[^/]+){' + strip + '}/'), '');
} else {
key = path.basename(modulePath);
}
prev[key] = modulePath;
return prev;
}, {});
}
var modules = {};
async.each(Object.keys(pluginModules), function (key, next) {
resolveModulePath(pluginData.path, pluginModules[key], function (err, modulePath) {
if (err) {
return next(err);
}
if (modulePath) {
modules[key] = path.relative(basePath, modulePath);
}
next();
});
}, function (err) {
if (err) {
return callback(err);
}
var len = Object.keys(modules).length;
winston.verbose('[plugins] Found ' + len + ' AMD-style module(s) for plugin ' + pluginData.id);
callback(null, modules);
});
}
Data.getModules = getModules;
function getSoundpack(pluginData, callback) {
var spack = pluginData.soundpack;
if (!spack || !spack.dir || !spack.sounds) {
return callback();
}
var soundpack = {};
soundpack.name = spack.name || pluginData.name;
soundpack.id = pluginData.id;
soundpack.dir = path.join(pluginData.path, spack.dir);
soundpack.sounds = {};
async.each(Object.keys(spack.sounds), function (name, next) {
var soundFile = spack.sounds[name];
file.exists(path.join(soundpack.dir, soundFile), function (err, exists) {
if (err) {
return next(err);
}
if (!exists) {
winston.warn('[plugins] Sound file not found: ' + soundFile);
return next();
}
soundpack.sounds[name] = soundFile;
next();
});
}, function (err) {
if (err) {
return callback(err);
}
var len = Object.keys(soundpack.sounds).length;
winston.verbose('[plugins] Found ' + len + ' sound file(s) for plugin ' + pluginData.id);
callback(null, soundpack);
});
}
Data.getSoundpack = getSoundpack;
function getLanguageData(pluginData, callback) {
if (typeof pluginData.languages !== 'string') {
return callback();
}
var pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
file.walk(pathToFolder, function (err, paths) {
if (err) {
return callback(err);
}
var namespaces = [];
var languages = [];
paths.forEach(function (p) {
var rel = path.relative(pathToFolder, p).split(/[/\\]/);
var language = rel.shift().replace('_', '-').replace('@', '-x-');
var namespace = rel.join('/').replace(/\.json$/, '');
if (!language || !namespace) {
return;
}
languages.push(language);
namespaces.push(namespace);
});
callback(null, {
languages: languages,
namespaces: namespaces,
});
});
}
Data.getLanguageData = getLanguageData;

@ -1,70 +1,124 @@
'use strict'; 'use strict';
var db = require('../database');
var fs = require('fs');
var path = require('path'); var path = require('path');
var semver = require('semver'); var semver = require('semver');
var async = require('async'); var async = require('async');
var winston = require('winston'); var winston = require('winston');
var nconf = require('nconf'); var nconf = require('nconf');
var _ = require('underscore'); var _ = require('underscore');
var file = require('../file');
var meta = require('../meta');
var meta = require('../meta');
module.exports = function (Plugins) { module.exports = function (Plugins) {
Plugins.getPluginPaths = function (callback) { function registerPluginAssets(pluginData, fields, callback) {
async.waterfall([ function add(dest, arr) {
function (next) { dest.push.apply(dest, arr || []);
db.getSortedSetRange('plugins:active', 0, -1, next); }
var handlers = {
staticDirs: function (next) {
Plugins.data.getStaticDirectories(pluginData, next);
}, },
function (plugins, next) { cssFiles: function (next) {
if (!Array.isArray(plugins)) { Plugins.data.getFiles(pluginData, 'css', next);
return next(); },
} lessFiles: function (next) {
Plugins.data.getFiles(pluginData, 'less', next);
},
clientScripts: function (next) {
Plugins.data.getScripts(pluginData, 'client', next);
},
acpScripts: function (next) {
Plugins.data.getScripts(pluginData, 'acp', next);
},
modules: function (next) {
Plugins.data.getModules(pluginData, next);
},
soundpack: function (next) {
Plugins.data.getSoundpack(pluginData, next);
},
languageData: function (next) {
Plugins.data.getLanguageData(pluginData, next);
},
};
var methods;
if (Array.isArray(fields)) {
methods = fields.reduce(function (prev, field) {
prev[field] = handlers[field];
return prev;
}, {});
} else {
methods = handlers;
}
plugins = plugins.filter(function (plugin) { async.parallel(methods, function (err, results) {
return plugin && typeof plugin === 'string'; if (err) {
}).map(function (plugin) { return callback(err);
return path.join(__dirname, '../../node_modules/', plugin); }
});
async.filter(plugins, file.exists, next); Object.assign(Plugins.staticDirs, results.staticDirs || {});
}, add(Plugins.cssFiles, results.cssFiles);
], callback); add(Plugins.lessFiles, results.lessFiles);
}; add(Plugins.clientScripts, results.clientScripts);
add(Plugins.acpScripts, results.acpScripts);
Object.assign(meta.js.scripts.modules, results.modules || {});
if (results.soundpack) {
Plugins.soundpacks.push(results.soundpack);
}
if (results.languageData) {
Plugins.languageData.languages = _.union(Plugins.languageData.languages, results.languageData.languages);
Plugins.languageData.namespaces = _.union(Plugins.languageData.namespaces, results.languageData.namespaces);
}
Plugins.pluginsData[pluginData.id] = pluginData;
Plugins.prepareForBuild = function (callback) { callback();
});
}
Plugins.prepareForBuild = function (targets, callback) {
Plugins.cssFiles.length = 0; Plugins.cssFiles.length = 0;
Plugins.lessFiles.length = 0; Plugins.lessFiles.length = 0;
Plugins.clientScripts.length = 0; Plugins.clientScripts.length = 0;
Plugins.acpScripts.length = 0; Plugins.acpScripts.length = 0;
Plugins.soundpacks.length = 0; Plugins.soundpacks.length = 0;
Plugins.languageData.languages = [];
Plugins.languageData.namespaces = [];
var map = {
'plugin static dirs': ['staticDirs'],
'requirejs modules': ['modules'],
'client js bundle': ['clientScripts'],
'admin js bundle': ['acpScripts'],
'client side styles': ['cssFiles', 'lessFiles'],
'admin control panel styles': ['cssFiles', 'lessFiles'],
sounds: ['soundpack'],
languages: ['languageData'],
};
var fields = targets.reduce(function (prev, target) {
if (!map[target]) {
return prev;
}
return prev.concat(map[target]);
}, []).filter(function (field, i, arr) {
return arr.indexOf(field) === i;
});
winston.verbose('[plugins] loading the following fields from plugin data: ' + fields.join(', '));
async.waterfall([ async.waterfall([
async.apply(Plugins.getPluginPaths), Plugins.data.getActive,
function (paths, next) {
async.map(paths, function (path, next) {
Plugins.loadPluginInfo(path, next);
}, next);
},
function (plugins, next) { function (plugins, next) {
async.each(plugins, function (pluginData, next) { async.each(plugins, function (pluginData, next) {
async.parallel([ registerPluginAssets(pluginData, fields, next);
async.apply(mapFiles, pluginData, 'css', 'cssFiles'),
async.apply(mapFiles, pluginData, 'less', 'lessFiles'),
async.apply(mapClientSideScripts, pluginData),
async.apply(mapClientModules, pluginData),
async.apply(mapStaticDirectories, pluginData, pluginData.path),
async.apply(mapSoundpack, pluginData),
], next);
}, next); }, next);
}, },
], callback); ], callback);
}; };
Plugins.loadPlugin = function (pluginPath, callback) { Plugins.loadPlugin = function (pluginPath, callback) {
Plugins.loadPluginInfo(pluginPath, function (err, pluginData) { Plugins.data.loadPluginInfo(pluginPath, function (err, pluginData) {
if (err) { if (err) {
if (err.message === '[[error:parse-error]]') { if (err.message === '[[error:parse-error]]') {
return callback(); return callback();
@ -76,25 +130,10 @@ module.exports = function (Plugins) {
async.parallel([ async.parallel([
function (next) { function (next) {
registerHooks(pluginData, pluginPath, next); registerHooks(pluginData, next);
},
function (next) {
mapStaticDirectories(pluginData, pluginPath, next);
},
function (next) {
mapFiles(pluginData, 'css', 'cssFiles', next);
},
function (next) {
mapFiles(pluginData, 'less', 'lessFiles', next);
}, },
function (next) { function (next) {
mapClientSideScripts(pluginData, next); registerPluginAssets(pluginData, ['soundpack'], next);
},
function (next) {
mapClientModules(pluginData, next);
},
function (next) {
mapSoundpack(pluginData, next);
}, },
], function (err) { ], function (err) {
if (err) { if (err) {
@ -124,12 +163,12 @@ module.exports = function (Plugins) {
} }
} }
function registerHooks(pluginData, pluginPath, callback) { function registerHooks(pluginData, callback) {
if (!pluginData.library) { if (!pluginData.library) {
return callback(); return callback();
} }
var libraryPath = path.join(pluginPath, pluginData.library); var libraryPath = path.join(pluginData.path, pluginData.library);
try { try {
if (!Plugins.libraries[pluginData.id]) { if (!Plugins.libraries[pluginData.id]) {
@ -149,199 +188,4 @@ module.exports = function (Plugins) {
callback(); callback();
} }
} }
function mapStaticDirectories(pluginData, pluginPath, callback) {
var validMappedPath = /^[\w\-_]+$/;
function mapStaticDirs(mappedPath, callback) {
if (Plugins.staticDirs[mappedPath]) {
winston.warn('[plugins/' + pluginData.id + '] Mapped path (' + mappedPath + ') already specified!');
callback();
} else if (!validMappedPath.test(mappedPath)) {
winston.warn('[plugins/' + pluginData.id + '] Invalid mapped path specified: ' + mappedPath + '. Path must adhere to: ' + validMappedPath.toString());
callback();
} else {
var realPath = pluginData.staticDirs[mappedPath];
var staticDir = path.join(pluginPath, realPath);
file.exists(staticDir, function (err, exists) {
if (exists) {
Plugins.staticDirs[pluginData.id + '/' + mappedPath] = staticDir;
} else {
winston.warn('[plugins/' + pluginData.id + '] Mapped path \'' + mappedPath + ' => ' + staticDir + '\' not found.');
}
callback(err);
});
}
}
pluginData.staticDirs = pluginData.staticDirs || {};
var dirs = Object.keys(pluginData.staticDirs);
async.each(dirs, mapStaticDirs, callback);
}
function mapFiles(pluginData, type, globalArray, callback) {
if (Array.isArray(pluginData[type])) {
if (global.env === 'development') {
winston.verbose('[plugins] Found ' + pluginData[type].length + ' ' + type + ' file(s) for plugin ' + pluginData.id);
}
Plugins[globalArray] = Plugins[globalArray].concat(pluginData[type].map(function (file) {
return path.join(pluginData.id, file);
}));
}
callback();
}
function mapClientSideScripts(pluginData, callback) {
function mapScripts(scripts, param) {
if (Array.isArray(scripts) && scripts.length) {
if (global.env === 'development') {
winston.verbose('[plugins] Found ' + scripts.length + ' js file(s) for plugin ' + pluginData.id);
}
Plugins[param] = Plugins[param].concat(scripts.map(function (file) {
return resolveModulePath(path.join(__dirname, '../../node_modules/', pluginData.id, file), file);
})).filter(Boolean);
}
}
mapScripts(pluginData.scripts, 'clientScripts');
mapScripts(pluginData.acpScripts, 'acpScripts');
callback();
}
function mapClientModules(pluginData, callback) {
if (!pluginData.hasOwnProperty('modules')) {
return callback();
}
var modules = {};
if (Array.isArray(pluginData.modules)) {
if (global.env === 'development') {
winston.verbose('[plugins] Found ' + pluginData.modules.length + ' AMD-style module(s) for plugin ' + pluginData.id);
}
var strip = pluginData.hasOwnProperty('modulesStrip') ? parseInt(pluginData.modulesStrip, 10) : 0;
pluginData.modules.forEach(function (file) {
if (strip) {
modules[file.replace(new RegExp('.?(/[^/]+){' + strip + '}/'), '')] = path.join('./node_modules/', pluginData.id, file);
} else {
modules[path.basename(file)] = path.join('./node_modules/', pluginData.id, file);
}
});
meta.js.scripts.modules = _.extend(meta.js.scripts.modules, modules);
} else {
var keys = Object.keys(pluginData.modules);
if (global.env === 'development') {
winston.verbose('[plugins] Found ' + keys.length + ' AMD-style module(s) for plugin ' + pluginData.id);
}
for (var name in pluginData.modules) {
if (pluginData.modules.hasOwnProperty(name)) {
modules[name] = path.join('./node_modules/', pluginData.id, pluginData.modules[name]);
}
}
meta.js.scripts.modules = _.extend(meta.js.scripts.modules, modules);
}
callback();
}
function mapSoundpack(pluginData, callback) {
var soundpack = pluginData.soundpack;
if (!soundpack || !soundpack.dir || !soundpack.sounds) {
return callback();
}
soundpack.name = soundpack.name || pluginData.name;
soundpack.id = pluginData.id;
soundpack.dir = path.join(pluginData.path, soundpack.dir);
async.each(Object.keys(soundpack.sounds), function (key, next) {
file.exists(path.join(soundpack.dir, soundpack.sounds[key]), function (err, exists) {
if (!exists) {
delete soundpack.sounds[key];
}
next(err);
});
}, function (err) {
if (err) {
return callback(err);
}
if (Object.keys(soundpack.sounds).length) {
Plugins.soundpacks.push(soundpack);
}
callback();
});
}
function resolveModulePath(fullPath, relPath) {
/**
* With npm@3, dependencies can become flattened, and appear at the root level.
* This method resolves these differences if it can.
*/
var matches = fullPath.match(/node_modules/g);
var atRootLevel = !matches || matches.length === 1;
try {
fs.statSync(fullPath);
winston.verbose('[plugins/load] File found: ' + fullPath);
return fullPath;
} catch (e) {
// File not visible to the calling process, ascend to root level if possible and try again
if (!atRootLevel && relPath) {
winston.verbose('[plugins/load] File not found: ' + fullPath + ' (Ascending)');
return resolveModulePath(path.join(__dirname, '../..', relPath));
}
// Already at root level, file was simply not found
winston.warn('[plugins/load] File not found: ' + fullPath + ' (Ignoring)');
return null;
}
}
Plugins.loadPluginInfo = function (pluginPath, callback) {
async.parallel({
package: function (next) {
fs.readFile(path.join(pluginPath, 'package.json'), next);
},
plugin: function (next) {
fs.readFile(path.join(pluginPath, 'plugin.json'), next);
},
}, function (err, results) {
if (err) {
return callback(err);
}
var pluginData;
var packageData;
try {
pluginData = JSON.parse(results.plugin);
packageData = JSON.parse(results.package);
pluginData.id = packageData.name;
pluginData.name = packageData.name;
pluginData.description = packageData.description;
pluginData.version = packageData.version;
pluginData.repository = packageData.repository;
pluginData.nbbpm = packageData.nbbpm;
pluginData.path = pluginPath;
} catch (err) {
var pluginDir = pluginPath.split(path.sep);
pluginDir = pluginDir[pluginDir.length - 1];
winston.error('[plugins/' + pluginDir + '] Error in plugin.json or package.json! ' + err.message);
return callback(new Error('[[error:parse-error]]'));
}
callback(null, pluginData);
});
};
}; };

@ -6,6 +6,7 @@ var fs = require('fs');
var assert = require('assert'); var assert = require('assert');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var rimraf = require('rimraf'); var rimraf = require('rimraf');
var async = require('async');
var db = require('./mocks/databasemock'); var db = require('./mocks/databasemock');
var file = require('../src/file'); var file = require('../src/file');
@ -103,7 +104,11 @@ describe('Build', function (done) {
var build = require('../src/meta/build'); var build = require('../src/meta/build');
before(function (done) { before(function (done) {
rimraf(path.join(__dirname, '../build/public'), done); async.series([
async.apply(rimraf, path.join(__dirname, '../build/public')),
db.setupMockDefaults,
async.apply(db.activatePlugin, 'nodebb-plugin-markdown'),
], done);
}); });
it('should build plugin static dirs', function (done) { it('should build plugin static dirs', function (done) {
@ -177,10 +182,17 @@ describe('Build', function (done) {
it('should build languages', function (done) { it('should build languages', function (done) {
build.build(['languages'], function (err) { build.build(['languages'], function (err) {
assert.ifError(err); assert.ifError(err);
var filename = path.join(__dirname, '../build/public/language/en-GB/global.json');
assert(file.existsSync(filename)); var globalFile = path.join(__dirname, '../build/public/language/en-GB/global.json');
var global = fs.readFileSync(filename).toString(); assert(file.existsSync(globalFile), 'global.json exists');
assert.strictEqual(JSON.parse(global).home, 'Home'); var global = fs.readFileSync(globalFile).toString();
assert.strictEqual(JSON.parse(global).home, 'Home', 'global.json contains correct translations');
var mdFile = path.join(__dirname, '../build/public/language/en-GB/markdown.json');
assert(file.existsSync(mdFile), 'markdown.json exists');
var md = fs.readFileSync(mdFile).toString();
assert.strictEqual(JSON.parse(md).bold, 'bolded text', 'markdown.json contains correct translations');
done(); done();
}); });
}); });
@ -188,9 +200,20 @@ describe('Build', function (done) {
it('should build sounds', function (done) { it('should build sounds', function (done) {
build.build(['sounds'], function (err) { build.build(['sounds'], function (err) {
assert.ifError(err); assert.ifError(err);
var filename = path.join(__dirname, '../build/public/sounds/fileMap.json');
assert(file.existsSync(filename)); var mapFile = path.join(__dirname, '../build/public/sounds/fileMap.json');
assert(file.existsSync(mapFile));
var fileMap = JSON.parse(fs.readFileSync(mapFile));
assert.strictEqual(fileMap['Default | Deedle-dum'], 'nodebb-plugin-soundpack-default/notification.mp3');
var deebleDumFile = path.join(__dirname, '../build/public/sounds/nodebb-plugin-soundpack-default/notification.mp3');
assert(file.existsSync(deebleDumFile));
done(); done();
}); });
}); });
after(function (done) {
db.emptydb(done);
});
}); });

@ -81,51 +81,21 @@ winston.info(testDbConfig);
var db = require('../../src/database'); var db = require('../../src/database');
module.exports = db; module.exports = db;
after(function (done) {
db.close(done);
});
before(function (done) { before(function (done) {
this.timeout(30000); this.timeout(30000);
var meta; async.series([
async.waterfall([
function (next) { function (next) {
db.init(next); db.init(next);
}, },
function (next) { function (next) {
db.emptydb(next); setupMockDefaults(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);
}, },
function (next) { function (next) {
db.initSessionStore(next); db.initSessionStore(next);
}, },
function (next) { function (next) {
meta.dependencies.check(next); var meta = require('../../src/meta');
},
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);
},
function (next) {
// nconf defaults, if not set in config // nconf defaults, if not set in config
if (!nconf.get('sessionKey')) { if (!nconf.get('sessionKey')) {
nconf.set('sessionKey', 'express.sid'); nconf.set('sessionKey', 'express.sid');
@ -161,6 +131,40 @@ before(function (done) {
], done); ], 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) { function setupDefaultConfigs(meta, next) {
winston.info('Populating database with default configs, if not already set...\n'); winston.info('Populating database with default configs, if not already set...\n');
@ -174,9 +178,14 @@ function enableDefaultPlugins(callback) {
var defaultEnabled = [ var defaultEnabled = [
'nodebb-plugin-dbsearch', 'nodebb-plugin-dbsearch',
'nodebb-plugin-soundpack-default',
]; ];
winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled); winston.info('[install/enableDefaultPlugins] activating default plugins', defaultEnabled);
db.sortedSetAdd('plugins:active', [0], defaultEnabled, callback); db.sortedSetAdd('plugins:active', Object.keys(defaultEnabled), defaultEnabled, callback);
} }
db.activatePlugin = function (id, callback) {
db.sortedSetAdd('plugins:active', Date.now(), id, callback);
};

@ -16,7 +16,6 @@ describe('Plugins', function () {
assert.ifError(err); assert.ifError(err);
assert(plugins.libraries[pluginId]); assert(plugins.libraries[pluginId]);
assert(plugins.loadedHooks['static:app.load']); assert(plugins.loadedHooks['static:app.load']);
assert(plugins.staticDirs['nodebb-plugin-markdown/js']);
done(); done();
}); });

Loading…
Cancel
Save