refactor(v2): use hash to compare generated content (#1397)

This commit is contained in:
Endilie Yacop Sucipto 2019-04-26 23:25:00 +07:00 committed by Yangshun Tay
parent 2a8d973a6e
commit 16bec1cb47

View file

@ -13,13 +13,18 @@ const _ = require(`lodash`);
const escapeStringRegexp = require('escape-string-regexp');
const fs = require('fs-extra');
const genCache = new Map();
const fileHash = new Map();
async function generate(generatedFilesDir, file, content) {
const cached = genCache.get(file);
if (cached !== content) {
await fs.ensureDir(generatedFilesDir);
await fs.writeFile(path.join(generatedFilesDir, file), content);
genCache.set(file, content);
const filepath = path.join(generatedFilesDir, file);
const lastHash = fileHash.get(filepath);
const currentHash = createHash('md5')
.update(content)
.digest('hex');
if (lastHash !== currentHash) {
await fs.ensureDir(path.dirname(filepath));
await fs.writeFile(filepath, content);
fileHash.set(filepath, currentHash);
}
}