cloudbuild 1.0.12
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +11 -0
- package/lib/index.js +144 -0
- package/package.json +31 -0
package/README.md
ADDED
package/lib/index.js
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
'use strict';
|
2
|
+
const log = require('@starerp-cli-dev/log')
|
3
|
+
const request = require('@starerp-cli-dev/request')
|
4
|
+
const WS_SERVER = 'http://127.0.0.1:7001'
|
5
|
+
const io = require('socket.io-client')
|
6
|
+
const inquirer = require('inquirer')
|
7
|
+
const get = require('lodash/get')
|
8
|
+
const TIMEOUT = 5 * 60 * 1000
|
9
|
+
const CONNECT_TIME_OUT = 5 * 1000
|
10
|
+
const FAILED_CODE = ['prepare failed', 'download failed', 'install failed', 'build failed', 'pre-publish failed', 'publish failed']
|
11
|
+
function parseMsg(msg) {
|
12
|
+
const action = get(msg, 'data.action');
|
13
|
+
const message = get(msg, 'data.payload.message');
|
14
|
+
return {
|
15
|
+
action,
|
16
|
+
message,
|
17
|
+
};
|
18
|
+
}
|
19
|
+
class Cloudbuild {
|
20
|
+
// TODO
|
21
|
+
constructor(git, options) {
|
22
|
+
this.git = git
|
23
|
+
this.buildCmd = options.buildCmd
|
24
|
+
this.timeout = TIMEOUT
|
25
|
+
this.prod = options.prod
|
26
|
+
}
|
27
|
+
doTimeout(fn, timeout) {
|
28
|
+
this.timer && clearTimeout(this.timer);
|
29
|
+
log.info('设置任务超时时间:', `${timeout / 1000}秒`);
|
30
|
+
this.timer = setTimeout(fn, timeout);
|
31
|
+
};
|
32
|
+
init() {
|
33
|
+
return new Promise((res, rej) => {
|
34
|
+
|
35
|
+
const socket = io(WS_SERVER, {
|
36
|
+
query: {
|
37
|
+
repo: this.git.remote,
|
38
|
+
name: this.git.name,
|
39
|
+
branch: this.git.branch,
|
40
|
+
version: this.git.version,
|
41
|
+
buildCmd: this.buildCmd,
|
42
|
+
prod: this.prod
|
43
|
+
}
|
44
|
+
})
|
45
|
+
|
46
|
+
socket.on('connect', () => {
|
47
|
+
clearTimeout(this.timer)
|
48
|
+
const { id } = socket
|
49
|
+
log.success('云构建socket成功', id)
|
50
|
+
|
51
|
+
socket.on(id, message => {
|
52
|
+
const parsedMsg = parseMsg(message)
|
53
|
+
log.verbose('message ', parsedMsg.action, parsedMsg.message)
|
54
|
+
})
|
55
|
+
|
56
|
+
res();
|
57
|
+
});
|
58
|
+
socket.on('disconnect', () => {
|
59
|
+
log.verbose('云构建socket断开');
|
60
|
+
disconnect();
|
61
|
+
rej()
|
62
|
+
});
|
63
|
+
socket.on('error', (error) => {
|
64
|
+
log.verbose('云构建socket出错 ', error);
|
65
|
+
disconnect();
|
66
|
+
})
|
67
|
+
const disconnect = () => {
|
68
|
+
clearTimeout(this.timer)
|
69
|
+
socket.disconnect();
|
70
|
+
socket.close();
|
71
|
+
}
|
72
|
+
this.doTimeout(() => {
|
73
|
+
log.error('云构建服务连接超时,自动终止');
|
74
|
+
disconnect();
|
75
|
+
}, CONNECT_TIME_OUT);
|
76
|
+
this.socket = socket;
|
77
|
+
})
|
78
|
+
}
|
79
|
+
build() {
|
80
|
+
let ret = true;
|
81
|
+
return new Promise((resolve, reject) => {
|
82
|
+
this.socket.emit('build');
|
83
|
+
this.socket.on('build', msg => {
|
84
|
+
const parsedMsg = parseMsg(msg);
|
85
|
+
if (FAILED_CODE.indexOf(parsedMsg.action) >= 0) {
|
86
|
+
log.error(parsedMsg.action, parsedMsg.message);
|
87
|
+
clearTimeout(this.timer);
|
88
|
+
this.socket.disconnect();
|
89
|
+
this.socket.close();
|
90
|
+
ret = false;
|
91
|
+
} else {
|
92
|
+
log.success(parsedMsg.action, parsedMsg.message);
|
93
|
+
}
|
94
|
+
});
|
95
|
+
this.socket.on('building', msg => {
|
96
|
+
console.log(msg);
|
97
|
+
});
|
98
|
+
this.socket.on('disconnect', () => {
|
99
|
+
resolve(ret);
|
100
|
+
});
|
101
|
+
this.socket.on('error', (err) => {
|
102
|
+
reject(err);
|
103
|
+
});
|
104
|
+
});
|
105
|
+
}
|
106
|
+
async prepare() {
|
107
|
+
// 判断是否处于正式发布
|
108
|
+
if (this.prod) {
|
109
|
+
// // 1.获取OSS文件
|
110
|
+
const projectName = this.git.name;
|
111
|
+
const projectType = this.prod ? 'prod' : 'dev';
|
112
|
+
const ossProject = await request({
|
113
|
+
url: '/project/oss',
|
114
|
+
params: {
|
115
|
+
name: projectName,
|
116
|
+
type: projectType,
|
117
|
+
},
|
118
|
+
});
|
119
|
+
// 2.判断当前项目的OSS文件是否存在
|
120
|
+
if (ossProject.code === 0 && ossProject.data.length > 0) {
|
121
|
+
// 3.询问用户是否进行覆盖安装
|
122
|
+
const cover = (await inquirer.prompt({
|
123
|
+
type: 'list',
|
124
|
+
name: 'cover',
|
125
|
+
choices: [{
|
126
|
+
name: '覆盖发布',
|
127
|
+
value: true,
|
128
|
+
}, {
|
129
|
+
name: '放弃发布',
|
130
|
+
value: false,
|
131
|
+
}],
|
132
|
+
defaultValue: true,
|
133
|
+
message: `OSS已存在 [${projectName}] 项目,是否强行覆盖发布?`,
|
134
|
+
})).cover;
|
135
|
+
if (!cover) {
|
136
|
+
throw new Error('发布终止');
|
137
|
+
}
|
138
|
+
}
|
139
|
+
}
|
140
|
+
}
|
141
|
+
|
142
|
+
|
143
|
+
}
|
144
|
+
module.exports = Cloudbuild;
|
package/package.json
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
{
|
2
|
+
"name": "cloudbuild",
|
3
|
+
"version": "1.0.12",
|
4
|
+
"description": "> TODO: description",
|
5
|
+
"author": "谭孝宇 <mrtan21@126.com>",
|
6
|
+
"homepage": "",
|
7
|
+
"license": "ISC",
|
8
|
+
"main": "lib/index.js",
|
9
|
+
"directories": {
|
10
|
+
"lib": "lib",
|
11
|
+
"test": "__tests__"
|
12
|
+
},
|
13
|
+
"files": [
|
14
|
+
"lib"
|
15
|
+
],
|
16
|
+
"repository": {
|
17
|
+
"type": "git",
|
18
|
+
"url": "git@gitee.com:tan_xiao_yu/starerp-cli-dev.git"
|
19
|
+
},
|
20
|
+
"scripts": {
|
21
|
+
"test": "echo \"Error: run tests from root\" && exit 1"
|
22
|
+
},
|
23
|
+
"dependencies": {
|
24
|
+
"@starerp-cli-dev/log": "^1.0.9",
|
25
|
+
"@starerp-cli-dev/request": "^1.0.10",
|
26
|
+
"inquirer": "^8.1.1",
|
27
|
+
"lodash": "^4.17.21",
|
28
|
+
"socket.io-client": "^2.0.0"
|
29
|
+
},
|
30
|
+
"gitHead": "a4523525f9ee37e947212cf0c4e7844c7af201c7"
|
31
|
+
}
|