Skip to content

Commit

Permalink
Merge pull request #198 from 142vip/feat/docker-try-catch
Browse files Browse the repository at this point in the history
feat(@142vip/utils): `docker`命令执行增加异常捕获机制
  • Loading branch information
mmdapl authored Dec 12, 2024
2 parents f9f7f14 + 38a46f3 commit 117f147
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
22 changes: 20 additions & 2 deletions packages/utils/src/core/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function isInstallDockerCompose(args?: DockerOptions) {
*/
export async function pushImage(imageName: string) {
const command = `docker push ${imageName}`
await commandStandardExecutor(command)
await dockerScriptExecutor(command)
}

/**
Expand Down Expand Up @@ -144,7 +144,8 @@ export async function buildImage(args: BuildImageDockerOptions) {
vipLog.log(`${command}\n`, { startLabel: vipSymbols.success })
}
vipLog.log(args.imageName, { startLabel: '构建镜像' })
await commandStandardExecutor(command)

await dockerScriptExecutor(command)

if (args.push) {
const exist = await isExistImage(args.imageName)
Expand Down Expand Up @@ -187,3 +188,20 @@ export async function createContainer(args: CreateContainerOptions) {
const command = `docker run -d --name ${args.containerName} --restart=unless-stopped ${networkParams} ${args.imageName}`
await commandStandardExecutor(command)
}

/**
* docker命令的通用执行器
*/
async function dockerScriptExecutor(command: string) {
try {
const errorCode = await commandStandardExecutor(command)
if (errorCode !== 0) {
vipLog.error(`Error Code: ${errorCode}`, { startLabel: 'commandStandardExecutor' })
process.exit(1)
}
}
catch {
// 构建镜像出错时,直接退出
process.exit(1)
}
}
2 changes: 1 addition & 1 deletion packages/utils/src/core/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function commandStandardExecutor(cmd: Command) {
}
})

// 进程退出
// 考虑进程非0退出
child.on('close', (code) => {
resolve(code)
})
Expand Down

0 comments on commit 117f147

Please sign in to comment.