import co_assert from '../../utils/co-assert' import PresentationFileDiff from './PresentationFileDiff' export default class PresentationFileChecksums { private checksums: any; public constructor(checksumsObject: any = {}) { this.validateChecksumsJson(checksumsObject); this.checksums = checksumsObject; } public getChecksums() { return this.checksums; } public add(relativePath: string, fileChecksum: string): void { co_assert(this.checksums[relativePath] === undefined, `Checksum for file "${relativePath}" already exists.`); this.checksums[relativePath] = fileChecksum; } public makeDiff(checksums: PresentationFileChecksums): PresentationFileDiff { const diff = new PresentationFileDiff(); for(const key in this.checksums) { if (!checksums.checksums.hasOwnProperty(key)) { diff.removed.push(key); continue; } if (this.checksums[key] !== checksums.checksums[key]) { diff.updated.push(key); } } for(const key in checksums.checksums) { if (!this.checksums.hasOwnProperty(key)) { diff.added.push(key); } } return diff; } private validateChecksumsJson(json: any) { const incorrectJsonErrorMessage = 'Incorrect checksums json was provided. It has to be simple' + ' json object with file paths as keys and checksum strings as values.'; if (json.constructor !== Object) { throw new Error(incorrectJsonErrorMessage); } for (const filePath in json) { const checksum = json[filePath]; if (typeof filePath !== 'string' || typeof checksum !== 'string') { throw new Error(incorrectJsonErrorMessage); } } } }