-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
104 lines (89 loc) · 2.81 KB
/
index.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* fis3-deploy-qcloudv2
* the source code is copyed from fis3-deploy-qcloud
* we only update the version
*/
// 引入 Node.js SDK
var COS = require('cos-nodejs-sdk-v5');
var fs = require('fs');
var cos;
var config;
/**
* 直接上传二进制流
*/
function uploadBuf(release, content, file, callback) {
var subpath = file.subpath;
var objkey = release.replace(/^\//, '');
fis.log.debug(config);
fis.log.debug('file.basename');
fis.log.debug(file.basename);
fis.log.debug('objkey', objkey);
fis.log.debug(fs.createReadStream(objkey));
fis.log.debug('fs.statSync(objkey).size', fs.statSync(objkey).size)
cos.putObject({
Bucket: config.Bucket, /* 必须 */
Region: config.Region,
Key: objkey, /* 必须 */
// Body: filepath,
Body: fs.createReadStream(objkey), /* 必须 */
ContentLength: fs.statSync(objkey).size, /* 必须 */
onProgress: function (progressData) {
console.log(JSON.stringify(progressData));
},
}, function (err, data) {
if (err) {
console.log(err);
callback(err);
} else {
fis.log.debug(data)
// var time = '[' + fis.log.now(true) + ']';
// process.stdout.write(
// ' uploadQcloud - '.green.bold +
// time.grey + ' ' +
// subpath.replace(/^\//, '') +
// ' >> '.yellow.bold +
// ret.key + '\n'
// );
callback();
}
});
}
/**
* deploy-qcloud 插件接口
* @param {Object} options 插件配置
* @param {Object} modified 修改了的文件列表(对应watch功能)
* @param {Object} total 所有文件列表
* @param {Function} callback 调用下一个插件
* @return {undefined}
*/
module.exports = function(options, modified, total, callback) {
cos = new COS(options);
config = options;
var steps = [];
modified.forEach(function(file) {
var reTryCount = options.retry;
steps.push(function(next) {
var _upload = arguments.callee;
uploadBuf(file.getHashRelease(), file.getContent(), file, function(error){
if (error) {
if (!--reTryCount) {
throw new Error(error);
} else {
_upload(next);
}
} else {
next();
}
});
});
});
fis.util.reduceRight(steps, function(next, current) {
return function() {
current(next);
};
}, callback)();
};
module.exports.options = {
// 允许重试两次。
retry: 2
};