-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
387 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.error(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; |
Oops, something went wrong.