mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-05 13:17:23 +02:00
refactor(v2): use hash to compare generated content (#1397)
This commit is contained in:
parent
2a8d973a6e
commit
16bec1cb47
1 changed files with 11 additions and 6 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue