Skip to content

Commit

Permalink
Merge 443fb5f into 31358dc
Browse files Browse the repository at this point in the history
  • Loading branch information
frensing committed Nov 24, 2021
2 parents 31358dc + 443fb5f commit 4427211
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
10 changes: 9 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const saml2 = require('./routes/saml2/saml2');
const authregistry = require('./routes/authregistry/authregistry');
const oauth2_controller = require('./controllers/oauth2/oauth2');

const translation_merger = require('./lib/json_directory_merger');

const app = express();
const helmet = require('helmet');

Expand Down Expand Up @@ -146,9 +148,15 @@ if (!config.headless) {
app.use(express.static(path.join(__dirname, 'public')));
app.use(method_override('_method'));

const translation_path = path.join(__dirname, 'etc/translations/');
app.use(
translation_merger.init({
directory_1: translation_path,
directory_2: path.join(__dirname, 'themes/' + config.site.theme + '/translations/'),
merged_path: path.join(translation_path, 'merged')
}),
i18n({
translationsPath: path.join(__dirname, 'etc/translations'), // eslint-disable-line snakecase/snakecase
translationsPath: translation_merger.get_merge_path(), // eslint-disable-line snakecase/snakecase
siteLangs: ['de', 'en', 'es', 'ja', 'ko'], // eslint-disable-line snakecase/snakecase
textsVarName: 'translation', // eslint-disable-line snakecase/snakecase
browserEnable: true, // eslint-disable-line snakecase/snakecase
Expand Down
87 changes: 87 additions & 0 deletions lib/json_directory_merger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const fs = require('fs');
const merge = require('deepmerge');
const path = require('path');

function get_file_list(path) {
if (!fs.existsSync(path)) {
return [];
}
return fs.readdirSync(path);
}

function merge_json_files(file_path_1, file_path_2) {
try {
delete require.cache[require.resolve(file_path_1)];
delete require.cache[require.resolve(file_path_2)];
} catch (e) { console.error(e); }

return merge(require(file_path_1), require(file_path_2));
}

function merge_files_from_paths(path_1, path_2, merged_path) {
const path_1_file_list = get_file_list(path_1);
const path_2_file_list = get_file_list(path_2);

if (!fs.existsSync(merged_path)) {
fs.mkdirSync(merged_path, { recursive: true });
}

if (path_2_file_list && path_1_file_list) {
for (const ind in path_1_file_list) {
const file_name = path_1_file_list[ind];

if (!file_name.toLowerCase().includes('.json')) {
continue;
}

const file_path_1 = path.join(path_1, file_name);

if (path_2_file_list.includes(file_name)) {
const file_path_2 = path.join(path_2, file_name);
const merged_obj = merge_json_files(file_path_1, file_path_2);
fs.writeFileSync(path.join(merged_path, file_name), JSON.stringify(merged_obj));
} else {
fs.copyFileSync(file_path_1, path.join(merged_path, file_name));
}
}
}
}

function get_merge_path() {
return json_path;
}

let json_path;

function init(opts) {
const directory_1 = opts.directory_1 || json_path;
const directory_2 = opts.directory_2 || directory_1;
const merged_path = opts.merged_path || path.join(directory_1, 'merged');

if (!fs.existsSync(directory_2)) {
json_path = directory_1;
return function (req, res, next) { next() }
}

merge_files_from_paths(directory_1, directory_2, merged_path);

function watch_func(event, filename) {
if (filename) {
try {
merge_files_from_paths(directory_1, directory_2, merged_path);
} catch (e) { console.error(e); }
}
}

fs.watch(directory_1, watch_func);
fs.watch(directory_2, watch_func);

json_path = merged_path;

return function (req, res, next) { next() }
}

module.exports = {
init,
get_merge_path
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"cors": "^2.8.5",
"csurf": "^1.9.0",
"debug": "~2.6.3",
"deepmerge": "^4.2.2",
"device-detector-js": "^2.0.5",
"ejs": "~2.5.7",
"express": "^4.16.4",
Expand Down

0 comments on commit 4427211

Please sign in to comment.