Build plugin `staticDirs`

- route `/plugins` -> `build/public/plugins`
- symlink `staticDirs` directories into `build/public/plugins`
- remove custom `/plugins` route handling
v1.18.x
Peter Jaszkowiak 8 years ago
parent 8c86b2e32c
commit 186209a0a3

@ -62,6 +62,7 @@ exports.buildTargets = function (targets, callback) {
var startTime = Date.now();
async.series([
meta.js.linkModules,
meta.js.linkStatics,
async.apply(meta.js.minify, 'nodebb.min.js'),
async.apply(meta.js.minify, 'acp.min.js')
], step.bind(this, startTime, 'js', next));

@ -110,4 +110,9 @@ file.link = function link(filePath, destPath, cb) {
}
};
file.linkDirs = function linkDirs(sourceDir, destDir, callback) {
var type = (process.platform === 'win32') ? 'junction' : 'dir';
fs.symlink(sourceDir, destDir, type, callback);
};
module.exports = file;

@ -94,7 +94,7 @@ module.exports = function (Meta) {
if (err) {
return callback(err);
}
async.each(Object.keys(Meta.js.scripts.modules), function (relPath, next) {
async.eachLimit(Object.keys(Meta.js.scripts.modules), 1000, function (relPath, next) {
var filePath = path.join(__dirname, '../../', Meta.js.scripts.modules[relPath]);
var destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
@ -109,6 +109,26 @@ module.exports = function (Meta) {
});
};
Meta.js.linkStatics = function (callback) {
rimraf(path.join(__dirname, '../../build/public/plugins'), function (err) {
if (err) {
return callback(err);
}
async.eachLimit(Object.keys(plugins.staticDirs), 1000, function (mappedPath, next) {
var sourceDir = plugins.staticDirs[mappedPath];
var destDir = path.join(__dirname, '../../build/public/plugins', mappedPath);
mkdirp(path.dirname(destDir), function (err) {
if (err) {
return next(err);
}
file.linkDirs(sourceDir, destDir, next);
});
}, callback);
});
};
Meta.js.minify = function (target, callback) {
winston.verbose('[meta/js] Minifying ' + target);

@ -56,6 +56,7 @@ module.exports = function (Plugins) {
async.apply(mapFiles, pluginData, 'less', 'lessFiles'),
async.apply(mapClientSideScripts, pluginData),
async.apply(mapClientModules, pluginData),
async.apply(mapStaticDirectories, pluginData, pluginData.path),
], next);
}, next);
}
@ -299,6 +300,7 @@ module.exports = function (Plugins) {
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];

@ -15,7 +15,6 @@ var metaRoutes = require('./meta');
var apiRoutes = require('./api');
var adminRoutes = require('./admin');
var feedRoutes = require('./feeds');
var pluginRoutes = require('./plugins');
var authRoutes = require('./authentication');
var helpers = require('./helpers');
@ -123,7 +122,6 @@ module.exports = function (app, middleware, hotswapIds) {
metaRoutes(router, middleware, controllers);
apiRoutes(router, middleware, controllers);
feedRoutes(router, middleware, controllers);
pluginRoutes(router, middleware, controllers);
mainRoutes(router, middleware, controllers);
topicRoutes(router, middleware, controllers);
@ -152,6 +150,10 @@ module.exports = function (app, middleware, hotswapIds) {
app.use(relativePath + '/assets', express.static(path.join(__dirname, '../../public'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
// TODO: deprecate?
app.use(relativePath + '/plugins', express.static(path.join(__dirname, '../../build/public/plugins'), {
maxAge: app.enabled('cache') ? 5184000000 : 0
}));
// DEPRECATED
var deprecatedPaths = [

@ -1,40 +0,0 @@
"use strict";
var _ = require('underscore');
var path = require('path');
var plugins = require('../plugins');
module.exports = function (app, middleware, controllers) {
// Static Assets
app.get('/plugins/:id/*', middleware.addExpiresHeaders, function (req, res, next) {
var relPath = req._parsedUrl.pathname.replace('/plugins/', '');
var matches = _.map(plugins.staticDirs, function (realPath, mappedPath) {
if (relPath.match(mappedPath)) {
var pathToFile = path.join(plugins.staticDirs[mappedPath], decodeURIComponent(relPath.slice(mappedPath.length)));
if (pathToFile.startsWith(plugins.staticDirs[mappedPath])) {
return pathToFile;
}
}
return null;
}).filter(Boolean);
if (!matches || !matches.length) {
return next();
}
res.sendFile(matches[0], {}, function (err) {
if (err) {
if (err.code === 'ENOENT') {
// File doesn't exist, this isn't an error, to send to 404 handler
return next();
} else {
return next(err);
}
}
});
});
};
Loading…
Cancel
Save