Skip to content

Commit 38a46f3

Browse files
committed
feat(@142vip/utils): docker命令执行增加异常捕获机制
1 parent f9f7f14 commit 38a46f3

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

packages/utils/src/core/docker.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ export async function isInstallDockerCompose(args?: DockerOptions) {
111111
*/
112112
export async function pushImage(imageName: string) {
113113
const command = `docker push ${imageName}`
114-
await commandStandardExecutor(command)
114+
await dockerScriptExecutor(command)
115115
}
116116

117117
/**
@@ -144,7 +144,8 @@ export async function buildImage(args: BuildImageDockerOptions) {
144144
vipLog.log(`${command}\n`, { startLabel: vipSymbols.success })
145145
}
146146
vipLog.log(args.imageName, { startLabel: '构建镜像' })
147-
await commandStandardExecutor(command)
147+
148+
await dockerScriptExecutor(command)
148149

149150
if (args.push) {
150151
const exist = await isExistImage(args.imageName)
@@ -187,3 +188,20 @@ export async function createContainer(args: CreateContainerOptions) {
187188
const command = `docker run -d --name ${args.containerName} --restart=unless-stopped ${networkParams} ${args.imageName}`
188189
await commandStandardExecutor(command)
189190
}
191+
192+
/**
193+
* docker命令的通用执行器
194+
*/
195+
async function dockerScriptExecutor(command: string) {
196+
try {
197+
const errorCode = await commandStandardExecutor(command)
198+
if (errorCode !== 0) {
199+
vipLog.error(`Error Code: ${errorCode}`, { startLabel: 'commandStandardExecutor' })
200+
process.exit(1)
201+
}
202+
}
203+
catch {
204+
// 构建镜像出错时,直接退出
205+
process.exit(1)
206+
}
207+
}

packages/utils/src/core/exec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export function commandStandardExecutor(cmd: Command) {
108108
}
109109
})
110110

111-
// 进程退出
111+
// 考虑进程非0退出
112112
child.on('close', (code) => {
113113
resolve(code)
114114
})

0 commit comments

Comments
 (0)