forked from UTCWeb/OutsideYourExpectations
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.js
33 lines (23 loc) · 789 Bytes
/
hash.js
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
const fs = require('fs');
const md5 = require('md5');
const assets = [
'css/styles.css',
'js/scripts.js'
];
const dataFile = 'src/_data/hash.json';
const production = process.env.NODE_ENV === 'development' ? false : true;
let jsonValue = {};
assets.forEach((asset) => {
if (production) {
let file = '_site/' + asset;
let fileHash = md5(fs.readFileSync(file)).substring(0, 15);
let assetNameArray = asset.split('.');
assetNameArray.splice(assetNameArray.length - 1, 0, fileHash);
let hashedAsset = assetNameArray.join('.');
fs.renameSync(file, '_site/' + hashedAsset);
jsonValue[asset] = hashedAsset;
} else {
jsonValue[asset] = asset;
}
});
fs.writeFileSync(dataFile, JSON.stringify(jsonValue));