From 3190ce6905bcc27d9c673b3fa3cdf114098af243 Mon Sep 17 00:00:00 2001 From: Jingdan Date: Mon, 23 Apr 2018 10:03:33 +0800 Subject: [PATCH] feat: publish CDN check script (#452) * feat: publish CDN check script * feat: publish check add md5 check --- package.json | 3 ++- publish-check.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ publish-npm-check.js | 6 ++++++ publish.js | 27 ++++++++++++++++++--------- 4 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 publish-check.js create mode 100644 publish-npm-check.js diff --git a/package.json b/package.json index d1797fceb..6cbd96444 100644 --- a/package.json +++ b/package.json @@ -25,12 +25,13 @@ "build-test": "node browser-build.js > test/browser/build/aliyun-oss-sdk.js && node task/browser-test-build.js > test/browser/build/tests.js", "browser-test": "npm run build-test && karma start", "build-dist": "node browser-build.js > dist/aliyun-oss-sdk.js && MINIFY=1 node browser-build.js > dist/aliyun-oss-sdk.min.js", + "publish-to-npm": "node publish-npm-check.js && npm publish", "publish-to-cdn": "node publish.js" }, "git-pre-hooks": { "pre-release": "npm run build-dist", "post-release": [ - "npm publish", + "npm run publish-to-npm", "npm run publish-to-cdn" ] }, diff --git a/publish-check.js b/publish-check.js new file mode 100644 index 000000000..7fd5cee24 --- /dev/null +++ b/publish-check.js @@ -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); + }); + }); +}; + diff --git a/publish-npm-check.js b/publish-npm-check.js new file mode 100644 index 000000000..57b989651 --- /dev/null +++ b/publish-npm-check.js @@ -0,0 +1,6 @@ + +const check = require('./publish-check'); + +const dist = './dist/aliyun-oss-sdk.min.js'; + +check.checkDist(dist); diff --git a/publish.js b/publish.js index 5511caaca..16aeabf47 100644 --- a/publish.js +++ b/publish.js @@ -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); });