mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
refactor: async/await
src/cli/manage.js src/meta/build.js src/meta/css.js src/meta/js.js
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
'use strict';
|
||||
|
||||
const async = require('async');
|
||||
const winston = require('winston');
|
||||
const childProcess = require('child_process');
|
||||
const _ = require('lodash');
|
||||
@@ -35,59 +34,40 @@ function buildTargets() {
|
||||
);
|
||||
}
|
||||
|
||||
function activate(plugin) {
|
||||
async function activate(plugin) {
|
||||
if (themeNamePattern.test(plugin)) {
|
||||
reset.reset({
|
||||
await reset.reset({
|
||||
theme: plugin,
|
||||
}, function (err) {
|
||||
if (err) { throw err; }
|
||||
process.exit();
|
||||
});
|
||||
return;
|
||||
process.exit();
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.init(next);
|
||||
},
|
||||
function (next) {
|
||||
try {
|
||||
await db.init();
|
||||
if (!pluginNamePattern.test(plugin)) {
|
||||
// Allow omission of `nodebb-plugin-`
|
||||
plugin = 'nodebb-plugin-' + plugin;
|
||||
}
|
||||
plugins.isInstalled(plugin, next);
|
||||
},
|
||||
function (isInstalled, next) {
|
||||
const isInstalled = await plugins.isInstalled(plugin);
|
||||
if (!isInstalled) {
|
||||
return next(new Error('plugin not installed'));
|
||||
throw new Error('plugin not installed');
|
||||
}
|
||||
plugins.isActive(plugin, next);
|
||||
},
|
||||
function (isActive, next) {
|
||||
const isActive = await plugins.isActive(plugin);
|
||||
if (isActive) {
|
||||
winston.info('Plugin `%s` already active', plugin);
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
db.sortedSetCard('plugins:active', next);
|
||||
},
|
||||
function (numPlugins, next) {
|
||||
const numPlugins = await db.sortedSetCard('plugins:active');
|
||||
winston.info('Activating plugin `%s`', plugin);
|
||||
db.sortedSetAdd('plugins:active', numPlugins, plugin, next);
|
||||
},
|
||||
function (next) {
|
||||
events.log({
|
||||
await db.sortedSetAdd('plugins:active', numPlugins, plugin);
|
||||
await events.log({
|
||||
type: 'plugin-activate',
|
||||
text: plugin,
|
||||
}, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
});
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
winston.error('An error occurred during plugin activation', err.stack);
|
||||
throw err;
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
async function listPlugins() {
|
||||
@@ -125,52 +105,31 @@ async function listPlugins() {
|
||||
process.exit();
|
||||
}
|
||||
|
||||
function listEvents(count) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.init(next);
|
||||
},
|
||||
async.apply(events.getEvents, '', 0, (count || 10) - 1),
|
||||
function (eventData) {
|
||||
async function listEvents(count) {
|
||||
await db.init();
|
||||
const eventData = await events.getEvents('', 0, (count || 10) - 1);
|
||||
console.log(('\nDisplaying last ' + count + ' administrative events...').bold);
|
||||
eventData.forEach(function (event) {
|
||||
console.log(' * ' + String(event.timestampISO).green + ' ' + String(event.type).yellow + (event.text ? ' ' + event.text : '') + ' (uid: '.reset + (event.uid ? event.uid : 0) + ')');
|
||||
});
|
||||
process.exit();
|
||||
},
|
||||
], function (err) {
|
||||
throw err;
|
||||
});
|
||||
}
|
||||
|
||||
function info() {
|
||||
async function info() {
|
||||
console.log('');
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
var version = require('../../package.json').version;
|
||||
const version = require('../../package.json').version;
|
||||
console.log(' version: ' + version);
|
||||
|
||||
console.log(' Node ver: ' + process.version);
|
||||
next();
|
||||
},
|
||||
function (next) {
|
||||
var hash = childProcess.execSync('git rev-parse HEAD');
|
||||
|
||||
const hash = childProcess.execSync('git rev-parse HEAD');
|
||||
console.log(' git hash: ' + hash);
|
||||
next();
|
||||
},
|
||||
function (next) {
|
||||
var config = require('../../config.json');
|
||||
|
||||
const config = require('../../config.json');
|
||||
console.log(' database: ' + config.database);
|
||||
next();
|
||||
},
|
||||
function (next) {
|
||||
db.init(next);
|
||||
},
|
||||
function (next) {
|
||||
db.info(db.client, next);
|
||||
},
|
||||
function (info, next) {
|
||||
var config = require('../../config.json');
|
||||
|
||||
await db.init();
|
||||
const info = await db.info(db.client);
|
||||
|
||||
switch (config.database) {
|
||||
case 'redis':
|
||||
@@ -184,11 +143,8 @@ function info() {
|
||||
break;
|
||||
}
|
||||
|
||||
next();
|
||||
},
|
||||
async.apply(analytics.getHourlyStatsForSet, 'analytics:pageviews', Date.now(), 24),
|
||||
function (data, next) {
|
||||
var graph = new CliGraph({
|
||||
const analyticsData = await analytics.getHourlyStatsForSet('analytics:pageviews', Date.now(), 24);
|
||||
const graph = new CliGraph({
|
||||
height: 12,
|
||||
width: 25,
|
||||
center: {
|
||||
@@ -196,32 +152,27 @@ function info() {
|
||||
y: 11,
|
||||
},
|
||||
});
|
||||
var min = Math.min(...data);
|
||||
var max = Math.max(...data);
|
||||
const min = Math.min(...analyticsData);
|
||||
const max = Math.max(...analyticsData);
|
||||
|
||||
data.forEach(function (point, idx) {
|
||||
analyticsData.forEach(function (point, idx) {
|
||||
graph.addPoint(idx + 1, Math.round(point / max * 10));
|
||||
});
|
||||
|
||||
console.log('');
|
||||
console.log(graph.toString());
|
||||
console.log('Pageviews, last 24h (min: ' + min + ' max: ' + max + ')');
|
||||
next();
|
||||
},
|
||||
], function (err) {
|
||||
if (err) { throw err; }
|
||||
process.exit();
|
||||
});
|
||||
}
|
||||
|
||||
function buildWrapper(targets, options) {
|
||||
build.build(targets, options, function (err) {
|
||||
if (err) {
|
||||
async function buildWrapper(targets, options) {
|
||||
try {
|
||||
await build.build(targets, options);
|
||||
process.exit(0);
|
||||
} catch (err) {
|
||||
winston.error(err.stack);
|
||||
process.exit(1);
|
||||
}
|
||||
process.exit(0);
|
||||
});
|
||||
}
|
||||
|
||||
exports.build = buildWrapper;
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const os = require('os');
|
||||
const async = require('async');
|
||||
const winston = require('winston');
|
||||
const nconf = require('nconf');
|
||||
const _ = require('lodash');
|
||||
@@ -9,35 +8,18 @@ const _ = require('lodash');
|
||||
const cacheBuster = require('./cacheBuster');
|
||||
let meta;
|
||||
|
||||
function step(target, callback) {
|
||||
var startTime = Date.now();
|
||||
winston.info('[build] ' + target + ' build started');
|
||||
|
||||
return function (err) {
|
||||
if (err) {
|
||||
winston.error('[build] ' + target + ' build failed');
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
var time = (Date.now() - startTime) / 1000;
|
||||
|
||||
winston.info('[build] ' + target + ' build completed in ' + time + 'sec');
|
||||
callback();
|
||||
};
|
||||
}
|
||||
|
||||
var targetHandlers = {
|
||||
'plugin static dirs': function (parallel, callback) {
|
||||
meta.js.linkStatics(callback);
|
||||
const targetHandlers = {
|
||||
'plugin static dirs': async function () {
|
||||
await meta.js.linkStatics();
|
||||
},
|
||||
'requirejs modules': function (parallel, callback) {
|
||||
meta.js.buildModules(parallel, callback);
|
||||
'requirejs modules': async function (parallel) {
|
||||
await meta.js.buildModules(parallel);
|
||||
},
|
||||
'client js bundle': function (parallel, callback) {
|
||||
meta.js.buildBundle('client', parallel, callback);
|
||||
'client js bundle': async function (parallel) {
|
||||
await meta.js.buildBundle('client', parallel);
|
||||
},
|
||||
'admin js bundle': function (parallel, callback) {
|
||||
meta.js.buildBundle('admin', parallel, callback);
|
||||
'admin js bundle': async function (parallel) {
|
||||
await meta.js.buildBundle('admin', parallel);
|
||||
},
|
||||
javascript: [
|
||||
'plugin static dirs',
|
||||
@@ -45,25 +27,25 @@ var targetHandlers = {
|
||||
'client js bundle',
|
||||
'admin js bundle',
|
||||
],
|
||||
'client side styles': function (parallel, callback) {
|
||||
meta.css.buildBundle('client', parallel, callback);
|
||||
'client side styles': async function (parallel) {
|
||||
await meta.css.buildBundle('client', parallel);
|
||||
},
|
||||
'admin control panel styles': function (parallel, callback) {
|
||||
meta.css.buildBundle('admin', parallel, callback);
|
||||
'admin control panel styles': async function (parallel) {
|
||||
await meta.css.buildBundle('admin', parallel);
|
||||
},
|
||||
styles: [
|
||||
'client side styles',
|
||||
'admin control panel styles',
|
||||
],
|
||||
templates: function (parallel, callback) {
|
||||
meta.templates.compile(callback);
|
||||
templates: async function () {
|
||||
await meta.templates.compile();
|
||||
},
|
||||
languages: function (parallel, callback) {
|
||||
meta.languages.build(callback);
|
||||
languages: async function () {
|
||||
await meta.languages.build();
|
||||
},
|
||||
};
|
||||
|
||||
var aliases = {
|
||||
let aliases = {
|
||||
'plugin static dirs': ['staticdirs'],
|
||||
'requirejs modules': ['rjs', 'modules'],
|
||||
'client js bundle': ['clientjs', 'clientscript', 'clientscripts'],
|
||||
@@ -91,53 +73,59 @@ aliases = Object.keys(aliases).reduce(function (prev, key) {
|
||||
return prev;
|
||||
}, {});
|
||||
|
||||
function beforeBuild(targets, callback) {
|
||||
var db = require('../database');
|
||||
async function beforeBuild(targets) {
|
||||
const db = require('../database');
|
||||
require('colors');
|
||||
process.stdout.write(' started'.green + '\n'.reset);
|
||||
|
||||
async.series([
|
||||
function (next) {
|
||||
db.init(next);
|
||||
},
|
||||
function (next) {
|
||||
try {
|
||||
await db.init();
|
||||
meta = require('./index');
|
||||
meta.themes.setupPaths(next);
|
||||
},
|
||||
function (next) {
|
||||
var plugins = require('../plugins');
|
||||
plugins.prepareForBuild(targets, next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
await meta.themes.setupPaths();
|
||||
const plugins = require('../plugins');
|
||||
await plugins.prepareForBuild(targets);
|
||||
} catch (err) {
|
||||
winston.error('[build] Encountered error preparing for build\n' + err.stack);
|
||||
return callback(err);
|
||||
throw err;
|
||||
}
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
var allTargets = Object.keys(targetHandlers).filter(function (name) {
|
||||
const allTargets = Object.keys(targetHandlers).filter(function (name) {
|
||||
return typeof targetHandlers[name] === 'function';
|
||||
});
|
||||
function buildTargets(targets, parallel, callback) {
|
||||
var all = parallel ? async.each : async.eachSeries;
|
||||
|
||||
var length = Math.max.apply(Math, targets.map(function (name) {
|
||||
return name.length;
|
||||
}));
|
||||
async function buildTargets(targets, parallel) {
|
||||
const length = Math.max.apply(Math, targets.map(name => name.length));
|
||||
|
||||
all(targets, function (target, next) {
|
||||
targetHandlers[target](parallel, step(_.padStart(target, length) + ' ', next));
|
||||
}, callback);
|
||||
if (parallel) {
|
||||
await Promise.all(
|
||||
targets.map(
|
||||
target => step(target, parallel, _.padStart(target, length) + ' ')
|
||||
)
|
||||
);
|
||||
} else {
|
||||
for (const target of targets) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await step(target, parallel, _.padStart(target, length) + ' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exports.build = function (targets, options, callback) {
|
||||
if (!callback && typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
} else if (!options) {
|
||||
async function step(target, parallel, targetStr) {
|
||||
const startTime = Date.now();
|
||||
winston.info('[build] ' + targetStr + ' build started');
|
||||
try {
|
||||
await targetHandlers[target](parallel);
|
||||
const time = (Date.now() - startTime) / 1000;
|
||||
|
||||
winston.info('[build] ' + targetStr + ' build completed in ' + time + 'sec');
|
||||
} catch (err) {
|
||||
winston.error('[build] ' + targetStr + ' build failed');
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
exports.build = async function (targets, options) {
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
@@ -186,15 +174,12 @@ exports.build = function (targets, options, callback) {
|
||||
|
||||
if (!targets) {
|
||||
winston.info('[build] No valid targets supplied. Aborting.');
|
||||
callback();
|
||||
return;
|
||||
}
|
||||
|
||||
var startTime;
|
||||
var totalTime;
|
||||
async.series([
|
||||
async.apply(beforeBuild, targets),
|
||||
function (next) {
|
||||
var threads = parseInt(nconf.get('threads'), 10);
|
||||
try {
|
||||
await beforeBuild(targets);
|
||||
const threads = parseInt(nconf.get('threads'), 10);
|
||||
if (threads) {
|
||||
require('./minifier').maxThreads = threads - 1;
|
||||
}
|
||||
@@ -205,26 +190,19 @@ exports.build = function (targets, options, callback) {
|
||||
winston.info('[build] Building in series mode');
|
||||
}
|
||||
|
||||
startTime = Date.now();
|
||||
buildTargets(targets, !series, next);
|
||||
},
|
||||
function (next) {
|
||||
totalTime = (Date.now() - startTime) / 1000;
|
||||
cacheBuster.write(next);
|
||||
},
|
||||
], function (err) {
|
||||
if (err) {
|
||||
winston.error('[build] Encountered error during build step\n' + (err.stack ? err.stack : err));
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
const startTime = Date.now();
|
||||
await buildTargets(targets, !series);
|
||||
const totalTime = (Date.now() - startTime) / 1000;
|
||||
await cacheBuster.write();
|
||||
winston.info('[build] Asset compilation successful. Completed in ' + totalTime + 'sec.');
|
||||
callback();
|
||||
});
|
||||
} catch (err) {
|
||||
winston.error('[build] Encountered error during build step\n' + (err.stack ? err.stack : err));
|
||||
throw err;
|
||||
}
|
||||
};
|
||||
|
||||
exports.buildAll = function (callback) {
|
||||
exports.build(allTargets, callback);
|
||||
exports.buildAll = async function () {
|
||||
await exports.build(allTargets);
|
||||
};
|
||||
|
||||
require('../promisify')(exports);
|
||||
|
||||
179
src/meta/css.js
179
src/meta/css.js
@@ -1,18 +1,19 @@
|
||||
'use strict';
|
||||
|
||||
var winston = require('winston');
|
||||
var nconf = require('nconf');
|
||||
var fs = require('fs');
|
||||
var path = require('path');
|
||||
var async = require('async');
|
||||
var rimraf = require('rimraf');
|
||||
const winston = require('winston');
|
||||
const nconf = require('nconf');
|
||||
const fs = require('fs');
|
||||
const util = require('util');
|
||||
const path = require('path');
|
||||
const rimraf = require('rimraf');
|
||||
const rimrafAsync = util.promisify(rimraf);
|
||||
|
||||
var plugins = require('../plugins');
|
||||
var db = require('../database');
|
||||
var file = require('../file');
|
||||
var minifier = require('./minifier');
|
||||
const plugins = require('../plugins');
|
||||
const db = require('../database');
|
||||
const file = require('../file');
|
||||
const minifier = require('./minifier');
|
||||
|
||||
var CSS = module.exports;
|
||||
const CSS = module.exports;
|
||||
|
||||
CSS.supportedSkins = [
|
||||
'cerulean', 'cyborg', 'flatly', 'journal', 'lumen', 'paper', 'simplex',
|
||||
@@ -20,7 +21,7 @@ CSS.supportedSkins = [
|
||||
'slate', 'superhero', 'yeti',
|
||||
];
|
||||
|
||||
var buildImports = {
|
||||
const buildImports = {
|
||||
client: function (source) {
|
||||
return '@import "./theme";\n' + source + '\n' + [
|
||||
'@import "font-awesome";',
|
||||
@@ -50,21 +51,22 @@ var buildImports = {
|
||||
},
|
||||
};
|
||||
|
||||
function filterMissingFiles(filepaths, callback) {
|
||||
async.filter(filepaths, function (filepath, next) {
|
||||
file.exists(path.join(__dirname, '../../node_modules', filepath), function (err, exists) {
|
||||
async function filterMissingFiles(filepaths) {
|
||||
const exists = await Promise.all(
|
||||
filepaths.map(async (filepath) => {
|
||||
const exists = await file.exists(path.join(__dirname, '../../node_modules', filepath));
|
||||
if (!exists) {
|
||||
winston.warn('[meta/css] File not found! ' + filepath);
|
||||
}
|
||||
|
||||
next(err, exists);
|
||||
});
|
||||
}, callback);
|
||||
return exists;
|
||||
})
|
||||
);
|
||||
return filepaths.filter((filePath, i) => exists[i]);
|
||||
}
|
||||
|
||||
function getImports(files, prefix, extension, callback) {
|
||||
var pluginDirectories = [];
|
||||
var source = '';
|
||||
async function getImports(files, prefix, extension) {
|
||||
const pluginDirectories = [];
|
||||
let source = '';
|
||||
|
||||
files.forEach(function (styleFile) {
|
||||
if (styleFile.endsWith(extension)) {
|
||||
@@ -73,26 +75,17 @@ function getImports(files, prefix, extension, callback) {
|
||||
pluginDirectories.push(styleFile);
|
||||
}
|
||||
});
|
||||
|
||||
async.each(pluginDirectories, function (directory, next) {
|
||||
file.walk(directory, function (err, styleFiles) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
await Promise.all(pluginDirectories.map(async function (directory) {
|
||||
const styleFiles = await file.walk(directory);
|
||||
styleFiles.forEach(function (styleFile) {
|
||||
source += prefix + path.sep + styleFile + '";';
|
||||
});
|
||||
|
||||
next();
|
||||
});
|
||||
}, function (err) {
|
||||
callback(err, source);
|
||||
});
|
||||
}));
|
||||
return source;
|
||||
}
|
||||
|
||||
function getBundleMetadata(target, callback) {
|
||||
var paths = [
|
||||
async function getBundleMetadata(target) {
|
||||
const paths = [
|
||||
path.join(__dirname, '../../node_modules'),
|
||||
path.join(__dirname, '../../public/less'),
|
||||
path.join(__dirname, '../../public/vendor/fontawesome/less'),
|
||||
@@ -107,106 +100,48 @@ function getBundleMetadata(target, callback) {
|
||||
target = 'client';
|
||||
}
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (target !== 'client') {
|
||||
return next(null, null);
|
||||
}
|
||||
|
||||
db.getObjectFields('config', ['theme:type', 'theme:id', 'bootswatchSkin'], next);
|
||||
},
|
||||
function (themeData, next) {
|
||||
let skinImport = [];
|
||||
if (target === 'client') {
|
||||
var themeId = (themeData['theme:id'] || 'nodebb-theme-persona');
|
||||
var baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla'));
|
||||
const themeData = await db.getObjectFields('config', ['theme:type', 'theme:id', 'bootswatchSkin']);
|
||||
const themeId = (themeData['theme:id'] || 'nodebb-theme-persona');
|
||||
const baseThemePath = path.join(nconf.get('themes_path'), (themeData['theme:type'] && themeData['theme:type'] === 'local' ? themeId : 'nodebb-theme-vanilla'));
|
||||
paths.unshift(baseThemePath);
|
||||
|
||||
themeData.bootswatchSkin = skin || themeData.bootswatchSkin;
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
less: function (cb) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
filterMissingFiles(plugins.lessFiles, next);
|
||||
},
|
||||
function (lessFiles, next) {
|
||||
getImports(lessFiles, '\n@import ".', '.less', next);
|
||||
},
|
||||
], cb);
|
||||
},
|
||||
acpLess: function (cb) {
|
||||
if (target === 'client') {
|
||||
return cb(null, '');
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
filterMissingFiles(plugins.acpLessFiles, next);
|
||||
},
|
||||
function (acpLessFiles, next) {
|
||||
getImports(acpLessFiles, '\n@import ".', '.less', next);
|
||||
},
|
||||
], cb);
|
||||
},
|
||||
css: function (cb) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
filterMissingFiles(plugins.cssFiles, next);
|
||||
},
|
||||
function (cssFiles, next) {
|
||||
getImports(cssFiles, '\n@import (inline) ".', '.css', next);
|
||||
},
|
||||
], cb);
|
||||
},
|
||||
skin: function (cb) {
|
||||
const skinImport = [];
|
||||
if (themeData && themeData.bootswatchSkin) {
|
||||
skinImport.push('\n@import "./@nodebb/bootswatch/' + themeData.bootswatchSkin + '/variables.less";');
|
||||
skinImport.push('\n@import "./@nodebb/bootswatch/' + themeData.bootswatchSkin + '/bootswatch.less";');
|
||||
}
|
||||
skinImport = skinImport.join('');
|
||||
}
|
||||
|
||||
cb(null, skinImport.join(''));
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (result, next) {
|
||||
var skinImport = result.skin;
|
||||
var cssImports = result.css;
|
||||
var lessImports = result.less;
|
||||
var acpLessImports = result.acpLess;
|
||||
const [lessImports, cssImports, acpLessImports] = await Promise.all([
|
||||
moo(plugins.lessFiles, '\n@import ".', '.less'),
|
||||
moo(plugins.cssFiles, '\n@import (inline) ".', '.css'),
|
||||
target === 'client' ? '' : moo(plugins.acpLessFiles, '\n@import ".', '.less'),
|
||||
]);
|
||||
|
||||
var imports = skinImport + '\n' + cssImports + '\n' + lessImports + '\n' + acpLessImports;
|
||||
async function moo(files, prefix, extension) {
|
||||
const filteredFiles = await filterMissingFiles(files);
|
||||
return await getImports(filteredFiles, prefix, extension);
|
||||
}
|
||||
|
||||
let imports = skinImport + '\n' + cssImports + '\n' + lessImports + '\n' + acpLessImports;
|
||||
imports = buildImports[target](imports);
|
||||
|
||||
next(null, { paths: paths, imports: imports });
|
||||
},
|
||||
], callback);
|
||||
return { paths: paths, imports: imports };
|
||||
}
|
||||
|
||||
CSS.buildBundle = function (target, fork, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
CSS.buildBundle = async function (target, fork) {
|
||||
if (target === 'client') {
|
||||
rimraf(path.join(__dirname, '../../build/public/client*'), next);
|
||||
} else {
|
||||
setImmediate(next);
|
||||
await rimrafAsync(path.join(__dirname, '../../build/public/client*'));
|
||||
}
|
||||
},
|
||||
function (next) {
|
||||
getBundleMetadata(target, next);
|
||||
},
|
||||
function (data, next) {
|
||||
var minify = process.env.NODE_ENV !== 'development';
|
||||
minifier.css.bundle(data.imports, data.paths, minify, fork, next);
|
||||
},
|
||||
function (bundle, next) {
|
||||
var filename = target + '.css';
|
||||
|
||||
fs.writeFile(path.join(__dirname, '../../build/public', filename), bundle.code, function (err) {
|
||||
next(err, bundle.code);
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
const data = await getBundleMetadata(target);
|
||||
const minify = process.env.NODE_ENV !== 'development';
|
||||
const bundle = await minifier.css.bundle(data.imports, data.paths, minify, fork);
|
||||
|
||||
const filename = target + '.css';
|
||||
await fs.promises.writeFile(path.join(__dirname, '../../build/public', filename), bundle.code);
|
||||
return bundle.code;
|
||||
};
|
||||
|
||||
285
src/meta/js.js
285
src/meta/js.js
@@ -1,25 +1,22 @@
|
||||
'use strict';
|
||||
|
||||
var path = require('path');
|
||||
var async = require('async');
|
||||
var fs = require('fs');
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const util = require('util');
|
||||
var mkdirp = require('mkdirp');
|
||||
var mkdirpCallback;
|
||||
if (mkdirp.hasOwnProperty('native')) {
|
||||
mkdirpCallback = util.callbackify(mkdirp);
|
||||
} else {
|
||||
mkdirpCallback = mkdirp;
|
||||
let mkdirp = require('mkdirp');
|
||||
// TODO: remove in 1.16.0
|
||||
if (!mkdirp.hasOwnProperty('native')) {
|
||||
mkdirp = util.promisify(mkdirp);
|
||||
}
|
||||
|
||||
var rimraf = require('rimraf');
|
||||
const rimraf = require('rimraf');
|
||||
const rimrafAsync = util.promisify(rimraf);
|
||||
|
||||
var file = require('../file');
|
||||
var plugins = require('../plugins');
|
||||
var minifier = require('./minifier');
|
||||
const file = require('../file');
|
||||
const plugins = require('../plugins');
|
||||
const minifier = require('./minifier');
|
||||
|
||||
var JS = module.exports;
|
||||
const JS = module.exports;
|
||||
|
||||
JS.scripts = {
|
||||
base: [
|
||||
@@ -109,31 +106,28 @@ JS.scripts = {
|
||||
},
|
||||
};
|
||||
|
||||
function linkIfLinux(srcPath, destPath, next) {
|
||||
async function linkIfLinux(srcPath, destPath) {
|
||||
if (process.platform === 'win32') {
|
||||
fs.copyFile(srcPath, destPath, next);
|
||||
await fs.promises.copyFile(srcPath, destPath);
|
||||
} else {
|
||||
file.link(srcPath, destPath, true, next);
|
||||
await file.link(srcPath, destPath, true);
|
||||
}
|
||||
}
|
||||
|
||||
var basePath = path.resolve(__dirname, '../..');
|
||||
const basePath = path.resolve(__dirname, '../..');
|
||||
|
||||
function minifyModules(modules, fork, callback) {
|
||||
var moduleDirs = modules.reduce(function (prev, mod) {
|
||||
var dir = path.resolve(path.dirname(mod.destPath));
|
||||
async function minifyModules(modules, fork) {
|
||||
const moduleDirs = modules.reduce(function (prev, mod) {
|
||||
const dir = path.resolve(path.dirname(mod.destPath));
|
||||
if (!prev.includes(dir)) {
|
||||
prev.push(dir);
|
||||
}
|
||||
return prev;
|
||||
}, []);
|
||||
|
||||
async.each(moduleDirs, mkdirpCallback, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
await Promise.all(moduleDirs.map(dir => mkdirp(dir)));
|
||||
|
||||
var filtered = modules.reduce(function (prev, mod) {
|
||||
const filtered = modules.reduce(function (prev, mod) {
|
||||
if (mod.srcPath.endsWith('.min.js') || path.dirname(mod.srcPath).endsWith('min')) {
|
||||
prev.skip.push(mod);
|
||||
} else {
|
||||
@@ -143,59 +137,43 @@ function minifyModules(modules, fork, callback) {
|
||||
return prev;
|
||||
}, { minify: [], skip: [] });
|
||||
|
||||
async.parallel([
|
||||
function (cb) {
|
||||
minifier.js.minifyBatch(filtered.minify, fork, cb);
|
||||
},
|
||||
function (cb) {
|
||||
async.each(filtered.skip, function (mod, next) {
|
||||
linkIfLinux(mod.srcPath, mod.destPath, next);
|
||||
}, cb);
|
||||
},
|
||||
], callback);
|
||||
});
|
||||
await Promise.all([
|
||||
minifier.js.minifyBatch(filtered.minify, fork),
|
||||
...filtered.skip.map(mod => linkIfLinux(mod.srcPath, mod.destPath)),
|
||||
]);
|
||||
}
|
||||
|
||||
function linkModules(callback) {
|
||||
var modules = JS.scripts.modules;
|
||||
async function linkModules() {
|
||||
const modules = JS.scripts.modules;
|
||||
|
||||
async.each(Object.keys(modules), function (relPath, next) {
|
||||
var srcPath = path.join(__dirname, '../../', modules[relPath]);
|
||||
var destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
|
||||
await Promise.all(Object.keys(modules).map(async function (relPath) {
|
||||
const srcPath = path.join(__dirname, '../../', modules[relPath]);
|
||||
const destPath = path.join(__dirname, '../../build/public/src/modules', relPath);
|
||||
const [stats] = await Promise.all([
|
||||
fs.promises.stat(srcPath),
|
||||
mkdirp(path.dirname(destPath)),
|
||||
]);
|
||||
|
||||
async.parallel({
|
||||
dir: function (cb) {
|
||||
mkdirpCallback(path.dirname(destPath), function (err) {
|
||||
cb(err);
|
||||
});
|
||||
},
|
||||
stats: function (cb) {
|
||||
fs.stat(srcPath, cb);
|
||||
},
|
||||
}, function (err, res) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
if (res.stats.isDirectory()) {
|
||||
return file.linkDirs(srcPath, destPath, true, next);
|
||||
if (stats.isDirectory()) {
|
||||
await file.linkDirs(srcPath, destPath, true);
|
||||
return;
|
||||
}
|
||||
|
||||
linkIfLinux(srcPath, destPath, next);
|
||||
});
|
||||
}, callback);
|
||||
await linkIfLinux(srcPath, destPath);
|
||||
}));
|
||||
}
|
||||
|
||||
var moduleDirs = ['modules', 'admin', 'client'];
|
||||
const moduleDirs = ['modules', 'admin', 'client'];
|
||||
|
||||
function getModuleList(callback) {
|
||||
var modules = Object.keys(JS.scripts.modules).map(function (relPath) {
|
||||
async function getModuleList() {
|
||||
let modules = Object.keys(JS.scripts.modules).map(function (relPath) {
|
||||
return {
|
||||
srcPath: path.join(__dirname, '../../', JS.scripts.modules[relPath]),
|
||||
destPath: path.join(__dirname, '../../build/public/src/modules', relPath),
|
||||
};
|
||||
});
|
||||
|
||||
var coreDirs = moduleDirs.map(function (dir) {
|
||||
const coreDirs = moduleDirs.map(function (dir) {
|
||||
return {
|
||||
srcPath: path.join(__dirname, '../../public/src', dir),
|
||||
destPath: path.join(__dirname, '../../build/public/src', dir),
|
||||
@@ -204,75 +182,55 @@ function getModuleList(callback) {
|
||||
|
||||
modules = modules.concat(coreDirs);
|
||||
|
||||
var moduleFiles = [];
|
||||
async.each(modules, function (module, next) {
|
||||
var srcPath = module.srcPath;
|
||||
var destPath = module.destPath;
|
||||
const moduleFiles = [];
|
||||
await Promise.all(modules.map(async function (module) {
|
||||
const srcPath = module.srcPath;
|
||||
const destPath = module.destPath;
|
||||
|
||||
fs.stat(srcPath, function (err, stats) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
const stats = await fs.promises.stat(srcPath);
|
||||
if (!stats.isDirectory()) {
|
||||
moduleFiles.push(module);
|
||||
return next();
|
||||
return;
|
||||
}
|
||||
|
||||
file.walk(srcPath, function (err, files) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
const files = await file.walk(srcPath);
|
||||
|
||||
var mods = files.filter(function (filePath) {
|
||||
return path.extname(filePath) === '.js';
|
||||
}).map(function (filePath) {
|
||||
const mods = files.filter(
|
||||
filePath => path.extname(filePath) === '.js'
|
||||
).map(function (filePath) {
|
||||
return {
|
||||
srcPath: path.normalize(filePath),
|
||||
destPath: path.join(destPath, path.relative(srcPath, filePath)),
|
||||
};
|
||||
});
|
||||
|
||||
moduleFiles = moduleFiles.concat(mods).map(function (mod) {
|
||||
moduleFiles.concat(mods).forEach(function (mod) {
|
||||
mod.filename = path.relative(basePath, mod.srcPath).replace(/\\/g, '/');
|
||||
return mod;
|
||||
});
|
||||
|
||||
next();
|
||||
});
|
||||
});
|
||||
}, function (err) {
|
||||
callback(err, moduleFiles);
|
||||
});
|
||||
}));
|
||||
return moduleFiles;
|
||||
}
|
||||
|
||||
function clearModules(callback) {
|
||||
var builtPaths = moduleDirs.map(function (p) {
|
||||
return path.join(__dirname, '../../build/public/src', p);
|
||||
});
|
||||
async.each(builtPaths, function (builtPath, next) {
|
||||
rimraf(builtPath, next);
|
||||
}, function (err) {
|
||||
callback(err);
|
||||
});
|
||||
async function clearModules() {
|
||||
const builtPaths = moduleDirs.map(
|
||||
p => path.join(__dirname, '../../build/public/src', p)
|
||||
);
|
||||
await Promise.all(
|
||||
builtPaths.map(builtPath => rimrafAsync(builtPath))
|
||||
);
|
||||
}
|
||||
|
||||
JS.buildModules = function (fork, callback) {
|
||||
async.waterfall([
|
||||
clearModules,
|
||||
function (next) {
|
||||
JS.buildModules = async function (fork) {
|
||||
await clearModules();
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return linkModules(callback);
|
||||
await linkModules();
|
||||
return;
|
||||
}
|
||||
|
||||
getModuleList(next);
|
||||
},
|
||||
function (modules, next) {
|
||||
minifyModules(modules, fork, next);
|
||||
},
|
||||
], callback);
|
||||
const modules = await getModuleList();
|
||||
await minifyModules(modules, fork);
|
||||
};
|
||||
|
||||
function requirejsOptimize(target, callback) {
|
||||
async function requirejsOptimize(target) {
|
||||
const requirejs = require('requirejs');
|
||||
let scriptText = '';
|
||||
const sharedCfg = {
|
||||
@@ -305,52 +263,41 @@ function requirejsOptimize(target, callback) {
|
||||
name: 'Sortable',
|
||||
},
|
||||
],
|
||||
client: [
|
||||
|
||||
],
|
||||
client: [],
|
||||
};
|
||||
async.eachSeries(bundledModules.concat(targetModules[target]), function (moduleCfg, next) {
|
||||
requirejs.optimize({ ...sharedCfg, ...moduleCfg }, function () {
|
||||
next();
|
||||
}, function (err) {
|
||||
next(err);
|
||||
const optimizeAsync = util.promisify(function (config, cb) {
|
||||
requirejs.optimize(config, () => cb(), err => cb(err));
|
||||
});
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
|
||||
const allModules = bundledModules.concat(targetModules[target]);
|
||||
|
||||
for (const moduleCfg of allModules) {
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await optimizeAsync({ ...sharedCfg, ...moduleCfg });
|
||||
}
|
||||
const filePath = path.join(__dirname, '../../build/public/rjs-bundle-' + target + '.js');
|
||||
fs.writeFile(filePath, scriptText, callback);
|
||||
});
|
||||
await fs.promises.writeFile(filePath, scriptText);
|
||||
}
|
||||
|
||||
JS.linkStatics = function (callback) {
|
||||
rimraf(path.join(__dirname, '../../build/public/plugins'), function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
async.each(Object.keys(plugins.staticDirs), function (mappedPath, next) {
|
||||
var sourceDir = plugins.staticDirs[mappedPath];
|
||||
var destDir = path.join(__dirname, '../../build/public/plugins', mappedPath);
|
||||
JS.linkStatics = async function () {
|
||||
await rimrafAsync(path.join(__dirname, '../../build/public/plugins'));
|
||||
|
||||
mkdirpCallback(path.dirname(destDir), function (err) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
await Promise.all(Object.keys(plugins.staticDirs).map(async function (mappedPath) {
|
||||
const sourceDir = plugins.staticDirs[mappedPath];
|
||||
const destDir = path.join(__dirname, '../../build/public/plugins', mappedPath);
|
||||
|
||||
file.linkDirs(sourceDir, destDir, true, next);
|
||||
});
|
||||
}, callback);
|
||||
});
|
||||
await mkdirp(path.dirname(destDir));
|
||||
await file.linkDirs(sourceDir, destDir, true);
|
||||
}));
|
||||
};
|
||||
|
||||
function getBundleScriptList(target, callback) {
|
||||
var pluginDirectories = [];
|
||||
async function getBundleScriptList(target) {
|
||||
const pluginDirectories = [];
|
||||
|
||||
if (target === 'admin') {
|
||||
target = 'acp';
|
||||
}
|
||||
var pluginScripts = plugins[target + 'Scripts'].filter(function (path) {
|
||||
let pluginScripts = plugins[target + 'Scripts'].filter(function (path) {
|
||||
if (path.endsWith('.js')) {
|
||||
return true;
|
||||
}
|
||||
@@ -359,21 +306,12 @@ function getBundleScriptList(target, callback) {
|
||||
return false;
|
||||
});
|
||||
|
||||
async.each(pluginDirectories, function (directory, next) {
|
||||
file.walk(directory, function (err, scripts) {
|
||||
if (err) {
|
||||
return next(err);
|
||||
}
|
||||
|
||||
await Promise.all(pluginDirectories.map(async function (directory) {
|
||||
const scripts = await file.walk(directory);
|
||||
pluginScripts = pluginScripts.concat(scripts);
|
||||
next();
|
||||
});
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
}));
|
||||
|
||||
var scripts = JS.scripts.base;
|
||||
let scripts = JS.scripts.base;
|
||||
|
||||
if (target === 'client' && process.env.NODE_ENV !== 'development') {
|
||||
scripts = scripts.concat(JS.scripts.rjs);
|
||||
@@ -382,50 +320,37 @@ function getBundleScriptList(target, callback) {
|
||||
}
|
||||
|
||||
scripts = scripts.concat(pluginScripts).map(function (script) {
|
||||
var srcPath = path.resolve(basePath, script).replace(/\\/g, '/');
|
||||
const srcPath = path.resolve(basePath, script).replace(/\\/g, '/');
|
||||
return {
|
||||
srcPath: srcPath,
|
||||
filename: path.relative(basePath, srcPath).replace(/\\/g, '/'),
|
||||
};
|
||||
});
|
||||
|
||||
callback(null, scripts);
|
||||
});
|
||||
return scripts;
|
||||
}
|
||||
|
||||
JS.buildBundle = function (target, fork, callback) {
|
||||
var fileNames = {
|
||||
JS.buildBundle = async function (target, fork) {
|
||||
const fileNames = {
|
||||
client: 'nodebb.min.js',
|
||||
admin: 'acp.min.js',
|
||||
};
|
||||
await requirejsOptimize(target);
|
||||
const files = await getBundleScriptList(target);
|
||||
await mkdirp(path.join(__dirname, '../../build/public'));
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
requirejsOptimize(target, next);
|
||||
},
|
||||
function (next) {
|
||||
getBundleScriptList(target, next);
|
||||
},
|
||||
function (files, next) {
|
||||
mkdirpCallback(path.join(__dirname, '../../build/public'), function (err) {
|
||||
next(err, files);
|
||||
});
|
||||
},
|
||||
function (files, next) {
|
||||
files.push({
|
||||
srcPath: path.join(__dirname, '../../build/public/rjs-bundle-' + target + '.js'),
|
||||
});
|
||||
|
||||
var minify = process.env.NODE_ENV !== 'development';
|
||||
var filePath = path.join(__dirname, '../../build/public', fileNames[target]);
|
||||
const minify = process.env.NODE_ENV !== 'development';
|
||||
const filePath = path.join(__dirname, '../../build/public', fileNames[target]);
|
||||
|
||||
minifier.js.bundle({
|
||||
await minifier.js.bundle({
|
||||
files: files,
|
||||
filename: fileNames[target],
|
||||
destPath: filePath,
|
||||
}, minify, fork, next);
|
||||
},
|
||||
], callback);
|
||||
}, minify, fork);
|
||||
};
|
||||
|
||||
JS.killMinifier = function () {
|
||||
|
||||
@@ -1,24 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
var fs = require('fs');
|
||||
var os = require('os');
|
||||
var uglify = require('uglify-es');
|
||||
var async = require('async');
|
||||
var winston = require('winston');
|
||||
var less = require('less');
|
||||
var postcss = require('postcss');
|
||||
var autoprefixer = require('autoprefixer');
|
||||
var clean = require('postcss-clean');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const uglify = require('uglify-es');
|
||||
const async = require('async');
|
||||
const winston = require('winston');
|
||||
const less = require('less');
|
||||
const postcss = require('postcss');
|
||||
const autoprefixer = require('autoprefixer');
|
||||
const clean = require('postcss-clean');
|
||||
|
||||
var fork = require('./debugFork');
|
||||
const fork = require('./debugFork');
|
||||
require('../file'); // for graceful-fs
|
||||
|
||||
var Minifier = module.exports;
|
||||
const Minifier = module.exports;
|
||||
|
||||
var pool = [];
|
||||
var free = [];
|
||||
const pool = [];
|
||||
const free = [];
|
||||
|
||||
var maxThreads = 0;
|
||||
let maxThreads = 0;
|
||||
|
||||
Object.defineProperty(Minifier, 'maxThreads', {
|
||||
get: function () {
|
||||
@@ -300,3 +300,5 @@ Minifier.css.bundle = function (source, paths, minify, fork, callback) {
|
||||
minify: minify,
|
||||
}, fork, callback);
|
||||
};
|
||||
|
||||
require('../promisify')(exports);
|
||||
|
||||
@@ -208,8 +208,7 @@ middleware.buildSkinAsset = helpers.try(async function buildSkinAsset(req, res,
|
||||
}
|
||||
|
||||
await plugins.prepareForBuild(['client side styles']);
|
||||
const buildBundle = util.promisify(meta.css.buildBundle);
|
||||
const css = await buildBundle(target[0], true);
|
||||
const css = await meta.css.buildBundle(target[0], true);
|
||||
require('../meta/minifier').killAll();
|
||||
res.status(200).type('text/css').send(css);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user