import * as path from 'path' import PresentationFileChecksums from './PresentationFileChecksums' import * as fsExtra from 'fs-extra-promise' import * as Promise from 'bluebird' const COBALT_FOLDER_NAME = '.cobalt'; export default class CobaltFilesHandler { private presentationPath: string; public constructor(presentationPath: string) { this.setPresentationPath(presentationPath); } public setPresentationPath(presentationPath: string): void { this.presentationPath = presentationPath; } public readChecksums(): Promise { const checksumPath = this.getChecksumPath(); return fsExtra.readJsonAsync(checksumPath) .then((content: Object) => { return new PresentationFileChecksums(content); }) .catch(() => { return new PresentationFileChecksums() }); } public saveChecksums(checksums: PresentationFileChecksums): Promise { const checksumPath = this.getChecksumPath(); return this.ensureCobaltFolderExists(checksumPath) .then(() => { return fsExtra.writeJsonAsync(checksumPath, checksums.getChecksums()); }); } public getCobaltFolderRelativePath() { return COBALT_FOLDER_NAME; } private getChecksumPath() { return path.join(this.presentationPath, COBALT_FOLDER_NAME, 'checksums.json'); } private ensureCobaltFolderExists(checksumPath: string): Promise { const checksumsDir = path.dirname(checksumPath); return fsExtra.ensureDirAsync(checksumsDir); } }