Make utils and translator easier to require

Move utils.walk to file.walk, backwards compatible
v1.18.x
Peter Jaszkowiak 8 years ago
parent 12a426b7b9
commit 1ed571189c

@ -32,6 +32,7 @@
"no-prototype-builtins": "off",
"new-cap": "off",
"no-plusplus": ["error", { "allowForLoopAfterthoughts": true }],
"import/no-unresolved": "error",
// ES6
"prefer-rest-params": "off",
@ -43,7 +44,6 @@
"vars-on-top": "off",
// TODO
"import/no-unresolved": "off",
"import/no-extraneous-dependencies": "off",
"import/no-dynamic-require": "off",
"import/newline-after-import": "off",

@ -1,13 +1,12 @@
'use strict';
(function (module) {
var utils;
var fs;
var XRegExp;
if (typeof window === 'undefined') {
fs = require('fs');
XRegExp = require('xregexp');
(function (factory) {
if (typeof module === 'object' && module.exports) {
var file = require('../../src/file');
module.exports = factory(require('xregexp'));
module.exports.walk = function (dir, done) {
file.walk(dir, done);
};
process.profile = function (operation, start) {
console.log('%s took %d milliseconds', operation, process.elapsedTimeSince(start));
@ -18,11 +17,10 @@
return (diff[0] * 1e3) + (diff[1] / 1e6);
};
} else {
XRegExp = window.XRegExp;
window.utils = factory(window.XRegExp);
}
utils = {
}(function (XRegExp) {
var utils = {
generateUUID: function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0;
@ -31,49 +29,6 @@
});
},
// Adapted from http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
walk: function (dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var pending = list.length;
if (!pending) {
return done(null, results);
}
list.forEach(function (file) {
file = dir + '/' + file;
fs.stat(file, function (err, stat) {
if (err) {
return done(err);
}
if (stat && stat.isDirectory()) {
utils.walk(file, function (err, res) {
if (err) {
return done(err);
}
results = results.concat(res);
pending -= 1;
if (!pending) {
done(null, results);
}
});
} else {
results.push(file);
pending -= 1;
if (!pending) {
done(null, results);
}
}
});
});
});
},
invalidUnicodeChars: XRegExp('[^\\p{L}\\s\\d\\-_]', 'g'),
invalidLatinChars: /[^\w\s\d\-_]/g,
trimRegex: /^\s+|\s+$/g,
@ -447,11 +402,6 @@
},
};
module.exports = utils;
if (typeof window !== 'undefined') {
window.utils = module.exports;
}
/* eslint "no-extend-native": "off" */
if (typeof String.prototype.startsWith !== 'function') {
String.prototype.startsWith = function (prefix) {
@ -479,8 +429,6 @@
return this.replace(/\s+$/g, '');
};
}
}(typeof module === 'undefined' ? {
module: {
exports: {},
},
} : module));
return utils;
}));

