@scoutello/i18n-magic 0.19.0 → 0.21.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.
@@ -1,165 +0,0 @@
1
- import glob from "fast-glob"
2
- import { Parser } from "i18next-scanner"
3
- import fs from "node:fs"
4
- import type { Configuration } from "../lib/types"
5
- import {
6
- getPureKey,
7
- loadLocalesFile,
8
- removeDuplicatesFromArray,
9
- writeLocalesFile,
10
- } from "../lib/utils"
11
-
12
- export interface PruneOptions {
13
- sourceNamespace: string
14
- newNamespace: string
15
- globPatterns: string[]
16
- }
17
-
18
- export interface PruneResult {
19
- locale: string
20
- keyCount: number
21
- success: boolean
22
- error?: string
23
- }
24
-
25
- export interface PruneResponse {
26
- success: boolean
27
- message: string
28
- keysCount: number
29
- results?: PruneResult[]
30
- }
31
-
32
- export const createPrunedNamespaceAutomated = async (
33
- config: Configuration,
34
- options: PruneOptions,
35
- ): Promise<PruneResponse> => {
36
- const { namespaces, loadPath, savePath, locales, defaultNamespace } = config
37
- const { sourceNamespace, newNamespace, globPatterns } = options
38
-
39
- // Validate inputs
40
- if (!namespaces.includes(sourceNamespace)) {
41
- throw new Error(
42
- `Source namespace '${sourceNamespace}' not found in configuration`,
43
- )
44
- }
45
-
46
- if (namespaces.includes(newNamespace)) {
47
- throw new Error(`Namespace '${newNamespace}' already exists`)
48
- }
49
-
50
- console.log(
51
- `Creating pruned namespace '${newNamespace}' from '${sourceNamespace}'`,
52
- )
53
- console.log(`Using glob patterns: ${globPatterns.join(", ")}`)
54
-
55
- // Extract keys from files matching the glob patterns
56
- const parser = new Parser({
57
- nsSeparator: false,
58
- keySeparator: false,
59
- })
60
-
61
- const files = await glob([...globPatterns, "!**/node_modules/**"])
62
- console.log(`Found ${files.length} files to scan`)
63
-
64
- const extractedKeys = []
65
-
66
- for (const file of files) {
67
- const content = fs.readFileSync(file, "utf-8")
68
- parser.parseFuncFromString(content, { list: ["t"] }, (key: string) => {
69
- extractedKeys.push(key)
70
- })
71
- }
72
-
73
- const uniqueExtractedKeys = removeDuplicatesFromArray(extractedKeys)
74
- console.log(`Found ${uniqueExtractedKeys.length} unique translation keys`)
75
-
76
- // Filter keys that belong to the source namespace
77
- const relevantKeys = []
78
-
79
- for (const key of uniqueExtractedKeys) {
80
- const pureKey = getPureKey(
81
- key,
82
- sourceNamespace,
83
- sourceNamespace === defaultNamespace,
84
- )
85
-
86
- if (pureKey) {
87
- relevantKeys.push(pureKey)
88
- }
89
- }
90
-
91
- console.log(
92
- `Found ${relevantKeys.length} keys from namespace '${sourceNamespace}'`,
93
- )
94
-
95
- if (relevantKeys.length === 0) {
96
- console.log("No relevant keys found. Exiting...")
97
- return {
98
- success: false,
99
- message: "No relevant keys found",
100
- keysCount: 0,
101
- }
102
- }
103
-
104
- // Get translations from source namespace and create new namespace files
105
- const results: PruneResult[] = []
106
-
107
- for (const locale of locales) {
108
- try {
109
- // Load source namespace translations
110
- const sourceTranslations = await loadLocalesFile(
111
- loadPath,
112
- locale,
113
- sourceNamespace,
114
- )
115
-
116
- // Create new namespace with only the keys used in the glob pattern files
117
- const newNamespaceTranslations: Record<string, string> = {}
118
-
119
- for (const key of relevantKeys) {
120
- if (sourceTranslations[key]) {
121
- newNamespaceTranslations[key] = sourceTranslations[key]
122
- }
123
- }
124
-
125
- // Write the new namespace file
126
- await writeLocalesFile(
127
- savePath,
128
- locale,
129
- newNamespace,
130
- newNamespaceTranslations,
131
- )
132
-
133
- const keyCount = Object.keys(newNamespaceTranslations).length
134
- console.log(
135
- `Created pruned namespace '${newNamespace}' for locale '${locale}' with ${keyCount} keys`,
136
- )
137
-
138
- results.push({
139
- locale,
140
- keyCount,
141
- success: true,
142
- })
143
- } catch (error) {
144
- console.error(
145
- `Error creating pruned namespace for locale '${locale}':`,
146
- error,
147
- )
148
- results.push({
149
- locale,
150
- keyCount: 0,
151
- success: false,
152
- error: error instanceof Error ? error.message : String(error),
153
- })
154
- }
155
- }
156
-
157
- console.log(`✅ Successfully created pruned namespace '${newNamespace}'`)
158
-
159
- return {
160
- success: true,
161
- message: `Created pruned namespace '${newNamespace}' with ${relevantKeys.length} keys`,
162
- keysCount: relevantKeys.length,
163
- results,
164
- }
165
- }
@@ -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
- }