Skip to content

Commit

Permalink
feat: 云构建build逻辑、完善构建日志输出
Browse files Browse the repository at this point in the history
  • Loading branch information
AKclown committed Jun 6, 2022
1 parent e237052 commit d353587
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 41 deletions.
1 change: 1 addition & 0 deletions commands/publish/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ class PublishCommand extends Command {
}

function publish(args) {
console.log('args: ', args);
// $ 这个args就是执行脚手架传入的参数
return new PublishCommand(args);
}
Expand Down
2 changes: 1 addition & 1 deletion core/exec/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async function exec() {
// $ 指定的本地init地址
let targetPath = process.env.CLI_TARGET_PATH;
// userHome 用户主目录
const homePath = process.env.DEFAULT_CLI_HOME;
const homePath = process.env.CLI_HOME_PATH;
log.verbose('targetPath', targetPath);
log.verbose('homePath', homePath);

Expand Down
109 changes: 70 additions & 39 deletions models/cloudBuild/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ const TIME_OUT = 5 * 60 * 1000;
// ws链接超时时间
const WS_CONNECT_TIMEOUT = 5 * 1000;

// socket错误code
const FAILED_CODE = ['prepare failed'];

// 解析socket的msg
function parseMsg(msg) {
const action = get(msg, 'data.action');
Expand All @@ -25,6 +28,7 @@ class CloudBuild {
this.git = git; // git实例
this.buildCmd = options.buildCmd;
this.timeout = TIME_OUT;
this.socket = null;
}

// 超时方法
Expand All @@ -36,52 +40,79 @@ class CloudBuild {

// 云构建初始化
init() {
const socket = io(WS_SERVER, {
query: {
// 服务端需要下载源码、安装依赖。 需要客户端提供一些基础信息
repo: this.git.remote,
name: this.git.name,
branch: this.git.branch,
version: this.git.version,
buildCmd: this.buildCmd,
},
});
return new Promise((resolve, reject) => {
const socket = io(WS_SERVER, {
query: {
// 服务端需要下载源码、安装依赖。 需要客户端提供一些基础信息
repo: this.git.remote,
name: this.git.name,
branch: this.git.branch,
version: this.git.version,
buildCmd: this.buildCmd,
},
});

// connect事件
socket.on('connect', () => {
// 链接成功清除超时任务
clearTimeout(this.timer);
const { id } = socket;
log.success('云构建任务创建成功', `任务ID:${id}`);
socket.on(id, msg => {
const parsedMsg = parseMsg(msg);
log.success(parsedMsg.action, parsedMsg.message);
// connect事件
socket.on('connect', () => {
// 链接成功清除超时任务
clearTimeout(this.timer);
const { id } = socket;
log.success('云构建任务创建成功', `任务ID:${id}`);
socket.on(id, msg => {
const parsedMsg = parseMsg(msg);
log.success(parsedMsg.action, parsedMsg.message);
});
resolve();
});
});

// 断开链接
const disconnect = () => {
clearTimeout(this.timer);
socket.disconnect();
socket.close();
};
// 断开链接
const disconnect = () => {
clearTimeout(this.timer);
socket.disconnect();
socket.close();
};

// 注册连接超时定时任务
this.doTimeout(() => {
log.error('云构建服务链接超时,自动终止');
disconnect();
}, WS_CONNECT_TIMEOUT);

// 注册连接超时定时任务
this.doTimeout(() => {
log.error('云构建服务链接超时,自动终止');
disconnect();
}, WS_CONNECT_TIMEOUT);
// disconnect - 服务端断开链接
socket.on('disconnect', () => {
log.success('disconnect', '云构建任务断开');
disconnect();
});

// disconnect - 服务端断开链接
socket.on('disconnect', () => {
log.success('disconnect', '云构建任务断开');
disconnect();
// 出现异常
socket.on('error', error => {
log.error('error', '云构建出错', error);
disconnect();
reject();
});
// 保存socket实例
this.socket = socket;
});
}

// 出现异常
socket.on('error', error => {
log.error('error', '云构建出错', error);
disconnect();
// 执行云构建
build() {
return new Promise(() => {
this.socket.emit('build');
this.socket.on('build', msg => {
const parsedMsg = parseMsg(msg);
if (FAILED_CODE.indexOf(parsedMsg.action) >= 0) {
// $ 服务端出错,主动断开链接
log.error(parsedMsg.action, parsedMsg.message);
clearTimeout(this.timer);
this.socket.disconnect();
this.socket.close();
}
log.success(parsedMsg.action, parsedMsg.message);
});
this.socket.on('building', msg => {
console.log('msg: ', msg);
});
});
}
}
Expand Down
3 changes: 2 additions & 1 deletion models/git/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ class Git {
async publish() {
await this.preparePublish();
const cloudBuild = new CloudBuild(this, { buildCmd: this.buildCmd });
cloudBuild.init();
await cloudBuild.init();
await cloudBuild.build();
}

// 发布准备,做一些检查相关的操作
Expand Down

0 comments on commit d353587

Please sign in to comment.