-
Notifications
You must be signed in to change notification settings - Fork 576
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: publish CDN check script (#452)
* feat: publish CDN check script * feat: publish check add md5 check
- Loading branch information
1 parent
54db574
commit 3190ce6
Showing
4 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
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 = function* (object, store) { | ||
const result = yield store.head(object); | ||
if (result.status !== 200 || result.res.headers['content-length'] === '0') { | ||
yield 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); | ||
}); | ||
}); | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
const check = require('./publish-check'); | ||
|
||
const dist = './dist/aliyun-oss-sdk.min.js'; | ||
|
||
check.checkDist(dist); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,29 @@ | ||
var env = process.env; | ||
var oss = require('.'); | ||
var co = require('co'); | ||
var pkg = require('./package.json'); | ||
const { env } = process; | ||
const oss = require('.'); | ||
const co = require('co'); | ||
const pkg = require('./package.json'); | ||
const check = require('./publish-check'); | ||
|
||
var store = oss({ | ||
const store = oss({ | ||
accessKeyId: env.ALI_SDK_OSS_CDN_ID, | ||
accessKeySecret: env.ALI_SDK_OSS_CDN_SECRET, | ||
endpoint: env.ALI_SDK_OSS_CDN_ENDPOINT, | ||
bucket: env.ALI_SDK_OSS_CDN_BUCKET, | ||
}); | ||
|
||
var current = 'aliyun-oss-sdk-' + pkg.version + '.min.js'; | ||
var dist = './dist/aliyun-oss-sdk.min.js'; | ||
const current = `aliyun-oss-sdk-${pkg.version}.min.js`; | ||
const dist = './dist/aliyun-oss-sdk.min.js'; | ||
|
||
co(function* () { | ||
yield store.put(current, dist); | ||
}).catch(function (err) { | ||
check.checkDist(dist); // check file exist | ||
const contentMd5 = yield check.caculateFileMd5(dist); // check md5 to server | ||
yield store.put(current, dist, { | ||
headers: { | ||
'Content-Md5': contentMd5, | ||
}, | ||
}); | ||
yield check.checkCDNFile(current, store);// check cdn file | ||
console.log(`publish CDN success, version is ${pkg.version}`); | ||
}).catch((err) => { | ||
console.log(err); | ||
}); |