* @param {Record } storageMapping * @param {Record } directoriesMapping * @param {Record []} existingFilesData * @returns {Promise >}
(storageMapping, directoriesMapping, existingFilesData)
| 198 | * @returns {Promise<Record<string, number>>} |
| 199 | */ |
| 200 | async function ensureFileStructure(storageMapping, directoriesMapping, existingFilesData) { |
| 201 | const result = {}; |
| 202 | let count = 0; |
| 203 | for (const [fullPath, storageId] of Object.entries(storageMapping)) { |
| 204 | const pathInCrowdin = toCrowdinPath(fullPath); |
| 205 | log.info(`Synchronizing '${pathInCrowdin}' (${++count} of ${Object.keys(storageMapping).length})`); |
| 206 | const fileData = existingFilesData.find((data) => data.path === pathInCrowdin); |
| 207 | if (fileData) { |
| 208 | result[fullPath] = fileData.id; |
| 209 | } else { |
| 210 | const parentFolderId = directoriesMapping[path.dirname(pathInCrowdin)]; |
| 211 | try { |
| 212 | const fileId = await addFile(encodeURIComponent(path.basename(pathInCrowdin)), storageId, parentFolderId); |
| 213 | result[fullPath] = fileId; |
| 214 | } catch (e) { |
| 215 | log.warn(`Cannot add '${pathInCrowdin}'. Skipping it`); |
| 216 | if (e.response) { |
| 217 | log.info(`Crowdin API status: ${e.response.status}`); |
| 218 | log.info(`Crowdin API error: ${JSON.stringify(e.response.data, null, 2)}`); |
| 219 | } else { |
| 220 | log.info(e); |
| 221 | } |
| 222 | continue; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | return result; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @returns {Promise<void>} |
no test coverage detected