From 413bb4736cabb8df203d17fe503a098d719b8d67 Mon Sep 17 00:00:00 2001 From: Peter Jaszkowiak Date: Thu, 20 Apr 2017 00:16:06 -0600 Subject: [PATCH] Save language metadata on build to avoid readdirs later --- src/languages.js | 81 ++++++++++++++++++++++++++----------------- src/meta/languages.js | 19 ++++++++++ 2 files changed, 69 insertions(+), 31 deletions(-) diff --git a/src/languages.js b/src/languages.js index c4c3d5ae0e..520ae8bba1 100644 --- a/src/languages.js +++ b/src/languages.js @@ -4,7 +4,7 @@ var fs = require('fs'); var path = require('path'); var async = require('async'); -var Languages = {}; +var Languages = module.exports; var languagesPath = path.join(__dirname, '../build/public/language'); Languages.init = function (next) { @@ -27,10 +27,13 @@ Languages.get = function (language, namespace, callback) { }); }; -Languages.list = function (callback) { - var languages = []; +var codeCache = null; +Languages.listCodes = function (callback) { + if (codeCache && codeCache.length) { + return callback(null, codeCache); + } - fs.readdir(languagesPath, function (err, files) { + fs.readFile(path.join(languagesPath, 'metadata.json'), function (err, buffer) { if (err && err.code === 'ENOENT') { return callback(null, []); } @@ -38,43 +41,59 @@ Languages.list = function (callback) { return callback(err); } - async.each(files, function (folder, next) { - fs.stat(path.join(languagesPath, folder), function (err, stat) { + var parsed; + try { + parsed = JSON.parse(buffer.toString()); + } catch (e) { + return callback(e); + } + + var langs = parsed.languages; + codeCache = langs; + callback(null, langs); + }); +}; + +var listCache = null; +Languages.list = function (callback) { + if (listCache && listCache.length) { + return callback(null, listCache); + } + + Languages.listCodes(function (err, codes) { + if (err) { + return callback(err); + } + + async.map(codes, function (folder, next) { + var configPath = path.join(languagesPath, folder, 'language.json'); + + fs.readFile(configPath, function (err, buffer) { + if (err && err.code === 'ENOENT') { + return next(); + } if (err) { return next(err); } - - if (!stat.isDirectory()) { - return next(); + try { + var lang = JSON.parse(buffer.toString()); + next(null, lang); + } catch (e) { + next(e); } - - var configPath = path.join(languagesPath, folder, 'language.json'); - - fs.readFile(configPath, function (err, buffer) { - if (err && err.code !== 'ENOENT') { - return next(err); - } - if (buffer) { - var lang = JSON.parse(buffer.toString()); - if (lang.name && lang.code && lang.dir) { - languages.push(lang); - } - } - next(); - }); }); - }, function (err) { + }, function (err, languages) { if (err) { return callback(err); } - // Sort alphabetically - languages = languages.sort(function (a, b) { - return a.code > b.code ? 1 : -1; + + // filter out invalid ones + languages = languages.filter(function (lang) { + return lang.code && lang.name && lang.dir; }); - callback(err, languages); + listCache = languages; + callback(null, languages); }); }); }; - -module.exports = Languages; diff --git a/src/meta/languages.js b/src/meta/languages.js index b420445f16..395711cba7 100644 --- a/src/meta/languages.js +++ b/src/meta/languages.js @@ -102,6 +102,25 @@ function getTranslationTree(callback) { }); }, + // save a list of languages to `${buildLanguagesPath}/metadata.json` + // avoids readdirs later on + function (ref, next) { + async.waterfall([ + function (next) { + mkdirp(buildLanguagesPath, next); + }, + function (x, next) { + fs.writeFile(path.join(buildLanguagesPath, 'metadata.json'), JSON.stringify({ + languages: ref.languages.sort(), + namespaces: ref.namespaces.sort(), + }), next); + }, + function (next) { + next(null, ref); + }, + ], next); + }, + // for each language and namespace combination, // run through core and all plugins to generate // a full translation hash