Skip to content

Commit ebe687d

Browse files
committed
WIP
1 parent 33ab064 commit ebe687d

File tree

9 files changed

+236
-56
lines changed

9 files changed

+236
-56
lines changed

app/scripts/translatte/commands/applyMigrations.ts

Lines changed: 18 additions & 21 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,
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 readSource(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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { join } from 'path';
2+
3+
import { Language, SourceStringItem } from "../types";
4+
import { fetchLanguageStrings, writeFileAsync } from "../utils";
5+
6+
const languages: Language[] = ['en', 'fr', 'es', 'ar'];
7+
8+
async function fetchServerString(apiUrl: string, authToken?: string) {
9+
const responsePromises = languages.map(
10+
(language) => fetchLanguageStrings(language, apiUrl, authToken)
11+
);
12+
13+
const responses = await Promise.all(responsePromises);
14+
15+
const languageJsonPromises = responses.map(
16+
(response) => response.json()
17+
);
18+
19+
const languageStrings = await Promise.all(languageJsonPromises);
20+
21+
const serverStrings = languageStrings.flatMap(
22+
(languageString) => {
23+
const language: Language = languageString.code;
24+
25+
const strings: SourceStringItem[] = languageString.strings.map(
26+
(string: Omit<SourceStringItem, 'language'>) => ({
27+
...string,
28+
language,
29+
})
30+
)
31+
32+
return strings;
33+
}
34+
);
35+
36+
return serverStrings;
37+
}
38+
39+
async function exportStrings(
40+
apiUrl: string,
41+
outputDir: string,
42+
) {
43+
const serverStrings = await fetchServerString(apiUrl);
44+
45+
const url = new URL(apiUrl);
46+
const now = new Date();
47+
const exportFileName = `${url.hostname}-${now.getTime()}.json`;
48+
const exportFilePath = join(outputDir, exportFileName);
49+
50+
await writeFileAsync(
51+
exportFilePath,
52+
JSON.stringify(serverStrings, null, 2),
53+
'utf8',
54+
);
55+
}
56+
57+
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: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isDefined, isNotDefined, listToGroupList, listToMap, mapToList } from '
22
import xlsx from 'exceljs';
33
import { Language, ServerActionItem } from '../types';
44
import { Md5 } from 'ts-md5';
5-
import { postLanguageStrings } from '../utils';
5+
import { postLanguageStrings, writeFileAsync } from '../utils';
66

77
async function importExcel(importFilePath: string, apiUrl: string, accessToken: string) {
88
const workbook = new xlsx.Workbook();
@@ -135,7 +135,17 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
135135
)
136136
)
137137

138-
await Promise.all(postPromises);
138+
const postResponses = await Promise.all(postPromises);
139+
140+
const postJsonResponses = await Promise.all(
141+
postResponses.map((response) => response.json())
142+
);
143+
144+
await writeFileAsync(
145+
'serverResponse.json',
146+
JSON.stringify(postJsonResponses, null, 2),
147+
'utf8',
148+
);
139149
}
140150

141151
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: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
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

6+
import prevServerStateStrings from './go-server-state-strings-2024-04-29.json'
7+
68
const languages: Language[] = ['en', 'fr', 'es', 'ar'];
79

810
async function fetchServerState(apiUrl: string, authToken: string) {
@@ -38,10 +40,13 @@ async function fetchServerState(apiUrl: string, authToken: string) {
3840

3941
async function pushMigration(migrationFilePath: string, apiUrl: string, authToken: string) {
4042
const serverStrings = await fetchServerState(apiUrl, authToken);
43+
// const serverStrings = prevServerStateStrings as SourceStringItem[];
4144

4245
const serverStringMapByCombinedKey = mapToMap(
4346
listToGroupList(
44-
serverStrings,
47+
serverStrings.filter(
48+
({ value, page_name }) => isTruthyString(page_name) && isTruthyString(value),
49+
),
4550
({ key, page_name }) => getCombinedKey(key, page_name),
4651
),
4752
(key) => key,
@@ -197,6 +202,38 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
197202
}
198203
});
199204

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

209-
await Promise.all(postResponsePromises);
246+
const postResponses = await Promise.all(postResponsePromises);
247+
const postJsonResponses = await Promise.all(
248+
postResponses.map((response) => response.json())
249+
);
250+
251+
await writeFileAsync(
252+
'serverResponse.json',
253+
JSON.stringify(postJsonResponses, null, 2),
254+
'utf8',
255+
);
210256
}
211257

