|
| 1 | +import xlsx, { CellValue } from 'exceljs'; |
| 2 | +import { fetchServerState, postLanguageStrings } from "../utils"; |
| 3 | +import { encodeDate, isDefined, isNotDefined, listToGroupList, listToMap, mapToList } from '@togglecorp/fujs'; |
| 4 | +import { Language, ServerActionItem, SourceStringItem } from '../types'; |
| 5 | + |
| 6 | + |
| 7 | +function getValueFromCellValue(cellValue: CellValue) { |
| 8 | + if (isNotDefined(cellValue)) { |
| 9 | + return undefined; |
| 10 | + } |
| 11 | + |
| 12 | + if ( |
| 13 | + typeof cellValue === 'number' |
| 14 | + || typeof cellValue === 'string' |
| 15 | + || typeof cellValue === 'boolean' |
| 16 | + ) { |
| 17 | + return cellValue; |
| 18 | + } |
| 19 | + |
| 20 | + if (cellValue instanceof Date) { |
| 21 | + return encodeDate(cellValue); |
| 22 | + } |
| 23 | + |
| 24 | + if ('error' in cellValue) { |
| 25 | + return undefined; |
| 26 | + } |
| 27 | + |
| 28 | + if ('richText' in cellValue) { |
| 29 | + return cellValue.richText.map(({ text }) => text).join(''); |
| 30 | + } |
| 31 | + |
| 32 | + if ('hyperlink' in cellValue) { |
| 33 | + const MAIL_IDENTIFIER = 'mailto:'; |
| 34 | + if (cellValue.hyperlink.startsWith(MAIL_IDENTIFIER)) { |
| 35 | + return cellValue.hyperlink.substring(MAIL_IDENTIFIER.length); |
| 36 | + } |
| 37 | + |
| 38 | + return cellValue.hyperlink; |
| 39 | + } |
| 40 | + |
| 41 | + if (isNotDefined(cellValue.result)) { |
| 42 | + return undefined; |
| 43 | + } |
| 44 | + |
| 45 | + if (typeof cellValue.result === 'object' && 'error' in cellValue.result) { |
| 46 | + return undefined; |
| 47 | + } |
| 48 | + |
| 49 | + // Formula result |
| 50 | + return getValueFromCellValue(cellValue.result); |
| 51 | +} |
| 52 | + |
| 53 | +async function pushStringsDref(importFilePath: string, apiUrl: string, accessToken: string) { |
| 54 | + const strings = await fetchServerState(apiUrl); |
| 55 | + const enStrings = strings.filter((string) => string.language === 'en'); |
| 56 | + |
| 57 | + const workbook = new xlsx.Workbook(); |
| 58 | + |
| 59 | + await workbook.xlsx.readFile(importFilePath); |
| 60 | + |
| 61 | + const firstSheet = workbook.worksheets[0]; |
| 62 | + const columns = firstSheet.columns.map( |
| 63 | + (column) => { |
| 64 | + const key = column.values?.[1]?.toString(); |
| 65 | + if (isNotDefined(key)) { |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + return { key, column: column.number } |
| 69 | + } |
| 70 | + ).filter(isDefined); |
| 71 | + |
| 72 | + const columnMap = listToMap( |
| 73 | + columns, |
| 74 | + ({ key }) => key, |
| 75 | + ({ column }) => column, |
| 76 | + ); |
| 77 | + |
| 78 | + const updatedStrings: SourceStringItem[] = []; |
| 79 | + |
| 80 | + firstSheet.eachRow((row) => { |
| 81 | + const enColumnKey = columnMap['EN']; |
| 82 | + const frColumnKey = columnMap['FR']; |
| 83 | + const esColumnKey = columnMap['ES']; |
| 84 | + const arColumnKey = columnMap['AR']; |
| 85 | + |
| 86 | + const enValue = isDefined(enColumnKey) ? getValueFromCellValue(row.getCell(enColumnKey).value) : undefined; |
| 87 | + |
| 88 | + const string = enStrings.find(({ value }) => value === enValue); |
| 89 | + |
| 90 | + if (string) { |
| 91 | + const frValue = isDefined(frColumnKey) ? getValueFromCellValue(row.getCell(frColumnKey).value) : undefined; |
| 92 | + const esValue = isDefined(esColumnKey) ? getValueFromCellValue(row.getCell(esColumnKey).value) : undefined; |
| 93 | + const arValue = isDefined(arColumnKey) ? getValueFromCellValue(row.getCell(arColumnKey).value) : undefined; |
| 94 | + |
| 95 | + updatedStrings.push({ |
| 96 | + ...string, |
| 97 | + language: 'fr', |
| 98 | + value: String(frValue), |
| 99 | + }); |
| 100 | + |
| 101 | + updatedStrings.push({ |
| 102 | + ...string, |
| 103 | + language: 'es', |
| 104 | + value: String(esValue), |
| 105 | + }); |
| 106 | + |
| 107 | + updatedStrings.push({ |
| 108 | + ...string, |
| 109 | + language: 'ar', |
| 110 | + value: String(arValue), |
| 111 | + }); |
| 112 | + } |
| 113 | + }); |
| 114 | + |
| 115 | + const languageGroupedActions = mapToList( |
| 116 | + listToGroupList( |
| 117 | + updatedStrings, |
| 118 | + ({ language }) => language, |
| 119 | + (languageString) => { |
| 120 | + const serverAction: ServerActionItem = { |
| 121 | + action: 'set', |
| 122 | + key: languageString.key, |
| 123 | + page_name: languageString.page_name, |
| 124 | + value: languageString.value, |
| 125 | + hash: languageString.hash, |
| 126 | + } |
| 127 | + |
| 128 | + return serverAction; |
| 129 | + }, |
| 130 | + ), |
| 131 | + (actions, language) => ({ |
| 132 | + language: language as Language, |
| 133 | + actions, |
| 134 | + }) |
| 135 | + ); |
| 136 | + |
| 137 | + for (let i = 0; i < languageGroupedActions.length; i++) { |
| 138 | + const action = languageGroupedActions[i]; |
| 139 | + |
| 140 | + console.log(`posting ${action.language} actions...`); |
| 141 | + const result = await postLanguageStrings( |
| 142 | + action.language, |
| 143 | + action.actions, |
| 144 | + apiUrl, |
| 145 | + accessToken, |
| 146 | + ) |
| 147 | + |
| 148 | + const resultJson = await result.json(); |
| 149 | + console.info(resultJson); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +export default pushStringsDref; |
0 commit comments