1 |
|
2 |
|
3 | const { readFile } = require('fs-extra');
|
4 |
|
5 | const generateHash = require('./generateHash');
|
6 |
|
7 | const HASH_DELIMITER = 'emdaerHash:';
|
8 | const EMDAER_HASH_COMMENT_LENGTH = 32;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | module.exports = async function getHashDiff(
|
19 | destination
|
20 | ) {
|
21 | const emptyTuple = ['', ''];
|
22 | let file;
|
23 | try {
|
24 | file = (await readFile(destination)).toString();
|
25 | } catch (e) {
|
26 | return emptyTuple;
|
27 | }
|
28 | const hashIndex = file.indexOf(HASH_DELIMITER);
|
29 | if (hashIndex === -1) {
|
30 | return emptyTuple;
|
31 | }
|
32 | const hashStartIndex = hashIndex + HASH_DELIMITER.length;
|
33 | const existingHash = file.slice(
|
34 | hashStartIndex,
|
35 | hashStartIndex + EMDAER_HASH_COMMENT_LENGTH
|
36 | );
|
37 | const existingContents = file.slice(
|
38 | hashStartIndex + EMDAER_HASH_COMMENT_LENGTH + 6,
|
39 | file.length
|
40 | );
|
41 | return [existingHash, generateHash(existingContents)];
|
42 | };
|