Skip to content

Commit 4a9bfed

Browse files
authored
feat(@142vip/utils): 封装VipDocker工具,修复引用错误 (#314)
* feat(@142vip/utils): 封装`VipDocker`工具,修复引用错误 * chore: update
1 parent 38871d4 commit 4a9bfed

File tree

3 files changed

+48
-31
lines changed

3 files changed

+48
-31
lines changed

packages/utils/src/core/docker.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,27 @@ interface BuildImageDockerOptions extends DockerOptions {
1919
memory?: number
2020
}
2121

22+
/**
23+
* docker命令的通用执行器
24+
*/
25+
async function scriptExecutor(command: string) {
26+
try {
27+
const errorCode = await commandStandardExecutor(command)
28+
if (errorCode !== 0) {
29+
vipLog.error(`Error Code: ${errorCode}`, { startLabel: 'commandStandardExecutor' })
30+
process.exit(1)
31+
}
32+
}
33+
catch {
34+
// 构建镜像出错时,直接退出
35+
process.exit(1)
36+
}
37+
}
38+
2239
/**
2340
* 判断是否存在镜像
2441
*/
25-
export async function isExistImage(imageName: string) {
42+
async function isExistImage(imageName: string) {
2643
const command = `docker images -q ${imageName}`
2744
const { code, stdout } = await execCommand(command)
2845
return code === 0 && stdout.trim() !== ''
@@ -31,23 +48,23 @@ export async function isExistImage(imageName: string) {
3148
/**
3249
* 删除Docker镜像
3350
*/
34-
export async function deleteImage(imageName: string) {
51+
async function deleteImage(imageName: string) {
3552
const command = `docker rmi -f ${imageName}`
3653
return await execCommand(command)
3754
}
3855

3956
/**
4057
* 删除虚悬镜像
4158
*/
42-
export async function deletePruneImages() {
59+
async function deletePruneImages() {
4360
const command = 'docker image prune -f'
4461
return await execCommand(command)
4562
}
4663

4764
/**
4865
* 判断容器是否存在
4966
*/
50-
export async function isExistContainer(containerName: string) {
67+
async function isExistContainer(containerName: string) {
5168
const command = `docker ps -aq -f name=${containerName}`
5269
const { code, stdout } = await execCommand(command)
5370

@@ -57,15 +74,15 @@ export async function isExistContainer(containerName: string) {
5774
/**
5875
* 删除容器
5976
*/
60-
export async function deleteContainer(containerName: string) {
77+
async function deleteContainer(containerName: string) {
6178
const command = `docker rm -f ${containerName}`
6279
return await execCommand(command)
6380
}
6481

6582
/**
6683
* 是否安装docker
6784
*/
68-
export async function isInstallDocker(args?: DockerOptions) {
85+
async function isExistDocker(args?: DockerOptions) {
6986
const command = 'docker -v'
7087
const { code, stdout, stderr } = await execCommand(command)
7188

@@ -87,7 +104,7 @@ export async function isInstallDocker(args?: DockerOptions) {
87104
/**
88105
* 是否安装docker-compose
89106
*/
90-
export async function isInstallDockerCompose(args?: DockerOptions) {
107+
async function isExistDockerCompose(args?: DockerOptions) {
91108
const command = 'docker-compose -v'
92109
const { code, stdout, stderr } = await execCommand(command)
93110

@@ -108,17 +125,17 @@ export async function isInstallDockerCompose(args?: DockerOptions) {
108125
/**
109126
* 推送Docker镜像到指定仓库
110127
*/
111-
export async function pushImage(imageName: string) {
128+
async function pushImage(imageName: string) {
112129
const command = `docker push ${imageName}`
113-
await dockerScriptExecutor(command)
130+
await scriptExecutor(command)
114131
}
115132

116133
/**
117134
* 构建Docker镜像
118135
* - 根据tag标记,推送到远程仓库
119136
* - 推送完成后,删除本地镜像
120137
*/
121-
export async function buildImage(args: BuildImageDockerOptions) {
138+
async function buildImage(args: BuildImageDockerOptions) {
122139
// 构建参数
123140
let buildArg = ''
124141
if (args.buildArgs != null) {
@@ -144,7 +161,7 @@ export async function buildImage(args: BuildImageDockerOptions) {
144161
}
145162
vipLog.log(args.imageName, { startLabel: '构建镜像' })
146163

147-
await dockerScriptExecutor(command)
164+
await scriptExecutor(command)
148165

149166
if (args.push) {
150167
const exist = await isExistImage(args.imageName)
@@ -176,7 +193,7 @@ interface CreateContainerOptions extends DockerOptions {
176193
/**
177194
* 创建容器
178195
*/
179-
export async function createContainer(args: CreateContainerOptions) {
196+
async function createContainer(args: CreateContainerOptions) {
180197
if (args.networkName && !args.ip) {
181198
console.log('只指定ip,没有指定容器局域网')
182199
process.exit(1)
@@ -189,18 +206,18 @@ export async function createContainer(args: CreateContainerOptions) {
189206
}
190207

191208
/**
192-
* docker命令的通用执行器
209+
* docker工具
193210
*/
194-
async function dockerScriptExecutor(command: string) {
195-
try {
196-
const errorCode = await commandStandardExecutor(command)
197-
if (errorCode !== 0) {
198-
vipLog.error(`Error Code: ${errorCode}`, { startLabel: 'commandStandardExecutor' })
199-
process.exit(1)
200-
}
201-
}
202-
catch {
203-
// 构建镜像出错时,直接退出
204-
process.exit(1)
205-
}
211+
export const VipDocker = {
212+
isExistDocker,
213+
isExistDockerCompose,
214+
isExistImage,
215+
isExistContainer,
216+
deleteImage,
217+
deletePruneImages,
218+
deleteContainer,
219+
pushImage,
220+
buildImage,
221+
createContainer,
222+
scriptExecutor,
206223
}

packages/utils/test/docker.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { buildImage, isInstallDocker, isInstallDockerCompose } from '@142vip/utils'
1+
import { VipDocker } from '@142vip/utils';
22

33
(async () => {
4-
await isInstallDocker({ logger: true })
5-
const exist = await isInstallDockerCompose({ logger: true })
4+
await VipDocker.isExistDocker({ logger: true })
5+
const exist = await VipDocker.isExistDockerCompose({ logger: true })
66
console.log(111, exist)
7-
await buildImage({
7+
await VipDocker.buildImage({
88
imageName: 'aaa',
99
buildArgs: [
1010
['aaa', 123],

scripts/bundle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import { createRequire } from 'node:module'
1111
import process from 'node:process'
1212
import {
1313
OPEN_SOURCE_ADDRESS,
14-
buildImage,
1514
getRecentGitCommit,
15+
VipDocker,
1616
} from '@142vip/utils'
1717

1818
(async () => {
@@ -27,7 +27,7 @@ import {
2727
const { hash: gitHash } = await getRecentGitCommit()
2828

2929
// 构建镜像
30-
await buildImage({
30+
await VipDocker.buildImage({
3131
imageName,
3232
buildArgs: [
3333
// 参数中是否包含 --proxy

0 commit comments

Comments
 (0)