-
Notifications
You must be signed in to change notification settings - Fork 167
/
build-translation-tree.js
66 lines (51 loc) · 2.12 KB
/
build-translation-tree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const fs = require('fs');
const path = require('path');
const funnel = require('broccoli-funnel');
const mergeTrees = require('broccoli-merge-trees');
const WatchedDir = require('broccoli-source').WatchedDir;
const enums = require('./enums');
function funnelTranslations(tree, destDir) {
if (!tree) {
return;
}
return funnel(tree, {
destDir: destDir || '.',
include: ['**/*.yaml', '**/*.yml', '**/*.json'],
});
}
function buildTranslationTree(project, inputPath, treeGenerator) {
const projectTranslations = path.join(project.root, inputPath);
const addonTrees = [];
const addonsWithTranslations = [];
const trees = [];
processAddons(project.addons, addonsWithTranslations, addonTrees, treeGenerator);
if (addonTrees.length > 0) {
trees.push(funnelTranslations(mergeTrees(addonTrees, { overwrite: true }), enums.addonNamespace));
}
if (fs.existsSync(projectTranslations)) {
trees.push(new WatchedDir(projectTranslations));
}
return [mergeTrees(trees, { overwrite: true }), addonsWithTranslations];
}
function processAddons(addons, addonsWithTranslations, translationTrees, treeGenerator) {
addons.forEach((addon) => _processAddon(addon, addonsWithTranslations, translationTrees, treeGenerator));
}
function _processAddon(addon, addonsWithTranslations, translationTrees, treeGenerator) {
const addonTranslationPath = path.join(addon.root, 'translations');
let addonGeneratedTree;
if (fs.existsSync(addonTranslationPath)) {
addonGeneratedTree = treeGenerator.call(addon, addonTranslationPath);
}
if (addon.treeForTranslations) {
let additionalTranslationTree = addon.treeForTranslations(addonGeneratedTree);
if (additionalTranslationTree) {
addonsWithTranslations.push(addon.name);
translationTrees.push(funnel(additionalTranslationTree, { destDir: addon.name }));
}
} else if (addonGeneratedTree !== undefined) {
addonsWithTranslations.push(addon.name);
translationTrees.push(funnel(addonGeneratedTree, { destDir: addon.name }));
}
processAddons(addon.addons, addonsWithTranslations, translationTrees, treeGenerator);
}
module.exports = buildTranslationTree;