@ -6,8 +6,8 @@ var async = require('async');
var sanitizeHTML = require('sanitize-html');
var nconf = require('nconf');
var utils = require('../../public/src/utils');
var Translator = require('../../public/src/modules/translator').Translator;
var file = require('../file');
var Translator = require('../translator').Translator;
function filterDirectories(directories) {
return directories.map(function (dir) {
@ -24,7 +24,7 @@ function filterDirectories(directories) {
}
function getAdminNamespaces(callback) {
utils.walk(path.resolve(nconf.get('views_dir'), 'admin'), function (err, directories) {
file.walk(path.resolve(nconf.get('views_dir'), 'admin'), function (err, directories) {
if (err) {
return callback(err);
}

@ -4,7 +4,7 @@
var async = require('async');
var db = require('./database');
var utils = require('../public/src/utils');
var utils = require('./utils');
var DEFAULT_BATCH_SIZE = 100;

@ -6,7 +6,7 @@ var db = require('../database');
var groups = require('../groups');
var plugins = require('../plugins');
var privileges = require('../privileges');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Categories) {
Categories.create = function (data, callback) {

@ -5,8 +5,8 @@ var async = require('async');
var db = require('../database');
var meta = require('../meta');
var utils = require('../../public/src/utils');
var translator = require('../../public/src/modules/translator');
var utils = require('../utils');
var translator = require('../translator');
var plugins = require('../plugins');
module.exports = function (Categories) {

@ -9,7 +9,7 @@ var user = require('../../user');
var groups = require('../../groups');
var plugins = require('../../plugins');
var meta = require('../../meta');
var utils = require('../../../public/src/utils');
var utils = require('../../utils');
var helpers = {};

@ -6,7 +6,7 @@ var categories = require('../../categories');
var privileges = require('../../privileges');
var analytics = require('../../analytics');
var plugins = require('../../plugins');
var translator = require('../../../public/src/modules/translator');
var translator = require('../../translator');
var categoriesController = {};

@ -24,13 +24,13 @@ settingsController.get = function (req, res, next) {
function renderEmail(req, res, next) {
var fs = require('fs');
var path = require('path');
var utils = require('../../../public/src/utils');
var file = require('../../file');
var emailsPath = path.join(nconf.get('views_dir'), 'emails');
async.waterfall([
function (next) {
utils.walk(emailsPath, next);
file.walk(emailsPath, next);
},
function (emails, next) {
async.map(emails, function (email, next) {

@ -12,7 +12,7 @@ var categories = require('../categories');
var privileges = require('../privileges');
var plugins = require('../plugins');
var widgets = require('../widgets');
var translator = require('../../public/src/modules/translator');
var translator = require('../translator');
var apiController = module.exports;

@ -11,7 +11,7 @@ var db = require('../database');
var meta = require('../meta');
var user = require('../user');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var Password = require('../password');
var sockets = require('../socket.io');

@ -11,7 +11,7 @@ var categories = require('../categories');
var meta = require('../meta');
var pagination = require('../pagination');
var helpers = require('./helpers');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var categoryController = {};

@ -13,7 +13,7 @@ var privileges = require('../privileges');
var plugins = require('../plugins');
var helpers = require('./helpers');
var pagination = require('../pagination');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var topicsController = {};

@ -1,7 +1,7 @@
'use strict';
var async = require('async');
var utils = require('../../../public/src/utils');
var utils = require('../../utils');
module.exports = function (db, module) {
var helpers = module.helpers.mongo;

@ -1,7 +1,7 @@
'use strict';
module.exports = function (redisClient, module) {
var utils = require('../../../public/src/utils');
var utils = require('../../utils');
var helpers = module.helpers.redis;

@ -13,7 +13,7 @@ var url = require('url');
var User = require('./user');
var Plugins = require('./plugins');
var meta = require('./meta');
var translator = require('../public/src/modules/translator');
var translator = require('./translator');
var transports = {
sendmail: nodemailer.createTransport(sendmailTransport()),

@ -7,7 +7,7 @@ var validator = require('validator');
var db = require('./database');
var batch = require('./batch');
var user = require('./user');
var utils = require('../public/src/utils');
var utils = require('./utils');
(function (events) {
events.log = function (data, callback) {

@ -8,9 +8,9 @@ var jimp = require('jimp');
var mkdirp = require('mkdirp');
var mime = require('mime');
var utils = require('../public/src/utils');
var utils = require('./utils');
var file = {};
var file = module.exports;
file.saveFileToLocal = function (filename, folder, tempPath, callback) {
/*
@ -129,4 +129,47 @@ file.typeToExtension = function (type) {
return extension;
};
// Adapted from http://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search
file.walk = function (dir, done) {
var results = [];
fs.readdir(dir, function (err, list) {
if (err) {
return done(err);
}
var pending = list.length;
if (!pending) {
return done(null, results);
}
list.forEach(function (filename) {
filename = dir + '/' + filename;
fs.stat(filename, function (err, stat) {
if (err) {
return done(err);
}
if (stat && stat.isDirectory()) {
file.walk(filename, function (err, res) {
if (err) {
return done(err);
}
results = results.concat(res);
pending -= 1;
if (!pending) {
done(null, results);
}
});
} else {
results.push(filename);
pending -= 1;
if (!pending) {
done(null, results);
}
}
});
});
});
};
module.exports = file;

@ -6,7 +6,7 @@ var validator = require('validator');
var user = require('./user');
var db = require('./database');
var plugins = require('./plugins');
var utils = require('../public/src/utils');
var utils = require('./utils');
var Groups = module.exports;

@ -3,7 +3,7 @@
var async = require('async');
var meta = require('../meta');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var db = require('../database');
module.exports = function (Groups) {

@ -5,7 +5,7 @@ var validator = require('validator');
var db = require('../database');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Groups) {
Groups.getGroupsData = function (groupNames, callback) {

@ -2,7 +2,7 @@
var async = require('async');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var db = require('./../database');
module.exports = function (Groups) {

@ -5,7 +5,7 @@ var winston = require('winston');
var _ = require('underscore');
var user = require('../user');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var plugins = require('../plugins');
var notifications = require('../notifications');
var db = require('../database');

@ -4,7 +4,7 @@ var async = require('async');
var winston = require('winston');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var db = require('../database');

@ -6,7 +6,7 @@ var path = require('path');
var prompt = require('prompt');
var winston = require('winston');
var nconf = require('nconf');
var utils = require('../public/src/utils.js');
var utils = require('./utils.js');
var install = {};
var questions = {};

@ -8,7 +8,7 @@ var db = require('./database');
var user = require('./user');
var plugins = require('./plugins');
var meta = require('./meta');
var utils = require('../public/src/utils');
var utils = require('./utils');
var Messaging = module.exports;

@ -5,7 +5,7 @@ var S = require('string');
var db = require('../database');
var user = require('../user');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Messaging) {
Messaging.getMessageField = function (mid, field, callback) {

@ -6,7 +6,7 @@ var os = require('os');
var nconf = require('nconf');
var pubsub = require('./pubsub');
var utils = require('../public/src/utils');
var utils = require('./utils');
(function (Meta) {
Meta.reloadRequired = false;

@ -13,7 +13,6 @@ var clean = require('postcss-clean');
var plugins = require('../plugins');
var db = require('../database');
var file = require('../file');
var utils = require('../../public/src/utils');
module.exports = function (Meta) {
Meta.css = {};
@ -106,7 +105,7 @@ module.exports = function (Meta) {
});
async.each(pluginDirectories, function (directory, next) {
utils.walk(directory, function (err, styleFiles) {
file.walk(directory, function (err, styleFiles) {
if (err) {
return next(err);
}

@ -11,7 +11,6 @@ var uglifyjs = require('uglify-js');
var file = require('../file');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var minifierPath = path.join(__dirname, 'minifier.js');
@ -168,7 +167,7 @@ module.exports = function (Meta) {
});
async.each(dirs, function (dir, next) {
utils.walk(dir, function (err, files) {
file.walk(dir, function (err, files) {
if (err) {
return next(err);
}
@ -293,7 +292,7 @@ module.exports = function (Meta) {
});
async.each(pluginDirectories, function (directory, next) {
utils.walk(directory, function (err, scripts) {
file.walk(directory, function (err, scripts) {
pluginsScripts = pluginsScripts.concat(scripts);
next(err);
});

@ -8,7 +8,6 @@ var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var file = require('../file');
var utils = require('../../public/src/utils');
var Plugins = require('../plugins');
var db = require('../database');
@ -67,7 +66,7 @@ function getTranslationTree(callback) {
async.parallel([
// get core languages and namespaces
function (nxt) {
utils.walk(coreLanguagesPath, function (err, paths) {
file.walk(coreLanguagesPath, function (err, paths) {
if (err) {
return nxt(err);
}
@ -80,7 +79,7 @@ function getTranslationTree(callback) {
function (nxt) {
async.each(plugins, function (pluginData, cb) {
var pathToFolder = path.join(__dirname, '../../node_modules/', pluginData.id, pluginData.languages);
utils.walk(pathToFolder, function (err, paths) {
file.walk(pathToFolder, function (err, paths) {
if (err) {
return cb(err);
}

@ -9,7 +9,7 @@ var fs = require('fs');
var nconf = require('nconf');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var file = require('../file');
var Templates = {};
@ -62,11 +62,11 @@ function preparePaths(baseTemplatesPaths, callback) {
async.parallel({
coreTpls: function (next) {
utils.walk(coreTemplatesPath, next);
file.walk(coreTemplatesPath, next);
},
baseThemes: function (next) {
async.map(baseTemplatesPaths, function (baseTemplatePath, next) {
utils.walk(baseTemplatePath, function (err, paths) {
file.walk(baseTemplatePath, function (err, paths) {
paths = paths.map(function (tpl) {
return {
base: baseTemplatePath,

@ -6,7 +6,7 @@ var validator = require('validator');
var winston = require('winston');
var plugins = require('../plugins');
var translator = require('../../public/src/modules/translator');
var translator = require('../translator');
module.exports = function (middleware) {
middleware.processRender = function (req, res, next) {

@ -3,7 +3,7 @@
var async = require('async');
var plugins = require('../plugins');
var db = require('../database');
var translator = require('../../public/src/modules/translator');
var translator = require('../translator');
var pubsub = require('../pubsub');
var admin = module.exports;

@ -2,7 +2,7 @@
var nconf = require('nconf');
var admin = require('./admin');
var translator = require('../../public/src/modules/translator');
var translator = require('../translator');
var navigation = {};

@ -13,7 +13,7 @@ var groups = require('./groups');
var meta = require('./meta');
var batch = require('./batch');
var plugins = require('./plugins');
var utils = require('../public/src/utils');
var utils = require('./utils');
(function (Notifications) {
Notifications.init = function () {

@ -9,7 +9,6 @@ var express = require('express');
var nconf = require('nconf');
var db = require('./database');
var utils = require('../public/src/utils');
var hotswap = require('./hotswap');
var file = require('./file');
var languages = require('./languages');
@ -176,7 +175,7 @@ var middleware;
if (plugin.templates || plugin.id.startsWith('nodebb-theme-')) {
winston.verbose('[plugins] Loading templates (' + plugin.id + ')');
var templatesPath = path.join(__dirname, '../node_modules', plugin.id, plugin.templates || 'templates');
utils.walk(templatesPath, function (err, pluginTemplates) {
file.walk(templatesPath, function (err, pluginTemplates) {
if (pluginTemplates) {
pluginTemplates.forEach(function (pluginTemplate) {
if (pluginTemplate.endsWith('.tpl')) {

@ -4,7 +4,7 @@ var async = require('async');
var _ = require('underscore');
var db = require('./database');
var utils = require('../public/src/utils');
var utils = require('./utils');
var user = require('./user');
var topics = require('./topics');
var privileges = require('./privileges');

@ -9,7 +9,7 @@ var plugins = require('../plugins');
var user = require('../user');
var topics = require('../topics');
var categories = require('../categories');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Posts) {
Posts.create = function (data, callback) {

@ -11,7 +11,7 @@ var privileges = require('../privileges');
var plugins = require('../plugins');
var cache = require('./cache');
var pubsub = require('../pubsub');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Posts) {
pubsub.on('post:edit', function (pid) {

@ -9,7 +9,7 @@ var S = require('string');
var meta = require('../meta');
var cache = require('./cache');
var plugins = require('../plugins');
var translator = require('../../public/src/modules/translator');
var translator = require('../translator');
var urlRegex = /href="([^"]+)"/g;

@ -9,7 +9,7 @@ var db = require('../database');
var user = require('../user');
var plugins = require('../plugins');
var categories = require('../categories');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Posts) {

@ -4,7 +4,7 @@
var async = require('async');
var topics = require('../topics');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Posts) {
Posts.getPostsFromSet = function (set, start, stop, uid, reverse, callback) {

@ -10,7 +10,7 @@ var categories = require('./categories');
var user = require('./user');
var plugins = require('./plugins');
var privileges = require('./privileges');
var utils = require('../public/src/utils');
var utils = require('./utils');
var search = {};

@ -10,7 +10,7 @@ var topics = require('./topics');
var privileges = require('./privileges');
var meta = require('./meta');
var plugins = require('./plugins');
var utils = require('../public/src/utils');
var utils = require('./utils');
var sitemap = {
maps: {

@ -5,7 +5,7 @@ var async = require('async');
var groups = require('../groups');
var meta = require('../meta');
var user = require('../user');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var groupsController = require('../controllers/groups');
var SocketGroups = {};

@ -7,7 +7,7 @@ var meta = require('../meta');
var notifications = require('../notifications');
var plugins = require('../plugins');
var Messaging = require('../messaging');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var server = require('./');
var user = require('../user');

@ -3,7 +3,7 @@
var async = require('async');
var user = require('../user');
var notifications = require('../notifications');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var SocketNotifs = {};

@ -9,7 +9,7 @@ var topics = require('../topics');
var user = require('../user');
var websockets = require('./index');
var socketHelpers = require('./helpers');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var apiController = require('../controllers/api');

@ -11,7 +11,7 @@ var privileges = require('../../privileges');
var notifications = require('../../notifications');
var plugins = require('../../plugins');
var meta = require('../../meta');
var utils = require('../../../public/src/utils');
var utils = require('../../utils');
module.exports = function (SocketPosts) {
SocketPosts.flag = function (socket, data, callback) {

@ -5,7 +5,7 @@ var async = require('async');
var topics = require('../../topics');
var privileges = require('../../privileges');
var meta = require('../../meta');
var utils = require('../../../public/src/utils');
var utils = require('../../utils');
var social = require('../../social');
module.exports = function (SocketTopics) {

@ -3,7 +3,7 @@
var async = require('async');
var db = require('../../database');
var topics = require('../../topics');
var utils = require('../../../public/src/utils');
var utils = require('../../utils');
module.exports = function (SocketTopics) {
SocketTopics.isTagAllowed = function (socket, data, callback) {

@ -5,7 +5,7 @@ var _ = require('underscore');
var db = require('./database');
var posts = require('./posts');
var utils = require('../public/src/utils');
var utils = require('./utils');
var plugins = require('./plugins');
var user = require('./user');
var categories = require('./categories');

@ -5,7 +5,7 @@ var async = require('async');
var validator = require('validator');
var S = require('string');
var db = require('../database');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var plugins = require('../plugins');
var analytics = require('../analytics');
var user = require('../user');

@ -4,7 +4,7 @@ var validator = require('validator');
var db = require('../database');
var categories = require('../categories');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Topics) {
Topics.getTopicField = function (tid, field, callback) {

@ -7,7 +7,7 @@ var db = require('../database');
var meta = require('../meta');
var _ = require('underscore');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Topics) {

@ -9,7 +9,7 @@ var meta = require('../meta');
var user = require('../user');
var posts = require('../posts');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Topics) {
Topics.getTeasers = function (topics, uid, callback) {

@ -9,7 +9,7 @@ var notifications = require('../notifications');
var categories = require('../categories');
var privileges = require('../privileges');
var meta = require('../meta');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (Topics) {
Topics.getTotalUnread = function (uid, filter, callback) {

@ -0,0 +1,3 @@
'use strict';
module.exports = require('../public/src/modules/translator');

@ -9,8 +9,8 @@ var meta = require('../meta');
var emailer = require('../emailer');
var notifications = require('../notifications');
var groups = require('../groups');
var translator = require('../../public/src/modules/translator');
var utils = require('../../public/src/utils');
var translator = require('../translator');
var utils = require('../utils');
var plugins = require('../plugins');
module.exports = function (User) {

@ -2,7 +2,7 @@
var async = require('async');
var db = require('../database');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var validator = require('validator');
var plugins = require('../plugins');
var groups = require('../groups');

@ -7,7 +7,7 @@ var winston = require('winston');
var db = require('../database');
var plugins = require('../plugins');
var utils = require('../../public/src/utils');
var utils = require('../utils');
module.exports = function (User) {
var iconBackgrounds = ['#f44336', '#e91e63', '#9c27b0', '#673ab7', '#3f51b5', '#2196f3',

@ -10,7 +10,7 @@ var user = require('../user');
var topics = require('../topics');
var plugins = require('../plugins');
var emailer = require('../emailer');
var utils = require('../../public/src/utils');
var utils = require('../utils');
(function (Digest) {
Digest.execute = function (interval, callback) {

@ -5,8 +5,8 @@ var async = require('async');
var nconf = require('nconf');
var user = require('../user');
var utils = require('../../public/src/utils');
var translator = require('../../public/src/modules/translator');
var utils = require('../utils');
var translator = require('../translator');
var plugins = require('../plugins');
var db = require('../database');
var meta = require('../meta');

@ -7,8 +7,8 @@ var nconf = require('nconf');
var db = require('./../database');
var meta = require('../meta');
var emailer = require('../emailer');
var translator = require('../../public/src/modules/translator');
var utils = require('../../public/src/utils');
var translator = require('../translator');
var utils = require('../utils');
module.exports = function (User) {

@ -4,7 +4,7 @@
var async = require('async');
var S = require('string');
var utils = require('../../public/src/utils');
var utils = require('../utils');
var meta = require('../meta');
var db = require('../database');
var groups = require('../groups');

@ -5,8 +5,8 @@ var nconf = require('nconf');
var winston = require('winston');
var user = require('../user');
var utils = require('../../public/src/utils');
var translator = require('../../public/src/modules/translator');
var utils = require('../utils');
var translator = require('../translator');
var db = require('../database');
var meta = require('../meta');

@ -0,0 +1,3 @@
'use strict';
module.exports = require('../public/src/utils');

@ -5,7 +5,7 @@ var winston = require('winston');
var templates = require('templates.js');
var plugins = require('../plugins');
var translator = require('../../public/src/modules/translator');
var translator = require('../translator');
var db = require('../database');
var widgets = {};

Loading…
Cancel
Save