Skip to content

Commit a8e9a88

Browse files
committed
WIP
1 parent b819592 commit a8e9a88

File tree

8 files changed

+398
-69
lines changed

8 files changed

+398
-69
lines changed

app/scripts/translatte/commands/applyMigrations.ts

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Md5 } from 'ts-md5';
22
import { listToMap, isDefined, unique } from '@togglecorp/fujs';
3-
import { isAbsolute, join, basename } from 'path';
3+
import { basename } from 'path';
44
import {
55
readSource,
6-
getMigrationFilesAttrs,
76
readMigrations,
87
writeFilePromisify,
8+
getMigrationFilesAttrsFromDir,
99
} from '../utils';
1010
import { merge } from './mergeMigrations';
1111
import {
@@ -39,7 +39,8 @@ function apply(
3939
const prevValue = stringsMapping[key];
4040
// NOTE: we are comparing hash instead of value so that this works for source language as well as other languages
4141
if (prevValue && prevValue.hash !== hash) {
42-
throw `Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`;
42+
// throw `Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`;
43+
console.info(`Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`);
4344
}
4445

4546
if (newMapping[key]) {
@@ -114,20 +115,16 @@ function apply(
114115
}
115116

116117
async function applyMigrations(
117-
projectPath: string,
118-
sourceFileName: string,
118+
migrationDir: string,
119+
sourceFilePath: string,
119120
destinationFileName: string,
120-
migrationFilePath: string,
121121
languages: string[],
122122
from: string | undefined,
123123
dryRun: boolean | undefined,
124124
) {
125-
const sourcePath = isAbsolute(sourceFileName)
126-
? sourceFileName
127-
: join(projectPath, sourceFileName)
128-
const sourceFile = await readSource(sourcePath)
125+
const sourceFile = await readSource(sourceFilePath)
129126

130-
const migrationFilesAttrs = await getMigrationFilesAttrs(projectPath, migrationFilePath);
127+
const migrationFilesAttrs = await getMigrationFilesAttrsFromDir(migrationDir);
131128
const selectedMigrationFilesAttrs = from
132129
? migrationFilesAttrs.filter((item) => (item.migrationName > from))
133130
: migrationFilesAttrs;
@@ -149,25 +146,23 @@ async function applyMigrations(
149146
);
150147

151148
const outputSourceFileContent: SourceFileContent = {
152-
...sourceFile.content,
149+
// ...sourceFile.content,
153150
last_migration: basename(lastMigration.file),
154151
strings: apply(
155-
sourceFile.content.strings,
152+
sourceFile.content.filter(
153+
({ page_name }) => isDefined(page_name)
154+
),
156155
mergedMigrationActions,
157156
languages,
158157
),
159158
};
160159

161-
const destinationPath = isAbsolute(destinationFileName)
162-
? destinationFileName
163-
: join(projectPath, destinationFileName)
164-
165160
if (dryRun) {
166-
console.info(`Creating file '${destinationPath}'`);
161+
console.info(`Creating file '${destinationFileName}'`);
167162
console.info(outputSourceFileContent);
168163
} else {
169164
await writeFilePromisify(
170-
destinationPath,
165+
destinationFileName,
171166
JSON.stringify(outputSourceFileContent, null, 4),
172167
'utf8',
173168
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { join } from 'path';
2+
3+
import { fetchAllServerStrings, writeFilePromisify } from "../utils";
4+
5+
async function exportStrings(
6+
apiUrl: string,
7+
outputDir: string,
8+
) {
9+
const serverStrings = await fetchAllServerStrings(apiUrl);
10+
11+
const url = new URL(apiUrl);
12+
const now = new Date();
13+
const exportFileName = `${url.hostname}-${now.getTime()}.json`;
14+
const exportFilePath = join(outputDir, exportFileName);
15+
16+
await writeFilePromisify(
17+
exportFilePath,
18+
JSON.stringify(serverStrings, null, 2),
19+
'utf8',
20+
);
21+
}
22+
23+
export default exportStrings;

app/scripts/translatte/commands/importExcel.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1-
import { isDefined, isNotDefined, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
1+
import { compareString, isDefined, isNotDefined, isTruthyString, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
22
import xlsx from 'exceljs';
3-
import { Language, ServerActionItem } from '../types';
3+
import { Language, ServerActionItem, SourceStringItem, StringItem } from '../types';
44
import { Md5 } from 'ts-md5';
5-
import { postLanguageStrings } from '../utils';
6-
7-
async function importExcel(importFilePath: string, apiUrl: string, accessToken: string) {
5+
import { fetchAllServerStrings, postLanguageStrings, readFilePromisify, writeFilePromisify } from '../utils';
6+
import stagingStrings from '../../../../../../go-temp/goadmin-stage.ifrc.org-1717130893109.json';
7+
8+
async function importExcel(
9+
importFilePath: string,
10+
apiUrl: string,
11+
accessToken: string,
12+
) {
813
const workbook = new xlsx.Workbook();
9-
1014
await workbook.xlsx.readFile(importFilePath);
1115

1216
const firstSheet = workbook.worksheets[0];
@@ -26,13 +30,7 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
2630
({ column }) => column,
2731
);
2832

29-
const strings: {
30-
key: string;
31-
namespace: string;
32-
language: Language;
33-
value: string;
34-
hash: string;
35-
}[] = [];
33+
const stringsFromExcel: StringItem[] = [];
3634

3735
firstSheet.eachRow(
3836
(row) => {
@@ -64,38 +62,38 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
6462

6563
const hash = Md5.hashStr(en);
6664

67-
strings.push({
68-
key,
65+
stringsFromExcel.push({
6966
namespace,
67+
key,
7068
language: 'en',
7169
value: en,
7270
hash,
7371
});
7472

7573
if (isDefined(ar)) {
76-
strings.push({
77-
key,
74+
stringsFromExcel.push({
7875
namespace,
76+
key,
7977
language: 'ar',
8078
value: ar,
81-
hash,
79+
hash,
8280
});
8381
}
8482

8583
if (isDefined(fr)) {
86-
strings.push({
87-
key,
84+
stringsFromExcel.push({
8885
namespace,
86+
key,
8987
language: 'fr',
9088
value: fr,
9189
hash,
9290
});
9391
}
9492

9593
if (isDefined(es)) {
96-
strings.push({
97-
key,
94+
stringsFromExcel.push({
9895
namespace,
96+
key,
9997
language: 'es',
10098
value: es,
10199
hash,
@@ -106,7 +104,7 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
106104

107105
const languageGroupedActions = mapToList(
108106
listToGroupList(
109-
strings,
107+
stringsFromExcel,
110108
({ language }) => language,
111109
(languageString) => {
112110
const serverAction: ServerActionItem = {
@@ -126,6 +124,7 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
126124
})
127125
);
128126

127+
/*
129128
const postPromises = languageGroupedActions.map(
130129
(languageStrings) => postLanguageStrings(
131130
languageStrings.language,
@@ -135,7 +134,20 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
135134
)
136135
)
137136
138-
await Promise.all(postPromises);
137+
const postResponses = await Promise.all(postPromises);
138+
139+
const postJsonResponses = await Promise.all(
140+
postResponses.map((response) => response.json())
141+
);
142+
*/
143+
144+
/*
145+
await writeFilePromisify(
146+
'serverResponse.json',
147+
JSON.stringify(postJsonResponses, null, 2),
148+
'utf8',
149+
);
150+
*/
139151
}
140152

141153
export default importExcel;

app/scripts/translatte/commands/pushMigration.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { isDefined, isNotDefined, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs";
1+
import { isDefined, isNotDefined, isTruthyString, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs";
22
import { Language, MigrationActionItem, SourceStringItem } from "../types";
3-
import { fetchLanguageStrings, getCombinedKey, postLanguageStrings, readMigrations } from "../utils";
3+
import { fetchLanguageStrings, getCombinedKey, postLanguageStrings, readMigrations, writeFilePromisify } from "../utils";
44
import { Md5 } from "ts-md5";
55

66
const languages: Language[] = ['en', 'fr', 'es', 'ar'];
@@ -38,10 +38,13 @@ async function fetchServerState(apiUrl: string, authToken: string) {
3838

3939
async function pushMigration(migrationFilePath: string, apiUrl: string, authToken: string) {
4040
const serverStrings = await fetchServerState(apiUrl, authToken);
41+
// const serverStrings = prevServerStateStrings as SourceStringItem[];
4142

4243
const serverStringMapByCombinedKey = mapToMap(
4344
listToGroupList(
44-
serverStrings,
45+
serverStrings.filter(
46+
({ value, page_name }) => isTruthyString(page_name) && isTruthyString(value),
47+
),
4548
({ key, page_name }) => getCombinedKey(key, page_name),
4649
),
4750
(key) => key,
@@ -197,6 +200,38 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
197200
}
198201
});
199202

203+
await writeFilePromisify(
204+
'prevServerState.json',
205+
JSON.stringify(serverStringMapByCombinedKey, null, 2),
206+
'utf8',
207+
);
208+
209+
await writeFilePromisify(
210+
'serverActions.json',
211+
JSON.stringify(serverActions, null, 2),
212+
'utf8',
213+
);
214+
215+
/*
216+
const postPromise = await postLanguageStrings('en', [
217+
{
218+
"action": "set",
219+
"key": "ifrc",
220+
"page_name": "countryPresence",
221+
"value": "IFRC",
222+
"hash": Md5.hashStr("IFRC"),
223+
},
224+
], apiUrl, authToken);
225+
226+
const postResponseJson = await postPromise.json();
227+
228+
await writeFilePromisify(
229+
'serverResponse.json',
230+
JSON.stringify(postResponseJson, null, 2),
231+
'utf8',
232+
);
233+
*/
234+
200235
const postResponsePromises = serverActions.map(
201236
(serverAction) => postLanguageStrings(
202237
serverAction.language,
@@ -206,7 +241,16 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
206241
)
207242
);
208243

209-
await Promise.all(postResponsePromises);
244+
const postResponses = await Promise.all(postResponsePromises);
245+
const postJsonResponses = await Promise.all(
246+
postResponses.map((response) => response.json())
247+
);
248+
249+
await writeFilePromisify(
250+
'serverResponse.json',
251+
JSON.stringify(postJsonResponses, null, 2),
252+
'utf8',
253+
);
210254
}
211255

212256
export default pushMigration;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { isDefined, listToGroupList } from "@togglecorp/fujs";
2+
import { isValidSourceStringItem, postLanguageStrings, readSource } from "../utils";
3+
import { Language, SourceStringItem } from "../types";
4+
5+
async function postSourceStrings(language: Language, strings: SourceStringItem[], apiUrl: string, authToken: string) {
6+
const response = await postLanguageStrings(
7+
language,
8+
strings.filter(isValidSourceStringItem).map(
9+
(sourceItem) => ({
10+
action: 'set',
11+
key: sourceItem.key,
12+
page_name: sourceItem.page_name,
13+
value: sourceItem.value,
14+
hash: sourceItem.hash,
15+
})
16+
),
17+
apiUrl,
18+
authToken,
19+
);
20+
21+
if (response.ok) {
22+
console.info(`${language} pushed to server`);
23+
} else {
24+
console.error(response.status, response.statusText);
25+
/*
26+
const responseJson = await response.json();
27+
await writeFilePromisify(
28+
'server-error.json',
29+
JSON.stringify(responseJson, null, 2),
30+
'utf-8'
31+
);
32+
*/
33+
}
34+
}
35+
36+
async function uploadJson(jsonFilePath: string, apiUrl: string, authToken: string) {
37+
const { content: strings } = await readSource(jsonFilePath);
38+
39+
const languageGroupedStringMap = listToGroupList(
40+
strings,
41+
({ language }) => language,
42+
);
43+
44+
if (isDefined(languageGroupedStringMap.en)) {
45+
await postSourceStrings(
46+
'en',
47+
languageGroupedStringMap.en,
48+
apiUrl,
49+
authToken,
50+
);
51+
52+
}
53+
54+
if (isDefined(languageGroupedStringMap.fr)) {
55+
await postSourceStrings(
56+
'fr',
57+
languageGroupedStringMap.fr,
58+
apiUrl,
59+
authToken,
60+
);
61+
}
62+
63+
if (isDefined(languageGroupedStringMap.es)) {
64+
await postSourceStrings(
65+
'es',
66+
languageGroupedStringMap.es,
67+
apiUrl,
68+
authToken,
69+
);
70+
}
71+
72+
if (isDefined(languageGroupedStringMap.ar)) {
73+
await postSourceStrings(
74+
'ar',
75+
languageGroupedStringMap.ar,
76+
apiUrl,
77+
authToken,
78+
);
79+
}
80+
}
81+
82+
export default uploadJson;

0 commit comments

Comments
 (0)