-
Notifications
You must be signed in to change notification settings - Fork 576
/
publish-check.js
42 lines (38 loc) · 1.21 KB
/
publish-check.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
34
35
36
37
38
39
40
41
42
const fs = require('fs');
const crypto = require('crypto');
const pkg = require('./package.json');
exports.checkDist = function checkDist(filePath) {
const stat = fs.statSync(filePath);
if (stat.size === 0) {
throw new Error('dist file size is 0');
}
const data = fs.readFileSync(filePath, 'utf8');
const arr = data.split('\n')[0].split(' ');
const distVer = arr[arr.length - 1];
console.log('pkgVer', `v${pkg.version}`);
console.log('distVer', distVer);
if (distVer !== `v${pkg.version}`) {
throw new Error('version is not match');
}
};
exports.checkCDNFile = async (object, store) => {
const result = await store.head(object);
if (result.status !== 200 || result.res.headers['content-length'] === '0') {
await store.delete(object);
throw new Error('CDN file is incorrect or size is 0');
}
};
exports.caculateFileMd5 = function (filePath) {
return new Promise((resolve, reject) => {
const rs = fs.createReadStream(filePath);
const hash = crypto.createHash('md5');
rs.on('data', hash.update.bind(hash));
rs.on('error', err => {
reject(err);
});
rs.on('end', () => {
const md5Content = hash.digest('base64');
resolve(md5Content);
});
});
};