Skip to content

Commit cc66b06

Browse files
committed
Add more commands to translatte
- Add command to export server strings - Add command to push migration to the server
1 parent 7c4e136 commit cc66b06

File tree

7 files changed

+620
-3
lines changed

7 files changed

+620
-3
lines changed

app/scripts/translatte/commands/exportMigration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ async function exportMigration(
5353
});
5454

5555
const fileName = isNotDefined(exportFileName)
56-
? `go-strings-${yyyy}-${mm}-${dd}`
56+
? `go-migration-strings-${yyyy}-${mm}-${dd}`
5757
: exportFileName;
5858

5959
await workbook.xlsx.writeFile(`${fileName}.xlsx`);
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import xlsx from 'exceljs';
2+
3+
import { fetchServerState } from "../utils";
4+
import { isFalsyString, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
5+
6+
async function exportServerStringsToExcel(
7+
apiUrl: string,
8+
authToken?: string,
9+
exportFileName?: string,
10+
) {
11+
const serverStrings = await fetchServerState(apiUrl, authToken);
12+
13+
const workbook = new xlsx.Workbook();
14+
const now = new Date();
15+
workbook.created = now;
16+
17+
const yyyy = now.getFullYear();
18+
const mm = (now.getMonth() + 1).toString().padStart(2, '0');
19+
const dd = now.getDate().toString().padStart(2, '0');
20+
const HH = now.getHours().toString().padStart(2, '0');
21+
const MM = now.getMinutes().toString().padStart(2, '0');
22+
23+
const worksheet = workbook.addWorksheet(
24+
`${yyyy}-${mm}-${dd} ${HH}-${MM}`
25+
);
26+
27+
worksheet.columns = [
28+
{ header: 'Namespace', key: 'namespace' },
29+
{ header: 'Key', key: 'key' },
30+
{ header: 'EN', key: 'en' },
31+
{ header: 'FR', key: 'fr' },
32+
{ header: 'ES', key: 'es' },
33+
{ header: 'AR', key: 'ar' },
34+
]
35+
36+
const keyGroupedStrings = mapToList(
37+
listToGroupList(
38+
serverStrings,
39+
({ page_name, key }) => `${page_name}:${key}`,
40+
),
41+
(list) => {
42+
const value = listToMap(
43+
list,
44+
({ language }) => language,
45+
({ value }) => value
46+
);
47+
const { key, page_name } = list[0];
48+
49+
return {
50+
namespace: page_name,
51+
key: key,
52+
en: value.en,
53+
fr: value.fr,
54+
es: value.es,
55+
ar: value.ar,
56+
};
57+
},
58+
);
59+
60+
Object.values(keyGroupedStrings).forEach((keyGroupedString) => {
61+
worksheet.addRow(keyGroupedString);
62+
});
63+
64+
const fileName = isFalsyString(exportFileName)
65+
? `go-server-strings-${yyyy}-${mm}-${dd}`
66+
: exportFileName;
67+
68+
await workbook.xlsx.writeFile(`${fileName}.xlsx`);
69+
}
70+
71+
export default exportServerStringsToExcel;
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
import { isDefined, isNotDefined, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs";
2+
import { Language, MigrationActionItem } from "../types";
3+
import { fetchServerState, getCombinedKey, languages, postLanguageStrings, readMigrations } from "../utils";
4+
import { Md5 } from "ts-md5";
5+
6+
async function pushMigration(migrationFilePath: string, apiUrl: string, authToken: string) {
7+
const serverStrings = await fetchServerState(apiUrl, authToken);
8+
9+
const serverStringMapByCombinedKey = mapToMap(
10+
listToGroupList(
11+
serverStrings,
12+
({ key, page_name }) => getCombinedKey(key, page_name),
13+
),
14+
(key) => key,
15+
(list) => listToMap(
16+
list,
17+
({ language }) => language,
18+
)
19+
);
20+
21+
const migrations = await readMigrations(
22+
[migrationFilePath]
23+
);
24+
25+
const actions = migrations[0].content.actions;
26+
27+
28+
function getItemsForNamespaceUpdate(actionItem: MigrationActionItem, language: Language) {
29+
if (actionItem.action !== 'update') {
30+
return undefined;
31+
}
32+
33+
if (isNotDefined(actionItem.newNamespace)) {
34+
return undefined;
35+
}
36+
37+
const oldCombinedKey = getCombinedKey(
38+
actionItem.key,
39+
actionItem.namespace,
40+
);
41+
42+
const oldStringItem = serverStringMapByCombinedKey[oldCombinedKey]?.[language];
43+
44+
if (isNotDefined(oldStringItem)) {
45+
return undefined;
46+
}
47+
48+
return [
49+
{
50+
action: 'delete' as const,
51+
key: actionItem.key,
52+
page_name: actionItem.namespace,
53+
},
54+
{
55+
action: 'set' as const,
56+
key: actionItem.key,
57+
page_name: actionItem.newNamespace,
58+
value: oldStringItem.value,
59+
hash: oldStringItem.hash,
60+
},
61+
];
62+
}
63+
64+
function getItemsForKeyUpdate(actionItem: MigrationActionItem, language: Language) {
65+
if (actionItem.action !== 'update') {
66+
return undefined;
67+
}
68+
69+
if (isNotDefined(actionItem.newKey)) {
70+
return undefined;
71+
}
72+
73+
const oldCombinedKey = getCombinedKey(
74+
actionItem.key,
75+
actionItem.namespace,
76+
);
77+
78+
const oldStringItem = serverStringMapByCombinedKey[oldCombinedKey]?.[language];
79+
80+
if (isNotDefined(oldStringItem)) {
81+
return undefined;
82+
}
83+
84+
return [
85+
{
86+
action: 'delete' as const,
87+
key: actionItem.key,
88+
page_name: actionItem.namespace,
89+
},
90+
{
91+
action: 'set' as const,
92+
key: actionItem.newKey,
93+
page_name: actionItem.namespace,
94+
value: oldStringItem.value,
95+
hash: oldStringItem.hash,
96+
},
97+
];
98+
}
99+
100+
const serverActions = listToMap(
101+
languages.map((language) => {
102+
const serverActionsForCurrentLanguage = actions.flatMap((actionItem) => {
103+
if (language === 'en') {
104+
if (actionItem.action === 'add') {
105+
return {
106+
action: 'set' as const,
107+
key: actionItem.key,
108+
page_name: actionItem.namespace,
109+
value: actionItem.value,
110+
hash: Md5.hashStr(actionItem.value),
111+
}
112+
}
113+
114+
if (actionItem.action === 'remove') {
115+
return {
116+
action: 'delete' as const,
117+
key: actionItem.key,
118+
page_name: actionItem.namespace,
119+
}
120+
}
121+
122+
if (isDefined(actionItem.newNamespace)) {
123+
return getItemsForNamespaceUpdate(actionItem, language);
124+
}
125+
126+
if (isDefined(actionItem.newKey)) {
127+
return getItemsForKeyUpdate(actionItem, language);
128+
}
129+
130+
if (isDefined(actionItem.newValue)) {
131+
return {
132+
action: 'set' as const,
133+
key: actionItem.key,
134+
page_name: actionItem.namespace,
135+
value: actionItem.newValue,
136+
hash: Md5.hashStr(actionItem.newValue),
137+
}
138+
}
139+
} else {
140+
if (actionItem.action === 'remove') {
141+
return {
142+
action: 'delete' as const,
143+
key: actionItem.key,
144+
page_name: actionItem.namespace,
145+
}
146+
}
147+
148+
if (actionItem.action === 'update') {
149+
if (isDefined(actionItem.newNamespace)) {
150+
return getItemsForNamespaceUpdate(actionItem, language);
151+
}
152+
153+
if (isDefined(actionItem.newKey)) {
154+
return getItemsForKeyUpdate(actionItem, language);
155+
}
156+
}
157+
}
158+
159+
return undefined;
160+
}).filter(isDefined);
161+
162+
return {
163+
language,
164+
actions: serverActionsForCurrentLanguage,
165+
}
166+
}),
167+
({ language }) => language,
168+
);
169+
170+
console.log('Pusing actions for en...')
171+
const enResponse = await postLanguageStrings(
172+
serverActions.en.language,
173+
serverActions.en.actions,
174+
apiUrl,
175+
authToken,
176+
);
177+
console.log(await enResponse.json());
178+
179+
180+
console.log('Pusing actions for fr...')
181+
const frResponse = await postLanguageStrings(
182+
serverActions.fr.language,
183+
serverActions.fr.actions,
184+
apiUrl,
185+
authToken,
186+
);
187+
console.log(await frResponse.json());
188+
189+
console.log('Pusing actions for es...')
190+
const esResponse = await postLanguageStrings(
191+
serverActions.es.language,
192+
serverActions.es.actions,
193+
apiUrl,
194+
authToken,
195+
);
196+
console.log(await esResponse.json());
197+
198+
console.log('Pusing actions for ar...')
199+
const arResponse = await postLanguageStrings(
200+
serverActions.ar.language,
201+
serverActions.ar.actions,
202+
apiUrl,
203+
authToken,
204+
);
205+
console.log(await arResponse.json());
206+
}
207+
208+
export default pushMigration;

0 commit comments

Comments
 (0)