(fileKey: string)
| 34 | }; |
| 35 | |
| 36 | export function createDeltaProcessor(fileKey: string) { |
| 37 | const lockfilePath = path.join(process.cwd(), "i18n.lock"); |
| 38 | return { |
| 39 | async checkIfLockExists() { |
| 40 | return checkIfFileExists(lockfilePath); |
| 41 | }, |
| 42 | async calculateDelta(params: { |
| 43 | sourceData: Record<string, any>; |
| 44 | targetData: Record<string, any>; |
| 45 | checksums: Record<string, string>; |
| 46 | }): Promise<Delta> { |
| 47 | let added = _.difference( |
| 48 | Object.keys(params.sourceData), |
| 49 | Object.keys(params.targetData), |
| 50 | ); |
| 51 | let removed = _.difference( |
| 52 | Object.keys(params.targetData), |
| 53 | Object.keys(params.sourceData), |
| 54 | ); |
| 55 | const updated = Object.keys(params.sourceData).filter( |
| 56 | (key) => |
| 57 | md5(params.sourceData[key]) !== params.checksums[key] && |
| 58 | params.checksums[key], |
| 59 | ); |
| 60 | |
| 61 | const renamed: [string, string][] = []; |
| 62 | for (const addedKey of added) { |
| 63 | const addedHash = md5(params.sourceData[addedKey]); |
| 64 | for (const removedKey of removed) { |
| 65 | if (params.checksums[removedKey] === addedHash) { |
| 66 | renamed.push([removedKey, addedKey]); |
| 67 | break; |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | added = added.filter( |
| 72 | (key) => !renamed.some(([oldKey, newKey]) => newKey === key), |
| 73 | ); |
| 74 | removed = removed.filter( |
| 75 | (key) => !renamed.some(([oldKey, newKey]) => oldKey === key), |
| 76 | ); |
| 77 | |
| 78 | const hasChanges = [ |
| 79 | added.length > 0, |
| 80 | removed.length > 0, |
| 81 | updated.length > 0, |
| 82 | renamed.length > 0, |
| 83 | ].some((v) => v); |
| 84 | |
| 85 | return { |
| 86 | added, |
| 87 | removed, |
| 88 | updated, |
| 89 | renamed, |
| 90 | hasChanges, |
| 91 | }; |
| 92 | }, |
| 93 | async loadLock() { |
no outgoing calls
no test coverage detected