|
| 1 | +import { isDefined, isFalsyString, isNotDefined, listToGroupList, listToMap, mapToMap } from "@togglecorp/fujs"; |
| 2 | +import { Language, MigrationActionItem, ServerActionItem } from "../types"; |
| 3 | +import { fetchServerState, getCombinedKey, languages, postLanguageStrings, readMigrations, writeFilePromisify } from "../utils"; |
| 4 | +import { Md5 } from "ts-md5"; |
| 5 | + |
| 6 | +async function pushMigration(migrationFilePath: string, apiUrl: string, authToken: string) { |
| 7 | + const serverStrings = await fetchServerState(apiUrl, authToken); |
| 8 | + |
| 9 | + const serverStringMapByCombinedKey = mapToMap( |
| 10 | + listToGroupList( |
| 11 | + serverStrings, |
| 12 | + ({ key, page_name }) => getCombinedKey(key, page_name), |
| 13 | + ), |
| 14 | + (key) => key, |
| 15 | + (list) => listToMap( |
| 16 | + list, |
| 17 | + ({ language }) => language, |
| 18 | + ) |
| 19 | + ); |
| 20 | + |
| 21 | + const migrations = await readMigrations( |
| 22 | + [migrationFilePath] |
| 23 | + ); |
| 24 | + |
| 25 | + const actions = migrations[0].content.actions; |
| 26 | + |
| 27 | + |
| 28 | + function getItemsForNamespaceUpdate(actionItem: MigrationActionItem, language: Language) { |
| 29 | + if (actionItem.action !== 'update') { |
| 30 | + return undefined; |
| 31 | + } |
| 32 | + |
| 33 | + if (isNotDefined(actionItem.newNamespace)) { |
| 34 | + return undefined; |
| 35 | + } |
| 36 | + |
| 37 | + const oldCombinedKey = getCombinedKey( |
| 38 | + actionItem.key, |
| 39 | + actionItem.namespace, |
| 40 | + ); |
| 41 | + |
| 42 | + const oldStringItem = serverStringMapByCombinedKey[oldCombinedKey]?.[language]; |
| 43 | + |
| 44 | + if (isNotDefined(oldStringItem) || isFalsyString(oldStringItem.value)) { |
| 45 | + return undefined; |
| 46 | + } |
| 47 | + |
| 48 | + return [ |
| 49 | + { |
| 50 | + action: 'delete' as const, |
| 51 | + key: actionItem.key, |
| 52 | + page_name: actionItem.namespace, |
| 53 | + }, |
| 54 | + { |
| 55 | + action: 'set' as const, |
| 56 | + key: actionItem.key, |
| 57 | + page_name: actionItem.newNamespace, |
| 58 | + value: oldStringItem.value, |
| 59 | + hash: oldStringItem.hash, |
| 60 | + }, |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + function getItemsForKeyUpdate(actionItem: MigrationActionItem, language: Language) { |
| 65 | + if (actionItem.action !== 'update') { |
| 66 | + return undefined; |
| 67 | + } |
| 68 | + |
| 69 | + if (isNotDefined(actionItem.newKey)) { |
| 70 | + return undefined; |
| 71 | + } |
| 72 | + |
| 73 | + const oldCombinedKey = getCombinedKey( |
| 74 | + actionItem.key, |
| 75 | + actionItem.namespace, |
| 76 | + ); |
| 77 | + |
| 78 | + const oldStringItem = serverStringMapByCombinedKey[oldCombinedKey]?.[language]; |
| 79 | + |
| 80 | + if (isNotDefined(oldStringItem) || isFalsyString(oldStringItem.value)) { |
| 81 | + return undefined; |
| 82 | + } |
| 83 | + |
| 84 | + return [ |
| 85 | + { |
| 86 | + action: 'delete' as const, |
| 87 | + key: actionItem.key, |
| 88 | + page_name: actionItem.namespace, |
| 89 | + }, |
| 90 | + { |
| 91 | + action: 'set' as const, |
| 92 | + key: actionItem.newKey, |
| 93 | + page_name: actionItem.namespace, |
| 94 | + value: oldStringItem.value, |
| 95 | + hash: oldStringItem.hash, |
| 96 | + }, |
| 97 | + ]; |
| 98 | + } |
| 99 | + |
| 100 | + const serverActions = listToMap( |
| 101 | + languages.map((language) => { |
| 102 | + const serverActionsForCurrentLanguage = actions.flatMap((actionItem) => { |
| 103 | + if (language === 'en') { |
| 104 | + if (actionItem.action === 'add') { |
| 105 | + return { |
| 106 | + action: 'set' as const, |
| 107 | + key: actionItem.key, |
| 108 | + page_name: actionItem.namespace, |
| 109 | + value: actionItem.value, |
| 110 | + hash: Md5.hashStr(actionItem.value), |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + if (actionItem.action === 'remove') { |
| 115 | + return { |
| 116 | + action: 'delete' as const, |
| 117 | + key: actionItem.key, |
| 118 | + page_name: actionItem.namespace, |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + if (isDefined(actionItem.newNamespace)) { |
| 123 | + return getItemsForNamespaceUpdate(actionItem, language); |
| 124 | + } |
| 125 | + |
| 126 | + if (isDefined(actionItem.newKey)) { |
| 127 | + return getItemsForKeyUpdate(actionItem, language); |
| 128 | + } |
| 129 | + |
| 130 | + if (isDefined(actionItem.newValue)) { |
| 131 | + return { |
| 132 | + action: 'set' as const, |
| 133 | + key: actionItem.key, |
| 134 | + page_name: actionItem.namespace, |
| 135 | + value: actionItem.newValue, |
| 136 | + hash: Md5.hashStr(actionItem.newValue), |
| 137 | + } |
| 138 | + } |
| 139 | + } else { |
| 140 | + if (actionItem.action === 'remove') { |
| 141 | + return { |
| 142 | + action: 'delete' as const, |
| 143 | + key: actionItem.key, |
| 144 | + page_name: actionItem.namespace, |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if (actionItem.action === 'update') { |
| 149 | + if (isDefined(actionItem.newNamespace)) { |
| 150 | + return getItemsForNamespaceUpdate(actionItem, language); |
| 151 | + } |
| 152 | + |
| 153 | + if (isDefined(actionItem.newKey)) { |
| 154 | + return getItemsForKeyUpdate(actionItem, language); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + return undefined; |
| 160 | + }).filter(isDefined); |
| 161 | + |
| 162 | + return { |
| 163 | + language, |
| 164 | + actions: serverActionsForCurrentLanguage, |
| 165 | + } |
| 166 | + }), |
| 167 | + ({ language }) => language, |
| 168 | + ); |
| 169 | + |
| 170 | + await writeFilePromisify( |
| 171 | + `server-actions.json`, |
| 172 | + JSON.stringify(serverActions, null, 2), |
| 173 | + 'utf8', |
| 174 | + ); |
| 175 | + |
| 176 | + const logs: { |
| 177 | + responseFor: string, |
| 178 | + response: object, |
| 179 | + }[] = []; |
| 180 | + |
| 181 | + async function applyAction(lang: Language, actions: ServerActionItem[]) { |
| 182 | + console.log(`Pusing actions for ${lang}...`) |
| 183 | + const response = await postLanguageStrings( |
| 184 | + lang, |
| 185 | + actions, |
| 186 | + apiUrl, |
| 187 | + authToken, |
| 188 | + ); |
| 189 | + const responseJson = await response.json(); |
| 190 | + logs.push({ responseFor: 'en', response: responseJson }); |
| 191 | + |
| 192 | + /* |
| 193 | + const setActions = actions.filter(({ action }) => action === 'set'); |
| 194 | + const deleteActions = actions.filter(({ action }) => action === 'delete'); |
| 195 | +
|
| 196 | + console.log(`Pusing deleted actions for ${lang}...`) |
| 197 | + const deleteResponse = await postLanguageStrings( |
| 198 | + lang, |
| 199 | + deleteActions, |
| 200 | + apiUrl, |
| 201 | + authToken, |
| 202 | + ); |
| 203 | + const deleteResponseJson = await deleteResponse.json(); |
| 204 | + logs.push({ responseFor: 'delete en', response: deleteResponseJson }); |
| 205 | +
|
| 206 | + console.log(`Pusing set actions for ${lang}...`) |
| 207 | + const setResponse = await postLanguageStrings( |
| 208 | + lang, |
| 209 | + setActions, |
| 210 | + apiUrl, |
| 211 | + authToken, |
| 212 | + ); |
| 213 | + const setResponseJson = await setResponse.json(); |
| 214 | + logs.push({ responseFor: 'set en', response: setResponseJson }); |
| 215 | + */ |
| 216 | + } |
| 217 | + |
| 218 | + await applyAction(serverActions.en.language, serverActions.en.actions); |
| 219 | + await applyAction(serverActions.fr.language, serverActions.fr.actions); |
| 220 | + await applyAction(serverActions.es.language, serverActions.es.actions); |
| 221 | + await applyAction(serverActions.ar.language, serverActions.ar.actions); |
| 222 | + |
| 223 | + await writeFilePromisify( |
| 224 | + `push-migration-logs.json`, |
| 225 | + JSON.stringify(logs, null, 2), |
| 226 | + 'utf8', |
| 227 | + ); |
| 228 | +} |
| 229 | + |
| 230 | +export default pushMigration; |
0 commit comments