Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tnagorra committed Nov 19, 2024
1 parent b819592 commit 54f600a
Show file tree
Hide file tree
Showing 8 changed files with 401 additions and 69 deletions.
35 changes: 16 additions & 19 deletions app/scripts/translatte/commands/applyMigrations.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Md5 } from 'ts-md5';
import { listToMap, isDefined, unique } from '@togglecorp/fujs';
import { isAbsolute, join, basename } from 'path';
import { basename } from 'path';
import {
readSource,
getMigrationFilesAttrs,
readMigrations,
writeFilePromisify,
getMigrationFilesAttrsFromDir,
} from '../utils';
import { merge } from './mergeMigrations';
import {
Expand Down Expand Up @@ -39,10 +39,13 @@ function apply(
const prevValue = stringsMapping[key];
// NOTE: we are comparing hash instead of value so that this works for source language as well as other languages
if (prevValue && prevValue.hash !== hash) {
throw `Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`;
// console.info(prevValue, action);
// throw `Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`;
console.info(`Add: We already have string with different value for namespace '${action.namespace}' and key '${action.key}'`);
}

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

Expand Down Expand Up @@ -114,20 +117,16 @@ function apply(
}

async function applyMigrations(
projectPath: string,
sourceFileName: string,
migrationDir: string,
sourceFilePath: string,
destinationFileName: string,
migrationFilePath: string,
languages: string[],
from: string | undefined,
dryRun: boolean | undefined,
) {
const sourcePath = isAbsolute(sourceFileName)
? sourceFileName
: join(projectPath, sourceFileName)
const sourceFile = await readSource(sourcePath)
const sourceFile = await readSource(sourceFilePath)

const migrationFilesAttrs = await getMigrationFilesAttrs(projectPath, migrationFilePath);
const migrationFilesAttrs = await getMigrationFilesAttrsFromDir(migrationDir);
const selectedMigrationFilesAttrs = from
? migrationFilesAttrs.filter((item) => (item.migrationName > from))
: migrationFilesAttrs;
Expand All @@ -149,25 +148,23 @@ async function applyMigrations(
);

const outputSourceFileContent: SourceFileContent = {
...sourceFile.content,
// ...sourceFile.content,
last_migration: basename(lastMigration.file),
strings: apply(
sourceFile.content.strings,
sourceFile.content.filter(
({ page_name }) => isDefined(page_name)
),
mergedMigrationActions,
languages,
),
};

const destinationPath = isAbsolute(destinationFileName)
? destinationFileName
: join(projectPath, destinationFileName)

if (dryRun) {
console.info(`Creating file '${destinationPath}'`);
console.info(`Creating file '${destinationFileName}'`);
console.info(outputSourceFileContent);
} else {
await writeFilePromisify(
destinationPath,
destinationFileName,
JSON.stringify(outputSourceFileContent, null, 4),
'utf8',
);
Expand Down
23 changes: 23 additions & 0 deletions app/scripts/translatte/commands/exportStrings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { join } from 'path';

import { fetchAllServerStrings, writeFilePromisify } from "../utils";

async function exportStrings(
apiUrl: string,
outputDir: string,
) {
const serverStrings = await fetchAllServerStrings(apiUrl);

const url = new URL(apiUrl);
const now = new Date();
const exportFileName = `${url.hostname}-${now.getTime()}.json`;
const exportFilePath = join(outputDir, exportFileName);

await writeFilePromisify(
exportFilePath,
JSON.stringify(serverStrings, null, 2),
'utf8',
);
}

export default exportStrings;
60 changes: 36 additions & 24 deletions app/scripts/translatte/commands/importExcel.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { isDefined, isNotDefined, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
import { compareString, isDefined, isNotDefined, isTruthyString, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs';
import xlsx from 'exceljs';
import { Language, ServerActionItem } from '../types';
import { Language, ServerActionItem, SourceStringItem, StringItem } from '../types';
import { Md5 } from 'ts-md5';
import { postLanguageStrings } from '../utils';

async function importExcel(importFilePath: string, apiUrl: string, accessToken: string) {
import { fetchAllServerStrings, postLanguageStrings, readFilePromisify, writeFilePromisify } from '../utils';
import stagingStrings from '../../../../../../go-temp/goadmin-stage.ifrc.org-1717130893109.json';

async function importExcel(
importFilePath: string,
apiUrl: string,
accessToken: string,
) {
const workbook = new xlsx.Workbook();

await workbook.xlsx.readFile(importFilePath);

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

const strings: {
key: string;
namespace: string;
language: Language;
value: string;
hash: string;
}[] = [];
const stringsFromExcel: StringItem[] = [];

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

const hash = Md5.hashStr(en);

strings.push({
key,
stringsFromExcel.push({
namespace,
key,
language: 'en',
value: en,
hash,
});

if (isDefined(ar)) {
strings.push({
key,
stringsFromExcel.push({
namespace,
key,
language: 'ar',
value: ar,
hash,
hash,
});
}

if (isDefined(fr)) {
strings.push({
key,
stringsFromExcel.push({
namespace,
key,
language: 'fr',
value: fr,
hash,
});
}

if (isDefined(es)) {
strings.push({
key,
stringsFromExcel.push({
namespace,
key,
language: 'es',
value: es,
hash,
Expand All @@ -106,7 +104,7 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:

const languageGroupedActions = mapToList(
listToGroupList(
strings,
stringsFromExcel,
({ language }) => language,
(languageString) => {
const serverAction: ServerActionItem = {
Expand All @@ -126,6 +124,7 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
})
);

/*
const postPromises = languageGroupedActions.map(
(languageStrings) => postLanguageStrings(
languageStrings.language,
Expand All @@ -135,7 +134,20 @@ async function importExcel(importFilePath: string, apiUrl: string, accessToken:
)
)
await Promise.all(postPromises);
const postResponses = await Promise.all(postPromises);
const postJsonResponses = await Promise.all(
postResponses.map((response) => response.json())
);
*/

/*
await writeFilePromisify(
'serverResponse.json',
JSON.stringify(postJsonResponses, null, 2),
'utf8',
);
*/
}

export default importExcel;
52 changes: 48 additions & 4 deletions app/scripts/translatte/commands/pushMigration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isDefined, isNotDefined, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs";
import { isDefined, isNotDefined, isTruthyString, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs";
import { Language, MigrationActionItem, SourceStringItem } from "../types";
import { fetchLanguageStrings, getCombinedKey, postLanguageStrings, readMigrations } from "../utils";
import { fetchLanguageStrings, getCombinedKey, postLanguageStrings, readMigrations, writeFilePromisify } from "../utils";
import { Md5 } from "ts-md5";

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

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

const serverStringMapByCombinedKey = mapToMap(
listToGroupList(
serverStrings,
serverStrings.filter(
({ value, page_name }) => isTruthyString(page_name) && isTruthyString(value),
),
({ key, page_name }) => getCombinedKey(key, page_name),
),
(key) => key,
Expand Down Expand Up @@ -197,6 +200,38 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
}
});

await writeFilePromisify(
'prevServerState.json',
JSON.stringify(serverStringMapByCombinedKey, null, 2),
'utf8',
);

await writeFilePromisify(
'serverActions.json',
JSON.stringify(serverActions, null, 2),
'utf8',
);

/*
const postPromise = await postLanguageStrings('en', [
{
"action": "set",
"key": "ifrc",
"page_name": "countryPresence",
"value": "IFRC",
"hash": Md5.hashStr("IFRC"),
},
], apiUrl, authToken);
const postResponseJson = await postPromise.json();
await writeFilePromisify(
'serverResponse.json',
JSON.stringify(postResponseJson, null, 2),
'utf8',
);
*/

const postResponsePromises = serverActions.map(
(serverAction) => postLanguageStrings(
serverAction.language,
Expand All @@ -206,7 +241,16 @@ async function pushMigration(migrationFilePath: string, apiUrl: string, authToke
)
);

await Promise.all(postResponsePromises);
const postResponses = await Promise.all(postResponsePromises);
const postJsonResponses = await Promise.all(
postResponses.map((response) => response.json())
);

await writeFilePromisify(
'serverResponse.json',
JSON.stringify(postJsonResponses, null, 2),
'utf8',
);
}

export default pushMigration;
82 changes: 82 additions & 0 deletions app/scripts/translatte/commands/uploadJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { isDefined, listToGroupList } from "@togglecorp/fujs";
import { isValidSourceStringItem, postLanguageStrings, readSource } from "../utils";
import { Language, SourceStringItem } from "../types";

async function postSourceStrings(language: Language, strings: SourceStringItem[], apiUrl: string, authToken: string) {
const response = await postLanguageStrings(
language,
strings.filter(isValidSourceStringItem).map(
(sourceItem) => ({
action: 'set',
key: sourceItem.key,
page_name: sourceItem.page_name,
value: sourceItem.value,
hash: sourceItem.hash,
})
),
apiUrl,
authToken,
);

if (response.ok) {
console.info(`${language} pushed to server`);
} else {
console.info(response.status, response.statusText);
/*
const responseJson = await response.json();
await writeFilePromisify(
'server-error.json',
JSON.stringify(responseJson, null, 2),
'utf-8'
);
*/
}
}

async function uploadJson(jsonFilePath: string, apiUrl: string, authToken: string) {
const { content: strings } = await readSource(jsonFilePath);

const languageGroupedStringMap = listToGroupList(
strings,
({ language }) => language,
);

if (isDefined(languageGroupedStringMap.en)) {
await postSourceStrings(
'en',
languageGroupedStringMap.en,
apiUrl,
authToken,
);

}

if (isDefined(languageGroupedStringMap.fr)) {
await postSourceStrings(
'fr',
languageGroupedStringMap.fr,
apiUrl,
authToken,
);
}

if (isDefined(languageGroupedStringMap.es)) {
await postSourceStrings(
'es',
languageGroupedStringMap.es,
apiUrl,
authToken,
);
}

if (isDefined(languageGroupedStringMap.ar)) {
await postSourceStrings(
'ar',
languageGroupedStringMap.ar,
apiUrl,
authToken,
);
}
}

export default uploadJson;
Loading

0 comments on commit 54f600a

Please sign in to comment.