@scoutello/i18n-magic 0.19.0 → 0.20.0
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/README.md +15 -31
- package/dist/cli.js +0 -6
- package/dist/cli.js.map +1 -1
- package/dist/commands/clean.d.ts.map +1 -1
- package/dist/commands/clean.js +16 -29
- package/dist/commands/clean.js.map +1 -1
- package/dist/commands/replace.d.ts.map +1 -1
- package/dist/commands/replace.js +79 -32
- package/dist/commands/replace.js.map +1 -1
- package/dist/index.d.ts +0 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -5
- package/dist/index.js.map +1 -1
- package/dist/lib/utils.d.ts +10 -2
- package/dist/lib/utils.d.ts.map +1 -1
- package/dist/lib/utils.js +50 -15
- package/dist/lib/utils.js.map +1 -1
- package/package.json +1 -1
- package/src/cli.ts +1 -6
- package/src/commands/clean.ts +19 -32
- package/src/commands/replace.ts +97 -46
- package/src/index.ts +2 -7
- package/src/lib/utils.ts +70 -19
- package/src/commands/create-pruned-namespace-automated.ts +0 -165
- package/src/commands/create-pruned-namespace.ts +0 -168
|
@@ -1,168 +0,0 @@
|
|
|
1
|
-
import glob from "fast-glob"
|
|
2
|
-
import { Parser } from "i18next-scanner"
|
|
3
|
-
import fs from "node:fs"
|
|
4
|
-
import prompts from "prompts"
|
|
5
|
-
import type { Configuration } from "../lib/types"
|
|
6
|
-
import {
|
|
7
|
-
getPureKey,
|
|
8
|
-
loadLocalesFile,
|
|
9
|
-
removeDuplicatesFromArray,
|
|
10
|
-
writeLocalesFile,
|
|
11
|
-
} from "../lib/utils"
|
|
12
|
-
|
|
13
|
-
export const createPrunedNamespace = async (config: Configuration) => {
|
|
14
|
-
const { namespaces, loadPath, savePath, locales, defaultNamespace } = config
|
|
15
|
-
|
|
16
|
-
// Step 1: Ask for source namespace
|
|
17
|
-
const sourceNamespaceResponse = await prompts({
|
|
18
|
-
type: "select",
|
|
19
|
-
name: "value",
|
|
20
|
-
message: "Select source namespace to create pruned version from:",
|
|
21
|
-
choices: namespaces.map((namespace) => ({
|
|
22
|
-
title: namespace,
|
|
23
|
-
value: namespace,
|
|
24
|
-
})),
|
|
25
|
-
onState: (state) => {
|
|
26
|
-
if (state.aborted) {
|
|
27
|
-
process.nextTick(() => {
|
|
28
|
-
process.exit(0)
|
|
29
|
-
})
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
})
|
|
33
|
-
|
|
34
|
-
const sourceNamespace = sourceNamespaceResponse.value
|
|
35
|
-
|
|
36
|
-
// Step 2: Ask for new namespace name
|
|
37
|
-
const newNamespaceResponse = await prompts({
|
|
38
|
-
type: "text",
|
|
39
|
-
name: "value",
|
|
40
|
-
message: "Enter the name for the new namespace:",
|
|
41
|
-
validate: (value) => {
|
|
42
|
-
if (!value) return "Namespace name cannot be empty"
|
|
43
|
-
if (namespaces.includes(value)) return "Namespace already exists"
|
|
44
|
-
return true
|
|
45
|
-
},
|
|
46
|
-
onState: (state) => {
|
|
47
|
-
if (state.aborted) {
|
|
48
|
-
process.nextTick(() => {
|
|
49
|
-
process.exit(0)
|
|
50
|
-
})
|
|
51
|
-
}
|
|
52
|
-
},
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
const newNamespace = newNamespaceResponse.value
|
|
56
|
-
|
|
57
|
-
// Step 3: Ask for glob patterns to find relevant keys
|
|
58
|
-
const allPatterns = config.globPatterns.map((pattern) =>
|
|
59
|
-
typeof pattern === "string" ? pattern : pattern.pattern,
|
|
60
|
-
)
|
|
61
|
-
const globPatternsResponse = await prompts({
|
|
62
|
-
type: "list",
|
|
63
|
-
name: "value",
|
|
64
|
-
message: "Enter glob patterns to find relevant keys (comma separated):",
|
|
65
|
-
initial: allPatterns.join(","),
|
|
66
|
-
separator: ",",
|
|
67
|
-
onState: (state) => {
|
|
68
|
-
if (state.aborted) {
|
|
69
|
-
process.nextTick(() => {
|
|
70
|
-
process.exit(0)
|
|
71
|
-
})
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
const selectedGlobPatterns = globPatternsResponse.value
|
|
77
|
-
|
|
78
|
-
console.log(
|
|
79
|
-
`Finding keys used in files matching: ${selectedGlobPatterns.join(", ")}`,
|
|
80
|
-
)
|
|
81
|
-
|
|
82
|
-
// Extract keys from files matching the glob patterns
|
|
83
|
-
const parser = new Parser({
|
|
84
|
-
nsSeparator: false,
|
|
85
|
-
keySeparator: false,
|
|
86
|
-
})
|
|
87
|
-
|
|
88
|
-
const files = await glob([...selectedGlobPatterns, "!**/node_modules/**"])
|
|
89
|
-
console.log(`Found ${files.length} files to scan`)
|
|
90
|
-
|
|
91
|
-
const extractedKeys = []
|
|
92
|
-
|
|
93
|
-
for (const file of files) {
|
|
94
|
-
const content = fs.readFileSync(file, "utf-8")
|
|
95
|
-
parser.parseFuncFromString(content, { list: ["t"] }, (key: string) => {
|
|
96
|
-
extractedKeys.push(key)
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
const uniqueExtractedKeys = removeDuplicatesFromArray(extractedKeys)
|
|
101
|
-
console.log(`Found ${uniqueExtractedKeys.length} unique translation keys`)
|
|
102
|
-
|
|
103
|
-
// Filter keys that belong to the source namespace
|
|
104
|
-
const relevantKeys = []
|
|
105
|
-
|
|
106
|
-
for (const key of uniqueExtractedKeys) {
|
|
107
|
-
const pureKey = getPureKey(
|
|
108
|
-
key,
|
|
109
|
-
sourceNamespace,
|
|
110
|
-
sourceNamespace === defaultNamespace,
|
|
111
|
-
)
|
|
112
|
-
|
|
113
|
-
if (pureKey) {
|
|
114
|
-
relevantKeys.push(pureKey)
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
console.log(
|
|
119
|
-
`Found ${relevantKeys.length} keys from namespace '${sourceNamespace}'`,
|
|
120
|
-
)
|
|
121
|
-
|
|
122
|
-
if (relevantKeys.length === 0) {
|
|
123
|
-
console.log("No relevant keys found. Exiting...")
|
|
124
|
-
return
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
// Get translations from source namespace and create new namespace files
|
|
128
|
-
for (const locale of locales) {
|
|
129
|
-
try {
|
|
130
|
-
// Load source namespace translations
|
|
131
|
-
const sourceTranslations = await loadLocalesFile(
|
|
132
|
-
loadPath,
|
|
133
|
-
locale,
|
|
134
|
-
sourceNamespace,
|
|
135
|
-
)
|
|
136
|
-
|
|
137
|
-
// Create new namespace with only the keys used in the glob pattern files
|
|
138
|
-
const newNamespaceTranslations: Record<string, string> = {}
|
|
139
|
-
|
|
140
|
-
for (const key of relevantKeys) {
|
|
141
|
-
if (sourceTranslations[key]) {
|
|
142
|
-
newNamespaceTranslations[key] = sourceTranslations[key]
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
// Write the new namespace file
|
|
147
|
-
await writeLocalesFile(
|
|
148
|
-
savePath,
|
|
149
|
-
locale,
|
|
150
|
-
newNamespace,
|
|
151
|
-
newNamespaceTranslations,
|
|
152
|
-
)
|
|
153
|
-
|
|
154
|
-
console.log(
|
|
155
|
-
`Created pruned namespace '${newNamespace}' for locale '${locale}' with ${
|
|
156
|
-
Object.keys(newNamespaceTranslations).length
|
|
157
|
-
} keys`,
|
|
158
|
-
)
|
|
159
|
-
} catch (error) {
|
|
160
|
-
console.error(
|
|
161
|
-
`Error creating pruned namespace for locale '${locale}':`,
|
|
162
|
-
error,
|
|
163
|
-
)
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
console.log(`✅ Successfully created pruned namespace '${newNamespace}'`)
|
|
168
|
-
}
|