212258
export default pushMigration;

app/scripts/translatte/main.ts

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import exportMigration from './commands/exportMigration';
1212
import { join, basename } from 'path';
1313
import pushMigration from './commands/pushMigration';
1414
import importExcel from './commands/importExcel';
15+
import exportStrings from './commands/exportStrings';
1516

1617
const currentDir = cwd();
1718

@@ -84,12 +85,12 @@ yargs(hideBin(process.argv))
8485
},
8586
)
8687
.command(
87-
'apply-migrations <MIGRATION_FILE_PATH>',
88+
'apply-migrations <SOURCE_DIR>',
8889
'Apply migrations',
8990
(yargs) => {
90-
yargs.positional('MIGRATION_FILE_PATH', {
91+
yargs.positional('SOURCE_DIR', {
9192
type: 'string',
92-
describe: 'Find the migration file on MIGRATION_FILE_PATH',
93+
describe: 'Find the migration files on SOURCE_DIR',
9394
});
9495
yargs.options({
9596
'dry-run': {
@@ -101,27 +102,31 @@ yargs(hideBin(process.argv))
101102
type: 'string',
102103
description: 'The file after which the migration will be applied',
103104
},
104-
'source': {
105+
'server-strings': {
105106
type: 'string',
106107
description: 'The source file to which migration is applied',
107108
demandOption: true,
108109
},
109-
'destination': {
110+
'output-dir': {
110111
type: 'string',
111-
description: 'The file where new source file is saved',
112+
description: 'Directory where new file is saved',
112113
demandOption: true,
113114
},
114115
});
115116
},
116117
async (argv) => {
117118
console.warn(argv);
119+
const destinationFilePath = join(
120+
argv.outputDir as string,
121+
`migrated-strings-${new Date().getTime()}.json`
122+
);
123+
118124
await applyMigrations(
119-
currentDir,
120-
argv.SOURCE_FILE as string,
121-
argv.DESTINATION_FILE as string,
122-
argv.MIGRATION_FILE_PATH as string,
125+
argv.SOURCE_DIR as string,
126+
argv.serverStrings as string,
127+
destinationFilePath,
123128
['es', 'ar', 'fr'],
124-
argv.lastMigration as (string | undefined),
129+
argv.lastMigration as string,
125130
argv.dryRun as (boolean | undefined),
126131
);
127132
},
@@ -185,6 +190,29 @@ yargs(hideBin(process.argv))
185190
);
186191
},
187192
)
193+
.command(
194+
'export-strings <SERVER_URL> <OUTPUT_DIR>',
195+
'Export strings from the server to a json file',
196+
(yargs) => {
197+
yargs.positional('SERVER_URL', {
198+
type: 'string',
199+
describe: 'URL from which strings are to be fetched',
200+
});
201+
yargs.positional('OUTPUT_DIR', {
202+
type: 'string',
203+
describe: 'Directory where the output xlsx should be saved',
204+
});
205+
},
206+
async (argv) => {
207+
const outputDir = argv.OUTPUT_DIR as string;
208+
const serverUrl = argv.SERVER_URL as string;
209+
210+
await exportStrings(
211+
serverUrl,
212+
outputDir,
213+
);
214+
},
215+
)
188216
.command(
189217
'push-migration <MIGRATION_FILE_PATH>',
190218
'Push migration file to the server',

0 commit comments

Comments
 (0)