Skip to content

Commit d36918a

Browse files
committed
fix(translatte): update commands
1 parent 9acc6e3 commit d36918a

File tree

6 files changed

+273
-32
lines changed

6 files changed

+273
-32
lines changed

app/scripts/translatte/commands/clearServerStrings.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { listToGroupList } from "@togglecorp/fujs";
1+
import { isTruthyString, listToGroupList } from "@togglecorp/fujs";
22
import { fetchServerState, postLanguageStrings, writeFilePromisify } from "../utils";
33

44
async function clearServerStrings(apiUrl: string, authToken: string) {
55
const serverStrings = await fetchServerState(apiUrl, authToken);
66

77
const bulkActions = listToGroupList(
8-
serverStrings,
8+
serverStrings.filter(({ page_name }) => isTruthyString(page_name)),
99
({ language }) => language,
1010
({ key, page_name }) => ({
1111
action: "delete" as const,
@@ -19,7 +19,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
1919
response: object,
2020
}[] = [];
2121

22-
console.log('Pusing delete actions for en...')
22+
console.log('Pushing delete actions for en...')
2323
const enResponse = await postLanguageStrings(
2424
'en',
2525
bulkActions.en,
@@ -31,7 +31,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
3131
logs.push({ responseFor: 'en', response: enResponseJson });
3232

3333

34-
console.log('Pusing delete actions for fr...')
34+
console.log('Pushing delete actions for fr...')
3535
const frResponse = await postLanguageStrings(
3636
'fr',
3737
bulkActions.fr,
@@ -42,7 +42,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
4242
const frResponseJson = await frResponse.json();
4343
logs.push({ responseFor: 'fr', response: frResponseJson });
4444

45-
console.log('Pusing delete actions for es...')
45+
console.log('Pushing delete actions for es...')
4646
const esResponse = await postLanguageStrings(
4747
'es',
4848
bulkActions.es,
@@ -52,7 +52,7 @@ async function clearServerStrings(apiUrl: string, authToken: string) {
5252
const esResponseJson = await esResponse.json();
5353
logs.push({ responseFor: 'es', response: esResponseJson });
5454

55-
console.log('Pusing delete actions for ar...')
55+
console.log('Pushing delete actions for ar...')
5656
const arResponse = await postLanguageStrings(
5757
'ar',
5858
bulkActions.ar,

app/scripts/translatte/commands/exportServerStringsToExcel.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import xlsx from 'exceljs';
22

33
import { fetchServerState } from "../utils";
4-
import { isFalsyString, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
4+
import { isFalsyString, isTruthyString, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
55

66
async function exportServerStringsToExcel(
77
apiUrl: string,
@@ -35,7 +35,7 @@ async function exportServerStringsToExcel(
3535

3636
const keyGroupedStrings = mapToList(
3737
listToGroupList(
38-
serverStrings,
38+
serverStrings.filter(({ page_name, key }) => isTruthyString(page_name) && isTruthyString(key)),
3939
({ page_name, key }) => `${page_name}:${key}`,
4040
),
4141
(list) => {

app/scripts/translatte/commands/pushMigration.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
102102
const serverActionsForCurrentLanguage = actions.flatMap((actionItem) => {
103103
if (language === 'en') {
104104
if (actionItem.action === 'add') {
105+
if (isFalsyString(actionItem.value)) {
106+
return undefined;
107+
}
108+
105109
return {
106110
action: 'set' as const,
107111
key: actionItem.key,
@@ -168,7 +172,7 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
168172
);
169173

170174
await writeFilePromisify(
171-
`server-actions.json`,
175+
`/tmp/server-actions.json`,
172176
JSON.stringify(serverActions, null, 2),
173177
'utf8',
174178
);
@@ -193,7 +197,7 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
193197
const setActions = actions.filter(({ action }) => action === 'set');
194198
const deleteActions = actions.filter(({ action }) => action === 'delete');
195199
196-
console.log(`Pusing deleted actions for ${lang}...`)
200+
console.log(`Pushing deleted actions for ${lang}...`)
197201
const deleteResponse = await postLanguageStrings(
198202
lang,
199203
deleteActions,
@@ -221,7 +225,7 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
221225
await applyAction(serverActions.ar.language, serverActions.ar.actions);
222226

223227
await writeFilePromisify(
224-
`push-migration-logs.json`,
228+
`/tmp/push-migration-logs.json`,
225229
JSON.stringify(logs, null, 2),
226230
'utf8',
227231
);
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import xlsx, { CellValue } from 'exceljs';
2+
import { fetchServerState, postLanguageStrings } from "../utils";
3+
import { encodeDate, isDefined, isNotDefined, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
4+
import { Language, ServerActionItem, SourceStringItem } from '../types';
5+
6+
7+
function getValueFromCellValue(cellValue: CellValue) {
8+
if (isNotDefined(cellValue)) {
9+
return undefined;
10+
}
11+
12+
if (
13+
typeof cellValue === 'number'
14+
|| typeof cellValue === 'string'
15+
|| typeof cellValue === 'boolean'
16+
) {
17+
return cellValue;
18+
}
19+
20+
if (cellValue instanceof Date) {
21+
return encodeDate(cellValue);
22+
}
23+
24+
if ('error' in cellValue) {
25+
return undefined;
26+
}
27+
28+
if ('richText' in cellValue) {
29+
return cellValue.richText.map(({ text }) => text).join('');
30+
}
31+
32+
if ('hyperlink' in cellValue) {
33+
const MAIL_IDENTIFIER = 'mailto:';
34+
if (cellValue.hyperlink.startsWith(MAIL_IDENTIFIER)) {
35+
return cellValue.hyperlink.substring(MAIL_IDENTIFIER.length);
36+
}
37+
38+
return cellValue.hyperlink;
39+
}
40+
41+
if (isNotDefined(cellValue.result)) {
42+
return undefined;
43+
}
44+
45+
if (typeof cellValue.result === 'object' && 'error' in cellValue.result) {
46+
return undefined;
47+
}
48+
49+
// Formula result
50+
return getValueFromCellValue(cellValue.result);
51+
}
52+
53+
async function pushStringsDref(importFilePath: string, apiUrl: string, accessToken: string) {
54+
const strings = await fetchServerState(apiUrl);
55+
const enStrings = strings.filter((string) => string.language === 'en');
56+
57+
const workbook = new xlsx.Workbook();
58+
59+
await workbook.xlsx.readFile(importFilePath);
60+
61+
const firstSheet = workbook.worksheets[0];
62+
const columns = firstSheet.columns.map(
63+
(column) => {
64+
const key = column.values?.[1]?.toString();
65+
if (isNotDefined(key)) {
66+
return undefined;
67+
}
68+
return { key, column: column.number }
69+
}
70+
).filter(isDefined);
71+
72+
const columnMap = listToMap(
73+
columns,
74+
({ key }) => key,
75+
({ column }) => column,
76+
);
77+
78+
const updatedStrings: SourceStringItem[] = [];
79+
80+
firstSheet.eachRow((row) => {
81+
const enColumnKey = columnMap['EN'];
82+
const frColumnKey = columnMap['FR'];
83+
const esColumnKey = columnMap['ES'];
84+
const arColumnKey = columnMap['AR'];
85+
86+
const enValue = isDefined(enColumnKey) ? getValueFromCellValue(row.getCell(enColumnKey).value) : undefined;
87+
88+
const string = enStrings.find(({ value }) => value === enValue);
89+
90+
if (string) {
91+
const frValue = isDefined(frColumnKey) ? getValueFromCellValue(row.getCell(frColumnKey).value) : undefined;
92+
const esValue = isDefined(esColumnKey) ? getValueFromCellValue(row.getCell(esColumnKey).value) : undefined;
93+
const arValue = isDefined(arColumnKey) ? getValueFromCellValue(row.getCell(arColumnKey).value) : undefined;
94+
95+
updatedStrings.push({
96+
...string,
97+
language: 'fr',
98+
value: String(frValue),
99+
});
100+
101+
updatedStrings.push({
102+
...string,
103+
language: 'es',
104+
value: String(esValue),
105+
});
106+
107+
updatedStrings.push({
108+
...string,
109+
language: 'ar',
110+
value: String(arValue),
111+
});
112+
}
113+
});
114+
115+
const languageGroupedActions = mapToList(
116+
listToGroupList(
117+
updatedStrings,
118+
({ language }) => language,
119+
(languageString) => {
120+
const serverAction: ServerActionItem = {
121+
action: 'set',
122+
key: languageString.key,
123+
page_name: languageString.page_name,
124+
value: languageString.value,
125+
hash: languageString.hash,
126+
}
127+
128+
return serverAction;
129+
},
130+
),
131+
(actions, language) => ({
132+
language: language as Language,
133+
actions,
134+
})
135+
);
136+
137+
for (let i = 0; i < languageGroupedActions.length; i++) {
138+
const action = languageGroupedActions[i];
139+
140+
console.log(`posting ${action.language} actions...`);
141+
const result = await postLanguageStrings(
142+
action.language,
143+
action.actions,
144+
apiUrl,
145+
accessToken,
146+
)
147+
148+
const resultJson = await result.json();
149+
console.info(resultJson);
150+
}
151+
}
152+
153+
export default pushStringsDref;

0 commit comments

Comments
 (0)