Skip to content

Commit 4612293

Browse files
frozenheliumtnagorra
authored andcommitted
WIP
1 parent b819592 commit 4612293

12 files changed

+419
-88
lines changed

app/scripts/translatte/commands/applyMigrations.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { join } from 'path';
44

55
import { testWithTmpDir } from '../testHelpers';
66
import {
7-
writeFilePromisify,
7+
writeFileAsync,
88
readJsonFilesContents,
99
} from '../utils';
1010
import {
@@ -29,7 +29,7 @@ testWithTmpDir('test applyMigrations with no data in server', async ({ tmpdir })
2929
{ name: '000003-1000000000000.json', content: migrationContent3 },
3030
{ name: '000004-1000000000000.json', content: migrationContent4 },
3131
{ name: '000005-1000000000000.json', content: migrationContent5 },
32-
].map(({ name, content }) => writeFilePromisify(
32+
].map(({ name, content }) => writeFileAsync(
3333
join(tmpdir, 'migrations', name),
3434
JSON.stringify(content, null, 4),
3535
'utf8',
@@ -42,7 +42,7 @@ testWithTmpDir('test applyMigrations with no data in server', async ({ tmpdir })
4242
last_migration: undefined,
4343
strings: [],
4444
};
45-
await writeFilePromisify(
45+
await writeFileAsync(
4646
join(tmpdir, 'strings', 'before.json'),
4747
JSON.stringify(emptySourceFile),
4848
'utf8',
@@ -70,7 +70,7 @@ testWithTmpDir('test applyMigrations with data in server', async ({ tmpdir }) =>
7070
mkdirSync(join(tmpdir, 'migrations'));
7171
const migrations = [
7272
{ name: '000006-1000000000000.json', content: migrationContent6 },
73-
].map(({ name, content }) => writeFilePromisify(
73+
].map(({ name, content }) => writeFileAsync(
7474
join(tmpdir, 'migrations', name),
7575
JSON.stringify(content, null, 4),
7676
'utf8',
@@ -79,7 +79,7 @@ testWithTmpDir('test applyMigrations with data in server', async ({ tmpdir }) =>
7979

8080
mkdirSync(join(tmpdir, 'strings'));
8181

82-
await writeFilePromisify(
82+
await writeFileAsync(
8383
join(tmpdir, 'strings', 'before.json'),
8484
JSON.stringify(strings1),
8585
'utf8',

app/scripts/translatte/commands/applyMigrations.ts

Lines changed: 19 additions & 22 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 {
5-
readSource,
6-
getMigrationFilesAttrs,
5+
readJsonSource,
76
readMigrations,
8-
writeFilePromisify,
7+
writeFileAsync,
8+
getMigrationFilesAttrsFromDir,
99
} from '../utils';
1010
import { merge } from './mergeMigrations';
1111
import {
@@ -39,10 +39,13 @@ 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+
// console.info(prevValue, action);
43+
// throw `Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`;
44+
console.info(`Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`);
4345
}
4446

4547
if (newMapping[key]) {
48+
console.info(prevValue, action, newMapping);
4649
throw `Add: We already have string for namespace '${action.namespace}' and key '${action.key}' in migration`;
4750
}
4851

@@ -114,20 +117,16 @@ function apply(
114117
}
115118

116119
async function applyMigrations(
117-
projectPath: string,
118-
sourceFileName: string,
120+
migrationDir: string,
121+
sourceFilePath: string,
119122
destinationFileName: string,
120-
migrationFilePath: string,
121123
languages: string[],
122124
from: string | undefined,
123125
dryRun: boolean | undefined,
124126
) {
125-
const sourcePath = isAbsolute(sourceFileName)
126-
? sourceFileName
127-
: join(projectPath, sourceFileName)
128-
const sourceFile = await readSource(sourcePath)
127+
const sourceFile = await readJsonSource(sourceFilePath)
129128

130-
const migrationFilesAttrs = await getMigrationFilesAttrs(projectPath, migrationFilePath);
129+
const migrationFilesAttrs = await getMigrationFilesAttrsFromDir(migrationDir);
131130
const selectedMigrationFilesAttrs = from
132131
? migrationFilesAttrs.filter((item) => (item.migrationName > from))
133132
: migrationFilesAttrs;
@@ -149,25 +148,23 @@ async function applyMigrations(
149148
);
150149

151150
const outputSourceFileContent: SourceFileContent = {
152-
...sourceFile.content,
151+
// ...sourceFile.content,
153152
last_migration: basename(lastMigration.file),
154153
strings: apply(
155-
sourceFile.content.strings,
154+
sourceFile.content.filter(
155+
({ page_name }) => isDefined(page_name)
156+
),
156157
mergedMigrationActions,
157158
languages,
158159
),
159160
};
160161

161-
const destinationPath = isAbsolute(destinationFileName)
162-
? destinationFileName
163-
: join(projectPath, destinationFileName)
164-
165162
if (dryRun) {
166-
console.info(`Creating file '${destinationPath}'`);
163+
console.info(`Creating file '${destinationFileName}'`);
167164
console.info(outputSourceFileContent);
168165
} else {
169-
await writeFilePromisify(
170-
destinationPath,
166+
await writeFileAsync(
167+
destinationFileName,
171168
JSON.stringify(outputSourceFileContent, null, 4),
172169
'utf8',
173170
);
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, writeFileAsync } 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 writeFileAsync(
17+
exportFilePath,
18+
JSON.stringify(serverStrings, null, 2),
19+
'utf8',
20+
);
21+
}
22+
23+
export default exportStrings;

app/scripts/translatte/commands/generateMigration.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Md5 } from 'ts-md5';
22
import { join, isAbsolute } from 'path';
33

44
import {
5-
writeFilePromisify,
5+
writeFileAsync,
66
oneOneMapping,
77
readTranslations,
88
getTranslationFileNames,
@@ -184,7 +184,7 @@ async function generate(
184184
console.info(`Creating migration file '${outputMigrationFile}'`);
185185
console.info(migrationContent);
186186
} else {
187-
await writeFilePromisify(
187+
await writeFileAsync(
188188
outputMigrationFile,
189189
JSON.stringify(migrationContent, null, 4),
190190
'utf8',

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, readFileAsync, writeFileAsync } 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 writeFileAsync(
146+
'serverResponse.json',
147+
JSON.stringify(postJsonResponses, null, 2),
148+
'utf8',
149+
);
150+
*/
139151
}
140152

141153
export default importExcel;

app/scripts/translatte/commands/mergeMigrations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
getMigrationFilesAttrs,
88
readMigrations,
99
removeFiles,
10-
writeFilePromisify
10+
writeFileAsync
1111
} from '../utils';
1212

1313
function getCanonicalKey(
@@ -209,7 +209,7 @@ async function mergeMigrations(
209209
console.info(`Creating migration file '${newFileName}'`);
210210
console.info(mergedMigrationContent);
211211
} else {
212-
await writeFilePromisify(
212+
await writeFileAsync(
213213
newFileName,
214214
JSON.stringify(mergedMigrationContent, null, 4),
215215
'utf8',

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, writeFileAsync } 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 writeFileAsync(
204+
'prevServerState.json',
205+
JSON.stringify(serverStringMapByCombinedKey, null, 2),
206+
'utf8',
207+
);
208+
209+
await writeFileAsync(
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 writeFileAsync(
250+
'serverResponse.json',
251+
JSON.stringify(postJsonResponses, null, 2),
252+
'utf8',
253+
);
210254
}
211255

212256
export default pushMigration;

0 commit comments

Comments
 (0)