@uxf/scripts 10.0.0-beta.47 → 10.0.0-beta.56
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/uxf-i18n-namespaces-gen/cli.js +7 -5
- package/src/uxf-i18n-namespaces-gen/index.js +6 -42
- package/src/uxf-i18n-namespaces-gen/utils/find-t-function-namespaces.js +20 -0
- package/src/uxf-i18n-namespaces-gen/utils/find-t-function-namespaces.test.js +28 -0
- package/src/uxf-i18n-namespaces-gen/utils/find-trans-component-namespaces.js +18 -0
- package/src/uxf-i18n-namespaces-gen/utils/find-trans-component-namespaces.test.js +13 -0
package/package.json
CHANGED
|
@@ -3,22 +3,24 @@ const { argv, env } = require("process");
|
|
|
3
3
|
module.exports = async () => {
|
|
4
4
|
const cli = require("yargs")
|
|
5
5
|
.command("$0", "UXF i18n namespaces generator", (yargs) => {
|
|
6
|
-
yargs.demandCommand(0, 0).usage(`
|
|
7
|
-
Usage:
|
|
6
|
+
yargs.demandCommand(0, 0).usage(`
|
|
7
|
+
Usage:
|
|
8
8
|
uxf-i18n-namespaces-gen [options]`);
|
|
9
9
|
})
|
|
10
|
-
.option("
|
|
10
|
+
.option("i", { alias: "include", array: true, default: [] })
|
|
11
|
+
.option("o", { alias: "output", default: "i18n-pages.json" })
|
|
12
|
+
.option("h", { alias: "help" })
|
|
11
13
|
.strict(false)
|
|
12
14
|
.exitProcess(false);
|
|
13
15
|
|
|
14
16
|
try {
|
|
15
|
-
const { help,
|
|
17
|
+
const { help, include, output } = cli.parse(argv.slice(2));
|
|
16
18
|
|
|
17
19
|
if (Boolean(help)) {
|
|
18
20
|
return 0;
|
|
19
21
|
}
|
|
20
22
|
|
|
21
|
-
await require("./index");
|
|
23
|
+
await require("./index")(include, output);
|
|
22
24
|
} catch (e) {
|
|
23
25
|
console.error(e);
|
|
24
26
|
return 1;
|
|
@@ -3,6 +3,8 @@ const madge = require("madge");
|
|
|
3
3
|
const path = require("path");
|
|
4
4
|
const fs = require("fs");
|
|
5
5
|
const { readFileSync, readdirSync, writeFileSync } = require("fs");
|
|
6
|
+
const { findTFunctionNamespaces } = require("./utils/find-t-function-namespaces");
|
|
7
|
+
const { findTransComponentNamespaces } = require("./utils/find-trans-component-namespaces");
|
|
6
8
|
const join = require("node:path").join;
|
|
7
9
|
|
|
8
10
|
const INCLUDE = ["src"];
|
|
@@ -29,40 +31,6 @@ const walk = (dirPath) => {
|
|
|
29
31
|
});
|
|
30
32
|
};
|
|
31
33
|
|
|
32
|
-
const findTFunctionNamespaces = (pageContent) => {
|
|
33
|
-
const regex = /\bt\("([^:]+):[^"]+"\)/g;
|
|
34
|
-
const matches = pageContent.matchAll(regex);
|
|
35
|
-
const namespaces = new Set();
|
|
36
|
-
|
|
37
|
-
for (const match of matches) {
|
|
38
|
-
const translationKey = match[0];
|
|
39
|
-
const separatorIndex = translationKey.indexOf(":");
|
|
40
|
-
if (separatorIndex !== -1) {
|
|
41
|
-
const namespace = translationKey.substring(3, separatorIndex);
|
|
42
|
-
namespaces.add(namespace);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return Array.from(namespaces);
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const findTransComponentNamespaces = (pageContent) => {
|
|
50
|
-
const regex = /<Trans[^>]*?i18nKey=(['"])(.*?)\1[^>]*\/>/g;
|
|
51
|
-
const matches = pageContent.matchAll(regex);
|
|
52
|
-
const namespaces = new Set();
|
|
53
|
-
|
|
54
|
-
for (const match of matches) {
|
|
55
|
-
const [, , translationKey] = match;
|
|
56
|
-
const separatorIndex = translationKey.indexOf(":");
|
|
57
|
-
if (separatorIndex !== -1) {
|
|
58
|
-
const namespace = translationKey.substring(0, separatorIndex);
|
|
59
|
-
namespaces.add(namespace);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return Array.from(namespaces);
|
|
64
|
-
};
|
|
65
|
-
|
|
66
34
|
const findNamespaces = (pageContent) => {
|
|
67
35
|
const namespaces = [...findTFunctionNamespaces(pageContent), ...findTransComponentNamespaces(pageContent)];
|
|
68
36
|
|
|
@@ -103,10 +71,10 @@ const filePathToRoute = (filePath) => {
|
|
|
103
71
|
return removeTrailingSlash(route);
|
|
104
72
|
};
|
|
105
73
|
|
|
106
|
-
function main() {
|
|
74
|
+
function main(includes, output) {
|
|
107
75
|
// Negative lookahead – ignore searching for any files
|
|
108
76
|
// that aren't part of our include list
|
|
109
|
-
const searchDirs = new RegExp(`^(?!(${INCLUDE.join("|")}))`, "i");
|
|
77
|
+
const searchDirs = new RegExp(`^(?!(${[...INCLUDE, includes].join("|")}))`, "i");
|
|
110
78
|
|
|
111
79
|
const pages = walk("src/pages").flat(Number.POSITIVE_INFINITY);
|
|
112
80
|
|
|
@@ -140,14 +108,10 @@ function main() {
|
|
|
140
108
|
}
|
|
141
109
|
}
|
|
142
110
|
|
|
143
|
-
writeFileSync(path.resolve(process.cwd(),
|
|
111
|
+
writeFileSync(path.resolve(process.cwd(), output), JSON.stringify(result, null, 4));
|
|
144
112
|
|
|
145
113
|
console.log("Namespaces generated!");
|
|
146
114
|
});
|
|
147
115
|
}
|
|
148
116
|
|
|
149
|
-
|
|
150
|
-
main();
|
|
151
|
-
} catch (e) {
|
|
152
|
-
console.error("Error:", e);
|
|
153
|
-
}
|
|
117
|
+
module.exports = main;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
function findTFunctionNamespaces(pageContent) {
|
|
2
|
+
const regex = /\bt\("([^:]+):[^"]+"/g;
|
|
3
|
+
const matches = pageContent.matchAll(regex);
|
|
4
|
+
const namespaces = new Set();
|
|
5
|
+
|
|
6
|
+
for (const match of matches) {
|
|
7
|
+
const translationKey = match[0];
|
|
8
|
+
const separatorIndex = translationKey.indexOf(":");
|
|
9
|
+
if (separatorIndex !== -1) {
|
|
10
|
+
const namespace = translationKey.substring(3, separatorIndex);
|
|
11
|
+
namespaces.add(namespace);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return Array.from(namespaces);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = {
|
|
19
|
+
findTFunctionNamespaces,
|
|
20
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
const { findTFunctionNamespaces } = require("./find-t-function-namespaces");
|
|
2
|
+
|
|
3
|
+
const content = `
|
|
4
|
+
<div>
|
|
5
|
+
<div>
|
|
6
|
+
{t("basic:title")}
|
|
7
|
+
</div>
|
|
8
|
+
<div title={
|
|
9
|
+
condition
|
|
10
|
+
? 'title'
|
|
11
|
+
: t("multiline-with-parameters:title", {
|
|
12
|
+
param: 1
|
|
13
|
+
})
|
|
14
|
+
}/>
|
|
15
|
+
{t("with-parameters:title", {a: t("in-translation-parameter:title")})}
|
|
16
|
+
</div>
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
describe("find namespaces in t functions", function () {
|
|
20
|
+
it("should find namespaces", function () {
|
|
21
|
+
expect(findTFunctionNamespaces(content)).toStrictEqual([
|
|
22
|
+
"basic",
|
|
23
|
+
"multiline-with-parameters",
|
|
24
|
+
"with-parameters",
|
|
25
|
+
"in-translation-parameter",
|
|
26
|
+
]);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function findTransComponentNamespaces(pageContent) {
|
|
2
|
+
const regex = /<Trans[^>]*?i18nKey=(['"])(.*?)\1[^>]*\/>/g;
|
|
3
|
+
const matches = pageContent.matchAll(regex);
|
|
4
|
+
const namespaces = new Set();
|
|
5
|
+
|
|
6
|
+
for (const match of matches) {
|
|
7
|
+
const [, , translationKey] = match;
|
|
8
|
+
const separatorIndex = translationKey.indexOf(":");
|
|
9
|
+
if (separatorIndex !== -1) {
|
|
10
|
+
const namespace = translationKey.substring(0, separatorIndex);
|
|
11
|
+
namespaces.add(namespace);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return Array.from(namespaces);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = { findTransComponentNamespaces };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
const { findTransComponentNamespaces } = require("./find-trans-component-namespaces");
|
|
2
|
+
|
|
3
|
+
const content = `
|
|
4
|
+
<div>
|
|
5
|
+
<Trans i18nKey="basic:title" />
|
|
6
|
+
</div>
|
|
7
|
+
`;
|
|
8
|
+
|
|
9
|
+
describe("find namespaces in trans component", function () {
|
|
10
|
+
it("should find namespaces", function () {
|
|
11
|
+
expect(findTransComponentNamespaces(content)).toStrictEqual(["basic"]);
|
|
12
|
+
});
|
|
13
|
+
});
|