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