-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathfiles-hash-service.ts
40 lines (32 loc) · 1.46 KB
/
files-hash-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { executeActionByChunks } from "../common/helpers";
import { DEFAULT_CHUNK_SIZE } from "../common/constants";
export class FilesHashService implements IFilesHashService {
constructor(private $fs: IFileSystem,
private $logger: ILogger) { }
public async generateHashes(files: string[]): Promise<IStringDictionary> {
const result: IStringDictionary = {};
const action = async (file: string) => {
try {
const isFile = this.$fs.getFsStats(file).isFile();
if (isFile) {
result[file] = await this.$fs.getFileShasum(file);
}
} catch (err) {
this.$logger.trace(`Unable to generate hash for file ${file}. Error is: ${err}`);
}
};
await executeActionByChunks(files, DEFAULT_CHUNK_SIZE, action);
return result;
}
public async getChanges(files: string[], oldHashes: IStringDictionary): Promise<IStringDictionary> {
const newHashes = await this.generateHashes(files);
return this.getChangesInShasums(oldHashes, newHashes);
}
public hasChangesInShasums(oldHashes: IStringDictionary, newHashes: IStringDictionary): boolean {
return !!_.keys(this.getChangesInShasums(oldHashes, newHashes)).length;
}
private getChangesInShasums(oldHashes: IStringDictionary, newHashes: IStringDictionary): IStringDictionary {
return _.omitBy(newHashes, (hash: string, pathToFile: string) => !!_.find(oldHashes, (oldHash: string, oldPath: string) => pathToFile === oldPath && hash === oldHash));
}
}
$injector.register("filesHashService", FilesHashService);