Skip to content

Commit

Permalink
feat: publish CDN check script (#452)
Browse files Browse the repository at this point in the history
* feat: publish CDN check script

* feat: publish check add md5 check
  • Loading branch information
binghaiwang authored and PeterRao committed Apr 23, 2018
1 parent 54db574 commit 3190ce6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
Expand Down
44 changes: 44 additions & 0 deletions publish-check.js
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);
});
});
};

6 changes: 6 additions & 0 deletions publish-npm-check.js
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);
27 changes: 18 additions & 9 deletions publish.js
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);
});

0 comments on commit 3190ce6

Please sign in to comment.