-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
110 lines (101 loc) · 3.47 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const path = require("path");
const fs = require("fs");
const merge = require("lodash/merge");
const globAll = require("glob-all");
const yaml = require("js-yaml");
const set = require("lodash/set");
function enumerateLangs(dir) {
return fs.readdirSync(dir).filter(function (file) {
return fs.statSync(path.join(dir, file)).isDirectory();
});
}
//https://github.com/jpillora/node-glob-all#usage
function findAll(globs, cwd) {
const globArray = Array.isArray(globs) ? globs : [globs];
return globAll.sync(globArray, { cwd, realpath: true });
}
module.exports = function () {
this.cacheable && this.cacheable();
const options = this.getOptions() || {};
if (!options.include) {
options.include = ["**/*.json", "**/*.yml", "**/*.yaml"];
}
if (!options.overrides) options.overrides = [];
const appLocalesDir = path.dirname(this.resource); // this is the absolute path to the index.js in the top level locales dir
if (!fs.existsSync(appLocalesDir)) {
throw new Error(
"Directory does not exist: " +
appLocalesDir +
" for resource: " +
this.resource
);
}
let appResBundle = {};
if (options.debug) {
console.info(
"Bundling locales from " +
appLocalesDir +
" (ordered least specific to most):"
);
}
// needs to be ordered in least specialized to most e.g. lib locale -> app locale
const moduleLocalesDirs = options.overrides.map((override) => {
if (path.isAbsolute(override)) {
return override;
} else {
return path.join(appLocalesDir, override);
}
});
moduleLocalesDirs.push(appLocalesDir);
moduleLocalesDirs.forEach((localesDir) => {
// all subdirectories match language codes
const langs = enumerateLangs(localesDir);
for (let i = 0; i < langs.length; i++) {
const lang = langs[i];
const resBundle = {};
resBundle[lang] = {};
const fullLangPath = path.join(localesDir, lang);
this.addContextDependency(fullLangPath);
const langFiles = findAll(options.include, fullLangPath);
for (let j = 0; j < langFiles.length; j++) {
const fullPath = langFiles[j];
this.addDependency(fullPath);
if (options.debug) {
console.info("\t" + fullPath);
}
const fileContent = fs.readFileSync(fullPath);
const extname = path.extname(fullPath);
let parsedContent;
if (extname === ".yaml" || extname === ".yml") {
parsedContent = yaml.load(fileContent);
} else {
parsedContent = JSON.parse(fileContent);
}
if (options.basenameAsNamespace || options.relativePathAsNamespace) {
let namespaceFilepath;
if (options.relativePathAsNamespace) {
namespaceFilepath = path.relative(
path.join(localesDir, lang),
fullPath
);
} else if (options.basenameAsNamespace) {
namespaceFilepath = path.basename(fullPath);
}
const namespaceParts = namespaceFilepath
.replace(extname, "")
.split(path.sep);
const namespace = [lang].concat(namespaceParts).join(".");
set(resBundle, namespace, parsedContent);
} else {
resBundle[lang] = parsedContent;
}
appResBundle = merge(appResBundle, resBundle);
}
}
});
const bundle = JSON.stringify(appResBundle);
if (options.debug) {
console.info("Final locales bundle: \n" + bundle);
}
return "module.exports = " + bundle;
};