UNPKG

1.11 kBJavaScriptView Raw
1/* */
2
3const { readFile } = require('fs-extra');
4
5const generateHash = require('./generateHash');
6
7const HASH_DELIMITER = 'emdaerHash:';
8const EMDAER_HASH_COMMENT_LENGTH = 32;
9
10/**
11 * Gets existing hash and content hash of destination file
12 * @param {string} destination
13 * The destination file path
14 * @returns {Promise<[string, string]>} contents tuple
15 * The existing hash and the existin file content,
16 * sans emdaer meta data, respectively
17 */
18module.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};