-
Notifications
You must be signed in to change notification settings - Fork 1
/
remoteCopy.js
119 lines (109 loc) · 3.04 KB
/
remoteCopy.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
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
* @Author: qinyang
* @Date: 2018-01-22 10:24:45
* @Last Modified by: qinyang
* @Last Modified time: 2018-01-24 01:14:05
* @for: remote copy to aliyun oss
*/
const pkg = require('./package.json');
const OSS = require('aliyun-sdk').OSS;
const path = require('path');
const sourceList = [
{ type: 'mac', key: 'pc-autoupdate/mac/check/release-mac.json' },
{ type: 'ia32', key: 'pc-autoupdate/win/ia32/check/release.json' },
{ type: 'x64', key: 'pc-autoupdate/win/x64/check/release.json' }
];
const copySource = {
mac: {
prefix: 'pc-autoupdate/mac',
list: [
`rishiqing-${pkg.version}-mac.zip`,
`rishiqing-mac-release-${pkg.version}.dmg`,
`release-mac.json`,
`release-mac.yml`
]
},
ia32: {
prefix: 'pc-autoupdate/win/ia32',
list: [
`rishiqing-win-ia32-release-${pkg.version}.exe`,
`release.json`,
`release.yml`
]
},
x64: {
prefix: 'pc-autoupdate/win/x64',
list: [
`rishiqing-win-x64-release-${pkg.version}.exe`,
`release.json`,
`release.yml`
]
}
};
const oss = new OSS({
'accessKeyId': process.env.ALY_OSS_Access_Id,
'secretAccessKey': process.env.ALY_OSS_Access_Key,
'endpoint': 'http://oss-cn-shenzhen.aliyuncs.com',
'apiVersion': '2013-10-15'
});
const copyObject = function (obj) {
return new Promise(function (resolve, reject) {
oss.copyObject(Object.assign({
Bucket: 'rishiqing-client'
}, obj), function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
const getObject = function (obj) {
return new Promise(function (resolve, reject) {
oss.getObject(Object.assign({
Bucket: 'rishiqing-client'
}, obj), function (err, data) {
if (err) {
reject(err);
} else {
resolve(data);
}
});
});
};
const deal = async function () {
const infoList = [];
for (let item of sourceList) {
try {
const result = await getObject({
Key: item.key
});
infoList.push({
type: item.type,
data: JSON.parse(result.Body.toString())
});
} catch (e) {}
}
for (let item of infoList) {
if (item && item.data && item.data.version === pkg.version) {
const copyDetail = copySource[item.type];
if (!copyDetail) continue;
for (let file of copyDetail.list) {
if (/\.json|\.yml$/.test(file)) {
const result = path.parse(file);
const base = result.name + '_' + (new Date()).getTime() + result.ext
await copyObject({
CopySource: path.join('/rishiqing-client', copyDetail.prefix, 'release', file).replace(/\\/g, '/'),
Key: path.join(copyDetail.prefix, 'release', base).replace(/\\/g, '/') // 这个地方不能以 '/' 开头,不然会报签名错误
});
}
await copyObject({
CopySource: path.join('/rishiqing-client', copyDetail.prefix, 'check', file).replace(/\\/g, '/'),
Key: path.join(copyDetail.prefix, 'release', file).replace(/\\/g, '/')
});
}
}
}
}
deal();